diff --git a/supervisor/scada-supervisor.lua b/supervisor/scada-supervisor.lua deleted file mode 100644 index 28a7e40..0000000 --- a/supervisor/scada-supervisor.lua +++ /dev/null @@ -1,55 +0,0 @@ --- --- Nuclear Generation Facility SCADA Supervisor --- - -os.loadAPI("scada-common/util.lua") -os.loadAPI("scada-common/comms.lua") -os.loadAPI("supervisor/config.lua") - -local SUPERVISOR_VERSION = "alpha-v0.1" - -local print_ts = util.print_ts - -local modem = peripheral.find("modem") - -print("| SCADA Supervisor - " .. SUPERVISOR_VERSION .. " |") - --- we need a modem -if modem == nil then - print("No modem found, exiting...") - return -end - --- determine active/backup mode -local mode = comms.SCADA_SV_MODES.BACKUP -if config.SYSTEM_TYPE == "active" then - mode = comms.SCADA_SV_MODES.ACTIVE -end - --- start comms, open all channels -if not modem.isOpen(config.SCADA_DEV_LISTEN) then - modem.open(config.SCADA_DEV_LISTEN) -end -if not modem.isOpen(config.SCADA_FO_CHANNEL) then - modem.open(config.SCADA_FO_CHANNEL) -end -if not modem.isOpen(config.SCADA_SV_CHANNEL) then - modem.open(config.SCADA_SV_CHANNEL) -end - -local comms = comms.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_FO_CHANNEL, config.SCADA_SV_CHANNEL) - --- base loop clock (4Hz, 5 ticks) -local loop_tick = os.startTimer(0.25) - --- event loop -while true do - local event, param1, param2, param3, param4, param5 = os.pullEvent() - - -- handle event - if event == "timer" and param1 == loop_tick then - -- basic event tick, send keep-alives - elseif event == "modem_message" then - -- got a packet - end -end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 2ee4d40..4ff56a9 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -1,3 +1,66 @@ -- --- Multi-Reactor Controller Server & GUI --- \ No newline at end of file +-- Nuclear Generation Facility SCADA Supervisor +-- + +os.loadAPI("scada-common/log.lua") +os.loadAPI("scada-common/util.lua") +os.loadAPI("scada-common/ppm.lua") +os.loadAPI("scada-common/comms.lua") + +os.loadAPI("supervisor/config.lua") +os.loadAPI("supervisor/supervisor.lua") + +local SUPERVISOR_VERSION = "alpha-v0.1.0" + +local print_ts = util.print_ts + +ppm.mount_all() + +local modem = ppm.get_device("modem") + +print("| SCADA Supervisor - " .. SUPERVISOR_VERSION .. " |") + +-- we need a modem +if modem == nil then + print("Please connect a modem.") + return +end + +-- determine active/backup mode +local mode = comms.SCADA_SV_MODES.BACKUP +if config.SYSTEM_TYPE == "active" then + mode = comms.SCADA_SV_MODES.ACTIVE +end + +-- start comms, open all channels +if not modem.isOpen(config.SCADA_DEV_LISTEN) then + modem.open(config.SCADA_DEV_LISTEN) +end +if not modem.isOpen(config.SCADA_FO_CHANNEL) then + modem.open(config.SCADA_FO_CHANNEL) +end +if not modem.isOpen(config.SCADA_SV_CHANNEL) then + modem.open(config.SCADA_SV_CHANNEL) +end + +local comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_FO_CHANNEL, config.SCADA_SV_CHANNEL) + +-- base loop clock (4Hz, 5 ticks) +local loop_tick = os.startTimer(0.25) + +-- event loop +while true do + local event, param1, param2, param3, param4, param5 = os.pullEventRaw() + + -- handle event + if event == "timer" and param1 == loop_tick then + -- basic event tick, send keep-alives + elseif event == "modem_message" then + -- got a packet + elseif event == "terminate" then + -- safe exit + print_ts("[alert] terminated\n") + -- todo: attempt failover, alert hot backup + return + end +end diff --git a/supervisor/supervisor.lua b/supervisor/supervisor.lua new file mode 100644 index 0000000..5f988a8 --- /dev/null +++ b/supervisor/supervisor.lua @@ -0,0 +1,15 @@ +-- #REQUIRES comms.lua + +-- supervisory controller communications +function superv_comms(mode, num_reactors, modem, dev_listen, fo_channel, sv_channel) + local self = { + mode = mode, + seq_num = 0, + num_reactors = num_reactors, + modem = modem, + dev_listen = dev_listen, + fo_channel = fo_channel, + sv_channel = sv_channel, + reactor_struct_cache = nil + } +end