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,6 +21,21 @@ 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
@ -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,14 +89,17 @@ 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 = ""
@ -117,6 +117,7 @@ log.error = function (msg, trace)
_log("[ERR] " .. dbg_info .. msg)
end
-- log fatal errors
log.fatal = function (msg)
_log("[FTL] " .. msg)
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