Skip to content

Commit

Permalink
Merge pull request #32 from cyberbit/feature/secure-modem
Browse files Browse the repository at this point in the history
Add Secure Modem adapters
  • Loading branch information
cyberbit authored Nov 20, 2023
2 parents 88182d1 + 4110c07 commit 36c0b67
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/telem/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local _Telem = {
_VERSION = '0.2.0',
_VERSION = '0.3.0',
util = require 'telem.lib.util',
input = require 'telem.lib.input',
output = require 'telem.lib.output',
Expand Down
80 changes: 68 additions & 12 deletions src/telem/lib/Backplane.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,69 @@ function Backplane:constructor ()
-- workaround to guarantee processing order
self.inputKeys = {}
self.outputKeys = {}

-- last recorded state
self.collection = MetricCollection()

self.asyncCycleHandlers = {}
end

-- TODO allow auto-named inputs based on type
function Backplane:addInput (name, input)
assert(type(name) == 'string', 'name must be a string')
assert(o.instanceof(input, InputAdapter), 'Input must be an InputAdapter')

-- propagate debug state
if self.debugState then
input:debug(self.debugState)
end

self.inputs[name] = input
table.insert(self.inputKeys, name)

if input.asyncCycleHandler then
self:addAsyncCycleHandler(name, input.asyncCycleHandler)
end

return self
end

function Backplane:addOutput (name, output)
assert(type(name) == 'string', 'name must be a string')
assert(o.instanceof(output, OutputAdapter), 'Output must be an OutputAdapter')

self:dlog('Backplane:addOutput :: adding output: ' .. name)

-- propagate debug state
if self.debugState then
output:debug(self.debugState)
end

self.outputs[name] = output
table.insert(self.outputKeys, name)

if output.asyncCycleHandler then
self:dlog('Backplane:addOutput :: registering async handler for ' .. name)

self:addAsyncCycleHandler(name, function ()
self:dlog('Backplane:asyncCycleHandler (closure) :: executing async handler for ' .. name)

local results = {pcall(output.asyncCycleHandler, self)}

if not table.remove(results, 1) then
t.log('Output fault in async handler for "' .. name .. '":')
t.pprint(table.remove(results, 1))
end
end)
end

return self
end

function Backplane:addAsyncCycleHandler (adapter, handler)
table.insert(self.asyncCycleHandlers, handler)
end

-- NYI
function Backplane:processMiddleware ()
--
Expand All @@ -50,16 +90,16 @@ function Backplane:cycle()
local tempMetrics = {}
local metrics = MetricCollection()

self:dlog(os.date())
self:dlog('** cycle START !')
self:dlog('Backplane:cycle :: ' .. os.date())
self:dlog('Backplane:cycle :: cycle START !')

self:dlog('reading inputs...')
self:dlog('Backplane:cycle :: reading inputs...')

-- read inputs
for _, key in ipairs(self.inputKeys) do
local input = self.inputs[key]

self:dlog(' - ' .. key)
self:dlog('Backplane:cycle :: - ' .. key)

local results = {pcall(input.read, input)}

Expand All @@ -71,7 +111,7 @@ function Backplane:cycle()

-- attach adapter name
for _,v in ipairs(inputMetrics.metrics) do
v.adapter = key
v.adapter = key .. (v.adapter and ':' .. v.adapter or '')

table.insert(tempMetrics, v)
end
Expand All @@ -80,9 +120,7 @@ function Backplane:cycle()

-- TODO process middleware

-- t.pprint(tempMetrics)

self:dlog('sorting metrics...')
self:dlog('Backplane:cycle :: sorting metrics...')

-- sort
-- TODO make this a middleware
Expand All @@ -91,13 +129,17 @@ function Backplane:cycle()
metrics:insert(v)
end

self:dlog('writing outputs...')
self:dlog('Backplane:cycle :: saving state...')

self.collection = metrics

self:dlog('Backplane:cycle :: writing outputs...')

-- write outputs
for _, key in pairs(self.outputKeys) do
local output = self.outputs[key]

self:dlog(' - ' .. key)
self:dlog('Backplane:cycle :: - ' .. key)

local results = {pcall(output.write, output, metrics)}

Expand All @@ -107,20 +149,34 @@ function Backplane:cycle()
end
end

self:dlog('** cycle END !')
self:dlog('Backplane:cycle :: cycle END !')

return self
end

-- return a function to cycle this Backplane on a set interval
function Backplane:cycleEvery(seconds)
return function()
local selfCycle = function()
while true do
self:cycle()

t.sleep(seconds)
end
end

-- TODO
-- this will break support for backplane:cycleEvery(3)() if
-- async-enabled adapters are attached. docs should be updated
-- to either remove the direct launching as an option, or
-- add guidance to all async-enabled adapters to use parallel.
if #{self.asyncCycleHandlers} > 0 then
self:dlog('Backplane:cycleEvery :: found async handlers, returning function list')

return selfCycle, table.unpack(self.asyncCycleHandlers)
end

self:dlog('Backplane:cycleEvery :: no async handlers found, returning cycle function')
return selfCycle
end

function Backplane:debug(debug)
Expand Down
21 changes: 21 additions & 0 deletions src/telem/lib/InputAdapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ InputAdapter.type = 'InputAdapter'
function InputAdapter:constructor()
assert(self.type ~= InputAdapter.type, 'InputAdapter cannot be instantiated')

self.debugState = false

self.prefix = ''
self.asyncCycleHandler = nil

-- boot components
self:setBoot(function ()
Expand All @@ -23,6 +26,14 @@ function InputAdapter:setBoot(proc)
return self.boot
end

function InputAdapter:setAsyncCycleHandler(proc)
assert(type(proc) == 'function', 'proc must be a function')

self.asyncCycleHandler = proc

return self.asyncCycleHandler
end

function InputAdapter:addComponentByPeripheralID (id)
local tempComponent = peripheral.wrap(id)

Expand All @@ -47,4 +58,14 @@ function InputAdapter:read ()
t.err(self.type .. ' has not implemented read()')
end

function InputAdapter:debug(debug)
self.debugState = debug and true or false

return self
end

function InputAdapter:dlog(msg)
if self.debugState then t.log(msg) end
end

return InputAdapter
49 changes: 46 additions & 3 deletions src/telem/lib/OutputAdapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,62 @@ OutputAdapter.type = 'OutputAdapter'
function OutputAdapter:constructor()
assert(self.type ~= OutputAdapter.type, 'OutputAdapter cannot be instantiated')

self.components = {}
self.debugState = false

self.asyncCycleHandler = nil

-- boot components
self:setBoot(function ()
self.components = {}
end)()
end

function OutputAdapter:setBoot(proc)
assert(type(proc) == 'function', 'proc must be a function')

self.boot = proc

return self.boot
end

function OutputAdapter:setAsyncCycleHandler(proc)
assert(type(proc) == 'function', 'proc must be a function')

self.asyncCycleHandler = proc

return self.asyncCycleHandler
end

function OutputAdapter:addComponentByPeripheralID (id)
self.components.insert(peripheral.wrap(id))
local tempComponent = peripheral.wrap(id)

assert(tempComponent, 'Could not find peripheral ID ' .. id)

self.components[id] = tempComponent
end

function OutputAdapter:addComponentByPeripheralType (type)
self.components.insert(peripheral.find(type))
local key = type .. '_' .. #{self.components}

local tempComponent = peripheral.find(type)

assert(tempComponent, 'Could not find peripheral type ' .. type)

self.components[key] = tempComponent
end

function OutputAdapter:write (metrics)
t.err(self.type .. ' has not implemented write()')
end

function OutputAdapter:debug(debug)
self.debugState = debug and true or false

return self
end

function OutputAdapter:dlog(msg)
if self.debugState then t.log(msg) end
end

return OutputAdapter
6 changes: 5 additions & 1 deletion src/telem/lib/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ return {
refinedStorage = require 'telem.lib.input.RefinedStorageInputAdapter',
meStorage = require 'telem.lib.input.MEStorageInputAdapter',

-- machinery
mekanism = {
fissionReactor = require 'telem.lib.input.mekanism.FissionReactorInputAdapter',
inductionMatrix = require 'telem.lib.input.mekanism.InductionMatrixInputAdapter',
industrialTurbine = require 'telem.lib.input.mekanism.IndustrialTurbineInputAdapter',
fusionReactor = require 'telem.lib.input.mekanism.FusionReactorInputAdapter',
}
},

-- modem
secureModem = require 'telem.lib.input.SecureModemInputAdapter'
}
Loading

0 comments on commit 36c0b67

Please sign in to comment.