-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
52 lines (42 loc) · 1.65 KB
/
index.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
import { MerossCloud, MerossCloudDevice } from 'meross-cloud-ts'
import { adjustTimeoutsThreshold, cleanDevices, resetBlackList } from './blacklist'
import config from './config'
import { pollSingleAndPublish, spreadFunctionCalls } from './meross'
const meross = new MerossCloud(config.merossConfig)
const RELOAD_DEVICES_AFTER = 360 // every 1 hour
// prepares polling functions and executes them spreading out in time
const operation = (devices: MerossCloudDevice[], measurement: string) =>
spreadFunctionCalls(devices.map(device => () => void pollSingleAndPublish(device, measurement)))
async function main () {
const { measurement } = config.influxOptions
let intervalCounter = 0
let deviceList = await meross.connect()
adjustTimeoutsThreshold(deviceList.length)
console.log(`meross connected, found ${deviceList.length} devices`)
operation(deviceList, measurement)
const onInterval = async () => {
intervalCounter++
if (intervalCounter > RELOAD_DEVICES_AFTER) {
meross.disconnectAll(true)
deviceList = await meross.getDeviceList()
console.log(`updated devices list, found ${deviceList.length} devices`)
intervalCounter = 0
resetBlackList()
} else {
deviceList = cleanDevices(deviceList)
}
adjustTimeoutsThreshold(deviceList.length)
operation(deviceList, measurement)
}
setInterval(() => void onInterval(), 10000)
}
try {
void main()
} catch (err) {
console.error('Error happened', err)
process.exit(1)
}
process.on('unhandledRejection', function (reason, p) {
console.log('Possibly Unhandled Rejection at: Promise ', p, ' reason: ', reason)
// application specific logging here
})