cc-mek-scada/supervisor/supervisor.lua

169 lines
6.1 KiB
Lua
Raw Normal View History

2022-03-25 16:18:33 +00:00
-- #REQUIRES comms.lua
2022-04-22 15:07:59 +00:00
-- #REQUIRES modbus.lua
-- #REQUIRES mqueue.lua
-- #REQUIRES svsessions.lua
local PROTOCOLS = comms.PROTOCOLS
local RPLC_TYPES = comms.RPLC_TYPES
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
local RTU_ADVERT_TYPES = comms.RTU_ADVERT_TYPES
local SESSION_TYPE = svsessions.SESSION_TYPE
2022-03-25 16:18:33 +00:00
-- supervisory controller communications
2022-04-24 17:22:45 +00:00
function superv_comms(num_reactors, modem, dev_listen, coord_listen)
2022-03-25 16:18:33 +00:00
local self = {
2022-04-22 15:07:59 +00:00
ln_seq_num = 0,
2022-03-25 16:18:33 +00:00
num_reactors = num_reactors,
modem = modem,
dev_listen = dev_listen,
2022-04-22 15:07:59 +00:00
coord_listen = coord_listen,
2022-03-25 16:18:33 +00:00
reactor_struct_cache = nil
}
2022-04-18 15:07:16 +00:00
-- 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
2022-04-22 15:07:59 +00:00
if not self.modem.isOpen(self.coord_listen) then
self.modem.open(self.coord_listen)
2022-04-18 15:07:16 +00:00
end
end
-- open at construct time
_open_channels()
2022-04-23 16:12:33 +00:00
-- link modem to svsessions
svsessions.link_modem(self.modem)
-- send PLC link request responses
2022-04-22 15:07:59 +00:00
local _send_plc_linking = function (dest, msg)
local s_pkt = comms.scada_packet()
local r_pkt = comms.rplc_packet()
r_pkt.make(0, RPLC_TYPES.LINK_REQ, msg)
s_pkt.make(self.ln_seq_num, PROTOCOLS.RPLC, r_pkt.raw_sendable())
self.modem.transmit(dest, self.dev_listen, s_pkt.raw_sendable())
2022-04-22 15:07:59 +00:00
self.ln_seq_num = self.ln_seq_num + 1
end
2022-04-18 15:07:16 +00:00
-- PUBLIC FUNCTIONS --
-- reconnect a newly connected modem
local reconnect_modem = function (modem)
self.modem = modem
2022-04-23 16:12:33 +00:00
svsessions.link_modem(self.modem)
2022-04-18 15:07:16 +00:00
_open_channels()
end
2022-04-22 15:07:59 +00:00
-- parse a packet
local parse_packet = function(side, sender, reply_to, message, distance)
local pkt = nil
local s_pkt = scada_packet()
-- parse packet as generic SCADA packet
s_pkt.receive(side, sender, reply_to, message, distance)
2022-04-22 15:07:59 +00:00
if s_pkt.is_valid() then
-- get as MODBUS TCP packet
if s_pkt.protocol() == PROTOCOLS.MODBUS_TCP then
local m_pkt = comms.modbus_packet()
if m_pkt.decode(s_pkt) then
pkt = m_pkt.get()
end
-- get as RPLC packet
elseif s_pkt.protocol() == PROTOCOLS.RPLC then
local rplc_pkt = comms.rplc_packet()
if rplc_pkt.decode(s_pkt) then
pkt = rplc_pkt.get()
end
-- get as SCADA management packet
elseif s_pkt.protocol() == PROTOCOLS.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet()
if mgmt_pkt.decode(s_pkt) then
pkt = mgmt_packet.get()
end
-- get as coordinator packet
elseif s_pkt.protocol() == PROTOCOLS.COORD_DATA then
local coord_pkt = comms.coord_packet()
if coord_pkt.decode(s_pkt) then
pkt = coord_pkt.get()
end
else
log._debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
end
end
return pkt
end
local handle_packet = function(packet)
if packet ~= nil then
local sender = packet.scada_frame.sender()
local receiver = packet.scada_frame.receiver()
local protocol = packet.scada_frame.protocol()
-- device (RTU/PLC) listening channel
if receiver == self.dev_listen then
if protocol == PROTOCOLS.MODBUS_TCP then
-- MODBUS response
elseif protocol == PROTOCOLS.RPLC then
-- reactor PLC packet
local session = svsessions.find_session(SESSION_TYPE.PLC_SESSION, sender)
if session then
if packet.type == RPLC_TYPES.LINK_REQ then
-- new device on this port? that's a collision
_send_plc_linking(sender, { RPLC_LINKING.COLLISION })
else
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
end
else
-- unknown session, is this a linking request?
if packet.type == RPLC_TYPES.LINK_REQ then
-- this is a linking request
local plc_id = svsessions.establish_plc_session(sender)
if plc_id == false then
-- reactor already has a PLC assigned
_send_plc_linking(sender, { RPLC_LINKING.COLLISION })
else
-- got an ID; assigned to a reactor successfully
_send_plc_linking(sender, { RPLC_LINKING.ALLOW })
end
else
-- force a re-link
_send_plc_linking(sender, { RPLC_LINKING.DENY })
end
end
elseif protocol == PROTOCOLS.SCADA_MGMT then
-- SCADA management packet
else
log._debug("illegal packet type " .. protocol .. " on device listening channel")
end
-- coordinator listening channel
elseif receiver == self.coord_listen then
2022-04-22 15:07:59 +00:00
if protocol == PROTOCOLS.SCADA_MGMT then
-- SCADA management packet
elseif protocol == PROTOCOLS.COORD_DATA then
-- coordinator packet
else
log._debug("illegal packet type " .. protocol .. " on coordinator listening channel")
end
else
log._error("received packet on unused channel " .. receiver, true)
end
end
end
2022-04-18 15:07:16 +00:00
return {
2022-04-22 15:07:59 +00:00
reconnect_modem = reconnect_modem,
parse_packet = parse_packet,
handle_packet = handle_packet
2022-04-18 15:07:16 +00:00
}
2022-03-25 16:18:33 +00:00
end