mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
initial base supervisor code
This commit is contained in:
parent
b89724ad59
commit
3da7b74cfb
@ -7,22 +7,29 @@ os.loadAPI("scada-common/util.lua")
|
|||||||
os.loadAPI("scada-common/ppm.lua")
|
os.loadAPI("scada-common/ppm.lua")
|
||||||
os.loadAPI("scada-common/comms.lua")
|
os.loadAPI("scada-common/comms.lua")
|
||||||
|
|
||||||
os.loadAPI("supervisor/config.lua")
|
os.loadAPI("config.lua")
|
||||||
os.loadAPI("supervisor/supervisor.lua")
|
os.loadAPI("supervisor.lua")
|
||||||
|
|
||||||
local SUPERVISOR_VERSION = "alpha-v0.1.0"
|
local SUPERVISOR_VERSION = "alpha-v0.1.0"
|
||||||
|
|
||||||
|
local print = util.print
|
||||||
|
local println = util.println
|
||||||
local print_ts = util.print_ts
|
local print_ts = util.print_ts
|
||||||
|
local println_ts = util.println_ts
|
||||||
|
|
||||||
|
log._info("========================================")
|
||||||
|
log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
|
||||||
|
log._info("========================================")
|
||||||
|
|
||||||
|
println(">> SCADA Supervisor " .. SUPERVISOR_VERSION .. " <<")
|
||||||
|
|
||||||
|
-- mount connected devices
|
||||||
ppm.mount_all()
|
ppm.mount_all()
|
||||||
|
|
||||||
local modem = ppm.get_device("modem")
|
local modem = ppm.get_wireless_modem()
|
||||||
|
|
||||||
print("| SCADA Supervisor - " .. SUPERVISOR_VERSION .. " |")
|
|
||||||
|
|
||||||
-- we need a modem
|
|
||||||
if modem == nil then
|
if modem == nil then
|
||||||
print("Please connect a modem.")
|
println("boot> wireless modem not found")
|
||||||
|
log._warning("no wireless modem on startup")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -33,34 +40,57 @@ if config.SYSTEM_TYPE == "active" then
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- start comms, open all channels
|
-- 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)
|
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)
|
-- base loop clock (4Hz, 5 ticks)
|
||||||
local loop_tick = os.startTimer(0.25)
|
local loop_clock = os.startTimer(0.25)
|
||||||
|
|
||||||
-- event loop
|
-- event loop
|
||||||
while true do
|
while true do
|
||||||
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
||||||
|
|
||||||
-- handle event
|
-- handle event
|
||||||
if event == "timer" and param1 == loop_tick then
|
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
|
||||||
-- basic event tick, send keep-alives
|
-- basic event tick, send keep-alives
|
||||||
|
loop_clock = os.startTimer(0.25)
|
||||||
elseif event == "modem_message" then
|
elseif event == "modem_message" then
|
||||||
-- got a packet
|
-- got a packet
|
||||||
elseif event == "terminate" then
|
end
|
||||||
-- safe exit
|
|
||||||
print_ts("[alert] terminated\n")
|
-- check for termination request
|
||||||
|
if event == "terminate" or ppm.should_terminate() then
|
||||||
|
log._warning("terminate requested, exiting...")
|
||||||
-- todo: attempt failover, alert hot backup
|
-- todo: attempt failover, alert hot backup
|
||||||
return
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
println_ts("exited")
|
||||||
|
log._info("exited")
|
||||||
|
@ -12,4 +12,31 @@ function superv_comms(mode, num_reactors, modem, dev_listen, fo_channel, sv_chan
|
|||||||
sv_channel = sv_channel,
|
sv_channel = sv_channel,
|
||||||
reactor_struct_cache = nil
|
reactor_struct_cache = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- PRIVATE FUNCTIONS --
|
||||||
|
|
||||||
|
-- open all channels
|
||||||
|
local _open_channels = function ()
|
||||||
|
if not self.modem.isOpen(self.dev_listen) then
|
||||||
|
self.modem.open(self.dev_listen)
|
||||||
|
end
|
||||||
|
if not self.modem.isOpen(self.fo_channel) then
|
||||||
|
self.modem.open(self.fo_channel)
|
||||||
|
end
|
||||||
|
if not self.modem.isOpen(self.sv_channel) then
|
||||||
|
self.modem.open(self.sv_channel)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- PUBLIC FUNCTIONS --
|
||||||
|
|
||||||
|
-- reconnect a newly connected modem
|
||||||
|
local reconnect_modem = function (modem)
|
||||||
|
self.modem = modem
|
||||||
|
_open_channels()
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
reconnect_modem = reconnect_modem
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user