Skip to content

Commit

Permalink
Apply filters during the polling itself, not during post-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 15, 2024
1 parent 6a70931 commit 919038d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 18 additions & 3 deletions src/circuit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PowerSensor, PowerSensorData } from './sensor'
import { applyFilters } from './filter/filter'

export enum CircuitType {
Main = 'main',
Expand Down Expand Up @@ -28,10 +29,24 @@ export const pollPowerSensors = async (
const promises = []

for (const circuit of circuits) {
const sensor = circuit.sensor

promises.push(sensor.pollFunc(timestamp, circuit, existingSensorData))
promises.push(pollPowerSensor(timestamp, circuit, existingSensorData))
}

return Promise.all(promises)
}

const pollPowerSensor = async (
timestamp: number,
circuit: Circuit,
existingSensorData?: PowerSensorData[],
): Promise<PowerSensorData> => {
const sensor = circuit.sensor

let data = await sensor.pollFunc(timestamp, circuit, existingSensorData)

if (sensor.filters) {
data = applyFilters(sensor.filters, data)
}

return data
}
6 changes: 0 additions & 6 deletions src/eachwatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { pollCharacteristicsSensors } from './characteristics'
import { createLogger, LogLevel, setLogLevel } from './logger'
import { setRequestTimeout as setHttpRequestTimeout } from './http/client'
import { setRequestTimeout as setModbusRequestTimeout } from './modbus/client'
import { applyFilters } from './filter/filter'
import { setIntervalAsync } from 'set-interval-async'

// Set up a signal handler, so we can exit on Ctrl + C when run from Docker
Expand Down Expand Up @@ -65,11 +64,6 @@ const mainPollerFunc = async (config: Config) => {

// Post-process power sensor data
powerSensorData = powerSensorData.map((data) => {
// Apply optional data filters
if (data.circuit.sensor.filters) {
data = applyFilters(data.circuit.sensor.filters, data)
}

if (data.power !== undefined) {
// Round all numbers to one decimal point
data.power = Number(data.power.toFixed(1))
Expand Down

0 comments on commit 919038d

Please sign in to comment.