2022-03-10 19:09:21 +00:00
|
|
|
--
|
|
|
|
-- Protected Peripheral Manager
|
|
|
|
--
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-31 20:09:06 +00:00
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class ppm
|
2022-05-04 17:37:01 +00:00
|
|
|
local ppm = {}
|
|
|
|
|
2023-07-30 16:24:54 +00:00
|
|
|
local ACCESS_FAULT = nil ---@type nil
|
|
|
|
local UNDEFINED_FIELD = "undefined field"
|
2022-11-12 06:35:31 +00:00
|
|
|
local VIRTUAL_DEVICE_TYPE = "ppm_vdev"
|
|
|
|
|
2023-07-30 16:24:54 +00:00
|
|
|
ppm.ACCESS_FAULT = ACCESS_FAULT
|
|
|
|
ppm.UNDEFINED_FIELD = UNDEFINED_FIELD
|
|
|
|
ppm.VIRTUAL_DEVICE_TYPE = VIRTUAL_DEVICE_TYPE
|
2022-04-18 01:12:25 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
----------------------------
|
|
|
|
-- PRIVATE DATA/FUNCTIONS --
|
|
|
|
----------------------------
|
|
|
|
|
2022-05-19 14:35:56 +00:00
|
|
|
local REPORT_FREQUENCY = 20 -- log every 20 faults per function
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
local ppm_sys = {
|
2022-04-02 15:45:43 +00:00
|
|
|
mounts = {},
|
2022-11-12 06:35:31 +00:00
|
|
|
next_vid = 0,
|
2022-04-18 01:12:25 +00:00
|
|
|
auto_cf = false,
|
|
|
|
faulted = false,
|
2022-05-19 13:19:51 +00:00
|
|
|
last_fault = "",
|
2022-04-18 14:31:24 +00:00
|
|
|
terminate = false,
|
2022-04-02 15:45:43 +00:00
|
|
|
mute = false
|
2022-03-10 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 16:24:54 +00:00
|
|
|
-- Wrap peripheral calls with lua protected call as we don't want a disconnect to crash a program.
|
|
|
|
-- Additionally provides peripheral-specific fault checks (auto-clear fault defaults to true).<br>
|
|
|
|
-- Note: assumes iface is a valid peripheral.
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param iface string CC peripheral interface
|
2022-05-31 20:09:06 +00:00
|
|
|
local function peri_init(iface)
|
2022-04-24 16:04:31 +00:00
|
|
|
local self = {
|
|
|
|
faulted = false,
|
2022-05-19 13:19:51 +00:00
|
|
|
last_fault = "",
|
2022-05-19 14:35:56 +00:00
|
|
|
fault_counts = {},
|
2022-04-24 16:04:31 +00:00
|
|
|
auto_cf = true,
|
2022-11-12 06:35:31 +00:00
|
|
|
type = VIRTUAL_DEVICE_TYPE,
|
|
|
|
device = {}
|
2022-04-24 16:04:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 06:35:31 +00:00
|
|
|
if iface ~= "__virtual__" then
|
|
|
|
self.type = peripheral.getType(iface)
|
|
|
|
self.device = peripheral.wrap(iface)
|
|
|
|
end
|
|
|
|
|
2022-04-24 16:04:31 +00:00
|
|
|
-- initialization process (re-map)
|
|
|
|
|
|
|
|
for key, func in pairs(self.device) do
|
2022-05-19 14:35:56 +00:00
|
|
|
self.fault_counts[key] = 0
|
2022-04-24 16:04:31 +00:00
|
|
|
self.device[key] = function (...)
|
2022-05-29 18:26:40 +00:00
|
|
|
local return_table = table.pack(pcall(func, ...))
|
|
|
|
|
|
|
|
local status = return_table[1]
|
|
|
|
table.remove(return_table, 1)
|
2022-03-10 19:12:07 +00:00
|
|
|
|
|
|
|
if status then
|
2022-04-18 01:12:25 +00:00
|
|
|
-- auto fault clear
|
|
|
|
if self.auto_cf then self.faulted = false end
|
2023-02-21 15:31:05 +00:00
|
|
|
if ppm_sys.auto_cf then ppm_sys.faulted = false end
|
2022-05-19 14:35:56 +00:00
|
|
|
|
|
|
|
self.fault_counts[key] = 0
|
|
|
|
|
2022-05-29 18:26:40 +00:00
|
|
|
return table.unpack(return_table)
|
2022-03-10 19:12:07 +00:00
|
|
|
else
|
2022-05-29 18:26:40 +00:00
|
|
|
local result = return_table[1]
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- function failed
|
2022-04-18 01:12:25 +00:00
|
|
|
self.faulted = true
|
2022-05-19 13:19:51 +00:00
|
|
|
self.last_fault = result
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.faulted = true
|
|
|
|
ppm_sys.last_fault = result
|
2022-04-18 14:31:24 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
if not ppm_sys.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then
|
2022-05-19 14:35:56 +00:00
|
|
|
local count_str = ""
|
|
|
|
if self.fault_counts[key] > 0 then
|
|
|
|
count_str = " [" .. self.fault_counts[key] .. " total faults]"
|
|
|
|
end
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
log.error(util.c("PPM: protected ", key, "() -> ", result, count_str))
|
2022-04-02 15:45:43 +00:00
|
|
|
end
|
2022-04-18 14:31:24 +00:00
|
|
|
|
2022-05-19 14:35:56 +00:00
|
|
|
self.fault_counts[key] = self.fault_counts[key] + 1
|
|
|
|
|
2022-04-18 14:31:24 +00:00
|
|
|
if result == "Terminated" then
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.terminate = true
|
2022-04-18 14:31:24 +00:00
|
|
|
end
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
return ACCESS_FAULT
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-04-24 16:04:31 +00:00
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
-- fault management & monitoring functions
|
2022-04-24 16:04:31 +00:00
|
|
|
|
2022-05-31 20:09:06 +00:00
|
|
|
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
|
2022-04-24 16:04:31 +00:00
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
-- check if a peripheral has any faulted functions<br>
|
|
|
|
-- contrasted with is_faulted() and is_ok() as those only check if the last operation failed,
|
|
|
|
-- unless auto fault clearing is disabled, at which point faults become sticky faults
|
|
|
|
local function is_healthy()
|
|
|
|
for _, v in pairs(self.fault_counts) do if v > 0 then return false end end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-05-31 20:09:06 +00:00
|
|
|
local function enable_afc() self.auto_cf = true end
|
|
|
|
local function disable_afc() self.auto_cf = false end
|
2022-04-24 16:04:31 +00:00
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
-- append PPM functions to device functions
|
2022-04-24 16:04:31 +00:00
|
|
|
|
|
|
|
self.device.__p_clear_fault = clear_fault
|
2022-05-19 13:19:51 +00:00
|
|
|
self.device.__p_last_fault = get_last_fault
|
2022-04-24 16:04:31 +00:00
|
|
|
self.device.__p_is_faulted = is_faulted
|
|
|
|
self.device.__p_is_ok = is_ok
|
2023-06-22 19:46:17 +00:00
|
|
|
self.device.__p_is_healthy = is_healthy
|
2022-04-24 16:04:31 +00:00
|
|
|
self.device.__p_enable_afc = enable_afc
|
|
|
|
self.device.__p_disable_afc = disable_afc
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- add default index function to catch undefined indicies
|
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
local mt = {
|
|
|
|
__index = function (_, key)
|
|
|
|
-- this will continuously be counting calls here as faults
|
|
|
|
-- unlike other functions, faults here can't be cleared as it is just not defined
|
|
|
|
if self.fault_counts[key] == nil then
|
|
|
|
self.fault_counts[key] = 0
|
|
|
|
end
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
-- function failed
|
|
|
|
self.faulted = true
|
|
|
|
self.last_fault = UNDEFINED_FIELD
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.faulted = true
|
|
|
|
ppm_sys.last_fault = UNDEFINED_FIELD
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
if not ppm_sys.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then
|
2022-10-26 03:40:36 +00:00
|
|
|
local count_str = ""
|
|
|
|
if self.fault_counts[key] > 0 then
|
|
|
|
count_str = " [" .. self.fault_counts[key] .. " total calls]"
|
|
|
|
end
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
log.error(util.c("PPM: caught undefined function ", key, "()", count_str))
|
2022-10-25 17:29:57 +00:00
|
|
|
end
|
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
self.fault_counts[key] = self.fault_counts[key] + 1
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
return (function () return ACCESS_FAULT end)
|
|
|
|
end
|
|
|
|
}
|
2022-10-25 17:29:57 +00:00
|
|
|
|
|
|
|
setmetatable(self.device, mt)
|
|
|
|
|
2023-11-04 16:56:49 +00:00
|
|
|
---@class ppm_entry
|
|
|
|
local entry = { type = self.type, dev = self.device }
|
|
|
|
|
|
|
|
return entry
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
----------------------
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
-- REPORTING --
|
|
|
|
|
2022-04-02 15:45:43 +00:00
|
|
|
-- silence error prints
|
2023-02-21 15:31:05 +00:00
|
|
|
function ppm.disable_reporting() ppm_sys.mute = true end
|
2022-04-02 15:45:43 +00:00
|
|
|
|
|
|
|
-- allow error prints
|
2023-02-21 15:31:05 +00:00
|
|
|
function ppm.enable_reporting() ppm_sys.mute = false end
|
2022-04-02 15:45:43 +00:00
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- FAULT MEMORY --
|
|
|
|
|
|
|
|
-- enable automatically clearing fault flag
|
2023-02-21 15:31:05 +00:00
|
|
|
function ppm.enable_afc() ppm_sys.auto_cf = true end
|
2022-04-18 01:12:25 +00:00
|
|
|
|
|
|
|
-- disable automatically clearing fault flag
|
2023-02-21 15:31:05 +00:00
|
|
|
function ppm.disable_afc() ppm_sys.auto_cf = false end
|
2022-04-18 01:12:25 +00:00
|
|
|
|
2022-05-19 13:19:51 +00:00
|
|
|
-- clear fault flag
|
2023-02-21 15:31:05 +00:00
|
|
|
function ppm.clear_fault() ppm_sys.faulted = false end
|
2022-05-19 13:19:51 +00:00
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- check fault flag
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
function ppm.is_faulted() return ppm_sys.faulted end
|
2022-04-18 01:12:25 +00:00
|
|
|
|
2022-05-19 13:19:51 +00:00
|
|
|
-- get the last fault message
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
function ppm.get_last_fault() return ppm_sys.last_fault end
|
2022-04-18 01:12:25 +00:00
|
|
|
|
2022-04-18 14:31:24 +00:00
|
|
|
-- TERMINATION --
|
|
|
|
|
|
|
|
-- if a caught error was a termination request
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
function ppm.should_terminate() return ppm_sys.terminate end
|
2022-04-18 14:31:24 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
-- MOUNTING --
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- mount all available peripherals (clears mounts first)
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.mount_all()
|
2022-03-10 19:12:07 +00:00
|
|
|
local ifaces = peripheral.getNames()
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts = {}
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
for i = 1, #ifaces do
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts[ifaces[i]] = peri_init(ifaces[i])
|
2022-04-05 21:29:27 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
log.info(util.c("PPM: found a ", ppm_sys.mounts[ifaces[i]].type, " (", ifaces[i], ")"))
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
2022-03-10 19:21:03 +00:00
|
|
|
|
|
|
|
if #ifaces == 0 then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("PPM: mount_all() -> no devices found")
|
2022-03-10 19:21:03 +00:00
|
|
|
end
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- mount a particular device
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param iface string CC peripheral interface
|
|
|
|
---@return string|nil type, table|nil device
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.mount(iface)
|
2022-03-10 19:12:07 +00:00
|
|
|
local ifaces = peripheral.getNames()
|
|
|
|
local pm_dev = nil
|
2022-04-24 16:04:31 +00:00
|
|
|
local pm_type = nil
|
2022-03-10 19:12:07 +00:00
|
|
|
|
|
|
|
for i = 1, #ifaces do
|
2022-04-05 21:29:27 +00:00
|
|
|
if iface == ifaces[i] then
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts[iface] = peri_init(iface)
|
2022-03-10 19:12:07 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
pm_type = ppm_sys.mounts[iface].type
|
|
|
|
pm_dev = ppm_sys.mounts[iface].dev
|
2022-05-22 21:57:24 +00:00
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
log.info(util.c("PPM: mount(", iface, ") -> found a ", pm_type))
|
2022-03-10 19:12:07 +00:00
|
|
|
break
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-24 16:04:31 +00:00
|
|
|
return pm_type, pm_dev
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-11-12 06:35:31 +00:00
|
|
|
-- mount a virtual, placeholder device (specifically designed for RTU startup with missing devices)
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-11-12 06:35:31 +00:00
|
|
|
---@return string type, table device
|
|
|
|
function ppm.mount_virtual()
|
2023-02-21 15:31:05 +00:00
|
|
|
local iface = "ppm_vdev_" .. ppm_sys.next_vid
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts[iface] = peri_init("__virtual__")
|
|
|
|
ppm_sys.next_vid = ppm_sys.next_vid + 1
|
2022-11-12 06:35:31 +00:00
|
|
|
|
|
|
|
log.info(util.c("PPM: mount_virtual() -> allocated new virtual device ", iface))
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
return ppm_sys.mounts[iface].type, ppm_sys.mounts[iface].dev
|
2022-11-12 06:35:31 +00:00
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- manually unmount a peripheral from the PPM
|
|
|
|
---@param device table device table
|
|
|
|
function ppm.unmount(device)
|
|
|
|
if device then
|
2023-02-21 15:31:05 +00:00
|
|
|
for side, data in pairs(ppm_sys.mounts) do
|
2022-10-25 17:29:57 +00:00
|
|
|
if data.dev == device then
|
|
|
|
log.warning(util.c("PPM: manually unmounted ", data.type, " mounted to ", side))
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts[side] = nil
|
2022-10-25 17:29:57 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- handle peripheral_detach event
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param iface string CC peripheral interface
|
|
|
|
---@return string|nil type, table|nil device
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.handle_unmount(iface)
|
2022-05-10 21:06:27 +00:00
|
|
|
local pm_dev = nil
|
|
|
|
local pm_type = nil
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- what got disconnected?
|
2023-02-21 15:31:05 +00:00
|
|
|
local lost_dev = ppm_sys.mounts[iface]
|
2022-04-22 14:58:18 +00:00
|
|
|
|
|
|
|
if lost_dev then
|
2022-05-10 21:06:27 +00:00
|
|
|
pm_type = lost_dev.type
|
|
|
|
pm_dev = lost_dev.dev
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
log.warning(util.c("PPM: lost device ", pm_type, " mounted to ", iface))
|
2022-04-22 14:58:18 +00:00
|
|
|
else
|
2022-06-05 15:16:25 +00:00
|
|
|
log.error(util.c("PPM: lost device unknown to the PPM mounted to ", iface))
|
2022-04-22 14:58:18 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
ppm_sys.mounts[iface] = nil
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
return pm_type, pm_dev
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
-- GENERAL ACCESSORS --
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- list all available peripherals
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return table names
|
2023-07-30 16:24:54 +00:00
|
|
|
function ppm.list_avail() return peripheral.getNames() end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- list mounted peripherals
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return table mounts
|
2023-11-04 16:56:49 +00:00
|
|
|
function ppm.list_mounts()
|
|
|
|
local list = {}
|
|
|
|
for k, v in pairs(ppm_sys.mounts) do list[k] = v end
|
|
|
|
return list
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- get a mounted peripheral side/interface by device table
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-10-25 17:29:57 +00:00
|
|
|
---@param device table device table
|
|
|
|
---@return string|nil iface CC peripheral interface
|
|
|
|
function ppm.get_iface(device)
|
|
|
|
if device then
|
2023-02-21 15:31:05 +00:00
|
|
|
for side, data in pairs(ppm_sys.mounts) do
|
2022-10-25 17:29:57 +00:00
|
|
|
if data.dev == device then return side end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- get a mounted peripheral by side/interface
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param iface string CC peripheral interface
|
|
|
|
---@return table|nil device function table
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_periph(iface)
|
2023-02-21 15:31:05 +00:00
|
|
|
if ppm_sys.mounts[iface] then
|
|
|
|
return ppm_sys.mounts[iface].dev
|
2022-04-22 14:21:15 +00:00
|
|
|
else return nil end
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-15 15:58:22 +00:00
|
|
|
-- get a mounted peripheral type by side/interface
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param iface string CC peripheral interface
|
|
|
|
---@return string|nil type
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_type(iface)
|
2023-02-21 15:31:05 +00:00
|
|
|
if ppm_sys.mounts[iface] then
|
|
|
|
return ppm_sys.mounts[iface].type
|
2022-04-22 14:21:15 +00:00
|
|
|
else return nil end
|
2022-03-15 15:58:22 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
-- get all mounted peripherals by type
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param name string type name
|
|
|
|
---@return table devices device function tables
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_all_devices(name)
|
2022-04-07 15:44:17 +00:00
|
|
|
local devices = {}
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
for _, data in pairs(ppm_sys.mounts) do
|
2022-04-07 15:44:17 +00:00
|
|
|
if data.type == name then
|
|
|
|
table.insert(devices, data.dev)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return devices
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get a mounted peripheral by type (if multiple, returns the first)
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param name string type name
|
|
|
|
---@return table|nil device function table
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_device(name)
|
2022-03-10 19:12:07 +00:00
|
|
|
local device = nil
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
for _, data in pairs(ppm_sys.mounts) do
|
2022-03-10 19:12:07 +00:00
|
|
|
if data.type == name then
|
2022-04-05 21:29:27 +00:00
|
|
|
device = data.dev
|
2022-03-10 19:12:07 +00:00
|
|
|
break
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-10 15:35:52 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
return device
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
-- SPECIFIC DEVICE ACCESSORS --
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
-- get the fission reactor (if multiple, returns the first)
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return table|nil reactor function table
|
2023-07-30 16:24:54 +00:00
|
|
|
function ppm.get_fission_reactor() return ppm.get_device("fissionReactorLogicAdapter") end
|
2022-04-07 15:44:17 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
-- get the wireless modem (if multiple, returns the first)<br>
|
2022-07-19 18:02:20 +00:00
|
|
|
-- if this is in a CraftOS emulated environment, wired modems will be used instead
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return table|nil modem function table
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_wireless_modem()
|
2022-04-07 15:44:17 +00:00
|
|
|
local w_modem = nil
|
2022-07-19 18:02:20 +00:00
|
|
|
local emulated_env = periphemu ~= nil
|
2022-04-07 15:44:17 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
for _, device in pairs(ppm_sys.mounts) do
|
2022-07-19 18:02:20 +00:00
|
|
|
if device.type == "modem" and (emulated_env or device.dev.isWireless()) then
|
2022-04-07 15:44:17 +00:00
|
|
|
w_modem = device.dev
|
|
|
|
break
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:44:17 +00:00
|
|
|
return w_modem
|
|
|
|
end
|
|
|
|
|
|
|
|
-- list all connected monitors
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return table monitors
|
2022-05-31 20:09:06 +00:00
|
|
|
function ppm.get_monitor_list()
|
2022-05-29 18:34:09 +00:00
|
|
|
local list = {}
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
for iface, device in pairs(ppm_sys.mounts) do
|
2023-07-30 16:24:54 +00:00
|
|
|
if device.type == "monitor" then list[iface] = device end
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return list
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2024-02-18 18:00:18 +00:00
|
|
|
-- HELPER FUNCTIONS
|
|
|
|
|
|
|
|
-- get the block size of a monitor given its width and height <b>at a text scale of 0.5</b>
|
|
|
|
---@nodiscard
|
2024-02-19 01:21:07 +00:00
|
|
|
---@param width integer character width
|
|
|
|
---@param height integer character height
|
2024-02-18 20:30:18 +00:00
|
|
|
---@return integer block_width, integer block_height
|
2024-02-19 01:21:07 +00:00
|
|
|
function ppm.monitor_block_size(width, height)
|
|
|
|
return math.floor((width - 15) / 21) + 1, math.floor((height - 10) / 14) + 1
|
2024-02-18 18:00:18 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return ppm
|