#33 lua module/require architecture changeover
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
local config = {}
|
||||
|
||||
-- scada network listen for PLC's and RTU's
|
||||
SCADA_DEV_LISTEN = 16000
|
||||
config.SCADA_DEV_LISTEN = 16000
|
||||
-- listen port for SCADA supervisor access by coordinators
|
||||
SCADA_SV_LISTEN = 16100
|
||||
config.SCADA_SV_LISTEN = 16100
|
||||
-- expected number of reactors
|
||||
NUM_REACTORS = 4
|
||||
config.NUM_REACTORS = 4
|
||||
-- log path
|
||||
LOG_PATH = "/log.txt"
|
||||
config.LOG_PATH = "/log.txt"
|
||||
-- log mode
|
||||
-- 0 = APPEND (adds to existing file on start)
|
||||
-- 1 = NEW (replaces existing file on start)
|
||||
LOG_MODE = 0
|
||||
config.LOG_MODE = 0
|
||||
|
||||
return config
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
local coordinator = {}
|
||||
|
||||
return coordinator
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
-- #REQUIRES mqueue.lua
|
||||
-- #REQUIRES comms.lua
|
||||
-- #REQUIRES log.lua
|
||||
-- #REQUIRES util.lua
|
||||
local comms = require("scada-common.comms")
|
||||
local log = require("scada-common.log")
|
||||
local mqueue = require("scada-common.mqueue")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local plc = {}
|
||||
|
||||
local PROTOCOLS = comms.PROTOCOLS
|
||||
local RPLC_TYPES = comms.RPLC_TYPES
|
||||
@@ -16,19 +18,21 @@ local println_ts = util.println_ts
|
||||
local INITIAL_WAIT = 1500
|
||||
local RETRY_PERIOD = 1000
|
||||
|
||||
PLC_S_CMDS = {
|
||||
local PLC_S_CMDS = {
|
||||
SCRAM = 0,
|
||||
ENABLE = 1,
|
||||
BURN_RATE = 2,
|
||||
ISS_CLEAR = 3
|
||||
}
|
||||
|
||||
plc.PLC_S_CMDS = PLC_S_CMDS
|
||||
|
||||
local PERIODICS = {
|
||||
KEEP_ALIVE = 2.0
|
||||
}
|
||||
|
||||
-- PLC supervisor session
|
||||
function new_session(id, for_reactor, in_queue, out_queue)
|
||||
plc.new_session = function (id, for_reactor, in_queue, out_queue)
|
||||
local log_header = "plc_session(" .. id .. "): "
|
||||
|
||||
local self = {
|
||||
@@ -204,7 +208,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
if pkt.length == 1 then
|
||||
return pkt.data[1]
|
||||
else
|
||||
log._warning(log_header .. "RPLC ACK length mismatch")
|
||||
log.warning(log_header .. "RPLC ACK length mismatch")
|
||||
return nil
|
||||
end
|
||||
end
|
||||
@@ -215,7 +219,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
if self.r_seq_num == nil then
|
||||
self.r_seq_num = pkt.scada_frame.seq_num()
|
||||
elseif self.r_seq_num >= pkt.scada_frame.seq_num() then
|
||||
log._warning(log_header .. "sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. pkt.scada_frame.seq_num())
|
||||
log.warning(log_header .. "sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. pkt.scada_frame.seq_num())
|
||||
return
|
||||
else
|
||||
self.r_seq_num = pkt.scada_frame.seq_num()
|
||||
@@ -225,7 +229,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
if pkt.scada_frame.protocol() == PROTOCOLS.RPLC then
|
||||
-- check reactor ID
|
||||
if pkt.id ~= for_reactor then
|
||||
log._warning(log_header .. "RPLC packet with ID not matching reactor ID: reactor " .. self.for_reactor .. " != " .. pkt.id)
|
||||
log.warning(log_header .. "RPLC packet with ID not matching reactor ID: reactor " .. self.for_reactor .. " != " .. pkt.id)
|
||||
return
|
||||
end
|
||||
|
||||
@@ -242,13 +246,13 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.last_rtt = srv_now - srv_start
|
||||
|
||||
if self.last_rtt > 500 then
|
||||
log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 500ms (" .. self.last_rtt .. ")")
|
||||
log.warning(log_header .. "PLC KEEP_ALIVE round trip time > 500ms (" .. self.last_rtt .. ")")
|
||||
end
|
||||
|
||||
-- log._debug(log_header .. "RPLC RTT = ".. self.last_rtt .. "ms")
|
||||
-- log._debug(log_header .. "RPLC TT = ".. (srv_now - plc_send) .. "ms")
|
||||
-- log.debug(log_header .. "RPLC RTT = ".. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "RPLC TT = ".. (srv_now - plc_send) .. "ms")
|
||||
else
|
||||
log._debug(log_header .. "RPLC keep alive packet length mismatch")
|
||||
log.debug(log_header .. "RPLC keep alive packet length mismatch")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.STATUS then
|
||||
-- status packet received, update data
|
||||
@@ -267,11 +271,11 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.received_status_cache = true
|
||||
else
|
||||
-- error copying status data
|
||||
log._error(log_header .. "failed to parse status packet data")
|
||||
log.error(log_header .. "failed to parse status packet data")
|
||||
end
|
||||
end
|
||||
else
|
||||
log._debug(log_header .. "RPLC status packet length mismatch")
|
||||
log.debug(log_header .. "RPLC status packet length mismatch")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.MEK_STRUCT then
|
||||
-- received reactor structure, record it
|
||||
@@ -282,10 +286,10 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.received_struct = true
|
||||
else
|
||||
-- error copying structure data
|
||||
log._error(log_header .. "failed to parse struct packet data")
|
||||
log.error(log_header .. "failed to parse struct packet data")
|
||||
end
|
||||
else
|
||||
log._debug(log_header .. "RPLC struct packet length mismatch")
|
||||
log.debug(log_header .. "RPLC struct packet length mismatch")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.MEK_SCRAM then
|
||||
-- SCRAM acknowledgement
|
||||
@@ -294,7 +298,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.acks.scram = true
|
||||
self.sDB.control_state = false
|
||||
elseif ack == false then
|
||||
log._debug(log_header .. "SCRAM failed!")
|
||||
log.debug(log_header .. "SCRAM failed!")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.MEK_ENABLE then
|
||||
-- enable acknowledgement
|
||||
@@ -303,7 +307,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.acks.enable = true
|
||||
self.sDB.control_state = true
|
||||
elseif ack == false then
|
||||
log._debug(log_header .. "enable failed!")
|
||||
log.debug(log_header .. "enable failed!")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.MEK_BURN_RATE then
|
||||
-- burn rate acknowledgement
|
||||
@@ -311,7 +315,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
if ack then
|
||||
self.acks.burn_rate = true
|
||||
elseif ack == false then
|
||||
log._debug(log_header .. "burn rate update failed!")
|
||||
log.debug(log_header .. "burn rate update failed!")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.ISS_STATUS then
|
||||
-- ISS status packet received, copy data
|
||||
@@ -321,10 +325,10 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
-- copied in ISS status data OK
|
||||
else
|
||||
-- error copying ISS status data
|
||||
log._error(log_header .. "failed to parse ISS status packet data")
|
||||
log.error(log_header .. "failed to parse ISS status packet data")
|
||||
end
|
||||
else
|
||||
log._debug(log_header .. "RPLC ISS status packet length mismatch")
|
||||
log.debug(log_header .. "RPLC ISS status packet length mismatch")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.ISS_ALARM then
|
||||
-- ISS alarm
|
||||
@@ -337,10 +341,10 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
-- copied in ISS status data OK
|
||||
else
|
||||
-- error copying ISS status data
|
||||
log._error(log_header .. "failed to parse ISS alarm status data")
|
||||
log.error(log_header .. "failed to parse ISS alarm status data")
|
||||
end
|
||||
else
|
||||
log._debug(log_header .. "RPLC ISS alarm packet length mismatch")
|
||||
log.debug(log_header .. "RPLC ISS alarm packet length mismatch")
|
||||
end
|
||||
elseif pkt.type == RPLC_TYPES.ISS_CLEAR then
|
||||
-- ISS clear acknowledgement
|
||||
@@ -350,17 +354,17 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.sDB.iss_tripped = false
|
||||
self.sDB.iss_trip_cause = "ok"
|
||||
elseif ack == false then
|
||||
log._debug(log_header .. "ISS clear failed")
|
||||
log.debug(log_header .. "ISS clear failed")
|
||||
end
|
||||
else
|
||||
log._debug(log_header .. "handler received unsupported RPLC packet type " .. pkt.type)
|
||||
log.debug(log_header .. "handler received unsupported RPLC packet type " .. pkt.type)
|
||||
end
|
||||
elseif pkt.scada_frame.protocol() == PROTOCOLS.SCADA_MGMT then
|
||||
if pkt.type == SCADA_MGMT_TYPES.CLOSE then
|
||||
-- close the session
|
||||
self.connected = false
|
||||
else
|
||||
log._debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type)
|
||||
log.debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -402,7 +406,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
self.connected = false
|
||||
_send_mgmt(SCADA_MGMT_TYPES.CLOSE, {})
|
||||
println("connection to reactor " .. self.for_reactor .. " PLC closed by server")
|
||||
log._info(log_header .. "session closed by server")
|
||||
log.info(log_header .. "session closed by server")
|
||||
end
|
||||
|
||||
-- iterate the session
|
||||
@@ -454,7 +458,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
|
||||
-- max 100ms spent processing queue
|
||||
if util.time() - handle_start > 100 then
|
||||
log._warning(log_header .. "exceeded 100ms queue process limit")
|
||||
log.warning(log_header .. "exceeded 100ms queue process limit")
|
||||
break
|
||||
end
|
||||
end
|
||||
@@ -463,7 +467,7 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
if not self.connected then
|
||||
self.plc_conn_watchdog.cancel()
|
||||
println("connection to reactor " .. self.for_reactor .. " PLC closed by remote host")
|
||||
log._info(log_header .. "session closed by remote host")
|
||||
log.info(log_header .. "session closed by remote host")
|
||||
return self.connected
|
||||
end
|
||||
|
||||
@@ -559,3 +563,5 @@ function new_session(id, for_reactor, in_queue, out_queue)
|
||||
iterate = iterate
|
||||
}
|
||||
end
|
||||
|
||||
return plc
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
local rtu = {}
|
||||
|
||||
return rtu
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
-- #REQUIRES mqueue.lua
|
||||
-- #REQUIRES log.lua
|
||||
local log = require("scada-common.log")
|
||||
local mqueue = require("scada-common.mqueue")
|
||||
|
||||
local coordinator = require("session.coordinator")
|
||||
local plc = require("session.plc")
|
||||
local rtu = require("session.rtu")
|
||||
|
||||
-- Supervisor Sessions Handler
|
||||
|
||||
SESSION_TYPE = {
|
||||
local svsessions = {}
|
||||
|
||||
local SESSION_TYPE = {
|
||||
RTU_SESSION = 0,
|
||||
PLC_SESSION = 1,
|
||||
COORD_SESSION = 2
|
||||
}
|
||||
|
||||
svsessions.SESSION_TYPE = SESSION_TYPE
|
||||
|
||||
local self = {
|
||||
modem = nil,
|
||||
num_reactors = 0,
|
||||
@@ -20,12 +28,97 @@ local self = {
|
||||
next_coord_id = 0
|
||||
}
|
||||
|
||||
function link_modem(modem)
|
||||
-- PRIVATE FUNCTIONS --
|
||||
|
||||
-- iterate all the given sessions
|
||||
local function _iterate(sessions)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
local ok = session.instance.iterate()
|
||||
if ok then
|
||||
-- send packets in out queue
|
||||
while session.out_queue.ready() do
|
||||
local msg = session.out_queue.pop()
|
||||
if msg.qtype == mqueue.TYPE.PACKET then
|
||||
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
|
||||
end
|
||||
end
|
||||
else
|
||||
session.open = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- cleanly close a session
|
||||
local function _shutdown(session)
|
||||
session.open = false
|
||||
session.instance.close()
|
||||
|
||||
-- send packets in out queue (namely the close packet)
|
||||
while session.out_queue.ready() do
|
||||
local msg = session.out_queue.pop()
|
||||
if msg.qtype == mqueue.TYPE.PACKET then
|
||||
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
|
||||
end
|
||||
end
|
||||
|
||||
log.debug("closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
|
||||
end
|
||||
|
||||
-- close connections
|
||||
local function _close(sessions)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
_shutdown(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- check if a watchdog timer event matches that of one of the provided sessions
|
||||
local function _check_watchdogs(sessions, timer_event)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
local triggered = session.instance.check_wd(timer_event)
|
||||
if triggered then
|
||||
log.debug("watchdog closing session " .. session.instance.get_id() .. " on remote port " .. session.r_port .. "...")
|
||||
_shutdown(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- delete any closed sessions
|
||||
local function _free_closed(sessions)
|
||||
local move_to = 1
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session ~= nil then
|
||||
if sessions[i].open then
|
||||
if sessions[move_to] == nil then
|
||||
sessions[move_to] = session
|
||||
sessions[i] = nil
|
||||
end
|
||||
move_to = move_to + 1
|
||||
else
|
||||
log.debug("free'ing closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
|
||||
sessions[i] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- PUBLIC FUNCTIONS --
|
||||
|
||||
svsessions.link_modem = function (modem)
|
||||
self.modem = modem
|
||||
end
|
||||
|
||||
-- find a session by the remote port
|
||||
function find_session(remote_port)
|
||||
svsessions.find_session = function (remote_port)
|
||||
-- check RTU sessions
|
||||
for i = 1, #self.rtu_sessions do
|
||||
if self.rtu_sessions[i].r_port == remote_port then
|
||||
@@ -51,7 +144,7 @@ function find_session(remote_port)
|
||||
end
|
||||
|
||||
-- get a session by reactor ID
|
||||
function get_reactor_session(reactor)
|
||||
svsessions.get_reactor_session = function (reactor)
|
||||
local session = nil
|
||||
|
||||
for i = 1, #self.plc_sessions do
|
||||
@@ -64,8 +157,8 @@ function get_reactor_session(reactor)
|
||||
end
|
||||
|
||||
-- establish a new PLC session
|
||||
function establish_plc_session(local_port, remote_port, for_reactor)
|
||||
if get_reactor_session(for_reactor) == nil then
|
||||
svsessions.establish_plc_session = function (local_port, remote_port, for_reactor)
|
||||
if svsessions.get_reactor_session(for_reactor) == nil then
|
||||
local plc_s = {
|
||||
open = true,
|
||||
reactor = for_reactor,
|
||||
@@ -79,7 +172,7 @@ function establish_plc_session(local_port, remote_port, for_reactor)
|
||||
plc_s.instance = plc.new_session(self.next_plc_id, for_reactor, plc_s.in_queue, plc_s.out_queue)
|
||||
table.insert(self.plc_sessions, plc_s)
|
||||
|
||||
log._debug("established new PLC session to " .. remote_port .. " with ID " .. self.next_plc_id)
|
||||
log.debug("established new PLC session to " .. remote_port .. " with ID " .. self.next_plc_id)
|
||||
|
||||
self.next_plc_id = self.next_plc_id + 1
|
||||
|
||||
@@ -91,38 +184,8 @@ function establish_plc_session(local_port, remote_port, for_reactor)
|
||||
end
|
||||
end
|
||||
|
||||
-- cleanly close a session
|
||||
local function _shutdown(session)
|
||||
session.open = false
|
||||
session.instance.close()
|
||||
|
||||
-- send packets in out queue (namely the close packet)
|
||||
while session.out_queue.ready() do
|
||||
local msg = session.out_queue.pop()
|
||||
if msg.qtype == mqueue.TYPE.PACKET then
|
||||
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
|
||||
end
|
||||
end
|
||||
|
||||
log._debug("closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
|
||||
end
|
||||
|
||||
-- check if a watchdog timer event matches that of one of the provided sessions
|
||||
local function _check_watchdogs(sessions, timer_event)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
local triggered = session.instance.check_wd(timer_event)
|
||||
if triggered then
|
||||
log._debug("watchdog closing session " .. session.instance.get_id() .. " on remote port " .. session.r_port .. "...")
|
||||
_shutdown(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- attempt to identify which session's watchdog timer fired
|
||||
function check_all_watchdogs(timer_event)
|
||||
svsessions.check_all_watchdogs = function (timer_event)
|
||||
-- check RTU session watchdogs
|
||||
_check_watchdogs(self.rtu_sessions, timer_event)
|
||||
|
||||
@@ -133,29 +196,8 @@ function check_all_watchdogs(timer_event)
|
||||
_check_watchdogs(self.coord_sessions, timer_event)
|
||||
end
|
||||
|
||||
-- iterate all the given sessions
|
||||
local function _iterate(sessions)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
local ok = session.instance.iterate()
|
||||
if ok then
|
||||
-- send packets in out queue
|
||||
while session.out_queue.ready() do
|
||||
local msg = session.out_queue.pop()
|
||||
if msg.qtype == mqueue.TYPE.PACKET then
|
||||
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
|
||||
end
|
||||
end
|
||||
else
|
||||
session.open = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- iterate all sessions
|
||||
function iterate_all()
|
||||
svsessions.iterate_all = function ()
|
||||
-- iterate RTU sessions
|
||||
_iterate(self.rtu_sessions)
|
||||
|
||||
@@ -166,28 +208,8 @@ function iterate_all()
|
||||
_iterate(self.coord_sessions)
|
||||
end
|
||||
|
||||
-- delete any closed sessions
|
||||
local function _free_closed(sessions)
|
||||
local move_to = 1
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session ~= nil then
|
||||
if sessions[i].open then
|
||||
if sessions[move_to] == nil then
|
||||
sessions[move_to] = session
|
||||
sessions[i] = nil
|
||||
end
|
||||
move_to = move_to + 1
|
||||
else
|
||||
log._debug("free'ing closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
|
||||
sessions[i] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- delete all closed sessions
|
||||
function free_all_closed()
|
||||
svsessions.free_all_closed = function ()
|
||||
-- free closed RTU sessions
|
||||
_free_closed(self.rtu_sessions)
|
||||
|
||||
@@ -198,23 +220,15 @@ function free_all_closed()
|
||||
_free_closed(self.coord_sessions)
|
||||
end
|
||||
|
||||
-- close connections
|
||||
local function _close(sessions)
|
||||
for i = 1, #sessions do
|
||||
local session = sessions[i]
|
||||
if session.open then
|
||||
_shutdown(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- close all open connections
|
||||
function close_all()
|
||||
svsessions.close_all = function ()
|
||||
-- close sessions
|
||||
_close(self.rtu_sessions)
|
||||
_close(self.plc_sessions)
|
||||
_close(self.coord_sessions)
|
||||
|
||||
-- free sessions
|
||||
free_all_closed()
|
||||
svsessions.free_all_closed()
|
||||
end
|
||||
|
||||
return svsessions
|
||||
|
||||
@@ -2,23 +2,19 @@
|
||||
-- Nuclear Generation Facility SCADA Supervisor
|
||||
--
|
||||
|
||||
os.loadAPI("scada-common/log.lua")
|
||||
os.loadAPI("scada-common/types.lua")
|
||||
os.loadAPI("scada-common/util.lua")
|
||||
os.loadAPI("scada-common/ppm.lua")
|
||||
os.loadAPI("scada-common/comms.lua")
|
||||
os.loadAPI("scada-common/mqueue.lua")
|
||||
local log = require("scada-common.log")
|
||||
local ppm = require("scada-common.ppm")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
os.loadAPI("config.lua")
|
||||
local coordinator = require("session.coordinator")
|
||||
local plc = require("session.plc")
|
||||
local rtu = require("session.rtu")
|
||||
local svsessions = require("session.svsessions")
|
||||
|
||||
os.loadAPI("session/rtu.lua")
|
||||
os.loadAPI("session/plc.lua")
|
||||
os.loadAPI("session/coordinator.lua")
|
||||
os.loadAPI("session/svsessions.lua")
|
||||
local config = require("config")
|
||||
local supervisor = require("supervisor")
|
||||
|
||||
os.loadAPI("supervisor.lua")
|
||||
|
||||
local SUPERVISOR_VERSION = "alpha-v0.2.0"
|
||||
local SUPERVISOR_VERSION = "alpha-v0.3.0"
|
||||
|
||||
local print = util.print
|
||||
local println = util.println
|
||||
@@ -27,9 +23,9 @@ local println_ts = util.println_ts
|
||||
|
||||
log.init(config.LOG_PATH, config.LOG_MODE)
|
||||
|
||||
log._info("========================================")
|
||||
log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
|
||||
log._info("========================================")
|
||||
log.info("========================================")
|
||||
log.info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
|
||||
log.info("========================================")
|
||||
println(">> SCADA Supervisor " .. SUPERVISOR_VERSION .. " <<")
|
||||
|
||||
-- mount connected devices
|
||||
@@ -38,12 +34,12 @@ ppm.mount_all()
|
||||
local modem = ppm.get_wireless_modem()
|
||||
if modem == nil then
|
||||
println("boot> wireless modem not found")
|
||||
log._warning("no wireless modem on startup")
|
||||
log.warning("no wireless modem on startup")
|
||||
return
|
||||
end
|
||||
|
||||
-- start comms, open all channels
|
||||
local superv_comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN)
|
||||
local superv_comms = supervisor.comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN)
|
||||
|
||||
-- base loop clock (6.67Hz, 3 ticks)
|
||||
local MAIN_CLOCK = 0.15
|
||||
@@ -61,9 +57,9 @@ while true do
|
||||
-- we only care if this is our wireless modem
|
||||
if device.dev == modem then
|
||||
println_ts("wireless modem disconnected!")
|
||||
log._error("comms modem disconnected!")
|
||||
log.error("comms modem disconnected!")
|
||||
else
|
||||
log._warning("non-comms modem disconnected")
|
||||
log.warning("non-comms modem disconnected")
|
||||
end
|
||||
end
|
||||
elseif event == "peripheral" then
|
||||
@@ -76,9 +72,9 @@ while true do
|
||||
superv_comms.reconnect_modem(modem)
|
||||
|
||||
println_ts("wireless modem reconnected.")
|
||||
log._info("comms modem reconnected.")
|
||||
log.info("comms modem reconnected.")
|
||||
else
|
||||
log._info("wired modem reconnected.")
|
||||
log.info("wired modem reconnected.")
|
||||
end
|
||||
end
|
||||
elseif event == "timer" and param1 == loop_clock then
|
||||
@@ -103,12 +99,12 @@ while true do
|
||||
-- check for termination request
|
||||
if event == "terminate" or ppm.should_terminate() then
|
||||
println_ts("closing sessions...")
|
||||
log._info("terminate requested, closing sessions...")
|
||||
log.info("terminate requested, closing sessions...")
|
||||
svsessions.close_all()
|
||||
log._info("sessions closed")
|
||||
log.info("sessions closed")
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
println_ts("exited")
|
||||
log._info("exited")
|
||||
log.info("exited")
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
-- #REQUIRES comms.lua
|
||||
-- #REQUIRES mqueue.lua
|
||||
-- #REQUIRES util.lua
|
||||
-- #REQUIRES svsessions.lua
|
||||
local comms = require("scada-common.comms")
|
||||
local log = require("scada-common.log")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local svsessions = require("session.svsessions")
|
||||
|
||||
local supervisor = {}
|
||||
|
||||
local PROTOCOLS = comms.PROTOCOLS
|
||||
local RPLC_TYPES = comms.RPLC_TYPES
|
||||
@@ -17,7 +20,7 @@ local print_ts = util.print_ts
|
||||
local println_ts = util.println_ts
|
||||
|
||||
-- supervisory controller communications
|
||||
function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
supervisor.comms = function (num_reactors, modem, dev_listen, coord_listen)
|
||||
local self = {
|
||||
ln_seq_num = 0,
|
||||
num_reactors = num_reactors,
|
||||
@@ -101,7 +104,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
pkt = coord_pkt.get()
|
||||
end
|
||||
else
|
||||
log._debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
|
||||
log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -126,7 +129,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
if session then
|
||||
if packet.type == RPLC_TYPES.LINK_REQ then
|
||||
-- new device on this port? that's a collision
|
||||
log._debug("PLC_LNK: request from existing connection received on " .. r_port .. ", responding with collision")
|
||||
log.debug("PLC_LNK: request from existing connection received on " .. r_port .. ", responding with collision")
|
||||
_send_plc_linking(r_port, { RPLC_LINKING.COLLISION })
|
||||
else
|
||||
-- pass the packet onto the session handler
|
||||
@@ -140,20 +143,20 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
local plc_id = svsessions.establish_plc_session(l_port, r_port, packet.data[1])
|
||||
if plc_id == false then
|
||||
-- reactor already has a PLC assigned
|
||||
log._debug("PLC_LNK: assignment collision with reactor " .. packet.data[1])
|
||||
log.debug("PLC_LNK: assignment collision with reactor " .. packet.data[1])
|
||||
_send_plc_linking(r_port, { RPLC_LINKING.COLLISION })
|
||||
else
|
||||
-- got an ID; assigned to a reactor successfully
|
||||
println("connected to reactor " .. packet.data[1] .. " PLC (port " .. r_port .. ")")
|
||||
log._debug("PLC_LNK: allowed for device at " .. r_port)
|
||||
log.debug("PLC_LNK: allowed for device at " .. r_port)
|
||||
_send_plc_linking(r_port, { RPLC_LINKING.ALLOW })
|
||||
end
|
||||
else
|
||||
log._debug("PLC_LNK: new linking packet length mismatch")
|
||||
log.debug("PLC_LNK: new linking packet length mismatch")
|
||||
end
|
||||
else
|
||||
-- force a re-link
|
||||
log._debug("PLC_LNK: no session but not a link, force relink")
|
||||
log.debug("PLC_LNK: no session but not a link, force relink")
|
||||
_send_plc_linking(r_port, { RPLC_LINKING.DENY })
|
||||
end
|
||||
end
|
||||
@@ -164,7 +167,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
session.in_queue.push_packet(packet)
|
||||
end
|
||||
else
|
||||
log._debug("illegal packet type " .. protocol .. " on device listening channel")
|
||||
log.debug("illegal packet type " .. protocol .. " on device listening channel")
|
||||
end
|
||||
-- coordinator listening channel
|
||||
elseif l_port == self.coord_listen then
|
||||
@@ -173,10 +176,10 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
elseif protocol == PROTOCOLS.COORD_DATA then
|
||||
-- coordinator packet
|
||||
else
|
||||
log._debug("illegal packet type " .. protocol .. " on coordinator listening channel")
|
||||
log.debug("illegal packet type " .. protocol .. " on coordinator listening channel")
|
||||
end
|
||||
else
|
||||
log._error("received packet on unused channel " .. l_port, true)
|
||||
log.error("received packet on unused channel " .. l_port, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -187,3 +190,5 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
|
||||
handle_packet = handle_packet
|
||||
}
|
||||
end
|
||||
|
||||
return supervisor
|
||||
|
||||
Reference in New Issue
Block a user