/
sensors.ts
51 lines (48 loc) · 1.38 KB
/
sensors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { JDBus } from "./bus"
import { isSensor, serviceSpecifications } from "./spec"
import { toMap } from "./utils"
let _sensorSpecs: jdspec.ServiceSpec[]
/**
* Gets the list of sensor specifications available
* @returns
*/
export function sensorSpecifications() {
if (!_sensorSpecs) {
_sensorSpecs = serviceSpecifications().filter(
srv => !srv.shortName.startsWith("_") && isSensor(srv),
)
}
return _sensorSpecs
}
/**
* Collects and flattens all sensor data into a serializable object
* @param bus
* @returns
*/
export function snapshotSensors(
bus: JDBus,
sparse?: boolean,
): Record<string, number[] | Record<string, number>[]> {
const r = toMap(
sensorSpecifications(),
srv => srv.camelName,
srv => {
const r = bus
.services({
serviceClass: srv.classIdentifier,
ignoreInfrastructure: true,
announced: true,
})
.map(srv => {
const reg = srv.readingRegister
const spec = reg.specification
return spec.fields.length === 1
? reg.unpackedValue?.[0] || 0
: reg.objectValue || {}
})
return sparse && !r.length ? undefined : r
},
sparse,
)
return r
}