-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeross.ts
56 lines (46 loc) · 1.58 KB
/
meross.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
52
53
54
55
56
import { InfluxDB, IPoint } from 'influx'
import {
ElectricityResponse, MerossCloudDevice, MerossMessage, TimeoutError,
} from 'meross-cloud-ts'
import { deviceDidRespond, deviceDidTimeout } from './blacklist'
import config from './config'
const influx = new InfluxDB(config.influxConfig)
interface ElectricityReading {
current: number
voltage: number
power: number
}
type ElectricityMessage = MerossMessage<ElectricityResponse>
const readElectricity = (msg: ElectricityMessage): ElectricityReading => {
const { current, voltage, power } = msg.payload
return {
current: current / 10000,
voltage: voltage / 10,
power: power / 1000,
}
}
// spread function calls over time
export function spreadFunctionCalls (calls: Function[], maxTime = 5000): void {
const delayEach = Math.floor(maxTime / calls.length)
calls.forEach((fnCall, idx) => setTimeout(() => fnCall(), idx * delayEach))
}
export async function pollSingleAndPublish (device: MerossCloudDevice, influxMeasurement: string): Promise<void> {
let merossMeasurement: ElectricityMessage
try {
merossMeasurement = await device.getControlElectricity()
} catch (err) {
if (err instanceof TimeoutError) return deviceDidTimeout(device)
return console.error('Error occurred', err)
}
deviceDidRespond(device)
const electricity = readElectricity(merossMeasurement)
const point: IPoint = {
tags: { device: device.dev.devName },
fields: electricity,
}
try {
await influx.writeMeasurement(influxMeasurement, [point])
} catch (err) {
return console.error('Write on database failed', err)
}
}