Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Remote monitor component for reactor control program
- --
- -- Author: VirtualDXS
- -- Based heavily on original reactor control program by kla_sch
- --
- -- History:
- -- v0.1, 2020-01-26
- -- First version
- --
- --
- -- Constant values (configuration)
- --
- -- Port to receive telemetry on (requires wireless modem)
- receivePort=39124
- -- Energy thresholds - make sure these match the reactor
- critEnergy=3000000
- lowEnergy=7000000
- highEnergy=9000000
- --
- -- Write text with colors, if possible (advance monitor)
- --
- -- Parameters:
- -- mon - handle of monitor
- -- color - text color
- -- text - text to write
- --
- function writeColor(mon, color, text)
- if (mon.isColor()) then
- mon.setTextColor(color)
- end
- mon.write(text)
- if (mon.isColor()) then
- mon.setTextColor(colors.white)
- end
- end
- --
- -- Display reactor status to a monitor.
- --
- -- Parameters:
- -- mon - handle of monitor.
- -- state - state of reactor (table, see main loop for structure)
- --
- function displayStatusToMonitor(mon, state)
- mon.clear()
- -- First get the monitor size and try to scale, if the feature ist
- -- available.
- if mon.setTextScale ~= nil then
- -- reset text scale
- mon.setTextScale(1)
- end
- local width, height = mon.getSize()
- if width < 15 or height < 5 then
- -- too small: try to scale down.
- if mon.setTextScale ~= nil then
- mon.setTextScale(0.5)
- else
- return -- too small and no text scale available
- end
- width, height = mon.getSize()
- if width < 15 or height < 5 then
- return -- still too small
- end
- else
- -- Huge monitors? Try to scale up, if possible (max scale=5).
- local scale = math.min(width / 16, height / 5, 5)
- scale = math.floor(scale * 2) / 2 -- multiple of 0.5
- if scale > 1 and mon.setTextScale ~= nil then
- mon.setTextScale(scale)
- width, height = mon.getSize()
- end
- end
- --
- -- Output the data
- --
- mon.setCursorPos(1,1)
- mon.write("Reactor")
- mon.setCursorPos(1,3)
- mon.write("Status: ")
- if state.power then
- writeColor(mon, colors.green, "ACTIVE")
- else
- writeColor(mon, colors.red, "INACTIVE")
- end
- mon.setCursorPos(1,4)
- mon.write("Rod Level: " .. state.rodLevel .. "%")
- mon.setCursorPos(1,5)
- if width < 16 then
- mon.write("Cell: ") -- One block monitor (15x5 with scale 0.5)
- else
- mon.write("Energy Buffer: ")
- end
- local c
- if state.cellEnergy < critEnergy then
- c = colors.red -- Red: We use too much energy
- elseif state.cellEnergy > lowEnergy then
- c = colors.green -- Green: More energy then low water mark
- else
- c = colors.orange -- Orange: Less energy the low water mark, but OK
- end
- writeColor(mon, c, string.format("%d", math.floor(state.cellEnergy/1000 + 0.5)))
- mon.write(" kRF")
- end
- --
- -- Display reactor status to any connected monitor and also to console.
- --
- -- Parameters:
- -- state - state of reactor (table, see main loop for structure)
- --
- function displayStatus(state)
- displayStatusToMonitor(term, state) -- console
- term.setCursorPos(1,7)
- term.write("* Hold Ctrl-T to terminate program")
- term.setCursorPos(1,8)
- local pList = peripheral.getNames()
- local i, name
- for i, name in pairs(pList) do
- if peripheral.getType(name) == "monitor" then
- -- found monitor as peripheral
- displayStatusToMonitor(peripheral.wrap(name), state)
- end
- end
- end
- --
- -- Receive and unserialize telemetry from a reactor.
- -- After perfoming some sanity checks, sets the global variable `state` to
- -- the received table.
- --
- function receiveTelemetry()
- _,_,_,_,message = os.pullEvent("modem_message")
- print("Message received: " .. message)
- local rxState = textutils.unserialize(message)
- if type(rxState.power) == "boolean"
- and type(rxState.rodLevel) == "number"
- and type(rxState.cellEnergy) == "number" then
- print("pass")
- state = rxState
- end
- end
- --
- -- Find the first connected wireless modem and return the wrapped handle.
- --
- -- If no wireless modem is found this function terminates the program.
- --
- -- Return:
- -- Handle of first connected wireless modem found.
- --
- function getModemHandle()
- local pList = peripheral.getNames()
- local i, name
- for i, name in pairs(pList) do
- if peripheral.getType(name) == "modem" then
- if peripheral.call(name,"isWireless") then
- return peripheral.wrap(name)
- end
- end
- end
- error("No wireless modem connected: Exit program")
- exit()
- end
- modem = getModemHandle()
- modem.open(receivePort)
- --
- -- Endless loop: Receive telemetry, display reactor state,
- -- and wait for 5 seconds.
- --
- while true do
- state = nil
- local success,err = pcall(receiveTelemetry)
- if not success then print(err) end
- if state then
- displayStatus(state)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement