Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Robot Power Level Monitor
- --
- -- Author: Sharidan
- -- www.sharidan.dk
- --
- --[[
- local event = require("event")
- local pmtid, lc = 0, 0
- local function setLight(nc)
- if (nc ~= lc) then
- require("robot").setLightColor(nc)
- lc = tonumber(nc)
- end
- end
- local function checkPower()
- local pct, nc = require("computer").energy()/(require("computer").maxEnergy()/100), 0
- if (pct >= 35) then
- setLight(0x00ff00)
- elseif (pct >= 20 and pct < 35) then
- setLight(0xffff33)
- elseif (pct >= 10 and pct < 20) then
- setLight(0xffa000)
- elseif (pct < 10) then
- setLight(0xff4040)
- end
- end
- function start()
- setLight(0x60c0ff)
- if (pmtid == 0) then
- pmtid = event.timer(5, checkPower, math.huge)
- end
- end
- function stop()
- if (pmtid > 0) then
- event.cancel(pmtid)
- pmtid = 0
- end
- setLight(0x60c0ff)
- end
- ]]--
- local function install()
- local sp = os.getenv("PWD")
- local fs = require("filesystem")
- if (fs.exists(sp.."rplm.lua")) then
- if (fs.exists("/etc/rc.d/rplm.lua")) then
- return false, "rplm already installed."
- else
- local sf = io.open(sp.."rplm.lua", "r")
- if (sf) then
- local tf = io.open("/etc/rc.d/rplm.lua", "w")
- if (tf) then
- local running, wc = true, false
- while running do
- local l = sf:read("*l")
- if (l == "--[[") then
- wc = true
- elseif (l == "]]--") then
- wc = nil
- running = nil
- elseif (string.sub(l, 1, 2) == "--" and not wc) then
- tf:write(l.."\n")
- elseif (wc) then
- tf:write(l.."\n")
- end
- end
- tf:close()
- sf:close()
- return true, ""
- end
- end
- end
- else
- return false, "rplm not found!\nPlease change to directory containing installer and try again."
- end
- end
- local function uninstall()
- local rc, fs = require("rc"), require("filesystem")
- if (rc) then
- local result, reason = rc.runCommand("rplm", "stop")
- result, reason = rc.runCommand("rplm", "disable")
- if (result) then
- if (fs.exists("/etc/rc.d/rplm.lua")) then
- fs.remove("/etc/rc.d/rplm.lua")
- return true, ""
- end
- else
- return false, reason
- end
- else
- return false, "rc not accessible!"
- end
- end
- local function showUsage()
- print("Robot Power Level Monitor")
- print("rplm -i/-u")
- print(" -i : install daemon")
- print(" -u : uninstall daemon")
- end
- local function checkRun(a)
- local bot = require("robot")
- if (bot) then
- if (a == "-i") then
- local s, r = install()
- if (s) then
- local rc = require("rc")
- if (rc) then
- s, r = rc.runCommand("rplm", "enable")
- s, r = rc.runCommand("rplm", "start")
- end
- print("rplm installed.")
- else
- print("rplm install failed:")
- print(r)
- end
- elseif (a == "-u") then
- local s, r = uninstall()
- if (s) then
- print("rplm uninstalled.")
- else
- print("rplm uninstall failed:")
- print(r)
- end
- else
- showUsage()
- end
- else
- showUsage()
- end
- end
- local args = {...}
- if (#args == 1) then
- checkRun(string.lower(args[1]))
- else
- showUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement