2022-01-13 15:12:44 +00:00
|
|
|
--
|
|
|
|
-- RTU: Remote Terminal Unit
|
|
|
|
--
|
|
|
|
|
2022-05-04 17:37:01 +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("config")
|
|
|
|
local modbus = require("modbus")
|
|
|
|
local rtu = require("rtu")
|
|
|
|
local threads = require("threads")
|
|
|
|
|
|
|
|
local redstone_rtu = require("dev.redstone_rtu")
|
|
|
|
local boiler_rtu = require("dev.boiler_rtu")
|
|
|
|
local boilerv_rtu = require("dev.boilerv_rtu")
|
|
|
|
local energymachine_rtu = require("dev.energymachine_rtu")
|
|
|
|
local imatrix_rtu = require("dev.imatrix_rtu")
|
|
|
|
local turbine_rtu = require("dev.turbine_rtu")
|
|
|
|
local turbinev_rtu = require("dev.turbinev_rtu")
|
|
|
|
|
|
|
|
local RTU_VERSION = "alpha-v0.6.0"
|
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-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-04-11 15:08:46 +00:00
|
|
|
println(">> RTU " .. 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-04-27 16:46:04 +00:00
|
|
|
local __shared_memory = {
|
|
|
|
-- RTU system state flags
|
|
|
|
rtu_state = {
|
|
|
|
linked = false,
|
|
|
|
shutdown = false
|
|
|
|
},
|
|
|
|
|
|
|
|
-- core RTU devices
|
|
|
|
rtu_dev = {
|
|
|
|
modem = ppm.get_wireless_modem()
|
|
|
|
},
|
|
|
|
|
|
|
|
-- system objects
|
|
|
|
rtu_sys = {
|
|
|
|
rtu_comms = nil,
|
2022-04-29 14:19:05 +00:00
|
|
|
conn_watchdog = nil,
|
2022-04-27 16:46:04 +00:00
|
|
|
units = {}
|
|
|
|
},
|
|
|
|
|
|
|
|
-- 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-04 17:37:01 +00:00
|
|
|
log.warning("no wireless modem on startup")
|
2022-03-15 16:02:31 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
smem_sys.rtu_comms = rtu.comms(smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT)
|
2022-03-15 16:02:31 +00:00
|
|
|
|
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-03-23 19:41:08 +00:00
|
|
|
-- redstone interfaces
|
2022-04-18 02:36:18 +00:00
|
|
|
for reactor_idx = 1, #rtu_redstone do
|
2022-04-18 14:09:44 +00:00
|
|
|
local rs_rtu = redstone_rtu.new()
|
2022-04-18 02:36:18 +00:00
|
|
|
local io_table = rtu_redstone[reactor_idx].io
|
2022-03-23 19:41:08 +00:00
|
|
|
|
|
|
|
local capabilities = {}
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("init> starting redstone RTU I/O linking for reactor " .. rtu_redstone[reactor_idx].for_reactor .. "...")
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
for i = 1, #io_table do
|
|
|
|
local valid = false
|
2022-04-27 16:46:04 +00:00
|
|
|
local conf = io_table[i]
|
2022-03-23 19:41:08 +00:00
|
|
|
|
|
|
|
-- verify configuration
|
2022-04-27 16:46:04 +00:00
|
|
|
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-03-23 19:41:08 +00:00
|
|
|
else
|
|
|
|
valid = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-18 02:36:18 +00:00
|
|
|
if not valid then
|
2022-04-18 13:35:08 +00:00
|
|
|
local message = "init> invalid redstone definition at index " .. i .. " in definition block #" .. reactor_idx ..
|
|
|
|
" (for reactor " .. rtu_redstone[reactor_idx].for_reactor .. ")"
|
2022-04-11 15:08:46 +00:00
|
|
|
println_ts(message)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(message)
|
2022-03-23 19:41:08 +00:00
|
|
|
else
|
|
|
|
-- link redstone in RTU
|
2022-04-27 16:46:04 +00:00
|
|
|
local mode = rsio.get_io_mode(conf.channel)
|
2022-03-23 19:41:08 +00:00
|
|
|
if mode == rsio.IO_MODE.DIGITAL_IN then
|
2022-04-27 16:46:04 +00:00
|
|
|
rs_rtu.link_di(conf.channel, conf.side, conf.bundled_color)
|
2022-03-23 19:41:08 +00:00
|
|
|
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
|
2022-04-27 16:46:04 +00:00
|
|
|
rs_rtu.link_do(conf.channel, conf.side, conf.bundled_color)
|
2022-03-23 19:41:08 +00:00
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
2022-04-27 16:46:04 +00:00
|
|
|
rs_rtu.link_ai(conf.channel, conf.side)
|
2022-03-23 19:41:08 +00:00
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
2022-04-27 16:46:04 +00:00
|
|
|
rs_rtu.link_ao(conf.channel, conf.side)
|
2022-03-23 19:41:08 +00:00
|
|
|
else
|
|
|
|
-- should be unreachable code, we already validated channels
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("init> fell through if chain attempting to identify IO mode", true)
|
2022-03-23 19:41:08 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2022-04-27 16:46:04 +00:00
|
|
|
table.insert(capabilities, conf.channel)
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("init> linked redstone " .. #capabilities .. ": " .. rsio.to_string(conf.channel) .. " (" .. conf.side ..
|
2022-04-18 02:36:18 +00:00
|
|
|
") for reactor " .. rtu_redstone[reactor_idx].for_reactor)
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(units, {
|
|
|
|
name = "redstone_io",
|
2022-05-03 14:44:18 +00:00
|
|
|
type = rtu_t.redstone,
|
2022-03-23 19:41:08 +00:00
|
|
|
index = 1,
|
2022-04-18 02:36:18 +00:00
|
|
|
reactor = rtu_redstone[reactor_idx].for_reactor,
|
2022-03-23 19:41:08 +00:00
|
|
|
device = capabilities, -- use device field for redstone channels
|
|
|
|
rtu = rs_rtu,
|
2022-05-03 15:39:03 +00:00
|
|
|
modbus_io = modbus.new(rs_rtu, false),
|
2022-04-29 17:19:01 +00:00
|
|
|
modbus_busy = false,
|
|
|
|
pkt_queue = nil,
|
|
|
|
thread = nil
|
2022-03-23 19:41:08 +00:00
|
|
|
})
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("init> initialized RTU unit #" .. #units .. ": redstone_io (redstone) [1] for reactor " .. rtu_redstone[reactor_idx].for_reactor)
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
|
|
|
|
-- mounted peripherals
|
2022-04-18 02:36:18 +00:00
|
|
|
for i = 1, #rtu_devices do
|
|
|
|
local device = ppm.get_periph(rtu_devices[i].name)
|
2022-03-15 16:02:31 +00:00
|
|
|
|
|
|
|
if device == nil then
|
2022-04-18 02:36:18 +00:00
|
|
|
local message = "init> '" .. rtu_devices[i].name .. "' not found"
|
2022-04-11 15:08:46 +00:00
|
|
|
println_ts(message)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(message)
|
2022-03-15 16:02:31 +00:00
|
|
|
else
|
2022-04-18 02:36:18 +00:00
|
|
|
local type = ppm.get_type(rtu_devices[i].name)
|
2022-03-15 16:02:31 +00:00
|
|
|
local rtu_iface = nil
|
|
|
|
local rtu_type = ""
|
|
|
|
|
|
|
|
if type == "boiler" then
|
|
|
|
-- boiler multiblock
|
2022-05-03 14:44:18 +00:00
|
|
|
rtu_type = rtu_t.boiler
|
2022-04-18 14:09:44 +00:00
|
|
|
rtu_iface = boiler_rtu.new(device)
|
2022-05-04 15:23:45 +00:00
|
|
|
elseif type == "boilerValve" then
|
|
|
|
-- boiler multiblock (10.1+)
|
|
|
|
rtu_type = rtu_t.boiler_valve
|
|
|
|
rtu_iface = boilerv_rtu.new(device)
|
2022-03-15 16:02:31 +00:00
|
|
|
elseif type == "turbine" then
|
|
|
|
-- turbine multiblock
|
2022-05-03 14:44:18 +00:00
|
|
|
rtu_type = rtu_t.turbine
|
2022-04-18 14:09:44 +00:00
|
|
|
rtu_iface = turbine_rtu.new(device)
|
2022-05-04 15:23:45 +00:00
|
|
|
elseif type == "turbineValve" then
|
|
|
|
-- turbine multiblock (10.1+)
|
|
|
|
rtu_type = rtu_t.turbine_valve
|
|
|
|
rtu_iface = turbinev_rtu.new(device)
|
2022-03-15 16:02:31 +00:00
|
|
|
elseif type == "mekanismMachine" then
|
2022-05-03 14:45:35 +00:00
|
|
|
-- assumed to be an induction matrix multiblock, pre Mekanism 10.1
|
2022-05-04 15:23:45 +00:00
|
|
|
-- also works with energy cubes
|
2022-05-03 14:45:35 +00:00
|
|
|
rtu_type = rtu_t.energy_machine
|
|
|
|
rtu_iface = energymachine_rtu.new(device)
|
2022-05-04 15:23:45 +00:00
|
|
|
elseif type == "inductionPort" then
|
|
|
|
-- induction matrix multiblock (10.1+)
|
2022-05-03 14:44:18 +00:00
|
|
|
rtu_type = rtu_t.induction_matrix
|
2022-04-18 14:09:44 +00:00
|
|
|
rtu_iface = imatrix_rtu.new(device)
|
2022-03-15 16:02:31 +00:00
|
|
|
else
|
2022-04-18 02:36:18 +00:00
|
|
|
local message = "init> device '" .. rtu_devices[i].name .. "' is not a known type (" .. type .. ")"
|
2022-04-11 15:08:46 +00:00
|
|
|
println_ts(message)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(message)
|
2022-03-15 16:02:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if rtu_iface ~= nil then
|
2022-04-29 17:19:01 +00:00
|
|
|
local rtu_unit = {
|
2022-04-18 02:36:18 +00:00
|
|
|
name = rtu_devices[i].name,
|
2022-03-15 16:02:31 +00:00
|
|
|
type = rtu_type,
|
2022-04-18 02:36:18 +00:00
|
|
|
index = rtu_devices[i].index,
|
|
|
|
reactor = rtu_devices[i].for_reactor,
|
2022-03-15 16:02:31 +00:00
|
|
|
device = device,
|
2022-03-23 19:41:08 +00:00
|
|
|
rtu = rtu_iface,
|
2022-05-03 15:39:03 +00:00
|
|
|
modbus_io = modbus.new(rtu_iface, true),
|
2022-04-29 17:19:01 +00:00
|
|
|
modbus_busy = false,
|
|
|
|
pkt_queue = mqueue.new(),
|
|
|
|
thread = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rtu_unit.thread = threads.thread__unit_comms(__shared_memory, rtu_unit)
|
|
|
|
|
|
|
|
table.insert(units, rtu_unit)
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("init> initialized RTU unit #" .. #units .. ": " .. rtu_devices[i].name .. " (" .. rtu_type .. ") [" ..
|
2022-04-18 02:36:18 +00:00
|
|
|
rtu_devices[i].index .. "] for reactor " .. rtu_devices[i].for_reactor)
|
2022-03-15 16:02:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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-04-27 16:46:04 +00:00
|
|
|
-- init threads
|
|
|
|
local main_thread = threads.thread__main(__shared_memory)
|
|
|
|
local comms_thread = threads.thread__comms(__shared_memory)
|
2022-04-11 21:27:57 +00:00
|
|
|
|
2022-04-29 14:19:05 +00:00
|
|
|
-- start connection watchdog
|
|
|
|
smem_sys.conn_watchdog = util.new_watchdog(5)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("init> conn watchdog started")
|
2022-04-29 14:19:05 +00:00
|
|
|
|
2022-04-29 17:19:01 +00:00
|
|
|
-- assemble thread list
|
|
|
|
local _threads = { main_thread.exec, comms_thread.exec }
|
|
|
|
for i = 1, #units do
|
|
|
|
if units[i].thread ~= nil then
|
|
|
|
table.insert(_threads, units[i].thread.exec)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-27 16:46:04 +00:00
|
|
|
-- run threads
|
2022-04-29 17:19:01 +00:00
|
|
|
parallel.waitForAll(table.unpack(_threads))
|
2022-04-18 14:21:29 +00:00
|
|
|
|
|
|
|
println_ts("exited")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("exited")
|