cc-mek-scada/reactor-plc/startup.lua

141 lines
3.6 KiB
Lua
Raw Normal View History

--
-- Reactor Programmable Logic Controller
--
os.loadAPI("scada-common/log.lua")
os.loadAPI("scada-common/util.lua")
2022-03-14 18:19:14 +00:00
os.loadAPI("scada-common/ppm.lua")
os.loadAPI("scada-common/comms.lua")
2022-04-27 16:21:10 +00:00
os.loadAPI("scada-common/mqueue.lua")
2022-03-14 18:19:14 +00:00
os.loadAPI("config.lua")
os.loadAPI("plc.lua")
2022-04-25 15:40:53 +00:00
os.loadAPI("threads.lua")
2022-04-27 20:24:28 +00:00
local R_PLC_VERSION = "alpha-v0.4.3"
local print = util.print
local println = util.println
local print_ts = util.print_ts
local println_ts = util.println_ts
log._info("========================================")
log._info("BOOTING reactor-plc.startup " .. R_PLC_VERSION)
log._info("========================================")
println(">> Reactor PLC " .. R_PLC_VERSION .. " <<")
-- mount connected devices
ppm.mount_all()
2022-04-25 15:40:53 +00:00
-- shared memory across threads
local __shared_memory = {
2022-04-27 16:21:10 +00:00
-- networked setting
2022-04-25 15:40:53 +00:00
networked = config.NETWORKED,
2022-04-27 16:21:10 +00:00
-- PLC system state flags
2022-04-25 15:40:53 +00:00
plc_state = {
init_ok = true,
2022-04-27 16:21:10 +00:00
shutdown = false,
2022-04-25 15:40:53 +00:00
scram = true,
degraded = false,
no_reactor = false,
no_modem = false
},
2022-04-27 16:21:10 +00:00
-- core PLC devices
plc_dev = {
2022-04-25 15:40:53 +00:00
reactor = ppm.get_fission_reactor(),
modem = ppm.get_wireless_modem()
},
2022-04-27 16:37:28 +00:00
-- system objects
2022-04-27 16:21:10 +00:00
plc_sys = {
2022-04-25 15:40:53 +00:00
iss = nil,
plc_comms = nil,
conn_watchdog = nil
2022-04-27 16:21:10 +00:00
},
-- message queues
q = {
mq_iss = mqueue.new(),
2022-04-27 19:52:34 +00:00
mq_comms = mqueue.new()
2022-04-25 15:40:53 +00:00
}
}
2022-04-27 16:21:10 +00:00
local smem_dev = __shared_memory.plc_dev
local smem_sys = __shared_memory.plc_sys
2022-04-25 15:40:53 +00:00
local plc_state = __shared_memory.plc_state
-- we need a reactor and a modem
2022-04-25 15:40:53 +00:00
if smem_dev.reactor == nil then
println("boot> fission reactor not found");
log._warning("no reactor on startup")
2022-04-05 13:41:06 +00:00
plc_state.init_ok = false
plc_state.degraded = true
plc_state.no_reactor = true
end
2022-04-25 15:40:53 +00:00
if networked and smem_dev.modem == nil then
2022-04-07 15:44:17 +00:00
println("boot> wireless modem not found")
log._warning("no wireless modem on startup")
2022-04-25 15:40:53 +00:00
if smem_dev.reactor ~= nil then
smem_dev.reactor.scram()
2022-04-05 13:41:06 +00:00
end
plc_state.init_ok = false
plc_state.degraded = true
plc_state.no_modem = true
end
function init()
if plc_state.init_ok then
-- just booting up, no fission allowed (neutrons stay put thanks)
2022-04-25 15:40:53 +00:00
smem_dev.reactor.scram()
-- init internal safety system
2022-04-25 15:40:53 +00:00
smem_sys.iss = plc.iss_init(smem_dev.reactor)
log._debug("iss init")
2022-04-25 15:40:53 +00:00
if __shared_memory.networked then
-- start comms
2022-04-25 15:40:53 +00:00
smem_sys.plc_comms = plc.comms_init(config.REACTOR_ID, smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT, smem_dev.reactor, smem_sys.iss)
log._debug("comms init")
-- comms watchdog, 3 second timeout
2022-04-25 15:40:53 +00:00
smem_sys.conn_watchdog = util.new_watchdog(3)
log._debug("conn watchdog started")
else
println("boot> starting in offline mode");
log._debug("running without networking")
end
2022-04-25 15:40:53 +00:00
os.queueEvent("clock_start")
println("boot> completed");
else
println("boot> system in degraded state, awaiting devices...")
log._warning("booted in a degraded state, awaiting peripheral connections...")
end
end
-- initialize PLC
init()
2022-04-25 15:40:53 +00:00
-- init threads
2022-04-27 16:21:10 +00:00
local main_thread = threads.thread__main(__shared_memory, init)
local iss_thread = threads.thread__iss(__shared_memory)
local comms_thread = threads.thread__comms(__shared_memory)
2022-04-25 15:40:53 +00:00
-- run threads
if __shared_memory.networked then
parallel.waitForAll(main_thread.exec, iss_thread.exec, comms_thread.exec)
else
parallel.waitForAll(main_thread.exec, iss_thread.exec)
end
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_SHUTDOWN) ?
println_ts("exited")
log._info("exited")