-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-house.ts
89 lines (75 loc) · 2.1 KB
/
run-house.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
process.env.TZ = 'Australia/Sydney'
import {
house,
update,
initialModel as makeInitialModel,
Model,
ModelCache,
ModelCacheT,
modelZero,
} from './lachies-house/index'
import { Msg, SetHour, MsgT } from './lachies-house/Msg'
import { subscriptions } from './lachies-house/subscriptions'
import { runtime } from './src/runtime'
import { InterfaceFactory } from './src/interfaces'
import * as mqtt from 'async-mqtt'
import { InfluxDB } from '@influxdata/influxdb-client'
import HttpInterfaceFactory from './src/interfaces/http'
import { ModelCacheFactory } from './src/interfaces/modelCache'
import path from 'path/posix'
import secrets from './secrets.json'
const logPath = path.resolve(__dirname, './log.json')
const oauthDBPath = path.resolve(__dirname, './oauthDB.json')
const modelCachePath = path.resolve(__dirname, './model.json')
const modelCacheFactory = new ModelCacheFactory<Model, ModelCache, Msg>(
modelCachePath,
ModelCacheT,
modelZero,
(m: Model): ModelCache => ({
rooms: {
playroom: {
scene: m.rooms.playroom.scene,
sensors: m.rooms.playroom.sensors,
},
backroom: {
scene: m.rooms.backroom.scene,
sensors: m.rooms.backroom.sensors,
},
office: {
scene: m.rooms.office.scene,
sensors: m.rooms.office.sensors,
},
},
}),
)
const initialDate = new Date()
export let initialModel: Model = modelCacheFactory.initialModel(
makeInitialModel(initialDate),
)
/*
* runtime
*/
const mqttClient = mqtt.connect(secrets.mqtt.broker, {
username: secrets.mqtt.username,
password: secrets.mqtt.password,
})
const influxClient = new InfluxDB({
url: secrets.influx.url,
token: secrets.influx.token,
}).getWriteApi(secrets.influx.org, secrets.influx.bucket)
const interfaces: InterfaceFactory<Msg, Model>[] = [
new HttpInterfaceFactory(MsgT, oauthDBPath, 3030),
modelCacheFactory,
]
;(async () => {
await runtime(
{
update,
house,
subscriptions,
initialModel,
},
{ mqttClient, influxClient, interfaces, logPath, secrets },
SetHour(new Date()), // initial message
)
})()