Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Top: list, click reactor
- --Needed info:
- --Poweron
- --Energy
- --Millibuckets per sec
- --Total rf
- --RF per reactor
- --RF per millibucket
- --Functionality:
- --shutdown button
- --automatic toggle
- function update_reactors()
- local reactor_names = modem.getNamesRemote()
- assert(#reactor_names ~= 0, "No reactors found!")
- return reactor_names
- end
- function get_reactor_values(reactor_name)
- local stuff = {}
- local function call(f)
- return modem.callRemote(reactor_name, f)
- end
- -- camelCase because lua's string matching is weird with words, so it thinks snake_case is snake case and not it's own word
- stuff["on"] = call("getActive")
- stuff["energy"] = call("getEnergyStored")
- stuff["energyPercentage"] = (stuff["energy"] / max_energy_total) * 100
- stuff["fuel"] = call("getFuelAmount")
- stuff["fuel_max"] = call("getFuelAmountMax")
- stuff["fuelPercentage"] = (stuff["fuel"] / stuff["fuel_max"]) * 100
- return stuff
- end
- function draw_column_names()
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write("ID State Energy Fuel")
- end
- function draw_reactor_status(index, reactor_data)
- monitor.setCursorPos(1, index + 1)
- monitor.write(tostring(index))
- monitor.setCursorPos(4, index + 1)
- if reactor_data["on"] then
- monitor.write("On")
- else
- monitor.write("Off")
- end
- local function truncate(val, len)
- local str = tostring(val)
- if str:len() > len then
- str = str:sub(1, len)
- end
- return str
- end
- monitor.setCursorPos(10, index + 1)
- monitor.write(truncate(reactor_data["energyPercentage"], 4) .. "%")
- monitor.setCursorPos(17, index + 1)
- monitor.write(truncate(reactor_data["fuelPercentage"], 4) .. "%")
- end
- function handle_control_request()
- local function split_string(str)
- local result = {}
- for word in string.gmatch(str, "%w+") do
- table.insert(result, word)
- end
- return result
- end
- local function contains_key(tab, k)
- return tab[k] ~= nil
- end
- while true do
- local event, modem_side, sender_channel, reply_channel, msg, distance = os.pullEvent("modem_message")
- if event == "modem_message" and modem_side == "left" then
- local output = ""
- local split = split_string(msg)
- print("Recieved message: " .. msg)
- if split[1] == "get" then
- if #split < 3 then
- wireless.transmit(reply_channel, 1, "ERROR: at least 3 Arguments needed")
- elseif not contains_key(reactors, tonumber(split[2])) then
- output = "ERROR: No such reactor: " .. split[2]
- else
- local reactor_values = get_reactor_values(reactors[tonumber(split[2])])
- for i = 3, #split do
- if not contains_key(reactor_values, split[i]) then
- output = "ERROR: No such entry: " .. split[i]
- else
- output = output .. split[i] .. ": " .. tostring(reactor_values[split[i]]) .. "\n"
- end
- end
- end
- else
- output = "ERROR: Unknown command: " .. msg
- end
- wireless.transmit(reply_channel, 1, output)
- end
- sleep(1)
- end
- end
- function main_loop()
- while true do
- reactors = update_reactors()
- draw_column_names()
- for i = 1, #reactors do
- local info = get_reactor_values(reactors[i])
- local percentage = info["energyPercentage"]
- local active = modem.callRemote(reactors[i], "getActive")
- if percentage < min_energyPercentage and not active then
- modem.callRemote(reactors[i], "setActive", true)
- print("Powered on reactor " .. i .. " at " .. os.time() .. " " .. os.day())
- elseif percentage >= max_energyPercentage and active then
- modem.callRemote(reactors[i], "setActive", false)
- print("Shut down reactor " .. i .. " at " .. os.time() .. " " .. os.day())
- end
- --Might have changed
- info["on"] = modem.callRemote(reactors[i], "getActive")
- draw_reactor_status(i, info)
- end
- sleep(1) --Don't crash server
- end
- end
- -- Init stuff
- modem = assert(peripheral.wrap("back"), "Modem not connected")
- wireless = assert(peripheral.wrap("left"), "Wireless not connected")
- assert(wireless.isWireless(), "Left modem not wireless")
- monitor = assert(peripheral.wrap("top"), "Monitor not connected")
- -- Settings
- max_energy_total = 10000000
- max_energyPercentage = 99.9 -- Power doesn't always go up to 100% due to drain
- min_energyPercentage = 90
- modem.open(0)
- wireless.open(1)
- monitor.clear()
- reactors = update_reactors()
- parallel.waitForAny(main_loop, handle_control_request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement