From cd0d7aa5a366e3de5f72a8bc772b54dfde7b856e Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 10 May 2022 11:35:52 -0400 Subject: [PATCH] cleanup/fixes of scada common code --- scada-common/comms.lua | 11 ++++----- scada-common/log.lua | 49 +++++++++++++++++++++-------------------- scada-common/mqueue.lua | 6 ++--- scada-common/ppm.lua | 2 +- scada-common/rsio.lua | 2 +- scada-common/util.lua | 8 ++++--- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/scada-common/comms.lua b/scada-common/comms.lua index 8685390..5a84fbc 100644 --- a/scada-common/comms.lua +++ b/scada-common/comms.lua @@ -2,6 +2,7 @@ -- Communications -- +local log = require("scada-common.log") local types = require("scada-common.types") local comms = {} @@ -146,11 +147,11 @@ comms.modbus_packet = function () local self = { frame = nil, raw = nil, - txn_id = txn_id, - length = length, - unit_id = unit_id, - func_code = func_code, - data = data + txn_id = nil, + length = nil, + unit_id = nil, + func_code = nil, + data = nil } -- make a MODBUS packet diff --git a/scada-common/log.lua b/scada-common/log.lua index a4f63b2..e841b23 100644 --- a/scada-common/log.lua +++ b/scada-common/log.lua @@ -13,10 +13,6 @@ local MODE = { log.MODE = MODE ----------------------------- --- PRIVATE DATA/FUNCTIONS -- ----------------------------- - local LOG_DEBUG = true local _log_sys = { @@ -25,12 +21,27 @@ local _log_sys = { file = nil } +local free_space = fs.getFreeSpace + +-- initialize logger +log.init = function (path, write_mode) + _log_sys.path = path + _log_sys.mode = write_mode + + if _log_sys.mode == MODE.APPEND then + _log_sys.file = fs.open(path, "a") + else + _log_sys.file = fs.open(path, "w+") + end +end + +-- private log write function local _log = function (msg) local time_stamp = os.date("[%c] ") local stamped = time_stamp .. msg -- attempt to write log - local status, result = pcall(function () + local status, result = pcall(function () _log_sys.file.writeLine(stamped) _log_sys.file.flush() end) @@ -45,11 +56,11 @@ local _log = function (msg) end end - if (result == "Out of space") or (fs.getFreeSpace(_log_sys.path) < 100) then + if (result == "Out of space") or (free_space(_log_sys.path) < 100) then -- delete the old log file and open a new one _log_sys.file.close() fs.delete(_log_sys.path) - init(_log_sys.path, _log_sys.mode) + log.init(_log_sys.path, _log_sys.mode) -- leave a message _log_sys.file.writeLine(time_stamp .. "recycled log file") @@ -58,21 +69,7 @@ local _log = function (msg) end end ----------------------- --- PUBLIC FUNCTIONS -- ----------------------- - -log.init = function (path, write_mode) - _log_sys.path = path - _log_sys.mode = write_mode - - if _log_sys.mode == MODE.APPEND then - _log_sys.file = fs.open(path, "a") - else - _log_sys.file = fs.open(path, "w+") - end -end - +-- log debug messages log.debug = function (msg, trace) if LOG_DEBUG then local dbg_info = "" @@ -92,17 +89,20 @@ log.debug = function (msg, trace) end end +-- log info messages log.info = function (msg) _log("[INF] " .. msg) end +-- log warning messages log.warning = function (msg) _log("[WRN] " .. msg) end +-- log error messages log.error = function (msg, trace) local dbg_info = "" - + if trace then local info = debug.getinfo(2) local name = "" @@ -110,13 +110,14 @@ log.error = function (msg, trace) if info.name ~= nil then name = ":" .. info.name .. "():" end - + dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > " end _log("[ERR] " .. dbg_info .. msg) end +-- log fatal errors log.fatal = function (msg) _log("[FTL] " .. msg) end diff --git a/scada-common/mqueue.lua b/scada-common/mqueue.lua index c24c15c..d1ac5c1 100644 --- a/scada-common/mqueue.lua +++ b/scada-common/mqueue.lua @@ -29,11 +29,11 @@ mqueue.new = function () local ready = function () return #queue ~= 0 end - + local _push = function (qtype, message) insert(queue, { qtype = qtype, message = message }) end - + local push_command = function (message) _push(TYPE.COMMAND, message) end @@ -49,7 +49,7 @@ mqueue.new = function () local pop = function () if #queue > 0 then return remove(queue, 1) - else + else return nil end end diff --git a/scada-common/ppm.lua b/scada-common/ppm.lua index 5e15724..e834946 100644 --- a/scada-common/ppm.lua +++ b/scada-common/ppm.lua @@ -234,7 +234,7 @@ ppm.get_device = function (name) break end end - + return device end diff --git a/scada-common/rsio.lua b/scada-common/rsio.lua index faf9008..d5a3d5a 100644 --- a/scada-common/rsio.lua +++ b/scada-common/rsio.lua @@ -231,7 +231,7 @@ rsio.digital_write = function (channel, active) if channel < RS_IO.WASTE_PO or channel > RS_IO.R_PLC_TIMEOUT then return IO_LVL.LOW else - return RS_DIO_MAP[channel]._f(level) + return RS_DIO_MAP[channel]._f(active) end end diff --git a/scada-common/util.lua b/scada-common/util.lua index a963a08..4ee8567 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -39,6 +39,7 @@ end -- PARALLELIZATION -- -- protected sleep call so we still are in charge of catching termination +-- EVENT_CONSUMER: this function consumes events util.psleep = function (t) pcall(os.sleep, t) end @@ -50,6 +51,7 @@ util.nop = function () end -- attempt to maintain a minimum loop timing (duration of execution) +-- EVENT_CONSUMER: this function consumes events util.adaptive_delay = function (target_timing, last_update) local sleep_for = target_timing - (util.time() - last_update) -- only if >50ms since worker loops already yield 0.05s @@ -64,15 +66,15 @@ end -- ComputerCraft OS Timer based Watchdog -- triggers a timer event if not fed within 'timeout' seconds util.new_watchdog = function (timeout) - local self = { - _timeout = timeout, + local self = { + _timeout = timeout, _wd_timer = os.startTimer(timeout) } local get_timer = function () return self._wd_timer end - + local feed = function () if self._wd_timer ~= nil then os.cancelTimer(self._wd_timer)