From 3bb95eb441b7506138b603d17232a1daff3211b7 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 31 May 2022 16:09:06 -0400 Subject: [PATCH] #64 util code cleanup --- coordinator/startup.lua | 2 +- reactor-plc/startup.lua | 2 +- rtu/startup.lua | 2 +- scada-common/alarm.lua | 10 ++--- scada-common/comms.lua | 88 ++++++++++++++++++++--------------------- scada-common/crypto.lua | 23 ++++++----- scada-common/log.lua | 22 +++++------ scada-common/mqueue.lua | 18 ++++----- scada-common/ppm.lua | 58 +++++++++++++-------------- scada-common/rsio.lua | 20 +++++----- scada-common/util.lua | 48 +++++++++++----------- supervisor/startup.lua | 2 +- 12 files changed, 147 insertions(+), 148 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 25a6c5e..66a58ea 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -12,7 +12,7 @@ local config = require("coordinator.config") local coordinator = require("coordinator.coordinator") local renderer = require("coordinator.renderer") -local COORDINATOR_VERSION = "alpha-v0.1.2" +local COORDINATOR_VERSION = "alpha-v0.1.3" local print = util.print local println = util.println diff --git a/reactor-plc/startup.lua b/reactor-plc/startup.lua index e0aeeba..48dad46 100644 --- a/reactor-plc/startup.lua +++ b/reactor-plc/startup.lua @@ -13,7 +13,7 @@ local config = require("reactor-plc.config") local plc = require("reactor-plc.plc") local threads = require("reactor-plc.threads") -local R_PLC_VERSION = "beta-v0.7.4" +local R_PLC_VERSION = "beta-v0.7.5" local print = util.print local println = util.println diff --git a/rtu/startup.lua b/rtu/startup.lua index d10fe54..3c01df3 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -24,7 +24,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu") local turbine_rtu = require("rtu.dev.turbine_rtu") local turbinev_rtu = require("rtu.dev.turbinev_rtu") -local RTU_VERSION = "beta-v0.7.3" +local RTU_VERSION = "beta-v0.7.4" local rtu_t = types.rtu_t diff --git a/scada-common/alarm.lua b/scada-common/alarm.lua index 2fcaa19..0a3038c 100644 --- a/scada-common/alarm.lua +++ b/scada-common/alarm.lua @@ -17,7 +17,7 @@ alarm.SEVERITY = SEVERITY -- severity integer to string ---@param severity SEVERITY -alarm.severity_to_string = function (severity) +function alarm.severity_to_string(severity) if severity == SEVERITY.INFO then return "INFO" elseif severity == SEVERITY.WARNING then @@ -39,7 +39,7 @@ end ---@param severity SEVERITY ---@param device string ---@param message string -alarm.scada_alarm = function (severity, device, message) +function alarm.scada_alarm(severity, device, message) local self = { time = util.time(), ts_string = os.date("[%H:%M:%S]"), @@ -53,12 +53,12 @@ alarm.scada_alarm = function (severity, device, message) -- format the alarm as a string ---@return string message - public.format = function () - return self.ts_string .. " [" .. alarm.severity_to_string(self.severity) .. "] (" .. self.device ") >> " .. self.message + function public.format() + return self.ts_string .. " [" .. alarm.severity_to_string(self.severity) .. "] (" .. self.device .. ") >> " .. self.message end -- get alarm properties - public.properties = function () + function public.properties() return { time = self.time, severity = self.severity, diff --git a/scada-common/comms.lua b/scada-common/comms.lua index 775127f..30cadcf 100644 --- a/scada-common/comms.lua +++ b/scada-common/comms.lua @@ -2,7 +2,7 @@ -- Communications -- -local log = require("scada-common.log") +local log = require("scada-common.log") local types = require("scada-common.types") ---@class comms @@ -66,7 +66,7 @@ comms.SCADA_MGMT_TYPES = SCADA_MGMT_TYPES comms.RTU_UNIT_TYPES = RTU_UNIT_TYPES -- generic SCADA packet object -comms.scada_packet = function () +function comms.scada_packet() local self = { modem_msg_in = nil, valid = false, @@ -84,7 +84,7 @@ comms.scada_packet = function () ---@param seq_num integer ---@param protocol PROTOCOLS ---@param payload table - public.make = function (seq_num, protocol, payload) + function public.make(seq_num, protocol, payload) self.valid = true self.seq_num = seq_num self.protocol = protocol @@ -99,7 +99,7 @@ comms.scada_packet = function () ---@param reply_to integer ---@param message any ---@param distance integer - public.receive = function (side, sender, reply_to, message, distance) + function public.receive(side, sender, reply_to, message, distance) self.modem_msg_in = { iface = side, s_port = sender, @@ -125,25 +125,25 @@ comms.scada_packet = function () -- public accessors -- - public.modem_event = function () return self.modem_msg_in end - public.raw_sendable = function () return self.raw end + function public.modem_event() return self.modem_msg_in end + function public.raw_sendable() return self.raw end - public.local_port = function () return self.modem_msg_in.s_port end - public.remote_port = function () return self.modem_msg_in.r_port end + function public.local_port() return self.modem_msg_in.s_port end + function public.remote_port() return self.modem_msg_in.r_port end - public.is_valid = function () return self.valid end + function public.is_valid() return self.valid end - public.seq_num = function () return self.seq_num end - public.protocol = function () return self.protocol end - public.length = function () return self.length end - public.data = function () return self.payload end + function public.seq_num() return self.seq_num end + function public.protocol() return self.protocol end + function public.length() return self.length end + function public.data() return self.payload end return public end -- MODBUS packet -- modeled after MODBUS TCP packet -comms.modbus_packet = function () +function comms.modbus_packet() local self = { frame = nil, raw = nil, @@ -162,7 +162,7 @@ comms.modbus_packet = function () ---@param unit_id integer ---@param func_code MODBUS_FCODE ---@param data table - public.make = function (txn_id, unit_id, func_code, data) + function public.make(txn_id, unit_id, func_code, data) self.txn_id = txn_id self.length = #data self.unit_id = unit_id @@ -179,7 +179,7 @@ comms.modbus_packet = function () -- decode a MODBUS packet from a SCADA frame ---@param frame scada_packet ---@return boolean success - public.decode = function (frame) + function public.decode(frame) if frame then self.frame = frame @@ -203,10 +203,10 @@ comms.modbus_packet = function () end -- get raw to send - public.raw_sendable = function () return self.raw end + function public.raw_sendable() return self.raw end -- get this packet as a frame with an immutable relation to this object - public.get = function () + function public.get() ---@class modbus_frame local frame = { scada_frame = self.frame, @@ -224,7 +224,7 @@ comms.modbus_packet = function () end -- reactor PLC packet -comms.rplc_packet = function () +function comms.rplc_packet() local self = { frame = nil, raw = nil, @@ -238,7 +238,7 @@ comms.rplc_packet = function () local public = {} -- check that type is known - local _rplc_type_valid = function () + local function _rplc_type_valid() return self.type == RPLC_TYPES.LINK_REQ or self.type == RPLC_TYPES.STATUS or self.type == RPLC_TYPES.MEK_STRUCT or @@ -254,7 +254,7 @@ comms.rplc_packet = function () ---@param id integer ---@param packet_type RPLC_TYPES ---@param data table - public.make = function (id, packet_type, data) + function public.make(id, packet_type, data) -- packet accessor properties self.id = id self.type = packet_type @@ -271,7 +271,7 @@ comms.rplc_packet = function () -- decode an RPLC packet from a SCADA frame ---@param frame scada_packet ---@return boolean success - public.decode = function (frame) + function public.decode(frame) if frame then self.frame = frame @@ -296,10 +296,10 @@ comms.rplc_packet = function () end -- get raw to send - public.raw_sendable = function () return self.raw end + function public.raw_sendable() return self.raw end -- get this packet as a frame with an immutable relation to this object - public.get = function () + function public.get() ---@class rplc_frame local frame = { scada_frame = self.frame, @@ -316,7 +316,7 @@ comms.rplc_packet = function () end -- SCADA management packet -comms.mgmt_packet = function () +function comms.mgmt_packet() local self = { frame = nil, raw = nil, @@ -329,7 +329,7 @@ comms.mgmt_packet = function () local public = {} -- check that type is known - local _scada_type_valid = function () + local function _scada_type_valid() return self.type == SCADA_MGMT_TYPES.KEEP_ALIVE or self.type == SCADA_MGMT_TYPES.CLOSE or self.type == SCADA_MGMT_TYPES.REMOTE_LINKED or @@ -339,7 +339,7 @@ comms.mgmt_packet = function () -- make a SCADA management packet ---@param packet_type SCADA_MGMT_TYPES ---@param data table - public.make = function (packet_type, data) + function public.make(packet_type, data) -- packet accessor properties self.type = packet_type self.length = #data @@ -355,7 +355,7 @@ comms.mgmt_packet = function () -- decode a SCADA management packet from a SCADA frame ---@param frame scada_packet ---@return boolean success - public.decode = function (frame) + function public.decode(frame) if frame then self.frame = frame @@ -380,10 +380,10 @@ comms.mgmt_packet = function () end -- get raw to send - public.raw_sendable = function () return self.raw end + function public.raw_sendable() return self.raw end -- get this packet as a frame with an immutable relation to this object - public.get = function () + function public.get() ---@class mgmt_frame local frame = { scada_frame = self.frame, @@ -400,7 +400,7 @@ end -- SCADA coordinator packet -- @todo -comms.coord_packet = function () +function comms.coord_packet() local self = { frame = nil, raw = nil, @@ -412,7 +412,7 @@ comms.coord_packet = function () ---@class coord_packet local public = {} - local _coord_type_valid = function () + local function _coord_type_valid() -- @todo return false end @@ -420,7 +420,7 @@ comms.coord_packet = function () -- make a coordinator packet ---@param packet_type any ---@param data table - public.make = function (packet_type, data) + function public.make(packet_type, data) -- packet accessor properties self.type = packet_type self.length = #data @@ -436,7 +436,7 @@ comms.coord_packet = function () -- decode a coordinator packet from a SCADA frame ---@param frame scada_packet ---@return boolean success - public.decode = function (frame) + function public.decode(frame) if frame then self.frame = frame @@ -461,10 +461,10 @@ comms.coord_packet = function () end -- get raw to send - public.raw_sendable = function () return self.raw end + function public.raw_sendable() return self.raw end -- get this packet as a frame with an immutable relation to this object - public.get = function () + function public.get() ---@class coord_frame local frame = { scada_frame = self.frame, @@ -481,7 +481,7 @@ end -- coordinator API (CAPI) packet -- @todo -comms.capi_packet = function () +function comms.capi_packet() local self = { frame = nil, raw = nil, @@ -493,7 +493,7 @@ comms.capi_packet = function () ---@class capi_packet local public = {} - local _coord_type_valid = function () + local function _coord_type_valid() -- @todo return false end @@ -501,7 +501,7 @@ comms.capi_packet = function () -- make a coordinator API packet ---@param packet_type any ---@param data table - public.make = function (packet_type, data) + function public.make(packet_type, data) -- packet accessor properties self.type = packet_type self.length = #data @@ -517,7 +517,7 @@ comms.capi_packet = function () -- decode a coordinator API packet from a SCADA frame ---@param frame scada_packet ---@return boolean success - public.decode = function (frame) + function public.decode(frame) if frame then self.frame = frame @@ -542,10 +542,10 @@ comms.capi_packet = function () end -- get raw to send - public.raw_sendable = function () return self.raw end + function public.raw_sendable() return self.raw end -- get this packet as a frame with an immutable relation to this object - public.get = function () + function public.get() ---@class capi_frame local frame = { scada_frame = self.frame, @@ -563,7 +563,7 @@ end -- convert rtu_t to RTU unit type ---@param type rtu_t ---@return RTU_UNIT_TYPES|nil -comms.rtu_t_to_unit_type = function (type) +function comms.rtu_t_to_unit_type(type) if type == rtu_t.redstone then return RTU_UNIT_TYPES.REDSTONE elseif type == rtu_t.boiler then @@ -586,7 +586,7 @@ end -- convert RTU unit type to rtu_t ---@param utype RTU_UNIT_TYPES ---@return rtu_t|nil -comms.advert_type_to_rtu_t = function (utype) +function comms.advert_type_to_rtu_t(utype) if utype == RTU_UNIT_TYPES.REDSTONE then return rtu_t.redstone elseif utype == RTU_UNIT_TYPES.BOILER then diff --git a/scada-common/crypto.lua b/scada-common/crypto.lua index 19db76b..d16bf84 100644 --- a/scada-common/crypto.lua +++ b/scada-common/crypto.lua @@ -1,20 +1,19 @@ -local aes128 = require("lockbox.cipher.aes128") -local ctr_mode = require("lockbox.cipher.mode.ctr"); +-- +-- Cryptographic Communications Engine +-- -local sha1 = require("lockbox.digest.sha1"); +local aes128 = require("lockbox.cipher.aes128") +local ctr_mode = require("lockbox.cipher.mode.ctr"); +local sha1 = require("lockbox.digest.sha1"); local sha2_224 = require("lockbox.digest.sha2_224"); local sha2_256 = require("lockbox.digest.sha2_256"); - -local pbkdf2 = require("lockbox.kdf.pbkdf2") - -local hmac = require("lockbox.mac.hmac") - +local pbkdf2 = require("lockbox.kdf.pbkdf2") +local hmac = require("lockbox.mac.hmac") local zero_pad = require("lockbox.padding.zero"); +local stream = require("lockbox.util.stream") +local array = require("lockbox.util.array") -local stream = require("lockbox.util.stream") -local array = require("lockbox.util.array") - -local log = require("scada-common.log") +local log = require("scada-common.log") local util = require("scada-common.util") local crypto = {} diff --git a/scada-common/log.lua b/scada-common/log.lua index 791b11b..f923c0c 100644 --- a/scada-common/log.lua +++ b/scada-common/log.lua @@ -1,9 +1,9 @@ -local util = require("scada-common.util") - -- -- File System Logger -- +local util = require("scada-common.util") + ---@class log local log = {} @@ -32,7 +32,7 @@ local free_space = fs.getFreeSpace ---@param path string file path ---@param write_mode MODE ---@param dmesg_redirect? table terminal/window to direct dmesg to -log.init = function (path, write_mode, dmesg_redirect) +function log.init(path, write_mode, dmesg_redirect) _log_sys.path = path _log_sys.mode = write_mode @@ -51,13 +51,13 @@ end -- direct dmesg output to a monitor/window ---@param window table window or terminal reference -log.direct_dmesg = function (window) +function log.direct_dmesg(window) _log_sys.dmesg_out = window end -- private log write function ---@param msg string -local _log = function (msg) +local function _log(msg) local time_stamp = os.date("[%c] ") local stamped = time_stamp .. util.strval(msg) @@ -94,7 +94,7 @@ end ---@param msg string message ---@param tag? string log tag ---@param tag_color? integer log tag color -log.dmesg = function (msg, tag, tag_color) +function log.dmesg(msg, tag, tag_color) msg = util.strval(msg) tag = tag or "" tag = util.strval(tag) @@ -183,7 +183,7 @@ end -- log debug messages ---@param msg string message ---@param trace? boolean include file trace -log.debug = function (msg, trace) +function log.debug(msg, trace) if LOG_DEBUG then local dbg_info = "" @@ -204,20 +204,20 @@ end -- log info messages ---@param msg string message -log.info = function (msg) +function log.info(msg) _log("[INF] " .. util.strval(msg)) end -- log warning messages ---@param msg string message -log.warning = function (msg) +function log.warning(msg) _log("[WRN] " .. util.strval(msg)) end -- log error messages ---@param msg string message ---@param trace? boolean include file trace -log.error = function (msg, trace) +function log.error(msg, trace) local dbg_info = "" if trace then @@ -236,7 +236,7 @@ end -- log fatal errors ---@param msg string message -log.fatal = function (msg) +function log.fatal(msg) _log("[FTL] " .. util.strval(msg)) end diff --git a/scada-common/mqueue.lua b/scada-common/mqueue.lua index 8069ecb..fd80cfa 100644 --- a/scada-common/mqueue.lua +++ b/scada-common/mqueue.lua @@ -14,7 +14,7 @@ local TYPE = { mqueue.TYPE = TYPE -- create a new message queue -mqueue.new = function () +function mqueue.new() local queue = {} local insert = table.insert @@ -32,44 +32,44 @@ mqueue.new = function () local public = {} -- get queue length - public.length = function () return #queue end + function public.length() return #queue end -- check if queue is empty ---@return boolean is_empty - public.empty = function () return #queue == 0 end + function public.empty() return #queue == 0 end -- check if queue has contents - public.ready = function () return #queue ~= 0 end + function public.ready() return #queue ~= 0 end -- push a new item onto the queue ---@param qtype TYPE ---@param message string - local _push = function (qtype, message) + local function _push(qtype, message) insert(queue, { qtype = qtype, message = message }) end -- push a command onto the queue ---@param message any - public.push_command = function (message) + function public.push_command(message) _push(TYPE.COMMAND, message) end -- push data onto the queue ---@param key any ---@param value any - public.push_data = function (key, value) + function public.push_data(key, value) _push(TYPE.DATA, { key = key, val = value }) end -- push a packet onto the queue ---@param packet scada_packet|modbus_packet|rplc_packet|coord_packet|capi_packet - public.push_packet = function (packet) + function public.push_packet(packet) _push(TYPE.PACKET, packet) end -- get an item off the queue ---@return queue_item|nil - public.pop = function () + function public.pop() if #queue > 0 then return remove(queue, 1) else diff --git a/scada-common/ppm.lua b/scada-common/ppm.lua index 5b65f47..740dc2b 100644 --- a/scada-common/ppm.lua +++ b/scada-common/ppm.lua @@ -1,9 +1,9 @@ -local log = require("scada-common.log") - -- -- Protected Peripheral Manager -- +local log = require("scada-common.log") + ---@class ppm local ppm = {} @@ -32,7 +32,7 @@ local _ppm_sys = { --- ---assumes iface is a valid peripheral ---@param iface string CC peripheral interface -local peri_init = function (iface) +local function peri_init(iface) local self = { faulted = false, last_fault = "", @@ -92,13 +92,13 @@ local peri_init = function (iface) -- fault management functions - local clear_fault = function () self.faulted = false end - local get_last_fault = function () return self.last_fault end - local is_faulted = function () return self.faulted end - local is_ok = function () return not self.faulted end + local function clear_fault() self.faulted = false end + local function get_last_fault() return self.last_fault end + local function is_faulted() return self.faulted end + local function is_ok() return not self.faulted end - local enable_afc = function () self.auto_cf = true end - local disable_afc = function () self.auto_cf = false end + local function enable_afc() self.auto_cf = true end + local function disable_afc() self.auto_cf = false end -- append to device functions @@ -122,53 +122,53 @@ end -- REPORTING -- -- silence error prints -ppm.disable_reporting = function () +function ppm.disable_reporting() _ppm_sys.mute = true end -- allow error prints -ppm.enable_reporting = function () +function ppm.enable_reporting() _ppm_sys.mute = false end -- FAULT MEMORY -- -- enable automatically clearing fault flag -ppm.enable_afc = function () +function ppm.enable_afc() _ppm_sys.auto_cf = true end -- disable automatically clearing fault flag -ppm.disable_afc = function () +function ppm.disable_afc() _ppm_sys.auto_cf = false end -- clear fault flag -ppm.clear_fault = function () +function ppm.clear_fault() _ppm_sys.faulted = false end -- check fault flag -ppm.is_faulted = function () +function ppm.is_faulted() return _ppm_sys.faulted end -- get the last fault message -ppm.get_last_fault = function () +function ppm.get_last_fault() return _ppm_sys.last_fault end -- TERMINATION -- -- if a caught error was a termination request -ppm.should_terminate = function () +function ppm.should_terminate() return _ppm_sys.terminate end -- MOUNTING -- -- mount all available peripherals (clears mounts first) -ppm.mount_all = function () +function ppm.mount_all() local ifaces = peripheral.getNames() _ppm_sys.mounts = {} @@ -187,7 +187,7 @@ end -- mount a particular device ---@param iface string CC peripheral interface ---@return string|nil type, table|nil device -ppm.mount = function (iface) +function ppm.mount(iface) local ifaces = peripheral.getNames() local pm_dev = nil local pm_type = nil @@ -210,7 +210,7 @@ end -- handle peripheral_detach event ---@param iface string CC peripheral interface ---@return string|nil type, table|nil device -ppm.handle_unmount = function (iface) +function ppm.handle_unmount(iface) local pm_dev = nil local pm_type = nil @@ -233,20 +233,20 @@ end -- list all available peripherals ---@return table names -ppm.list_avail = function () +function ppm.list_avail() return peripheral.getNames() end -- list mounted peripherals ---@return table mounts -ppm.list_mounts = function () +function ppm.list_mounts() return _ppm_sys.mounts end -- get a mounted peripheral by side/interface ---@param iface string CC peripheral interface ---@return table|nil device function table -ppm.get_periph = function (iface) +function ppm.get_periph(iface) if _ppm_sys.mounts[iface] then return _ppm_sys.mounts[iface].dev else return nil end @@ -255,7 +255,7 @@ end -- get a mounted peripheral type by side/interface ---@param iface string CC peripheral interface ---@return string|nil type -ppm.get_type = function (iface) +function ppm.get_type(iface) if _ppm_sys.mounts[iface] then return _ppm_sys.mounts[iface].type else return nil end @@ -264,7 +264,7 @@ end -- get all mounted peripherals by type ---@param name string type name ---@return table devices device function tables -ppm.get_all_devices = function (name) +function ppm.get_all_devices(name) local devices = {} for _, data in pairs(_ppm_sys.mounts) do @@ -279,7 +279,7 @@ end -- get a mounted peripheral by type (if multiple, returns the first) ---@param name string type name ---@return table|nil device function table -ppm.get_device = function (name) +function ppm.get_device(name) local device = nil for side, data in pairs(_ppm_sys.mounts) do @@ -296,13 +296,13 @@ end -- get the fission reactor (if multiple, returns the first) ---@return table|nil reactor function table -ppm.get_fission_reactor = function () +function ppm.get_fission_reactor() return ppm.get_device("fissionReactor") end -- get the wireless modem (if multiple, returns the first) ---@return table|nil modem function table -ppm.get_wireless_modem = function () +function ppm.get_wireless_modem() local w_modem = nil for _, device in pairs(_ppm_sys.mounts) do @@ -317,7 +317,7 @@ end -- list all connected monitors ---@return table monitors -ppm.get_monitor_list = function () +function ppm.get_monitor_list() local list = {} for iface, device in pairs(_ppm_sys.mounts) do diff --git a/scada-common/rsio.lua b/scada-common/rsio.lua index 9ba6878..05f078c 100644 --- a/scada-common/rsio.lua +++ b/scada-common/rsio.lua @@ -77,7 +77,7 @@ rsio.IO = RS_IO -- channel to string ---@param channel RS_IO -rsio.to_string = function (channel) +function rsio.to_string(channel) local names = { "F_SCRAM", "R_SCRAM", @@ -160,7 +160,7 @@ local RS_DIO_MAP = { -- get the mode of a channel ---@param channel RS_IO ---@return IO_MODE -rsio.get_io_mode = function (channel) +function rsio.get_io_mode(channel) local modes = { IO_MODE.DIGITAL_IN, -- F_SCRAM IO_MODE.DIGITAL_IN, -- R_SCRAM @@ -200,14 +200,14 @@ local RS_SIDES = rs.getSides() -- check if a channel is valid ---@param channel RS_IO ---@return boolean valid -rsio.is_valid_channel = function (channel) +function rsio.is_valid_channel(channel) return (type(channel) == "number") and (channel > 0) and (channel <= RS_IO.R_PLC_TIMEOUT) end -- check if a side is valid ---@param side string ---@return boolean valid -rsio.is_valid_side = function (side) +function rsio.is_valid_side(side) if side ~= nil then for i = 0, #RS_SIDES do if RS_SIDES[i] == side then return true end @@ -219,7 +219,7 @@ end -- check if a color is a valid single color ---@param color integer ---@return boolean valid -rsio.is_color = function (color) +function rsio.is_color(color) return (type(color) == "number") and (color > 0) and (_B_AND(color, (color - 1)) == 0); end @@ -230,7 +230,7 @@ end -- get digital IO level reading ---@param rs_value boolean ---@return IO_LVL -rsio.digital_read = function (rs_value) +function rsio.digital_read(rs_value) if rs_value then return IO_LVL.HIGH else @@ -242,7 +242,7 @@ end ---@param channel RS_IO ---@param level IO_LVL ---@return boolean -rsio.digital_write = function (channel, level) +function rsio.digital_write(channel, level) if type(channel) ~= "number" or channel < RS_IO.F_ALARM or channel > RS_IO.R_PLC_TIMEOUT then return false else @@ -254,7 +254,7 @@ end ---@param channel RS_IO ---@param level IO_LVL ---@return boolean -rsio.digital_is_active = function (channel, level) +function rsio.digital_is_active(channel, level) if type(channel) ~= "number" or channel > RS_IO.R_ENABLE then return false else @@ -271,7 +271,7 @@ end ---@param min number minimum of range ---@param max number maximum of range ---@return number value scaled reading (min to max) -rsio.analog_read = function (rs_value, min, max) +function rsio.analog_read(rs_value, min, max) local value = rs_value / 15 return (value * (max - min)) + min end @@ -281,7 +281,7 @@ end ---@param min number minimum of range ---@param max number maximum of range ---@return number rs_value scaled redstone reading (0 to 15) -rsio.analog_write = function (value, min, max) +function rsio.analog_write(value, min, max) local scaled_value = (value - min) / (max - min) return scaled_value * 15 end diff --git a/scada-common/util.lua b/scada-common/util.lua index 5bbe2b8..bfba8d9 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -12,7 +12,7 @@ local util = {} ---@param a any return if true ---@param b any return if false ---@return any value -util.trinary = function (cond, a, b) +function util.trinary(cond, a, b) if cond then return a else return b end end @@ -20,25 +20,25 @@ end -- print ---@param message any -util.print = function (message) +function util.print(message) term.write(tostring(message)) end -- print line ---@param message any -util.println = function (message) +function util.println(message) print(tostring(message)) end -- timestamped print ---@param message any -util.print_ts = function (message) +function util.print_ts(message) term.write(os.date("[%H:%M:%S] ") .. tostring(message)) end -- timestamped print line ---@param message any -util.println_ts = function (message) +function util.println_ts(message) print(os.date("[%H:%M:%S] ") .. tostring(message)) end @@ -47,7 +47,7 @@ end -- get a value as a string ---@param val any ---@return string -util.strval = function (val) +function util.strval(val) local t = type(val) if t == "table" or t == "function" then return "[" .. tostring(val) .. "]" @@ -59,7 +59,7 @@ end -- concatenation with built-in to string ---@vararg any ---@return string -util.concat = function (...) +function util.concat(...) local str = "" for _, v in ipairs(arg) do str = str .. util.strval(v) @@ -73,7 +73,7 @@ util.c = util.concat -- sprintf implementation ---@param format string ---@vararg any -util.sprintf = function (format, ...) +function util.sprintf(format, ...) return string.format(format, table.unpack(arg)) end @@ -81,7 +81,7 @@ end -- round a number to an integer ---@return integer rounded -util.round = function (x) +function util.round(x) return math.floor(x + 0.5) end @@ -89,21 +89,21 @@ end -- current time ---@return integer milliseconds -util.time_ms = function () +function util.time_ms() ---@diagnostic disable-next-line: undefined-field return os.epoch('local') end -- current time ---@return number seconds -util.time_s = function () +function util.time_s() ---@diagnostic disable-next-line: undefined-field return os.epoch('local') / 1000.0 end -- current time ---@return integer milliseconds -util.time = function () +function util.time() return util.time_ms() end @@ -112,7 +112,7 @@ end -- protected sleep call so we still are in charge of catching termination ---@param t integer seconds --- EVENT_CONSUMER: this function consumes events -util.psleep = function (t) +function util.psleep(t) ---@diagnostic disable-next-line: undefined-field pcall(os.sleep, t) end @@ -120,7 +120,7 @@ end -- no-op to provide a brief pause (1 tick) to yield --- --- EVENT_CONSUMER: this function consumes events -util.nop = function () +function util.nop() util.psleep(0.05) end @@ -129,7 +129,7 @@ end ---@param last_update integer millisecond time of last update ---@return integer time_now -- EVENT_CONSUMER: this function consumes events -util.adaptive_delay = function (target_timing, last_update) +function util.adaptive_delay(target_timing, last_update) local sleep_for = target_timing - (util.time() - last_update) -- only if >50ms since worker loops already yield 0.05s if sleep_for >= 50 then @@ -146,7 +146,7 @@ end ---@param t table table to remove elements from ---@param f function should return false to delete an element when passed the element: f(elem) = true|false ---@param on_delete? function optional function to execute on deletion, passed the table element to be deleted as the parameter -util.filter_table = function (t, f, on_delete) +function util.filter_table(t, f, on_delete) local move_to = 1 for i = 1, #t do local element = t[i] @@ -168,7 +168,7 @@ end -- check if a table contains the provided element ---@param t table table to check ---@param element any element to check for -util.table_contains = function (t, element) +function util.table_contains(t, element) for i = 1, #t do if t[i] == element then return true end end @@ -213,7 +213,7 @@ end ---@param timeout number timeout duration --- --- triggers a timer event if not fed within 'timeout' seconds -util.new_watchdog = function (timeout) +function util.new_watchdog(timeout) ---@diagnostic disable-next-line: undefined-field local start_timer = os.startTimer ---@diagnostic disable-next-line: undefined-field @@ -228,12 +228,12 @@ util.new_watchdog = function (timeout) local public = {} ---@param timer number timer event timer ID - public.is_timer = function (timer) + function public.is_timer(timer) return self.wd_timer == timer end -- satiate the beast - public.feed = function () + function public.feed() if self.wd_timer ~= nil then cancel_timer(self.wd_timer) end @@ -241,7 +241,7 @@ util.new_watchdog = function (timeout) end -- cancel the watchdog - public.cancel = function () + function public.cancel() if self.wd_timer ~= nil then cancel_timer(self.wd_timer) end @@ -256,7 +256,7 @@ end ---@param period number clock period --- --- fires a timer event at the specified period, does not start at construct time -util.new_clock = function (period) +function util.new_clock(period) ---@diagnostic disable-next-line: undefined-field local start_timer = os.startTimer @@ -269,12 +269,12 @@ util.new_clock = function (period) local public = {} ---@param timer number timer event timer ID - public.is_clock = function (timer) + function public.is_clock(timer) return self.timer == timer end -- start the clock - public.start = function () + function public.start() self.timer = start_timer(self.period) end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 1e77fa9..bf17a08 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -13,7 +13,7 @@ local svsessions = require("supervisor.session.svsessions") local config = require("supervisor.config") local supervisor = require("supervisor.supervisor") -local SUPERVISOR_VERSION = "beta-v0.4.3" +local SUPERVISOR_VERSION = "beta-v0.4.4" local print = util.print local println = util.println