Skip to content

Commit

Permalink
Merge pull request #55 from cyberbit/feature/ap-energy-detector
Browse files Browse the repository at this point in the history
AP Energy Detector
  • Loading branch information
cyberbit authored Jun 21, 2024
2 parents cd99739 + 60dd093 commit 3bae242
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"gruntfuggly.triggertaskonsave",
"jackmacwindows.vscode-computercraft"
]
}
9 changes: 8 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export default defineConfig({
{ text: 'Fluid Storage', link: '/reference/input/FluidStorage' },
{ text: 'Secure Modem', link: '/reference/input/SecureModem' },
{ text: 'Custom', link: '/reference/input/Custom' },
{
text: 'Advanced Peripherals',
collapsed: true,
items: [
{ text: 'Energy Detector', link: '/reference/input/advanced-peripherals/EnergyDetector' },
]
},
{
text: 'Applied Energistics',
collapsed: true,
Expand All @@ -65,7 +72,7 @@ export default defineConfig({
items: [
{ text: 'Refined Storage', link: '/reference/input/refined-storage/RefinedStorage' },
]
}
},
]
},
{
Expand Down
60 changes: 60 additions & 0 deletions docs/reference/input/advanced-peripherals/EnergyDetector.md
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[] | &quot;*&quot;',
default: '{ &quot;basic&quot; }'
}
]"
>
<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' },
]"
/>
2 changes: 1 addition & 1 deletion init_devcontainer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mkdir .luatmp
cd .luatmp

# install lua
# sudo apt update
sudo apt update
sudo apt install lua5.3 liblua5.3-dev

# install luarocks
Expand Down
2 changes: 1 addition & 1 deletion src/telem/init.lua
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',
Expand Down
4 changes: 4 additions & 0 deletions src/telem/lib/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ return {
fusionReactor = require 'telem.lib.input.mekanism.FusionReactorInputAdapter',
},

advancedPeripherals = {
energyDetector = require 'telem.lib.input.advancedPeripherals.EnergyDetectorInputAdapter',
},

-- modem
secureModem = require 'telem.lib.input.SecureModemInputAdapter'
}
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
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

0 comments on commit 3bae242

Please sign in to comment.