cleanup/fixes of scada common code

This commit is contained in:
Mikayla Fischler 2022-05-10 11:35:52 -04:00
parent 25558df22d
commit cd0d7aa5a3
6 changed files with 41 additions and 37 deletions

View File

@ -2,6 +2,7 @@
-- Communications -- Communications
-- --
local log = require("scada-common.log")
local types = require("scada-common.types") local types = require("scada-common.types")
local comms = {} local comms = {}
@ -146,11 +147,11 @@ comms.modbus_packet = function ()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
txn_id = txn_id, txn_id = nil,
length = length, length = nil,
unit_id = unit_id, unit_id = nil,
func_code = func_code, func_code = nil,
data = data data = nil
} }
-- make a MODBUS packet -- make a MODBUS packet

View File

@ -13,10 +13,6 @@ local MODE = {
log.MODE = MODE log.MODE = MODE
----------------------------
-- PRIVATE DATA/FUNCTIONS --
----------------------------
local LOG_DEBUG = true local LOG_DEBUG = true
local _log_sys = { local _log_sys = {
@ -25,12 +21,27 @@ local _log_sys = {
file = nil 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 _log = function (msg)
local time_stamp = os.date("[%c] ") local time_stamp = os.date("[%c] ")
local stamped = time_stamp .. msg local stamped = time_stamp .. msg
-- attempt to write log -- attempt to write log
local status, result = pcall(function () local status, result = pcall(function ()
_log_sys.file.writeLine(stamped) _log_sys.file.writeLine(stamped)
_log_sys.file.flush() _log_sys.file.flush()
end) end)
@ -45,11 +56,11 @@ local _log = function (msg)
end end
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 -- delete the old log file and open a new one
_log_sys.file.close() _log_sys.file.close()
fs.delete(_log_sys.path) fs.delete(_log_sys.path)
init(_log_sys.path, _log_sys.mode) log.init(_log_sys.path, _log_sys.mode)
-- leave a message -- leave a message
_log_sys.file.writeLine(time_stamp .. "recycled log file") _log_sys.file.writeLine(time_stamp .. "recycled log file")
@ -58,21 +69,7 @@ local _log = function (msg)
end end
end end
---------------------- -- log debug messages
-- 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 = function (msg, trace) log.debug = function (msg, trace)
if LOG_DEBUG then if LOG_DEBUG then
local dbg_info = "" local dbg_info = ""
@ -92,17 +89,20 @@ log.debug = function (msg, trace)
end end
end end
-- log info messages
log.info = function (msg) log.info = function (msg)
_log("[INF] " .. msg) _log("[INF] " .. msg)
end end
-- log warning messages
log.warning = function (msg) log.warning = function (msg)
_log("[WRN] " .. msg) _log("[WRN] " .. msg)
end end
-- log error messages
log.error = function (msg, trace) log.error = function (msg, trace)
local dbg_info = "" local dbg_info = ""
if trace then if trace then
local info = debug.getinfo(2) local info = debug.getinfo(2)
local name = "" local name = ""
@ -110,13 +110,14 @@ log.error = function (msg, trace)
if info.name ~= nil then if info.name ~= nil then
name = ":" .. info.name .. "():" name = ":" .. info.name .. "():"
end end
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > " dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
end end
_log("[ERR] " .. dbg_info .. msg) _log("[ERR] " .. dbg_info .. msg)
end end
-- log fatal errors
log.fatal = function (msg) log.fatal = function (msg)
_log("[FTL] " .. msg) _log("[FTL] " .. msg)
end end

View File

@ -29,11 +29,11 @@ mqueue.new = function ()
local ready = function () local ready = function ()
return #queue ~= 0 return #queue ~= 0
end end
local _push = function (qtype, message) local _push = function (qtype, message)
insert(queue, { qtype = qtype, message = message }) insert(queue, { qtype = qtype, message = message })
end end
local push_command = function (message) local push_command = function (message)
_push(TYPE.COMMAND, message) _push(TYPE.COMMAND, message)
end end
@ -49,7 +49,7 @@ mqueue.new = function ()
local pop = function () local pop = function ()
if #queue > 0 then if #queue > 0 then
return remove(queue, 1) return remove(queue, 1)
else else
return nil return nil
end end
end end

View File

@ -234,7 +234,7 @@ ppm.get_device = function (name)
break break
end end
end end
return device return device
end end

View File

@ -231,7 +231,7 @@ rsio.digital_write = function (channel, active)
if channel < RS_IO.WASTE_PO or channel > RS_IO.R_PLC_TIMEOUT then if channel < RS_IO.WASTE_PO or channel > RS_IO.R_PLC_TIMEOUT then
return IO_LVL.LOW return IO_LVL.LOW
else else
return RS_DIO_MAP[channel]._f(level) return RS_DIO_MAP[channel]._f(active)
end end
end end

View File

@ -39,6 +39,7 @@ end
-- PARALLELIZATION -- -- PARALLELIZATION --
-- protected sleep call so we still are in charge of catching termination -- protected sleep call so we still are in charge of catching termination
-- EVENT_CONSUMER: this function consumes events
util.psleep = function (t) util.psleep = function (t)
pcall(os.sleep, t) pcall(os.sleep, t)
end end
@ -50,6 +51,7 @@ util.nop = function ()
end end
-- attempt to maintain a minimum loop timing (duration of execution) -- attempt to maintain a minimum loop timing (duration of execution)
-- EVENT_CONSUMER: this function consumes events
util.adaptive_delay = function (target_timing, last_update) util.adaptive_delay = function (target_timing, last_update)
local sleep_for = target_timing - (util.time() - last_update) local sleep_for = target_timing - (util.time() - last_update)
-- only if >50ms since worker loops already yield 0.05s -- only if >50ms since worker loops already yield 0.05s
@ -64,15 +66,15 @@ end
-- ComputerCraft OS Timer based Watchdog -- ComputerCraft OS Timer based Watchdog
-- triggers a timer event if not fed within 'timeout' seconds -- triggers a timer event if not fed within 'timeout' seconds
util.new_watchdog = function (timeout) util.new_watchdog = function (timeout)
local self = { local self = {
_timeout = timeout, _timeout = timeout,
_wd_timer = os.startTimer(timeout) _wd_timer = os.startTimer(timeout)
} }
local get_timer = function () local get_timer = function ()
return self._wd_timer return self._wd_timer
end end
local feed = function () local feed = function ()
if self._wd_timer ~= nil then if self._wd_timer ~= nil then
os.cancelTimer(self._wd_timer) os.cancelTimer(self._wd_timer)