watchdog cleanup and loop clock object
This commit is contained in:
parent
168341db39
commit
6e1ece8183
@ -39,7 +39,7 @@ threads.thread__main = function (smem, init)
|
|||||||
-- send link requests at 0.5Hz (every 40 server ticks) (every 4 loop ticks)
|
-- send link requests at 0.5Hz (every 40 server ticks) (every 4 loop ticks)
|
||||||
local LINK_TICKS = 4
|
local LINK_TICKS = 4
|
||||||
local ticks_to_update = 0
|
local ticks_to_update = 0
|
||||||
local loop_clock = nil
|
local loop_clock = util.new_clock(MAIN_CLOCK)
|
||||||
|
|
||||||
-- load in from shared memory
|
-- load in from shared memory
|
||||||
local networked = smem.networked
|
local networked = smem.networked
|
||||||
@ -47,7 +47,7 @@ threads.thread__main = function (smem, init)
|
|||||||
local plc_dev = smem.plc_dev
|
local plc_dev = smem.plc_dev
|
||||||
local rps = smem.plc_sys.rps
|
local rps = smem.plc_sys.rps
|
||||||
local plc_comms = smem.plc_sys.plc_comms
|
local plc_comms = smem.plc_sys.plc_comms
|
||||||
local conn_watchdog = smem.plc_sys.conn_watchdog
|
local conn_watchdog = smem.plc_sys.conn_watchdog ---@type watchdog
|
||||||
|
|
||||||
-- event loop
|
-- event loop
|
||||||
while true do
|
while true do
|
||||||
@ -55,11 +55,11 @@ threads.thread__main = function (smem, init)
|
|||||||
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
||||||
|
|
||||||
-- handle event
|
-- handle event
|
||||||
if event == "timer" and param1 == loop_clock then
|
if event == "timer" and loop_clock.is_clock(param1) then
|
||||||
-- core clock tick
|
-- core clock tick
|
||||||
if networked then
|
if networked then
|
||||||
-- start next clock timer
|
-- start next clock timer
|
||||||
loop_clock = os.startTimer(MAIN_CLOCK)
|
loop_clock.start()
|
||||||
|
|
||||||
-- send updated data
|
-- send updated data
|
||||||
if not plc_state.no_modem then
|
if not plc_state.no_modem then
|
||||||
@ -82,7 +82,7 @@ threads.thread__main = function (smem, init)
|
|||||||
-- pass the packet onto the comms message queue
|
-- pass the packet onto the comms message queue
|
||||||
smem.q.mq_comms_rx.push_packet(packet)
|
smem.q.mq_comms_rx.push_packet(packet)
|
||||||
end
|
end
|
||||||
elseif event == "timer" and networked and param1 == conn_watchdog.get_timer() then
|
elseif event == "timer" and networked and conn_watchdog.is_timer(param1) then
|
||||||
-- haven't heard from server recently? shutdown reactor
|
-- haven't heard from server recently? shutdown reactor
|
||||||
plc_comms.unlink()
|
plc_comms.unlink()
|
||||||
smem.q.mq_rps.push_command(MQ__RPS_CMD.TRIP_TIMEOUT)
|
smem.q.mq_rps.push_command(MQ__RPS_CMD.TRIP_TIMEOUT)
|
||||||
@ -165,7 +165,7 @@ threads.thread__main = function (smem, init)
|
|||||||
end
|
end
|
||||||
elseif event == "clock_start" then
|
elseif event == "clock_start" then
|
||||||
-- start loop clock
|
-- start loop clock
|
||||||
loop_clock = os.startTimer(MAIN_CLOCK)
|
loop_clock.start()
|
||||||
log.debug("main thread clock started")
|
log.debug("main thread clock started")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -35,29 +35,30 @@ threads.thread__main = function (smem)
|
|||||||
local exec = function ()
|
local exec = function ()
|
||||||
log.debug("main thread start")
|
log.debug("main thread start")
|
||||||
|
|
||||||
-- advertisement/heartbeat clock
|
-- main loop clock
|
||||||
local loop_clock = os.startTimer(MAIN_CLOCK)
|
local loop_clock = util.new_clock(MAIN_CLOCK)
|
||||||
|
|
||||||
-- load in from shared memory
|
-- load in from shared memory
|
||||||
local rtu_state = smem.rtu_state
|
local rtu_state = smem.rtu_state
|
||||||
local rtu_dev = smem.rtu_dev
|
local rtu_dev = smem.rtu_dev
|
||||||
local rtu_comms = smem.rtu_sys.rtu_comms
|
local rtu_comms = smem.rtu_sys.rtu_comms
|
||||||
local conn_watchdog = smem.rtu_sys.conn_watchdog
|
local conn_watchdog = smem.rtu_sys.conn_watchdog ---@type watchdog
|
||||||
local units = smem.rtu_sys.units
|
local units = smem.rtu_sys.units
|
||||||
|
|
||||||
|
-- start clock
|
||||||
|
loop_clock.start()
|
||||||
|
|
||||||
-- event loop
|
-- event loop
|
||||||
while true do
|
while true do
|
||||||
---@diagnostic disable-next-line: undefined-field
|
---@diagnostic disable-next-line: undefined-field
|
||||||
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
||||||
|
|
||||||
if event == "timer" and param1 == loop_clock then
|
if event == "timer" and loop_clock.is_clock(param1) then
|
||||||
-- start next clock timer
|
-- start next clock timer
|
||||||
loop_clock = os.startTimer(MAIN_CLOCK)
|
loop_clock.start()
|
||||||
|
|
||||||
-- period tick, if we are linked send heartbeat, if not send advertisement
|
-- period tick, if we are not linked send advertisement
|
||||||
if rtu_state.linked then
|
if not rtu_state.linked then
|
||||||
rtu_comms.send_heartbeat()
|
|
||||||
else
|
|
||||||
-- advertise units
|
-- advertise units
|
||||||
rtu_comms.send_advertisement(units)
|
rtu_comms.send_advertisement(units)
|
||||||
end
|
end
|
||||||
@ -68,7 +69,7 @@ threads.thread__main = function (smem)
|
|||||||
-- pass the packet onto the comms message queue
|
-- pass the packet onto the comms message queue
|
||||||
smem.q.mq_comms.push_packet(packet)
|
smem.q.mq_comms.push_packet(packet)
|
||||||
end
|
end
|
||||||
elseif event == "timer" and param1 == conn_watchdog.get_timer() then
|
elseif event == "timer" and conn_watchdog.is_timer(param1) then
|
||||||
-- haven't heard from server recently? unlink
|
-- haven't heard from server recently? unlink
|
||||||
rtu_comms.unlink(rtu_state)
|
rtu_comms.unlink(rtu_state)
|
||||||
elseif event == "peripheral_detach" then
|
elseif event == "peripheral_detach" then
|
||||||
@ -162,7 +163,6 @@ threads.thread__comms = function (smem)
|
|||||||
-- load in from shared memory
|
-- load in from shared memory
|
||||||
local rtu_state = smem.rtu_state
|
local rtu_state = smem.rtu_state
|
||||||
local rtu_comms = smem.rtu_sys.rtu_comms
|
local rtu_comms = smem.rtu_sys.rtu_comms
|
||||||
local conn_watchdog = smem.rtu_sys.conn_watchdog
|
|
||||||
local units = smem.rtu_sys.units
|
local units = smem.rtu_sys.units
|
||||||
|
|
||||||
local comms_queue = smem.q.mq_comms
|
local comms_queue = smem.q.mq_comms
|
||||||
|
|||||||
@ -67,40 +67,75 @@ end
|
|||||||
-- WATCHDOG --
|
-- WATCHDOG --
|
||||||
|
|
||||||
-- ComputerCraft OS Timer based Watchdog
|
-- ComputerCraft OS Timer based Watchdog
|
||||||
-- triggers a timer event if not fed within 'timeout' seconds
|
---@param timeout number timeout duration
|
||||||
|
---
|
||||||
|
--- triggers a timer event if not fed within 'timeout' seconds
|
||||||
util.new_watchdog = function (timeout)
|
util.new_watchdog = function (timeout)
|
||||||
---@diagnostic disable-next-line: undefined-field
|
---@diagnostic disable-next-line: undefined-field
|
||||||
local start_timer = os.startTimer
|
local start_timer = os.startTimer
|
||||||
---@diagnostic disable-next-line: undefined-field
|
---@diagnostic disable-next-line: undefined-field
|
||||||
local cancel_timer = os.cancelTimer
|
local cancel_timer = os.cancelTimer
|
||||||
|
|
||||||
local self = {
|
local self = {
|
||||||
_timeout = timeout,
|
timeout = timeout,
|
||||||
_wd_timer = start_timer(timeout)
|
wd_timer = start_timer(timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
local get_timer = function ()
|
---@class watchdog
|
||||||
return self._wd_timer
|
local public = {}
|
||||||
|
|
||||||
|
---@param timer number timer event timer ID
|
||||||
|
public.is_timer = function (timer)
|
||||||
|
return self.wd_timer == timer
|
||||||
end
|
end
|
||||||
|
|
||||||
local feed = function ()
|
-- satiate the beast
|
||||||
if self._wd_timer ~= nil then
|
public.feed = function ()
|
||||||
cancel_timer(self._wd_timer)
|
if self.wd_timer ~= nil then
|
||||||
|
cancel_timer(self.wd_timer)
|
||||||
end
|
end
|
||||||
self._wd_timer = start_timer(self._timeout)
|
self.wd_timer = start_timer(self.timeout)
|
||||||
end
|
end
|
||||||
|
|
||||||
local cancel = function ()
|
-- cancel the watchdog
|
||||||
if self._wd_timer ~= nil then
|
public.cancel = function ()
|
||||||
cancel_timer(self._wd_timer)
|
if self.wd_timer ~= nil then
|
||||||
|
cancel_timer(self.wd_timer)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return public
|
||||||
get_timer = get_timer,
|
end
|
||||||
feed = feed,
|
|
||||||
cancel = cancel
|
-- LOOP CLOCK --
|
||||||
|
|
||||||
|
-- ComputerCraft OS Timer based Loop Clock
|
||||||
|
---@param period number clock period
|
||||||
|
---
|
||||||
|
--- fires a timer event at the specified period, does not start at construct time
|
||||||
|
util.new_clock = function (period)
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
|
local start_timer = os.startTimer
|
||||||
|
|
||||||
|
local self = {
|
||||||
|
period = period,
|
||||||
|
timer = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@class clock
|
||||||
|
local public = {}
|
||||||
|
|
||||||
|
---@param timer number timer event timer ID
|
||||||
|
public.is_clock = function (timer)
|
||||||
|
return self.timer == timer
|
||||||
|
end
|
||||||
|
|
||||||
|
-- start the clock
|
||||||
|
public.start = function ()
|
||||||
|
self.timer = start_timer(self.period)
|
||||||
|
end
|
||||||
|
|
||||||
|
return public
|
||||||
end
|
end
|
||||||
|
|
||||||
return util
|
return util
|
||||||
|
|||||||
@ -403,7 +403,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue)
|
|||||||
|
|
||||||
-- check if a timer matches this session's watchdog
|
-- check if a timer matches this session's watchdog
|
||||||
local check_wd = function (timer)
|
local check_wd = function (timer)
|
||||||
return timer == self.plc_conn_watchdog.get_timer()
|
return self.plc_conn_watchdog.is_timer(timer)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- close the connection
|
-- close the connection
|
||||||
|
|||||||
@ -115,7 +115,7 @@ rtu.new_session = function (id, in_queue, out_queue)
|
|||||||
|
|
||||||
-- check if a timer matches this session's watchdog
|
-- check if a timer matches this session's watchdog
|
||||||
local check_wd = function (timer)
|
local check_wd = function (timer)
|
||||||
return timer == self.rtu_conn_watchdog.get_timer()
|
return self.rtu_conn_watchdog.is_timer(timer)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- close the connection
|
-- close the connection
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user