2022-01-13 15:23:38 +00:00
|
|
|
--
|
2022-03-25 16:18:33 +00:00
|
|
|
-- 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")
|
2022-04-22 15:07:59 +00:00
|
|
|
os.loadAPI("scada-common/modbus.lua")
|
2022-04-27 16:21:10 +00:00
|
|
|
os.loadAPI("scada-common/mqueue.lua")
|
2022-03-25 16:18:33 +00:00
|
|
|
|
2022-04-18 15:07:16 +00:00
|
|
|
os.loadAPI("config.lua")
|
2022-03-25 16:18:33 +00:00
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
os.loadAPI("session/rtu.lua")
|
|
|
|
os.loadAPI("session/plc.lua")
|
|
|
|
os.loadAPI("session/coordinator.lua")
|
|
|
|
os.loadAPI("session/svsessions.lua")
|
|
|
|
|
2022-04-24 17:22:45 +00:00
|
|
|
os.loadAPI("supervisor.lua")
|
|
|
|
|
2022-04-29 02:41:08 +00:00
|
|
|
local SUPERVISOR_VERSION = "alpha-v0.1.8"
|
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-18 15:07:16 +00:00
|
|
|
log._info("========================================")
|
|
|
|
log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
|
|
|
|
log._info("========================================")
|
|
|
|
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
|
2022-04-24 17:22:45 +00:00
|
|
|
local superv_comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN)
|
2022-03-25 16:18:33 +00:00
|
|
|
|
2022-04-27 22:52:06 +00:00
|
|
|
-- base loop clock (6.67Hz, 3 ticks)
|
|
|
|
local MAIN_CLOCK = 0.15
|
|
|
|
local loop_clock = os.startTimer(MAIN_CLOCK)
|
2022-03-25 16:18:33 +00:00
|
|
|
|
|
|
|
-- event loop
|
|
|
|
while true do
|
|
|
|
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 device = ppm.handle_unmount(param1)
|
|
|
|
|
|
|
|
if device.type == "modem" then
|
|
|
|
-- we only care if this is our wireless modem
|
|
|
|
if device.dev == modem then
|
|
|
|
println_ts("wireless modem disconnected!")
|
|
|
|
log._error("comms modem disconnected!")
|
|
|
|
else
|
|
|
|
log._warning("non-comms modem disconnected")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif event == "peripheral" then
|
|
|
|
local type, device = ppm.mount(param1)
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
elseif event == "timer" and param1 == loop_clock 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()
|
|
|
|
|
2022-04-27 22:52:06 +00:00
|
|
|
loop_clock = os.startTimer(MAIN_CLOCK)
|
2022-04-23 16:12:33 +00:00
|
|
|
elseif event == "timer" then
|
|
|
|
-- another timer event, check watchdogs
|
|
|
|
svsessions.check_all_watchdogs(param1)
|
2022-03-25 16:18:33 +00:00
|
|
|
elseif event == "modem_message" then
|
|
|
|
-- got a packet
|
2022-04-26 01:00:50 +00:00
|
|
|
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-04-29 17:20:56 +00:00
|
|
|
log._info("terminate requested, exiting...")
|
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")
|