Skip to content

Commit

Permalink
Merge pull request #67 from cyberbit/feature/miscadapters
Browse files Browse the repository at this point in the history
Add Powah, Bigger Reactors, and Advanced Peripherals adapters
  • Loading branch information
cyberbit authored Jul 19, 2024
2 parents 258878d + e209c02 commit 37ebde8
Show file tree
Hide file tree
Showing 23 changed files with 889 additions and 123 deletions.
44 changes: 36 additions & 8 deletions src/telem/lib/input.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
local function requireInput(target) return require ('telem.lib.input.' .. target) end
local function requireAP(target) return requireInput('advancedPeripherals.' .. target) end
local function requireBR(target) return requireInput('biggerReactors.' .. target) end
local function requireMek(target) return requireInput('mekanism.' .. target) end
local function requirePowah(target) return requireInput('powah.' .. target) end

return {
local export = {
helloWorld = requireInput('HelloWorldInputAdapter'),
custom = requireInput('CustomInputAdapter'),

-- storage
itemStorage = requireInput('ItemStorageInputAdapter'),
fluidStorage = requireInput('FluidStorageInputAdapter'),
refinedStorage = requireInput('RefinedStorageInputAdapter'),
meStorage = requireInput('MEStorageInputAdapter'),

-- communication
secureModem = requireInput('SecureModemInputAdapter'),

advancedPeripherals = {
energyDetector = requireAP('EnergyDetectorInputAdapter'),
environmentDetector = requireAP('EnvironmentDetectorInputAdapter'),
geoScanner = requireAP('GeoScannerInputAdapter'),
inventoryManager = requireAP('InventoryManagerInputAdapter'),
meBridge = requireAP('MEBridgeInputAdapter'),
playerDetector = requireAP('PlayerDetectorInputAdapter'),
redstoneIntegrator = requireAP('RedstoneIntegratorInputAdapter'),
rsBridge = requireAP('RSBridgeInputAdapter'),
},

biggerReactors = {
reactor = requireBR('ReactorInputAdapter'),
turbine = requireBR('TurbineInputAdapter'),
},

mekanism = {
-- machines
Expand Down Expand Up @@ -88,10 +108,18 @@ return {
qioDriveArray = requireMek('QIODriveArrayInputAdapter'),
},

advancedPeripherals = {
energyDetector = require 'telem.lib.input.advancedPeripherals.EnergyDetectorInputAdapter',
powah = {
energyCell = requirePowah('EnergyCellInputAdapter'),
furnator = requirePowah('FurnatorInputAdapter'),
magmator = requirePowah('MagmatorInputAdapter'),
reactor = requirePowah('ReactorInputAdapter'),
solarPanel = requirePowah('SolarPanelInputAdapter'),
thermoGenerator = requirePowah('ThermoGeneratorInputAdapter'),
},

-- modem
secureModem = requireInput('SecureModemInputAdapter'),
}

-- aliases that will be deprecated in the future
export.refinedStorage = export.advancedPeripherals.rsBridge
export.meStorage = export.advancedPeripherals.meBridge

return export
46 changes: 0 additions & 46 deletions src/telem/lib/input/MEStorageInputAdapter.lua

This file was deleted.

45 changes: 0 additions & 45 deletions src/telem/lib/input/RefinedStorageInputAdapter.lua

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local o = require 'telem.lib.ObjectModel'
local fl = require 'telem.vendor'.fluent
local fn = fl.fn

local InputAdapter = require 'telem.lib.InputAdapter'
local Metric = require 'telem.lib.Metric'
Expand All @@ -7,19 +9,47 @@ local MetricCollection = require 'telem.lib.MetricCollection'
local BaseAdvancedPeripheralsInputAdapter = o.class(InputAdapter)
BaseAdvancedPeripheralsInputAdapter.type = 'BaseAdvancedPeripheralsInputAdapter'

function BaseAdvancedPeripheralsInputAdapter:constructor (peripheralName)
function BaseAdvancedPeripheralsInputAdapter:constructor (peripheralName, categories, ...)
self:super('constructor')

self.prefix = 'ap:'

self.categories = categories or { 'basic' }

---@type table<string, table<string, cyberbit.Fluent>>
self.queries = {}

---@type cyberbit.Fluent[]
self.storageQueries = {}

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

self:addComponentByPeripheralID(peripheralName)
end)()

self:beforeRegister(peripheralName, categories, ...)

self:register()
end

function BaseAdvancedPeripheralsInputAdapter:beforeRegister (peripheralName, categories, ...)
-- nothing by default, should be overridden by subclasses
end

function BaseAdvancedPeripheralsInputAdapter:register ()
local allCategories = fl(self.queries):keys():result()

if self.categories == '*' then
self.categories = allCategories
elseif type(self.categories) == 'table' then
self.categories = fl(self.categories):intersect(allCategories):result()
else
error('categories must be a list of categories or "*"')
end

return self
end

local function queueHelper (results, index, query)
Expand All @@ -30,12 +60,13 @@ end

function BaseAdvancedPeripheralsInputAdapter:read ()
self:boot()

local source, component = next(self.components)

local tempMetrics = {}
local queue = {}

-- execute single-metric queries from a queue
for _, category in ipairs(self.categories) do
for k, v in pairs(self.queries[category]) do
table.insert(queue, queueHelper(
Expand All @@ -48,7 +79,31 @@ function BaseAdvancedPeripheralsInputAdapter:read ()

parallel.waitForAll(table.unpack(queue))

-- execute storage queries, which may return multiple metrics
-- these have no category and are always included
for k, v in pairs(self.storageQueries) do
local tempResult = v:from(component):result()

for _, metric in ipairs(tempResult) do
metric.name = 'storage:' .. metric.name
metric.source = source

table.insert(tempMetrics, metric)
end
end

return MetricCollection(table.unpack(tempMetrics))
end

function BaseAdvancedPeripheralsInputAdapter.mintAdapter (type)
local adapter = o.class(BaseAdvancedPeripheralsInputAdapter)
adapter.type = type

function adapter:constructor (peripheralName, categories, ...)
self:super('constructor', peripheralName, categories, ...)
end

return adapter
end

return BaseAdvancedPeripheralsInputAdapter
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
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 base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'

local EnergyDetectorInputAdapter = o.class(BaseAdvancedPeripheralsInputAdapter)
EnergyDetectorInputAdapter.type = 'EnergyDetectorInputAdapter'
local EnergyDetectorInputAdapter = base.mintAdapter('EnergyDetectorInputAdapter')

function EnergyDetectorInputAdapter:constructor (peripheralName, categories)
self:super('constructor', peripheralName)

-- TODO this will be a configurable feature later
function EnergyDetectorInputAdapter:beforeRegister (peripheralName, categories)
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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'

local EnvironmentDetectorInputAdapter = base.mintAdapter('EnvironmentDetectorInputAdapter')

function EnvironmentDetectorInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apenv:'

self.queries = {
basic = {
block_light_level = fn():call('getBlockLightLevel'),
day_light_level = fn():call('getDayLightLevel'),
sky_light_level = fn():call('getSkyLightLevel'),
moon_id = fn():call('getMoonId'),
time = fn():call('getTime'),
radiation = fn():call('getRadiationRaw'):with('unit', 'Sv/h'),
can_sleep = fn():call('canSleepHere'):toFlag(),
raining = fn():call('isRaining'):toFlag(),
sunny = fn():call('isSunny'):toFlag(),
thundering = fn():call('isThunder'):toFlag(),
slime_chunk = fn():call('isSlimeChunk'):toFlag(),
},
}

-- getBiome
-- getDimensionName
-- getDimensionPaN
-- getDimensionProvider
-- getMoonName
-- getOperationCooldown
-- isDimension
-- listDimensions
-- scanCost
-- scanEntities
end

return EnvironmentDetectorInputAdapter
24 changes: 24 additions & 0 deletions src/telem/lib/input/advancedPeripherals/GeoScannerInputAdapter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'
local Metric = require 'telem.lib.Metric'

local GeoScannerInputAdapter = base.mintAdapter('GeoScannerInputAdapter')

function GeoScannerInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apgeo:'

self.queries = {
basic = {},
}

self.storageQueries = {
fn():callElse('chunkAnalyze', {})
:map(function (k, v) return Metric{ name = k, value = v } end)
:values()
}

-- scan
end

return GeoScannerInputAdapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'
local Metric = require 'telem.lib.Metric'

local InventoryManagerInputAdapter = base.mintAdapter('InventoryManagerInputAdapter')

function InventoryManagerInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apinv:'

local armor = fn():call('getArmor')

self.queries = {
basic = {
equipped = fn():call('isPlayerEquipped'):toFlag(),
equipped_helmet = armor:firstWhere('slot', 103):toFlag(),
equipped_chestplate = armor:firstWhere('slot', 102):toFlag(),
equipped_leggings = armor:firstWhere('slot', 101):toFlag(),
equipped_boots = armor:firstWhere('slot', 100):toFlag(),
inventory_available = fn():call('isSpaceAvailable'):toFlag(),
inventory_slots_available = fn():call('getEmptySpace'),
},
}

self.storageQueries = {
fn():call('getItems')
:sum('count', 'name')
:map(function (k, v) return Metric{ name = k, value = v, unit = 'item' } end)
:values()
}
end

return InventoryManagerInputAdapter
Loading

0 comments on commit 37ebde8

Please sign in to comment.