#64 util code cleanup

This commit is contained in:
Mikayla Fischler
2022-05-31 16:09:06 -04:00
parent 341df1a739
commit 3bb95eb441
12 changed files with 147 additions and 148 deletions

View File

@ -12,7 +12,7 @@ local config = require("coordinator.config")
local coordinator = require("coordinator.coordinator") local coordinator = require("coordinator.coordinator")
local renderer = require("coordinator.renderer") local renderer = require("coordinator.renderer")
local COORDINATOR_VERSION = "alpha-v0.1.2" local COORDINATOR_VERSION = "alpha-v0.1.3"
local print = util.print local print = util.print
local println = util.println local println = util.println

View File

@ -13,7 +13,7 @@ local config = require("reactor-plc.config")
local plc = require("reactor-plc.plc") local plc = require("reactor-plc.plc")
local threads = require("reactor-plc.threads") 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 print = util.print
local println = util.println local println = util.println

View File

@ -24,7 +24,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu")
local turbine_rtu = require("rtu.dev.turbine_rtu") local turbine_rtu = require("rtu.dev.turbine_rtu")
local turbinev_rtu = require("rtu.dev.turbinev_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 local rtu_t = types.rtu_t

View File

@ -17,7 +17,7 @@ alarm.SEVERITY = SEVERITY
-- severity integer to string -- severity integer to string
---@param severity SEVERITY ---@param severity SEVERITY
alarm.severity_to_string = function (severity) function alarm.severity_to_string(severity)
if severity == SEVERITY.INFO then if severity == SEVERITY.INFO then
return "INFO" return "INFO"
elseif severity == SEVERITY.WARNING then elseif severity == SEVERITY.WARNING then
@ -39,7 +39,7 @@ end
---@param severity SEVERITY ---@param severity SEVERITY
---@param device string ---@param device string
---@param message string ---@param message string
alarm.scada_alarm = function (severity, device, message) function alarm.scada_alarm(severity, device, message)
local self = { local self = {
time = util.time(), time = util.time(),
ts_string = os.date("[%H:%M:%S]"), ts_string = os.date("[%H:%M:%S]"),
@ -53,12 +53,12 @@ alarm.scada_alarm = function (severity, device, message)
-- format the alarm as a string -- format the alarm as a string
---@return string message ---@return string message
public.format = function () function public.format()
return self.ts_string .. " [" .. alarm.severity_to_string(self.severity) .. "] (" .. self.device ") >> " .. self.message return self.ts_string .. " [" .. alarm.severity_to_string(self.severity) .. "] (" .. self.device .. ") >> " .. self.message
end end
-- get alarm properties -- get alarm properties
public.properties = function () function public.properties()
return { return {
time = self.time, time = self.time,
severity = self.severity, severity = self.severity,

View File

@ -2,7 +2,7 @@
-- Communications -- Communications
-- --
local log = require("scada-common.log") local log = require("scada-common.log")
local types = require("scada-common.types") local types = require("scada-common.types")
---@class comms ---@class comms
@ -66,7 +66,7 @@ comms.SCADA_MGMT_TYPES = SCADA_MGMT_TYPES
comms.RTU_UNIT_TYPES = RTU_UNIT_TYPES comms.RTU_UNIT_TYPES = RTU_UNIT_TYPES
-- generic SCADA packet object -- generic SCADA packet object
comms.scada_packet = function () function comms.scada_packet()
local self = { local self = {
modem_msg_in = nil, modem_msg_in = nil,
valid = false, valid = false,
@ -84,7 +84,7 @@ comms.scada_packet = function ()
---@param seq_num integer ---@param seq_num integer
---@param protocol PROTOCOLS ---@param protocol PROTOCOLS
---@param payload table ---@param payload table
public.make = function (seq_num, protocol, payload) function public.make(seq_num, protocol, payload)
self.valid = true self.valid = true
self.seq_num = seq_num self.seq_num = seq_num
self.protocol = protocol self.protocol = protocol
@ -99,7 +99,7 @@ comms.scada_packet = function ()
---@param reply_to integer ---@param reply_to integer
---@param message any ---@param message any
---@param distance integer ---@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 = { self.modem_msg_in = {
iface = side, iface = side,
s_port = sender, s_port = sender,
@ -125,25 +125,25 @@ comms.scada_packet = function ()
-- public accessors -- -- public accessors --
public.modem_event = function () return self.modem_msg_in end function public.modem_event() return self.modem_msg_in end
public.raw_sendable = function () return self.raw end function public.raw_sendable() return self.raw end
public.local_port = function () return self.modem_msg_in.s_port end function public.local_port() return self.modem_msg_in.s_port end
public.remote_port = function () return self.modem_msg_in.r_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 function public.seq_num() return self.seq_num end
public.protocol = function () return self.protocol end function public.protocol() return self.protocol end
public.length = function () return self.length end function public.length() return self.length end
public.data = function () return self.payload end function public.data() return self.payload end
return public return public
end end
-- MODBUS packet -- MODBUS packet
-- modeled after MODBUS TCP packet -- modeled after MODBUS TCP packet
comms.modbus_packet = function () function comms.modbus_packet()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
@ -162,7 +162,7 @@ comms.modbus_packet = function ()
---@param unit_id integer ---@param unit_id integer
---@param func_code MODBUS_FCODE ---@param func_code MODBUS_FCODE
---@param data table ---@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.txn_id = txn_id
self.length = #data self.length = #data
self.unit_id = unit_id self.unit_id = unit_id
@ -179,7 +179,7 @@ comms.modbus_packet = function ()
-- decode a MODBUS packet from a SCADA frame -- decode a MODBUS packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return boolean success
public.decode = function (frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
@ -203,10 +203,10 @@ comms.modbus_packet = function ()
end end
-- get raw to send -- 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 -- get this packet as a frame with an immutable relation to this object
public.get = function () function public.get()
---@class modbus_frame ---@class modbus_frame
local frame = { local frame = {
scada_frame = self.frame, scada_frame = self.frame,
@ -224,7 +224,7 @@ comms.modbus_packet = function ()
end end
-- reactor PLC packet -- reactor PLC packet
comms.rplc_packet = function () function comms.rplc_packet()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
@ -238,7 +238,7 @@ comms.rplc_packet = function ()
local public = {} local public = {}
-- check that type is known -- check that type is known
local _rplc_type_valid = function () local function _rplc_type_valid()
return self.type == RPLC_TYPES.LINK_REQ or return self.type == RPLC_TYPES.LINK_REQ or
self.type == RPLC_TYPES.STATUS or self.type == RPLC_TYPES.STATUS or
self.type == RPLC_TYPES.MEK_STRUCT or self.type == RPLC_TYPES.MEK_STRUCT or
@ -254,7 +254,7 @@ comms.rplc_packet = function ()
---@param id integer ---@param id integer
---@param packet_type RPLC_TYPES ---@param packet_type RPLC_TYPES
---@param data table ---@param data table
public.make = function (id, packet_type, data) function public.make(id, packet_type, data)
-- packet accessor properties -- packet accessor properties
self.id = id self.id = id
self.type = packet_type self.type = packet_type
@ -271,7 +271,7 @@ comms.rplc_packet = function ()
-- decode an RPLC packet from a SCADA frame -- decode an RPLC packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return boolean success
public.decode = function (frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
@ -296,10 +296,10 @@ comms.rplc_packet = function ()
end end
-- get raw to send -- 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 -- get this packet as a frame with an immutable relation to this object
public.get = function () function public.get()
---@class rplc_frame ---@class rplc_frame
local frame = { local frame = {
scada_frame = self.frame, scada_frame = self.frame,
@ -316,7 +316,7 @@ comms.rplc_packet = function ()
end end
-- SCADA management packet -- SCADA management packet
comms.mgmt_packet = function () function comms.mgmt_packet()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
@ -329,7 +329,7 @@ comms.mgmt_packet = function ()
local public = {} local public = {}
-- check that type is known -- check that type is known
local _scada_type_valid = function () local function _scada_type_valid()
return self.type == SCADA_MGMT_TYPES.KEEP_ALIVE or return self.type == SCADA_MGMT_TYPES.KEEP_ALIVE or
self.type == SCADA_MGMT_TYPES.CLOSE or self.type == SCADA_MGMT_TYPES.CLOSE or
self.type == SCADA_MGMT_TYPES.REMOTE_LINKED or self.type == SCADA_MGMT_TYPES.REMOTE_LINKED or
@ -339,7 +339,7 @@ comms.mgmt_packet = function ()
-- make a SCADA management packet -- make a SCADA management packet
---@param packet_type SCADA_MGMT_TYPES ---@param packet_type SCADA_MGMT_TYPES
---@param data table ---@param data table
public.make = function (packet_type, data) function public.make(packet_type, data)
-- packet accessor properties -- packet accessor properties
self.type = packet_type self.type = packet_type
self.length = #data self.length = #data
@ -355,7 +355,7 @@ comms.mgmt_packet = function ()
-- decode a SCADA management packet from a SCADA frame -- decode a SCADA management packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return boolean success
public.decode = function (frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
@ -380,10 +380,10 @@ comms.mgmt_packet = function ()
end end
-- get raw to send -- 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 -- get this packet as a frame with an immutable relation to this object
public.get = function () function public.get()
---@class mgmt_frame ---@class mgmt_frame
local frame = { local frame = {
scada_frame = self.frame, scada_frame = self.frame,
@ -400,7 +400,7 @@ end
-- SCADA coordinator packet -- SCADA coordinator packet
-- @todo -- @todo
comms.coord_packet = function () function comms.coord_packet()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
@ -412,7 +412,7 @@ comms.coord_packet = function ()
---@class coord_packet ---@class coord_packet
local public = {} local public = {}
local _coord_type_valid = function () local function _coord_type_valid()
-- @todo -- @todo
return false return false
end end
@ -420,7 +420,7 @@ comms.coord_packet = function ()
-- make a coordinator packet -- make a coordinator packet
---@param packet_type any ---@param packet_type any
---@param data table ---@param data table
public.make = function (packet_type, data) function public.make(packet_type, data)
-- packet accessor properties -- packet accessor properties
self.type = packet_type self.type = packet_type
self.length = #data self.length = #data
@ -436,7 +436,7 @@ comms.coord_packet = function ()
-- decode a coordinator packet from a SCADA frame -- decode a coordinator packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return boolean success
public.decode = function (frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
@ -461,10 +461,10 @@ comms.coord_packet = function ()
end end
-- get raw to send -- 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 -- get this packet as a frame with an immutable relation to this object
public.get = function () function public.get()
---@class coord_frame ---@class coord_frame
local frame = { local frame = {
scada_frame = self.frame, scada_frame = self.frame,
@ -481,7 +481,7 @@ end
-- coordinator API (CAPI) packet -- coordinator API (CAPI) packet
-- @todo -- @todo
comms.capi_packet = function () function comms.capi_packet()
local self = { local self = {
frame = nil, frame = nil,
raw = nil, raw = nil,
@ -493,7 +493,7 @@ comms.capi_packet = function ()
---@class capi_packet ---@class capi_packet
local public = {} local public = {}
local _coord_type_valid = function () local function _coord_type_valid()
-- @todo -- @todo
return false return false
end end
@ -501,7 +501,7 @@ comms.capi_packet = function ()
-- make a coordinator API packet -- make a coordinator API packet
---@param packet_type any ---@param packet_type any
---@param data table ---@param data table
public.make = function (packet_type, data) function public.make(packet_type, data)
-- packet accessor properties -- packet accessor properties
self.type = packet_type self.type = packet_type
self.length = #data self.length = #data
@ -517,7 +517,7 @@ comms.capi_packet = function ()
-- decode a coordinator API packet from a SCADA frame -- decode a coordinator API packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return boolean success
public.decode = function (frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
@ -542,10 +542,10 @@ comms.capi_packet = function ()
end end
-- get raw to send -- 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 -- get this packet as a frame with an immutable relation to this object
public.get = function () function public.get()
---@class capi_frame ---@class capi_frame
local frame = { local frame = {
scada_frame = self.frame, scada_frame = self.frame,
@ -563,7 +563,7 @@ end
-- convert rtu_t to RTU unit type -- convert rtu_t to RTU unit type
---@param type rtu_t ---@param type rtu_t
---@return RTU_UNIT_TYPES|nil ---@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 if type == rtu_t.redstone then
return RTU_UNIT_TYPES.REDSTONE return RTU_UNIT_TYPES.REDSTONE
elseif type == rtu_t.boiler then elseif type == rtu_t.boiler then
@ -586,7 +586,7 @@ end
-- convert RTU unit type to rtu_t -- convert RTU unit type to rtu_t
---@param utype RTU_UNIT_TYPES ---@param utype RTU_UNIT_TYPES
---@return rtu_t|nil ---@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 if utype == RTU_UNIT_TYPES.REDSTONE then
return rtu_t.redstone return rtu_t.redstone
elseif utype == RTU_UNIT_TYPES.BOILER then elseif utype == RTU_UNIT_TYPES.BOILER then

View File

@ -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_224 = require("lockbox.digest.sha2_224");
local sha2_256 = require("lockbox.digest.sha2_256"); local sha2_256 = require("lockbox.digest.sha2_256");
local pbkdf2 = require("lockbox.kdf.pbkdf2")
local pbkdf2 = require("lockbox.kdf.pbkdf2") local hmac = require("lockbox.mac.hmac")
local hmac = require("lockbox.mac.hmac")
local zero_pad = require("lockbox.padding.zero"); 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 log = require("scada-common.log")
local array = require("lockbox.util.array")
local log = require("scada-common.log")
local util = require("scada-common.util") local util = require("scada-common.util")
local crypto = {} local crypto = {}

View File

@ -1,9 +1,9 @@
local util = require("scada-common.util")
-- --
-- File System Logger -- File System Logger
-- --
local util = require("scada-common.util")
---@class log ---@class log
local log = {} local log = {}
@ -32,7 +32,7 @@ local free_space = fs.getFreeSpace
---@param path string file path ---@param path string file path
---@param write_mode MODE ---@param write_mode MODE
---@param dmesg_redirect? table terminal/window to direct dmesg to ---@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.path = path
_log_sys.mode = write_mode _log_sys.mode = write_mode
@ -51,13 +51,13 @@ end
-- direct dmesg output to a monitor/window -- direct dmesg output to a monitor/window
---@param window table window or terminal reference ---@param window table window or terminal reference
log.direct_dmesg = function (window) function log.direct_dmesg(window)
_log_sys.dmesg_out = window _log_sys.dmesg_out = window
end end
-- private log write function -- private log write function
---@param msg string ---@param msg string
local _log = function (msg) local function _log(msg)
local time_stamp = os.date("[%c] ") local time_stamp = os.date("[%c] ")
local stamped = time_stamp .. util.strval(msg) local stamped = time_stamp .. util.strval(msg)
@ -94,7 +94,7 @@ end
---@param msg string message ---@param msg string message
---@param tag? string log tag ---@param tag? string log tag
---@param tag_color? integer log tag color ---@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) msg = util.strval(msg)
tag = tag or "" tag = tag or ""
tag = util.strval(tag) tag = util.strval(tag)
@ -183,7 +183,7 @@ end
-- log debug messages -- log debug messages
---@param msg string message ---@param msg string message
---@param trace? boolean include file trace ---@param trace? boolean include file trace
log.debug = function (msg, trace) function log.debug(msg, trace)
if LOG_DEBUG then if LOG_DEBUG then
local dbg_info = "" local dbg_info = ""
@ -204,20 +204,20 @@ end
-- log info messages -- log info messages
---@param msg string message ---@param msg string message
log.info = function (msg) function log.info(msg)
_log("[INF] " .. util.strval(msg)) _log("[INF] " .. util.strval(msg))
end end
-- log warning messages -- log warning messages
---@param msg string message ---@param msg string message
log.warning = function (msg) function log.warning(msg)
_log("[WRN] " .. util.strval(msg)) _log("[WRN] " .. util.strval(msg))
end end
-- log error messages -- log error messages
---@param msg string message ---@param msg string message
---@param trace? boolean include file trace ---@param trace? boolean include file trace
log.error = function (msg, trace) function log.error(msg, trace)
local dbg_info = "" local dbg_info = ""
if trace then if trace then
@ -236,7 +236,7 @@ end
-- log fatal errors -- log fatal errors
---@param msg string message ---@param msg string message
log.fatal = function (msg) function log.fatal(msg)
_log("[FTL] " .. util.strval(msg)) _log("[FTL] " .. util.strval(msg))
end end

View File

@ -14,7 +14,7 @@ local TYPE = {
mqueue.TYPE = TYPE mqueue.TYPE = TYPE
-- create a new message queue -- create a new message queue
mqueue.new = function () function mqueue.new()
local queue = {} local queue = {}
local insert = table.insert local insert = table.insert
@ -32,44 +32,44 @@ mqueue.new = function ()
local public = {} local public = {}
-- get queue length -- get queue length
public.length = function () return #queue end function public.length() return #queue end
-- check if queue is empty -- check if queue is empty
---@return boolean 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 -- 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 -- push a new item onto the queue
---@param qtype TYPE ---@param qtype TYPE
---@param message string ---@param message string
local _push = function (qtype, message) local function _push(qtype, message)
insert(queue, { qtype = qtype, message = message }) insert(queue, { qtype = qtype, message = message })
end end
-- push a command onto the queue -- push a command onto the queue
---@param message any ---@param message any
public.push_command = function (message) function public.push_command(message)
_push(TYPE.COMMAND, message) _push(TYPE.COMMAND, message)
end end
-- push data onto the queue -- push data onto the queue
---@param key any ---@param key any
---@param value any ---@param value any
public.push_data = function (key, value) function public.push_data(key, value)
_push(TYPE.DATA, { key = key, val = value }) _push(TYPE.DATA, { key = key, val = value })
end end
-- push a packet onto the queue -- push a packet onto the queue
---@param packet scada_packet|modbus_packet|rplc_packet|coord_packet|capi_packet ---@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) _push(TYPE.PACKET, packet)
end end
-- get an item off the queue -- get an item off the queue
---@return queue_item|nil ---@return queue_item|nil
public.pop = function () function public.pop()
if #queue > 0 then if #queue > 0 then
return remove(queue, 1) return remove(queue, 1)
else else

View File

@ -1,9 +1,9 @@
local log = require("scada-common.log")
-- --
-- Protected Peripheral Manager -- Protected Peripheral Manager
-- --
local log = require("scada-common.log")
---@class ppm ---@class ppm
local ppm = {} local ppm = {}
@ -32,7 +32,7 @@ local _ppm_sys = {
--- ---
---assumes iface is a valid peripheral ---assumes iface is a valid peripheral
---@param iface string CC peripheral interface ---@param iface string CC peripheral interface
local peri_init = function (iface) local function peri_init(iface)
local self = { local self = {
faulted = false, faulted = false,
last_fault = "", last_fault = "",
@ -92,13 +92,13 @@ local peri_init = function (iface)
-- fault management functions -- fault management functions
local clear_fault = function () self.faulted = false end local function clear_fault() self.faulted = false end
local get_last_fault = function () return self.last_fault end local function get_last_fault() return self.last_fault end
local is_faulted = function () return self.faulted end local function is_faulted() return self.faulted end
local is_ok = function () return not self.faulted end local function is_ok() return not self.faulted end
local enable_afc = function () self.auto_cf = true end local function enable_afc() self.auto_cf = true end
local disable_afc = function () self.auto_cf = false end local function disable_afc() self.auto_cf = false end
-- append to device functions -- append to device functions
@ -122,53 +122,53 @@ end
-- REPORTING -- -- REPORTING --
-- silence error prints -- silence error prints
ppm.disable_reporting = function () function ppm.disable_reporting()
_ppm_sys.mute = true _ppm_sys.mute = true
end end
-- allow error prints -- allow error prints
ppm.enable_reporting = function () function ppm.enable_reporting()
_ppm_sys.mute = false _ppm_sys.mute = false
end end
-- FAULT MEMORY -- -- FAULT MEMORY --
-- enable automatically clearing fault flag -- enable automatically clearing fault flag
ppm.enable_afc = function () function ppm.enable_afc()
_ppm_sys.auto_cf = true _ppm_sys.auto_cf = true
end end
-- disable automatically clearing fault flag -- disable automatically clearing fault flag
ppm.disable_afc = function () function ppm.disable_afc()
_ppm_sys.auto_cf = false _ppm_sys.auto_cf = false
end end
-- clear fault flag -- clear fault flag
ppm.clear_fault = function () function ppm.clear_fault()
_ppm_sys.faulted = false _ppm_sys.faulted = false
end end
-- check fault flag -- check fault flag
ppm.is_faulted = function () function ppm.is_faulted()
return _ppm_sys.faulted return _ppm_sys.faulted
end end
-- get the last fault message -- get the last fault message
ppm.get_last_fault = function () function ppm.get_last_fault()
return _ppm_sys.last_fault return _ppm_sys.last_fault
end end
-- TERMINATION -- -- TERMINATION --
-- if a caught error was a termination request -- if a caught error was a termination request
ppm.should_terminate = function () function ppm.should_terminate()
return _ppm_sys.terminate return _ppm_sys.terminate
end end
-- MOUNTING -- -- MOUNTING --
-- mount all available peripherals (clears mounts first) -- mount all available peripherals (clears mounts first)
ppm.mount_all = function () function ppm.mount_all()
local ifaces = peripheral.getNames() local ifaces = peripheral.getNames()
_ppm_sys.mounts = {} _ppm_sys.mounts = {}
@ -187,7 +187,7 @@ end
-- mount a particular device -- mount a particular device
---@param iface string CC peripheral interface ---@param iface string CC peripheral interface
---@return string|nil type, table|nil device ---@return string|nil type, table|nil device
ppm.mount = function (iface) function ppm.mount(iface)
local ifaces = peripheral.getNames() local ifaces = peripheral.getNames()
local pm_dev = nil local pm_dev = nil
local pm_type = nil local pm_type = nil
@ -210,7 +210,7 @@ end
-- handle peripheral_detach event -- handle peripheral_detach event
---@param iface string CC peripheral interface ---@param iface string CC peripheral interface
---@return string|nil type, table|nil device ---@return string|nil type, table|nil device
ppm.handle_unmount = function (iface) function ppm.handle_unmount(iface)
local pm_dev = nil local pm_dev = nil
local pm_type = nil local pm_type = nil
@ -233,20 +233,20 @@ end
-- list all available peripherals -- list all available peripherals
---@return table names ---@return table names
ppm.list_avail = function () function ppm.list_avail()
return peripheral.getNames() return peripheral.getNames()
end end
-- list mounted peripherals -- list mounted peripherals
---@return table mounts ---@return table mounts
ppm.list_mounts = function () function ppm.list_mounts()
return _ppm_sys.mounts return _ppm_sys.mounts
end end
-- get a mounted peripheral by side/interface -- get a mounted peripheral by side/interface
---@param iface string CC peripheral interface ---@param iface string CC peripheral interface
---@return table|nil device function table ---@return table|nil device function table
ppm.get_periph = function (iface) function ppm.get_periph(iface)
if _ppm_sys.mounts[iface] then if _ppm_sys.mounts[iface] then
return _ppm_sys.mounts[iface].dev return _ppm_sys.mounts[iface].dev
else return nil end else return nil end
@ -255,7 +255,7 @@ end
-- get a mounted peripheral type by side/interface -- get a mounted peripheral type by side/interface
---@param iface string CC peripheral interface ---@param iface string CC peripheral interface
---@return string|nil type ---@return string|nil type
ppm.get_type = function (iface) function ppm.get_type(iface)
if _ppm_sys.mounts[iface] then if _ppm_sys.mounts[iface] then
return _ppm_sys.mounts[iface].type return _ppm_sys.mounts[iface].type
else return nil end else return nil end
@ -264,7 +264,7 @@ end
-- get all mounted peripherals by type -- get all mounted peripherals by type
---@param name string type name ---@param name string type name
---@return table devices device function tables ---@return table devices device function tables
ppm.get_all_devices = function (name) function ppm.get_all_devices(name)
local devices = {} local devices = {}
for _, data in pairs(_ppm_sys.mounts) do for _, data in pairs(_ppm_sys.mounts) do
@ -279,7 +279,7 @@ end
-- get a mounted peripheral by type (if multiple, returns the first) -- get a mounted peripheral by type (if multiple, returns the first)
---@param name string type name ---@param name string type name
---@return table|nil device function table ---@return table|nil device function table
ppm.get_device = function (name) function ppm.get_device(name)
local device = nil local device = nil
for side, data in pairs(_ppm_sys.mounts) do for side, data in pairs(_ppm_sys.mounts) do
@ -296,13 +296,13 @@ end
-- get the fission reactor (if multiple, returns the first) -- get the fission reactor (if multiple, returns the first)
---@return table|nil reactor function table ---@return table|nil reactor function table
ppm.get_fission_reactor = function () function ppm.get_fission_reactor()
return ppm.get_device("fissionReactor") return ppm.get_device("fissionReactor")
end end
-- get the wireless modem (if multiple, returns the first) -- get the wireless modem (if multiple, returns the first)
---@return table|nil modem function table ---@return table|nil modem function table
ppm.get_wireless_modem = function () function ppm.get_wireless_modem()
local w_modem = nil local w_modem = nil
for _, device in pairs(_ppm_sys.mounts) do for _, device in pairs(_ppm_sys.mounts) do
@ -317,7 +317,7 @@ end
-- list all connected monitors -- list all connected monitors
---@return table monitors ---@return table monitors
ppm.get_monitor_list = function () function ppm.get_monitor_list()
local list = {} local list = {}
for iface, device in pairs(_ppm_sys.mounts) do for iface, device in pairs(_ppm_sys.mounts) do

View File

@ -77,7 +77,7 @@ rsio.IO = RS_IO
-- channel to string -- channel to string
---@param channel RS_IO ---@param channel RS_IO
rsio.to_string = function (channel) function rsio.to_string(channel)
local names = { local names = {
"F_SCRAM", "F_SCRAM",
"R_SCRAM", "R_SCRAM",
@ -160,7 +160,7 @@ local RS_DIO_MAP = {
-- get the mode of a channel -- get the mode of a channel
---@param channel RS_IO ---@param channel RS_IO
---@return IO_MODE ---@return IO_MODE
rsio.get_io_mode = function (channel) function rsio.get_io_mode(channel)
local modes = { local modes = {
IO_MODE.DIGITAL_IN, -- F_SCRAM IO_MODE.DIGITAL_IN, -- F_SCRAM
IO_MODE.DIGITAL_IN, -- R_SCRAM IO_MODE.DIGITAL_IN, -- R_SCRAM
@ -200,14 +200,14 @@ local RS_SIDES = rs.getSides()
-- check if a channel is valid -- check if a channel is valid
---@param channel RS_IO ---@param channel RS_IO
---@return boolean valid ---@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) return (type(channel) == "number") and (channel > 0) and (channel <= RS_IO.R_PLC_TIMEOUT)
end end
-- check if a side is valid -- check if a side is valid
---@param side string ---@param side string
---@return boolean valid ---@return boolean valid
rsio.is_valid_side = function (side) function rsio.is_valid_side(side)
if side ~= nil then if side ~= nil then
for i = 0, #RS_SIDES do for i = 0, #RS_SIDES do
if RS_SIDES[i] == side then return true end if RS_SIDES[i] == side then return true end
@ -219,7 +219,7 @@ end
-- check if a color is a valid single color -- check if a color is a valid single color
---@param color integer ---@param color integer
---@return boolean valid ---@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); return (type(color) == "number") and (color > 0) and (_B_AND(color, (color - 1)) == 0);
end end
@ -230,7 +230,7 @@ end
-- get digital IO level reading -- get digital IO level reading
---@param rs_value boolean ---@param rs_value boolean
---@return IO_LVL ---@return IO_LVL
rsio.digital_read = function (rs_value) function rsio.digital_read(rs_value)
if rs_value then if rs_value then
return IO_LVL.HIGH return IO_LVL.HIGH
else else
@ -242,7 +242,7 @@ end
---@param channel RS_IO ---@param channel RS_IO
---@param level IO_LVL ---@param level IO_LVL
---@return boolean ---@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 if type(channel) ~= "number" or channel < RS_IO.F_ALARM or channel > RS_IO.R_PLC_TIMEOUT then
return false return false
else else
@ -254,7 +254,7 @@ end
---@param channel RS_IO ---@param channel RS_IO
---@param level IO_LVL ---@param level IO_LVL
---@return boolean ---@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 if type(channel) ~= "number" or channel > RS_IO.R_ENABLE then
return false return false
else else
@ -271,7 +271,7 @@ end
---@param min number minimum of range ---@param min number minimum of range
---@param max number maximum of range ---@param max number maximum of range
---@return number value scaled reading (min to max) ---@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 local value = rs_value / 15
return (value * (max - min)) + min return (value * (max - min)) + min
end end
@ -281,7 +281,7 @@ end
---@param min number minimum of range ---@param min number minimum of range
---@param max number maximum of range ---@param max number maximum of range
---@return number rs_value scaled redstone reading (0 to 15) ---@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) local scaled_value = (value - min) / (max - min)
return scaled_value * 15 return scaled_value * 15
end end

View File

@ -12,7 +12,7 @@ local util = {}
---@param a any return if true ---@param a any return if true
---@param b any return if false ---@param b any return if false
---@return any value ---@return any value
util.trinary = function (cond, a, b) function util.trinary(cond, a, b)
if cond then return a else return b end if cond then return a else return b end
end end
@ -20,25 +20,25 @@ end
-- print -- print
---@param message any ---@param message any
util.print = function (message) function util.print(message)
term.write(tostring(message)) term.write(tostring(message))
end end
-- print line -- print line
---@param message any ---@param message any
util.println = function (message) function util.println(message)
print(tostring(message)) print(tostring(message))
end end
-- timestamped print -- timestamped print
---@param message any ---@param message any
util.print_ts = function (message) function util.print_ts(message)
term.write(os.date("[%H:%M:%S] ") .. tostring(message)) term.write(os.date("[%H:%M:%S] ") .. tostring(message))
end end
-- timestamped print line -- timestamped print line
---@param message any ---@param message any
util.println_ts = function (message) function util.println_ts(message)
print(os.date("[%H:%M:%S] ") .. tostring(message)) print(os.date("[%H:%M:%S] ") .. tostring(message))
end end
@ -47,7 +47,7 @@ end
-- get a value as a string -- get a value as a string
---@param val any ---@param val any
---@return string ---@return string
util.strval = function (val) function util.strval(val)
local t = type(val) local t = type(val)
if t == "table" or t == "function" then if t == "table" or t == "function" then
return "[" .. tostring(val) .. "]" return "[" .. tostring(val) .. "]"
@ -59,7 +59,7 @@ end
-- concatenation with built-in to string -- concatenation with built-in to string
---@vararg any ---@vararg any
---@return string ---@return string
util.concat = function (...) function util.concat(...)
local str = "" local str = ""
for _, v in ipairs(arg) do for _, v in ipairs(arg) do
str = str .. util.strval(v) str = str .. util.strval(v)
@ -73,7 +73,7 @@ util.c = util.concat
-- sprintf implementation -- sprintf implementation
---@param format string ---@param format string
---@vararg any ---@vararg any
util.sprintf = function (format, ...) function util.sprintf(format, ...)
return string.format(format, table.unpack(arg)) return string.format(format, table.unpack(arg))
end end
@ -81,7 +81,7 @@ end
-- round a number to an integer -- round a number to an integer
---@return integer rounded ---@return integer rounded
util.round = function (x) function util.round(x)
return math.floor(x + 0.5) return math.floor(x + 0.5)
end end
@ -89,21 +89,21 @@ end
-- current time -- current time
---@return integer milliseconds ---@return integer milliseconds
util.time_ms = function () function util.time_ms()
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
return os.epoch('local') return os.epoch('local')
end end
-- current time -- current time
---@return number seconds ---@return number seconds
util.time_s = function () function util.time_s()
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
return os.epoch('local') / 1000.0 return os.epoch('local') / 1000.0
end end
-- current time -- current time
---@return integer milliseconds ---@return integer milliseconds
util.time = function () function util.time()
return util.time_ms() return util.time_ms()
end end
@ -112,7 +112,7 @@ end
-- protected sleep call so we still are in charge of catching termination -- protected sleep call so we still are in charge of catching termination
---@param t integer seconds ---@param t integer seconds
--- EVENT_CONSUMER: this function consumes events --- EVENT_CONSUMER: this function consumes events
util.psleep = function (t) function util.psleep(t)
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
pcall(os.sleep, t) pcall(os.sleep, t)
end end
@ -120,7 +120,7 @@ end
-- no-op to provide a brief pause (1 tick) to yield -- no-op to provide a brief pause (1 tick) to yield
--- ---
--- EVENT_CONSUMER: this function consumes events --- EVENT_CONSUMER: this function consumes events
util.nop = function () function util.nop()
util.psleep(0.05) util.psleep(0.05)
end end
@ -129,7 +129,7 @@ end
---@param last_update integer millisecond time of last update ---@param last_update integer millisecond time of last update
---@return integer time_now ---@return integer time_now
-- EVENT_CONSUMER: this function consumes events -- 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) 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
if sleep_for >= 50 then if sleep_for >= 50 then
@ -146,7 +146,7 @@ end
---@param t table table to remove elements from ---@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 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 ---@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 local move_to = 1
for i = 1, #t do for i = 1, #t do
local element = t[i] local element = t[i]
@ -168,7 +168,7 @@ end
-- check if a table contains the provided element -- check if a table contains the provided element
---@param t table table to check ---@param t table table to check
---@param element any element to check for ---@param element any element to check for
util.table_contains = function (t, element) function util.table_contains(t, element)
for i = 1, #t do for i = 1, #t do
if t[i] == element then return true end if t[i] == element then return true end
end end
@ -213,7 +213,7 @@ end
---@param timeout number timeout duration ---@param timeout number timeout duration
--- ---
--- 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) function util.new_watchdog(timeout)
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
local start_timer = os.startTimer local start_timer = os.startTimer
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
@ -228,12 +228,12 @@ util.new_watchdog = function (timeout)
local public = {} local public = {}
---@param timer number timer event timer ID ---@param timer number timer event timer ID
public.is_timer = function (timer) function public.is_timer(timer)
return self.wd_timer == timer return self.wd_timer == timer
end end
-- satiate the beast -- satiate the beast
public.feed = function () function public.feed()
if self.wd_timer ~= nil then if self.wd_timer ~= nil then
cancel_timer(self.wd_timer) cancel_timer(self.wd_timer)
end end
@ -241,7 +241,7 @@ util.new_watchdog = function (timeout)
end end
-- cancel the watchdog -- cancel the watchdog
public.cancel = function () function public.cancel()
if self.wd_timer ~= nil then if self.wd_timer ~= nil then
cancel_timer(self.wd_timer) cancel_timer(self.wd_timer)
end end
@ -256,7 +256,7 @@ end
---@param period number clock period ---@param period number clock period
--- ---
--- fires a timer event at the specified period, does not start at construct time --- 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 ---@diagnostic disable-next-line: undefined-field
local start_timer = os.startTimer local start_timer = os.startTimer
@ -269,12 +269,12 @@ util.new_clock = function (period)
local public = {} local public = {}
---@param timer number timer event timer ID ---@param timer number timer event timer ID
public.is_clock = function (timer) function public.is_clock(timer)
return self.timer == timer return self.timer == timer
end end
-- start the clock -- start the clock
public.start = function () function public.start()
self.timer = start_timer(self.period) self.timer = start_timer(self.period)
end end

View File

@ -13,7 +13,7 @@ local svsessions = require("supervisor.session.svsessions")
local config = require("supervisor.config") local config = require("supervisor.config")
local supervisor = require("supervisor.supervisor") local supervisor = require("supervisor.supervisor")
local SUPERVISOR_VERSION = "beta-v0.4.3" local SUPERVISOR_VERSION = "beta-v0.4.4"
local print = util.print local print = util.print
local println = util.println local println = util.println