#34 RTU modem disconnect/reconnect handling, bugfix in comms thread
This commit is contained in:
parent
07e9101ac7
commit
ef1fdc7f39
11
rtu/rtu.lua
11
rtu/rtu.lua
@ -153,6 +153,16 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
|
|
||||||
-- PUBLIC FUNCTIONS --
|
-- PUBLIC FUNCTIONS --
|
||||||
|
|
||||||
|
-- reconnect a newly connected modem
|
||||||
|
local reconnect_modem = function (modem)
|
||||||
|
self.modem = modem
|
||||||
|
|
||||||
|
-- open modem
|
||||||
|
if not self.modem.isOpen(self.l_port) then
|
||||||
|
self.modem.open(self.l_port)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- parse a MODBUS/SCADA packet
|
-- parse a MODBUS/SCADA packet
|
||||||
local parse_packet = function(side, sender, reply_to, message, distance)
|
local parse_packet = function(side, sender, reply_to, message, distance)
|
||||||
local pkt = nil
|
local pkt = nil
|
||||||
@ -270,6 +280,7 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
reconnect_modem = reconnect_modem,
|
||||||
parse_packet = parse_packet,
|
parse_packet = parse_packet,
|
||||||
handle_packet = handle_packet,
|
handle_packet = handle_packet,
|
||||||
send_advertisement = send_advertisement,
|
send_advertisement = send_advertisement,
|
||||||
|
|||||||
@ -19,7 +19,7 @@ os.loadAPI("dev/boiler_rtu.lua")
|
|||||||
os.loadAPI("dev/imatrix_rtu.lua")
|
os.loadAPI("dev/imatrix_rtu.lua")
|
||||||
os.loadAPI("dev/turbine_rtu.lua")
|
os.loadAPI("dev/turbine_rtu.lua")
|
||||||
|
|
||||||
local RTU_VERSION = "alpha-v0.4.5"
|
local RTU_VERSION = "alpha-v0.4.6"
|
||||||
|
|
||||||
local print = util.print
|
local print = util.print
|
||||||
local println = util.println
|
local println = util.println
|
||||||
|
|||||||
@ -36,38 +36,62 @@ function thread__main(smem)
|
|||||||
-- handle loss of a device
|
-- handle loss of a device
|
||||||
local device = ppm.handle_unmount(param1)
|
local device = ppm.handle_unmount(param1)
|
||||||
|
|
||||||
for i = 1, #units do
|
if device.type == "modem" then
|
||||||
-- find disconnected device
|
-- we only care if this is our wireless modem
|
||||||
if units[i].device == device.dev then
|
if device.dev == rtu_dev.modem then
|
||||||
-- we are going to let the PPM prevent crashes
|
println_ts("wireless modem disconnected!")
|
||||||
-- return fault flags/codes to MODBUS queries
|
log._warning("comms modem disconnected!")
|
||||||
local unit = units[i]
|
else
|
||||||
println_ts("lost the " .. unit.type .. " on interface " .. unit.name)
|
log._warning("non-comms modem disconnected")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i = 1, #units do
|
||||||
|
-- find disconnected device
|
||||||
|
if units[i].device == device.dev then
|
||||||
|
-- we are going to let the PPM prevent crashes
|
||||||
|
-- return fault flags/codes to MODBUS queries
|
||||||
|
local unit = units[i]
|
||||||
|
println_ts("lost the " .. unit.type .. " on interface " .. unit.name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif event == "peripheral" then
|
elseif event == "peripheral" then
|
||||||
-- relink lost peripheral to correct unit entry
|
-- peripheral connect
|
||||||
local type, device = ppm.mount(param1)
|
local type, device = ppm.mount(param1)
|
||||||
|
|
||||||
for i = 1, #units do
|
if type == "modem" then
|
||||||
local unit = units[i]
|
if device.isWireless() then
|
||||||
|
-- reconnected modem
|
||||||
|
rtu_dev.modem = device
|
||||||
|
rtu_comms.reconnect_modem(rtu_dev.modem)
|
||||||
|
|
||||||
-- find disconnected device to reconnect
|
println_ts("wireless modem reconnected.")
|
||||||
if unit.name == param1 then
|
log._info("comms modem reconnected.")
|
||||||
-- found, re-link
|
else
|
||||||
unit.device = device
|
log._info("wired modem reconnected.")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- relink lost peripheral to correct unit entry
|
||||||
|
for i = 1, #units do
|
||||||
|
local unit = units[i]
|
||||||
|
|
||||||
if unit.type == "boiler" then
|
-- find disconnected device to reconnect
|
||||||
unit.rtu = boiler_rtu.new(device)
|
if unit.name == param1 then
|
||||||
elseif unit.type == "turbine" then
|
-- found, re-link
|
||||||
unit.rtu = turbine_rtu.new(device)
|
unit.device = device
|
||||||
elseif unit.type == "imatrix" then
|
|
||||||
unit.rtu = imatrix_rtu.new(device)
|
if unit.type == "boiler" then
|
||||||
|
unit.rtu = boiler_rtu.new(device)
|
||||||
|
elseif unit.type == "turbine" then
|
||||||
|
unit.rtu = turbine_rtu.new(device)
|
||||||
|
elseif unit.type == "imatrix" then
|
||||||
|
unit.rtu = imatrix_rtu.new(device)
|
||||||
|
end
|
||||||
|
|
||||||
|
unit.modbus_io = modbus.new(unit.rtu)
|
||||||
|
|
||||||
|
println_ts("reconnected the " .. unit.type .. " on interface " .. unit.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
unit.modbus_io = modbus.new(unit.rtu)
|
|
||||||
|
|
||||||
println_ts("reconnected the " .. unit.type .. " on interface " .. unit.name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif event == "timer" and param1 == loop_clock then
|
elseif event == "timer" and param1 == loop_clock then
|
||||||
@ -87,8 +111,6 @@ function thread__main(smem)
|
|||||||
if packet ~= nil then
|
if packet ~= nil then
|
||||||
smem.q.mq_comms.push_packet(packet)
|
smem.q.mq_comms.push_packet(packet)
|
||||||
end
|
end
|
||||||
|
|
||||||
rtu_comms.handle_packet(packet, units, link_ref)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- check for termination request
|
-- check for termination request
|
||||||
@ -121,7 +143,7 @@ function thread__comms(smem)
|
|||||||
-- thread loop
|
-- thread loop
|
||||||
while true do
|
while true do
|
||||||
-- check for messages in the message queue
|
-- check for messages in the message queue
|
||||||
while comms_queue.ready() and not plc_state.shutdown do
|
while comms_queue.ready() and not rtu_state.shutdown do
|
||||||
local msg = comms_queue.pop()
|
local msg = comms_queue.pop()
|
||||||
|
|
||||||
if msg.qtype == mqueue.TYPE.COMMAND then
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user