Skip to content

Commit

Permalink
Add scale filter, useful for Modbus sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 14, 2024
1 parent b9f8a04 commit e23d7c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/filter/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PowerSensorData } from '../sensor'
export type PowerSensorFilters = {
clamp?: 'positive'
highPass?: number
scale?: number
}

export const applyFilters = (filters: PowerSensorFilters, data: PowerSensorData): PowerSensorData => {
Expand All @@ -21,5 +22,11 @@ export const applyFilters = (filters: PowerSensorFilters, data: PowerSensorData)
data.power = 0
}

// Scale
const scale = filters?.scale
if (scale !== undefined) {
data.power = data.power / scale
}

return data
}
17 changes: 15 additions & 2 deletions tests/filter/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { emptySensorData, PowerSensorData, SensorType } from '../../src/sensor'
import { applyFilters, PowerSensorFilters } from '../../src/filter/filter'
import { Circuit } from '../../src/circuit'
import { getSensorData as getDummySensorData } from '../../src/sensor/dummy'
import exp = require('node:constants')

test('clamping works', () => {
const filters: PowerSensorFilters = {}
Expand All @@ -21,7 +20,7 @@ test('clamping works', () => {
expect(data.power).toEqual(0)
})

test('high-pas works', () => {
test('high-pass works', () => {
const filters: PowerSensorFilters = {}
let data: PowerSensorData = dummySensorData()

Expand All @@ -38,6 +37,20 @@ test('high-pas works', () => {
expect(data.power).toEqual(3)
})

test('scale works', () => {
const filters: PowerSensorFilters = {}
let data: PowerSensorData = dummySensorData()

data.power = 155
data = applyFilters(filters, data)
expect(data.power).toEqual(155)

data.power = 155
filters.scale = 0.1
data = applyFilters(filters, data)
expect(data.power).toEqual(1550)
})

const dummySensorData = (): PowerSensorData => {
const circuit: Circuit = {
name: 'dummy',
Expand Down

0 comments on commit e23d7c6

Please sign in to comment.