-
Notifications
You must be signed in to change notification settings - Fork 5
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 #55 from cyberbit/feature/ap-energy-detector
AP Energy Detector
- Loading branch information
Showing
8 changed files
with
171 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"recommendations": [ | ||
"gruntfuggly.triggertaskonsave", | ||
"jackmacwindows.vscode-computercraft" | ||
] | ||
} |
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
60 changes: 60 additions & 0 deletions
60
docs/reference/input/advanced-peripherals/EnergyDetector.md
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,60 @@ | ||
# Advanced Peripherals Energy Detector <RepoLink path="lib/input/advancedPeripherals/EnergyDetectorInputAdapter.lua" /> | ||
|
||
```lua | ||
telem.input.advancedPeripherals.energyDetector ( | ||
peripheralID: string, | ||
categories?: string[] | '*' | ||
) | ||
``` | ||
|
||
::: warning Mod Dependencies | ||
Requires **Advanced Peripherals**. | ||
::: | ||
|
||
This adapter produces metrics for the transfer rate and limit of an attached Energy Detector. By default, the metrics are limited to an opinionated basic list, but this can be expanded with the `categories` parameter at initialization. | ||
|
||
See the Usage section for a complete list of the metrics in each category. | ||
|
||
<PropertiesTable | ||
:properties="[ | ||
{ | ||
name: 'peripheralID', | ||
type: 'string', | ||
default: 'nil', | ||
description: 'Peripheral ID of the Energy Detector' | ||
}, | ||
{ | ||
name: 'categories', | ||
type: 'string[] | "*"', | ||
default: '{ "basic" }' | ||
} | ||
]" | ||
> | ||
<template v-slot:categories> | ||
List of metric categories to query. The value `"*"` can be used to include all categories, which are listed below. | ||
|
||
```lua | ||
{ "basic" } | ||
``` | ||
</template> | ||
</PropertiesTable> | ||
|
||
## Usage | ||
|
||
```lua{4} | ||
local telem = require 'telem' | ||
local backplane = telem.backplane() | ||
:addInput('my_energy', telem.input.advancedPeripherals.energyDetector('right')) | ||
:cycleEvery(1)() | ||
``` | ||
|
||
Given an Energy Detector peripheral on the `right` side of the computer, this appends the following metrics to the backplane: | ||
|
||
<MetricTable | ||
:metrics="[ | ||
{ name: 'apenergy:transfer_rate', value: '0 - inf', unit: 'FE/t' }, | ||
{ name: 'apenergy:transfer_rate_limit', value: '0 - inf', unit: 'FE/t' }, | ||
]" | ||
/> |
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
-- Telem by cyberbit | ||
-- MIT License | ||
-- Version 0.7.1 | ||
-- Version 0.7.2 | ||
|
||
local _Telem = { | ||
_VERSION = '0.7.1', | ||
|
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 BaseAdvancedPeripheralsInputAdapter = o.class(InputAdapter) | ||
BaseAdvancedPeripheralsInputAdapter.type = 'BaseAdvancedPeripheralsInputAdapter' | ||
|
||
function BaseAdvancedPeripheralsInputAdapter: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 BaseAdvancedPeripheralsInputAdapter: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 BaseAdvancedPeripheralsInputAdapter |
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 |