-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from bananasov/feature/ap-energy-detector
Add Advanced Peripherals Energy Detector InputAdapter
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/telem/lib/input/advancedPeripherals/BaseAdvancedPeripheralsInputAdapter.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
local o = require 'telem.lib.ObjectModel' | ||
|
||
local InputAdapter = require 'telem.lib.InputAdapter' | ||
local Metric = require 'telem.lib.Metric' | ||
local MetricCollection = require 'telem.lib.MetricCollection' | ||
|
||
local BaseAdvancedPeripheralsAdapter = o.class(InputAdapter) | ||
BaseAdvancedPeripheralsAdapter.type = 'BaseAdvancedPeripheralsAdapter' | ||
|
||
function BaseAdvancedPeripheralsAdapter:constructor (peripheralName) | ||
self:super('constructor') | ||
|
||
self.prefix = 'ap:' | ||
|
||
self.queries = {} | ||
|
||
-- boot components | ||
self:setBoot(function () | ||
self.components = {} | ||
|
||
self:addComponentByPeripheralID(peripheralName) | ||
end)() | ||
end | ||
|
||
local function queueHelper (results, index, query) | ||
return function () | ||
results[index] = Metric(query:metricable():result()) | ||
end | ||
end | ||
|
||
function BaseAdvancedPeripheralsAdapter:read () | ||
self:boot() | ||
|
||
local source, component = next(self.components) | ||
|
||
local tempMetrics = {} | ||
local queue = {} | ||
|
||
for _, category in ipairs(self.categories) do | ||
for k, v in pairs(self.queries[category]) do | ||
table.insert(queue, queueHelper( | ||
tempMetrics, | ||
#queue + 1, | ||
v:from(component):with('name', self.prefix .. k):with('source', source) | ||
)) | ||
end | ||
end | ||
|
||
parallel.waitForAll(table.unpack(queue)) | ||
|
||
return MetricCollection(table.unpack(tempMetrics)) | ||
end | ||
|
||
return BaseAdvancedPeripheralsAdapter |
37 changes: 37 additions & 0 deletions
37
src/telem/lib/input/advancedPeripherals/EnergyDetectorInputAdapter.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local o = require 'telem.lib.ObjectModel' | ||
local t = require 'telem.lib.util' | ||
local fn = require 'telem.vendor'.fluent.fn | ||
|
||
local BaseAdvancedPeripheralsInputAdapter = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter' | ||
|
||
local EnergyDetectorInputAdapter = o.class(BaseAdvancedPeripheralsInputAdapter) | ||
EnergyDetectorInputAdapter.type = 'EnergyDetectorInputAdapter' | ||
|
||
function EnergyDetectorInputAdapter:constructor (peripheralName, categories) | ||
self:super('constructor', peripheralName) | ||
|
||
-- TODO this will be a configurable feature later | ||
self.prefix = 'apenergy:' | ||
|
||
-- TODO make these constants | ||
local allCategories = { | ||
'basic', | ||
} | ||
|
||
if not categories then | ||
self.categories = { 'basic' } | ||
elseif categories == '*' then | ||
self.categories = allCategories | ||
else | ||
self.categories = categories | ||
end | ||
|
||
self.queries = { | ||
basic = { | ||
transfer_rate = fn():call('getTransferRate'):energyRate(), | ||
transfer_rate_limit = fn():call('getTransferRateLimit'):energyRate(), | ||
} | ||
} | ||
end | ||
|
||
return EnergyDetectorInputAdapter |