2022-01-13 15:12:44 +00:00
|
|
|
--
|
|
|
|
-- RTU: Remote Terminal Unit
|
|
|
|
--
|
|
|
|
|
2022-05-14 17:32:42 +00:00
|
|
|
require("/initenv").init_env()
|
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
local types = require("scada-common.types")
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local config = require("rtu.config")
|
|
|
|
local modbus = require("rtu.modbus")
|
|
|
|
local rtu = require("rtu.rtu")
|
|
|
|
local threads = require("rtu.threads")
|
|
|
|
|
|
|
|
local boilerv_rtu = require("rtu.dev.boilerv_rtu")
|
|
|
|
local envd_rtu = require("rtu.dev.envd_rtu")
|
|
|
|
local imatrix_rtu = require("rtu.dev.imatrix_rtu")
|
|
|
|
local redstone_rtu = require("rtu.dev.redstone_rtu")
|
|
|
|
local sna_rtu = require("rtu.dev.sna_rtu")
|
|
|
|
local sps_rtu = require("rtu.dev.sps_rtu")
|
|
|
|
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
|
|
|
|
|
2022-09-23 01:31:07 +00:00
|
|
|
local RTU_VERSION = "beta-v0.8.2"
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-05-03 14:44:18 +00:00
|
|
|
local rtu_t = types.rtu_t
|
|
|
|
|
2022-04-11 15:08:46 +00:00
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
2022-03-15 16:02:31 +00:00
|
|
|
local print_ts = util.print_ts
|
2022-04-11 15:08:46 +00:00
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
2022-06-05 19:09:02 +00:00
|
|
|
----------------------------------------
|
|
|
|
-- config validation
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
local cfv = util.new_validator()
|
|
|
|
|
|
|
|
cfv.assert_port(config.SERVER_PORT)
|
|
|
|
cfv.assert_port(config.LISTEN_PORT)
|
|
|
|
cfv.assert_type_str(config.LOG_PATH)
|
|
|
|
cfv.assert_type_int(config.LOG_MODE)
|
|
|
|
cfv.assert_type_table(config.RTU_DEVICES)
|
|
|
|
cfv.assert_type_table(config.RTU_REDSTONE)
|
|
|
|
assert(cfv.valid(), "bad config file: missing/invalid fields")
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- log init
|
|
|
|
----------------------------------------
|
|
|
|
|
2022-04-29 17:36:00 +00:00
|
|
|
log.init(config.LOG_PATH, config.LOG_MODE)
|
2022-04-29 17:32:37 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("========================================")
|
|
|
|
log.info("BOOTING rtu.startup " .. RTU_VERSION)
|
|
|
|
log.info("========================================")
|
2022-06-05 18:49:50 +00:00
|
|
|
println(">> RTU GATEWAY " .. RTU_VERSION .. " <<")
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
|
|
|
-- startup
|
|
|
|
----------------------------------------
|
|
|
|
|
2022-03-15 16:02:31 +00:00
|
|
|
-- mount connected devices
|
|
|
|
ppm.mount_all()
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
---@class rtu_shared_memory
|
2022-04-27 16:46:04 +00:00
|
|
|
local __shared_memory = {
|
|
|
|
-- RTU system state flags
|
2022-05-11 16:03:15 +00:00
|
|
|
---@class rtu_state
|
2022-04-27 16:46:04 +00:00
|
|
|
rtu_state = {
|
|
|
|
linked = false,
|
|
|
|
shutdown = false
|
|
|
|
},
|
|
|
|
|
|
|
|
-- core RTU devices
|
|
|
|
rtu_dev = {
|
|
|
|
modem = ppm.get_wireless_modem()
|
|
|
|
},
|
|
|
|
|
|
|
|
-- system objects
|
|
|
|
rtu_sys = {
|
2022-05-11 16:03:15 +00:00
|
|
|
rtu_comms = nil, ---@type rtu_comms
|
|
|
|
conn_watchdog = nil, ---@type watchdog
|
|
|
|
units = {} ---@type table
|
2022-04-27 16:46:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
-- message queues
|
|
|
|
q = {
|
2022-04-27 19:52:34 +00:00
|
|
|
mq_comms = mqueue.new()
|
2022-04-27 16:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local smem_dev = __shared_memory.rtu_dev
|
2022-04-27 19:52:34 +00:00
|
|
|
local smem_sys = __shared_memory.rtu_sys
|
2022-04-27 16:46:04 +00:00
|
|
|
|
2022-03-15 16:02:31 +00:00
|
|
|
-- get modem
|
2022-04-27 16:46:04 +00:00
|
|
|
if smem_dev.modem == nil then
|
2022-04-07 15:44:17 +00:00
|
|
|
println("boot> wireless modem not found")
|
2022-05-29 18:26:40 +00:00
|
|
|
log.fatal("no wireless modem on startup")
|
2022-03-15 16:02:31 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
2022-04-27 16:46:04 +00:00
|
|
|
-- interpret config and init units
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
|
|
|
|
2022-04-27 16:46:04 +00:00
|
|
|
local units = __shared_memory.rtu_sys.units
|
|
|
|
|
2022-04-18 02:36:18 +00:00
|
|
|
local rtu_redstone = config.RTU_REDSTONE
|
|
|
|
local rtu_devices = config.RTU_DEVICES
|
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- configure RTU gateway based on config file definitions
|
|
|
|
local function configure()
|
|
|
|
-- redstone interfaces
|
|
|
|
for entry_idx = 1, #rtu_redstone do
|
|
|
|
local rs_rtu = redstone_rtu.new()
|
|
|
|
local io_table = rtu_redstone[entry_idx].io
|
|
|
|
local io_reactor = rtu_redstone[entry_idx].for_reactor
|
|
|
|
|
|
|
|
-- CHECK: reactor ID must be >= to 1
|
2022-06-05 20:51:38 +00:00
|
|
|
if (not util.is_int(io_reactor)) or (io_reactor <= 0) then
|
2022-06-05 18:49:50 +00:00
|
|
|
println(util.c("configure> redstone entry #", entry_idx, " : ", io_reactor, " isn't an integer >= 1"))
|
|
|
|
return false
|
|
|
|
end
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- CHECK: io table exists
|
|
|
|
if type(io_table) ~= "table" then
|
|
|
|
println(util.c("configure> redstone entry #", entry_idx, " no IO table found"))
|
|
|
|
return false
|
|
|
|
end
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
local capabilities = {}
|
2022-05-18 18:30:48 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
log.debug(util.c("configure> starting redstone RTU I/O linking for reactor ", io_reactor, "..."))
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
local continue = true
|
2022-05-18 18:30:48 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- check for duplicate entries
|
|
|
|
for i = 1, #units do
|
|
|
|
local unit = units[i] ---@type rtu_unit_registry_entry
|
|
|
|
if unit.reactor == io_reactor and unit.type == rtu_t.redstone then
|
|
|
|
-- duplicate entry
|
|
|
|
local message = util.c("configure> skipping definition block #", entry_idx, " for reactor ", io_reactor,
|
|
|
|
" with already defined redstone I/O")
|
|
|
|
println(message)
|
|
|
|
log.warning(message)
|
|
|
|
continue = false
|
|
|
|
break
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- not a duplicate
|
|
|
|
if continue then
|
|
|
|
for i = 1, #io_table do
|
|
|
|
local valid = false
|
|
|
|
local conf = io_table[i]
|
|
|
|
|
|
|
|
-- verify configuration
|
|
|
|
if rsio.is_valid_channel(conf.channel) and rsio.is_valid_side(conf.side) then
|
|
|
|
if conf.bundled_color then
|
|
|
|
valid = rsio.is_color(conf.bundled_color)
|
2022-05-18 18:30:48 +00:00
|
|
|
else
|
2022-06-05 18:49:50 +00:00
|
|
|
valid = true
|
2022-05-18 18:30:48 +00:00
|
|
|
end
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not valid then
|
|
|
|
local message = util.c("configure> invalid redstone definition at index ", i, " in definition block #", entry_idx,
|
|
|
|
" (for reactor ", io_reactor, ")")
|
|
|
|
println(message)
|
|
|
|
log.error(message)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
-- link redstone in RTU
|
|
|
|
local mode = rsio.get_io_mode(conf.channel)
|
|
|
|
if mode == rsio.IO_MODE.DIGITAL_IN then
|
|
|
|
-- can't have duplicate inputs
|
|
|
|
if util.table_contains(capabilities, conf.channel) then
|
|
|
|
local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
|
|
|
|
println(message)
|
|
|
|
log.warning(message)
|
|
|
|
else
|
|
|
|
rs_rtu.link_di(conf.side, conf.bundled_color)
|
|
|
|
end
|
|
|
|
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
|
|
|
|
rs_rtu.link_do(conf.channel, conf.side, conf.bundled_color)
|
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
|
|
|
-- can't have duplicate inputs
|
|
|
|
if util.table_contains(capabilities, conf.channel) then
|
|
|
|
local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
|
|
|
|
println(message)
|
|
|
|
log.warning(message)
|
|
|
|
else
|
|
|
|
rs_rtu.link_ai(conf.side)
|
|
|
|
end
|
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
|
|
|
rs_rtu.link_ao(conf.side)
|
2022-05-18 18:30:48 +00:00
|
|
|
else
|
2022-06-05 18:49:50 +00:00
|
|
|
-- should be unreachable code, we already validated channels
|
|
|
|
log.error("configure> fell through if chain attempting to identify IO mode", true)
|
|
|
|
println("configure> encountered a software error, check logs")
|
|
|
|
return false
|
2022-05-18 18:30:48 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
table.insert(capabilities, conf.channel)
|
2022-05-18 18:30:48 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
log.debug(util.c("configure> linked redstone ", #capabilities, ": ", rsio.to_string(conf.channel),
|
|
|
|
" (", conf.side, ") for reactor ", io_reactor))
|
|
|
|
end
|
2022-05-18 18:30:48 +00:00
|
|
|
end
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
---@class rtu_unit_registry_entry
|
|
|
|
local unit = {
|
|
|
|
name = "redstone_io",
|
|
|
|
type = rtu_t.redstone,
|
|
|
|
index = entry_idx,
|
|
|
|
reactor = io_reactor,
|
|
|
|
device = capabilities, -- use device field for redstone channels
|
2022-09-21 19:53:51 +00:00
|
|
|
rtu = rs_rtu, ---@type rtu_device|rtu_rs_device
|
2022-06-05 18:49:50 +00:00
|
|
|
modbus_io = modbus.new(rs_rtu, false),
|
2022-09-21 19:53:51 +00:00
|
|
|
pkt_queue = nil, ---@type mqueue|nil
|
2022-06-05 18:49:50 +00:00
|
|
|
thread = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(units, unit)
|
|
|
|
|
|
|
|
log.debug(util.c("init> initialized RTU unit #", #units, ": redstone_io (redstone) [1] for reactor ", io_reactor))
|
|
|
|
end
|
2022-05-18 18:30:48 +00:00
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- mounted peripherals
|
|
|
|
for i = 1, #rtu_devices do
|
|
|
|
local name = rtu_devices[i].name
|
|
|
|
local index = rtu_devices[i].index
|
|
|
|
local for_reactor = rtu_devices[i].for_reactor
|
|
|
|
|
|
|
|
-- CHECK: name is a string
|
|
|
|
if type(name) ~= "string" then
|
|
|
|
println(util.c("configure> device entry #", i, ": device ", name, " isn't a string"))
|
|
|
|
return false
|
2022-03-15 16:02:31 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- CHECK: index is an integer >= 1
|
2022-06-05 20:51:38 +00:00
|
|
|
if (not util.is_int(index)) or (index <= 0) then
|
2022-06-05 18:49:50 +00:00
|
|
|
println(util.c("configure> device entry #", i, ": index ", index, " isn't an integer >= 1"))
|
|
|
|
return false
|
|
|
|
end
|
2022-04-29 17:19:01 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- CHECK: reactor is an integer >= 1
|
2022-06-05 20:51:38 +00:00
|
|
|
if (not util.is_int(for_reactor)) or (for_reactor <= 0) then
|
2022-06-05 18:49:50 +00:00
|
|
|
println(util.c("configure> device entry #", i, ": reactor ", for_reactor, " isn't an integer >= 1"))
|
|
|
|
return false
|
|
|
|
end
|
2022-04-29 17:19:01 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
local device = ppm.get_periph(name)
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
if device == nil then
|
|
|
|
local message = util.c("configure> '", name, "' not found")
|
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
local type = ppm.get_type(name)
|
|
|
|
local rtu_iface = nil ---@type rtu_device
|
|
|
|
local rtu_type = ""
|
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
if type == "boilerValve" then
|
2022-06-05 18:49:50 +00:00
|
|
|
-- boiler multiblock
|
|
|
|
rtu_type = rtu_t.boiler_valve
|
|
|
|
rtu_iface = boilerv_rtu.new(device)
|
|
|
|
elseif type == "turbineValve" then
|
2022-09-21 19:53:51 +00:00
|
|
|
-- turbine multiblock
|
2022-06-05 18:49:50 +00:00
|
|
|
rtu_type = rtu_t.turbine_valve
|
|
|
|
rtu_iface = turbinev_rtu.new(device)
|
|
|
|
elseif type == "inductionPort" then
|
2022-09-21 19:53:51 +00:00
|
|
|
-- induction matrix multiblock
|
2022-06-05 18:49:50 +00:00
|
|
|
rtu_type = rtu_t.induction_matrix
|
|
|
|
rtu_iface = imatrix_rtu.new(device)
|
2022-09-21 19:53:51 +00:00
|
|
|
elseif type == "spsPort" then
|
|
|
|
-- SPS multiblock
|
|
|
|
rtu_type = rtu_t.sps
|
|
|
|
rtu_iface = sps_rtu.new(device)
|
|
|
|
elseif type == "solarNeutronActivator" then
|
|
|
|
-- SNA
|
|
|
|
rtu_type = rtu_t.sps
|
|
|
|
rtu_iface = sna_rtu.new(device)
|
2022-06-05 18:49:50 +00:00
|
|
|
elseif type == "environmentDetector" then
|
|
|
|
-- advanced peripherals environment detector
|
|
|
|
rtu_type = rtu_t.env_detector
|
|
|
|
rtu_iface = envd_rtu.new(device)
|
|
|
|
else
|
|
|
|
local message = util.c("configure> device '", name, "' is not a known type (", type, ")")
|
|
|
|
println_ts(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if rtu_iface ~= nil then
|
|
|
|
---@class rtu_unit_registry_entry
|
|
|
|
local rtu_unit = {
|
|
|
|
name = name,
|
|
|
|
type = rtu_type,
|
|
|
|
index = index,
|
|
|
|
reactor = for_reactor,
|
|
|
|
device = device,
|
2022-09-21 19:53:51 +00:00
|
|
|
rtu = rtu_iface, ---@type rtu_device|rtu_rs_device
|
2022-06-05 18:49:50 +00:00
|
|
|
modbus_io = modbus.new(rtu_iface, true),
|
2022-09-21 19:53:51 +00:00
|
|
|
pkt_queue = mqueue.new(), ---@type mqueue|nil
|
2022-06-05 18:49:50 +00:00
|
|
|
thread = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rtu_unit.thread = threads.thread__unit_comms(__shared_memory, rtu_unit)
|
|
|
|
|
|
|
|
table.insert(units, rtu_unit)
|
|
|
|
|
|
|
|
log.debug(util.c("configure> initialized RTU unit #", #units, ": ", name, " (", rtu_type, ") [", index, "] for reactor ", for_reactor))
|
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
end
|
|
|
|
end
|
2022-06-05 18:49:50 +00:00
|
|
|
|
|
|
|
-- we made it through all that trusting-user-to-write-a-config-file chaos
|
|
|
|
return true
|
2022-03-15 16:02:31 +00:00
|
|
|
end
|
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
2022-04-27 16:46:04 +00:00
|
|
|
-- start system
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
log.debug("boot> running configure()")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
if configure() then
|
|
|
|
-- start connection watchdog
|
|
|
|
smem_sys.conn_watchdog = util.new_watchdog(5)
|
|
|
|
log.debug("boot> conn watchdog started")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- setup comms
|
|
|
|
smem_sys.rtu_comms = rtu.comms(RTU_VERSION, smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT, smem_sys.conn_watchdog)
|
|
|
|
log.debug("boot> comms init")
|
2022-04-11 21:27:57 +00:00
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- init threads
|
|
|
|
local main_thread = threads.thread__main(__shared_memory)
|
|
|
|
local comms_thread = threads.thread__comms(__shared_memory)
|
|
|
|
|
|
|
|
-- assemble thread list
|
|
|
|
local _threads = { main_thread.p_exec, comms_thread.p_exec }
|
|
|
|
for i = 1, #units do
|
|
|
|
if units[i].thread ~= nil then
|
|
|
|
table.insert(_threads, units[i].thread.p_exec)
|
|
|
|
end
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 18:49:50 +00:00
|
|
|
-- run threads
|
|
|
|
parallel.waitForAll(table.unpack(_threads))
|
|
|
|
else
|
|
|
|
println("configuration failed, exiting...")
|
|
|
|
end
|
2022-04-18 14:21:29 +00:00
|
|
|
|
|
|
|
println_ts("exited")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("exited")
|