diff --git a/reactor-plc/plc.lua b/reactor-plc/plc.lua index 6e92cfd..67708d5 100644 --- a/reactor-plc/plc.lua +++ b/reactor-plc/plc.lua @@ -1,5 +1,6 @@ -- #REQUIRES comms.lua -- #REQUIRES ppm.lua +-- #REQUIRES util.lua local PROTOCOLS = comms.PROTOCOLS local RPLC_TYPES = comms.RPLC_TYPES @@ -272,7 +273,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) -- keep alive ack local _send_keep_alive_ack = function (srv_time) - _send(RPLC_TYPES.KEEP_ALIVE, { srv_time, os.epoch() }) + _send(RPLC_TYPES.KEEP_ALIVE, { srv_time, util.time() }) end -- general ack @@ -369,7 +370,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) if packet.type == RPLC_TYPES.KEEP_ALIVE then -- keep alive request received, echo back local timestamp = packet.data[1] - local trip_time = os.epoch() - timestamp + local trip_time = util.time() - timestamp if trip_time < 0 then log._warning("PLC KEEP_ALIVE trip time less than 0 (" .. trip_time .. ")") @@ -494,7 +495,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) end local sys_status = { - os.epoch(), + util.time(), (not self.scrammed), overridden, degraded, diff --git a/scada-common/alarm.lua b/scada-common/alarm.lua index d6670a4..e8464a5 100644 --- a/scada-common/alarm.lua +++ b/scada-common/alarm.lua @@ -1,3 +1,5 @@ +-- #REQUIRES util.lua + SEVERITY = { INFO = 0, -- basic info message WARNING = 1, -- warning about some abnormal state @@ -9,7 +11,7 @@ SEVERITY = { function scada_alarm(severity, device, message) local self = { - time = os.epoch(), + time = util.time(), ts_string = os.date("[%H:%M:%S]"), severity = severity, device = device, diff --git a/scada-common/util.lua b/scada-common/util.lua index 1d4e11c..050b18a 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -1,3 +1,5 @@ +-- PRINT -- + -- we are overwriting 'print' so save it first local _print = print @@ -21,6 +23,28 @@ function println_ts(message) _print(os.date("[%H:%M:%S] ") .. message) end +-- TIME -- + +function time_ms() + return os.epoch('local') +end + +function time_s() + return os.epoch('local') / 1000 +end + +function time() + return time_ms() +end + +-- PARALLELIZATION -- + +-- block waiting for parallel call +function task_wait(f) + parallel.waitForAll(f) +end + +-- WATCHDOG -- -- ComputerCraft OS Timer based Watchdog -- triggers a timer event if not fed within 'timeout' seconds diff --git a/supervisor/session/plc.lua b/supervisor/session/plc.lua index 3adb85d..695b34b 100644 --- a/supervisor/session/plc.lua +++ b/supervisor/session/plc.lua @@ -185,7 +185,7 @@ function new_session(id, for_reactor, in_queue, out_queue) if rplc_pkt.length == 2 then local srv_start = rplc_pkt.data[1] local plc_send = rplc_pkt.data[2] - local srv_now = os.epoch() + local srv_now = util.time() self.last_rtt = srv_now - srv_start if self.last_rtt < 0 then @@ -351,7 +351,7 @@ function new_session(id, for_reactor, in_queue, out_queue) self.periodics.keep_alive = self.periodics.keep_alive + elapsed if self.periodics.keep_alive >= PERIODICS.KEEP_ALIVE then - _send(RPLC_TYPES.KEEP_ALIVE, { os.epoch() }) + _send(RPLC_TYPES.KEEP_ALIVE, { util.time() }) self.periodics.keep_alive = 0 end