From 945b761fc2e06f36f31d6627dc9607bb6cdbebde Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Mon, 11 Apr 2022 17:27:57 -0400 Subject: [PATCH] #2 RTU handle disconnects/reconnects --- rtu/startup.lua | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/rtu/startup.lua b/rtu/startup.lua index bde8732..2caa50d 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -170,9 +170,43 @@ while true do local event, param1, param2, param3, param4, param5 = os.pullEventRaw() if event == "peripheral_detach" then - ppm.handle_unmount(param1) + -- handle loss of a device + local device = ppm.handle_unmount(param1) - -- todo: handle unit change + for i = 1, #units do + -- find disconnected device + if units[i].device == device 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 + elseif event == "peripheral" then + -- relink lost peripheral to correct unit entry + local type, device = ppm.mount(param1) + + for i = 1, #units do + local unit = units[i] + + -- find disconnected device to reconnect + if unit.name == param1 then + -- found, re-link + unit.device = device + + if unit.type == "boiler" then + unit.rtu = boiler_rtu(device) + elseif unit.type == "turbine" then + unit.rtu = turbine_rtu(device) + elseif unit.type == "imatrix" then + unit.rtu = imatrix_rtu(device) + end + + unit.modbus_io = modbus_init(unit.rtu) + + println_ts("reconnected the " .. unit.type .. " on interface " .. unit.name) + end + end elseif event == "timer" and param1 == loop_tick then -- period tick, if we are linked send heartbeat, if not send advertisement if linked then