cc-mek-scada/supervisor/startup.lua

113 lines
3.4 KiB
Lua
Raw Normal View History

2022-01-13 15:23:38 +00:00
--
2022-03-25 16:18:33 +00:00
-- Nuclear Generation Facility SCADA Supervisor
--
local log = require("scada-common.log")
local ppm = require("scada-common.ppm")
local util = require("scada-common.util")
2022-03-25 16:18:33 +00:00
2022-05-11 15:31:02 +00:00
local svsessions = require("supervisor.session.svsessions")
2022-03-25 16:18:33 +00:00
2022-05-11 15:31:02 +00:00
local config = require("supervisor.config")
local supervisor = require("supervisor.supervisor")
2022-04-22 15:07:59 +00:00
2022-05-11 16:31:19 +00:00
local SUPERVISOR_VERSION = "alpha-v0.3.5"
2022-03-25 16:18:33 +00:00
2022-04-18 15:07:16 +00:00
local print = util.print
local println = util.println
2022-03-25 16:18:33 +00:00
local print_ts = util.print_ts
2022-04-18 15:07:16 +00:00
local println_ts = util.println_ts
2022-03-25 16:18:33 +00:00
2022-04-29 17:36:00 +00:00
log.init(config.LOG_PATH, config.LOG_MODE)
log.info("========================================")
log.info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
log.info("========================================")
2022-04-18 15:07:16 +00:00
println(">> SCADA Supervisor " .. SUPERVISOR_VERSION .. " <<")
2022-03-25 16:18:33 +00:00
2022-04-18 15:07:16 +00:00
-- mount connected devices
ppm.mount_all()
2022-03-25 16:18:33 +00:00
2022-04-18 15:07:16 +00:00
local modem = ppm.get_wireless_modem()
2022-03-25 16:18:33 +00:00
if modem == nil then
2022-04-18 15:07:16 +00:00
println("boot> wireless modem not found")
log.warning("no wireless modem on startup")
2022-03-25 16:18:33 +00:00
return
end
-- start comms, open all channels
local superv_comms = supervisor.comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN)
2022-03-25 16:18:33 +00:00
-- base loop clock (6.67Hz, 3 ticks)
local MAIN_CLOCK = 0.15
local loop_clock = util.new_clock(MAIN_CLOCK)
2022-03-25 16:18:33 +00:00
-- event loop
while true do
---@diagnostic disable-next-line: undefined-field
2022-03-25 16:18:33 +00:00
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
-- handle event
2022-04-18 15:07:16 +00:00
if event == "peripheral_detach" then
local type, device = ppm.handle_unmount(param1)
if type ~= nil and device ~= nil then
if type == "modem" then
-- we only care if this is our wireless modem
if device == modem then
println_ts("wireless modem disconnected!")
log.error("comms modem disconnected!")
else
log.warning("non-comms modem disconnected")
end
2022-04-18 15:07:16 +00:00
end
end
elseif event == "peripheral" then
local type, device = ppm.mount(param1)
if type ~= nil and device ~= nil then
if type == "modem" then
if device.isWireless() then
-- reconnected modem
modem = device
superv_comms.reconnect_modem(modem)
println_ts("wireless modem reconnected.")
log.info("comms modem reconnected.")
else
log.info("wired modem reconnected.")
end
2022-04-18 15:07:16 +00:00
end
end
elseif event == "timer" and loop_clock.is_clock(param1) then
2022-04-23 16:12:33 +00:00
-- main loop tick
-- iterate sessions
svsessions.iterate_all()
-- free any closed sessions
svsessions.free_all_closed()
loop_clock.start()
2022-04-23 16:12:33 +00:00
elseif event == "timer" then
-- a non-clock timer event, check watchdogs
2022-04-23 16:12:33 +00:00
svsessions.check_all_watchdogs(param1)
2022-03-25 16:18:33 +00:00
elseif event == "modem_message" then
-- got a packet
local packet = superv_comms.parse_packet(param1, param2, param3, param4, param5)
2022-04-22 15:07:59 +00:00
superv_comms.handle_packet(packet)
2022-04-18 15:07:16 +00:00
end
-- check for termination request
if event == "terminate" or ppm.should_terminate() then
2022-05-02 15:42:24 +00:00
println_ts("closing sessions...")
log.info("terminate requested, closing sessions...")
2022-05-02 15:42:24 +00:00
svsessions.close_all()
log.info("sessions closed")
2022-04-18 15:07:16 +00:00
break
2022-03-25 16:18:33 +00:00
end
end
2022-04-18 15:07:16 +00:00
println_ts("exited")
log.info("exited")