Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Tame your naughty diesel generator with this script! No more wasted power!
- print("Starting Diesel Control v3.5\n")
- --End config
- --Constants
- local component = require("component")
- local event = require("event")
- local diesel = component.ie_diesel_generator
- local gpu = component.gpu
- --Variables
- local run = true
- --Enable computer control of the generator.
- diesel.enableComputerControl(true)
- os.sleep(1)
- diesel.setEnabled(false)
- local function display(str)
- local x = gpu.getViewport() - (string.len(str) - 1)
- gpu.set(x, 1, str)
- end
- local function getCaps()
- --Reset on every read
- local totalMax = 0
- local total = 0
- --Read every capacitor's max charge, and it's charge
- for addr, typ in component.list("v_capacitor") do
- totalMax = totalMax + component.invoke(addr, "getMaxEnergyStored")
- total = total + component.invoke(addr, "getEnergyStored")
- end
- if totalMax == 0 then error("no capacitors :(") end
- --Return the charge level in percent form.
- return math.ceil((total / totalMax) * 100)
- end
- local function getTank()
- local raw = diesel.getTankInfo()
- local out = {}
- out["percent"] = math.ceil((raw["amount"] / raw["capacity"]) * 100)
- out["type"] = raw["label"]
- return out
- end
- local function run()
- --Read the charge level in percent
- charge = getCaps()
- --Read the fuel level and type
- if charge ~= lastMeas then
- if charge > 90 then
- status = "Off "
- diesel.setEnabled(false)
- elseif charge < 80 then
- status = " On "
- diesel.setEnabled(true)
- end
- display(status .. charge .. "%")
- end
- --Set the last measurement so we can tell if it has
- --changed while we were away.
- lastMeas = charge
- end
- local timer = event.timer(1, run, math.huge)
- local function stop()
- print("Caught interrupt, stopping.")
- --Stop main control loop
- event.cancel(timer)
- --Shutdown generator
- diesel.setEnabled(false)
- diesel.enableComputerControl(false)
- --Stop script
- event.ignore("interrupted", stop)
- print("Stopped")
- end
- event.listen("interrupted", stop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement