mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
cleanup/fixes of scada common code
This commit is contained in:
parent
25558df22d
commit
cd0d7aa5a3
@ -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
|
||||||
|
@ -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,6 +21,21 @@ 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
|
||||||
@ -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,14 +89,17 @@ 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 = ""
|
||||||
|
|
||||||
@ -117,6 +117,7 @@ log.error = function (msg, trace)
|
|||||||
_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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user