Advertisement
Sharidan

(oc)rplm.lua

Feb 3rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. --
  2. -- Robot Power Level Monitor
  3. --
  4. -- Author: Sharidan
  5. --     www.sharidan.dk
  6. --
  7. --[[
  8. local event = require("event")
  9. local pmtid, lc = 0, 0
  10. local function setLight(nc)
  11.   if (nc ~= lc) then
  12.     require("robot").setLightColor(nc)
  13.     lc = tonumber(nc)
  14.   end
  15. end
  16. local function checkPower()
  17.   local pct, nc = require("computer").energy()/(require("computer").maxEnergy()/100), 0
  18.   if (pct >= 35) then
  19.     setLight(0x00ff00)
  20.   elseif (pct >= 20 and pct < 35) then
  21.     setLight(0xffff33)
  22.   elseif (pct >= 10 and pct < 20) then
  23.     setLight(0xffa000)
  24.   elseif (pct < 10) then
  25.     setLight(0xff4040)
  26.   end
  27. end
  28. function start()
  29.   setLight(0x60c0ff)
  30.   if (pmtid == 0) then
  31.     pmtid = event.timer(5, checkPower, math.huge)
  32.   end
  33. end
  34. function stop()
  35.   if (pmtid > 0) then
  36.     event.cancel(pmtid)
  37.     pmtid = 0
  38.   end
  39.   setLight(0x60c0ff)
  40. end
  41. ]]--
  42. local function install()
  43.   local sp = os.getenv("PWD")
  44.   local fs = require("filesystem")
  45.   if (fs.exists(sp.."rplm.lua")) then
  46.     if (fs.exists("/etc/rc.d/rplm.lua")) then
  47.       return false, "rplm already installed."
  48.     else
  49.       local sf = io.open(sp.."rplm.lua", "r")
  50.       if (sf) then
  51.         local tf = io.open("/etc/rc.d/rplm.lua", "w")
  52.         if (tf) then
  53.           local running, wc = true, false
  54.           while running do
  55.             local l = sf:read("*l")
  56.             if (l == "--[[") then
  57.               wc = true
  58.             elseif (l == "]]--") then
  59.               wc = nil
  60.               running = nil
  61.             elseif (string.sub(l, 1, 2) == "--" and not wc) then
  62.               tf:write(l.."\n")
  63.             elseif (wc) then
  64.               tf:write(l.."\n")
  65.             end
  66.           end
  67.           tf:close()
  68.           sf:close()
  69.           return true, ""
  70.         end
  71.       end
  72.     end
  73.   else
  74.     return false, "rplm not found!\nPlease change to directory containing installer and try again."
  75.   end
  76. end
  77. local function uninstall()
  78.   local rc, fs = require("rc"), require("filesystem")
  79.   if (rc) then
  80.     local result, reason = rc.runCommand("rplm", "stop")
  81.     result, reason = rc.runCommand("rplm", "disable")
  82.     if (result) then
  83.       if (fs.exists("/etc/rc.d/rplm.lua")) then
  84.         fs.remove("/etc/rc.d/rplm.lua")
  85.         return true, ""
  86.       end
  87.     else
  88.       return false, reason
  89.     end
  90.   else
  91.     return false, "rc not accessible!"
  92.   end
  93. end
  94.  
  95. local function showUsage()
  96.   print("Robot Power Level Monitor")
  97.   print("rplm -i/-u")
  98.   print("  -i : install daemon")
  99.   print("  -u : uninstall daemon")
  100. end
  101.  
  102. local function checkRun(a)
  103.   local bot = require("robot")
  104.   if (bot) then
  105.     if (a == "-i") then
  106.       local s, r = install()
  107.       if (s) then
  108.         local rc = require("rc")
  109.         if (rc) then
  110.           s, r = rc.runCommand("rplm", "enable")
  111.           s, r = rc.runCommand("rplm", "start")
  112.         end
  113.         print("rplm installed.")
  114.       else
  115.         print("rplm install failed:")
  116.         print(r)
  117.       end
  118.     elseif (a == "-u") then
  119.       local s, r = uninstall()
  120.       if (s) then
  121.         print("rplm uninstalled.")
  122.       else
  123.         print("rplm uninstall failed:")
  124.         print(r)
  125.       end
  126.     else
  127.       showUsage()
  128.     end
  129.   else
  130.     showUsage()
  131.   end
  132. end
  133.  
  134. local args = {...}
  135. if (#args == 1) then
  136.   checkRun(string.lower(args[1]))
  137. else
  138.   showUsage()
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement