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
--
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

View File

@ -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

View File

@ -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

View File

@ -234,7 +234,7 @@ ppm.get_device = function (name)
break
end
end
return device
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
return IO_LVL.LOW
else
return RS_DIO_MAP[channel]._f(level)
return RS_DIO_MAP[channel]._f(active)
end
end

View File

@ -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)