-
Notifications
You must be signed in to change notification settings - Fork 0
/
regulate.js
245 lines (226 loc) · 6.24 KB
/
regulate.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const ir = require("friedrich-mini-split-remote")
let broadlink = require("./index")
let temps = require("./temps")
let home = require("./home")
const { logger } = require("./logging")
const process = require("process")
const HUE_USERNAME = process.env.HUE_USERNAME
process.on("uncaughtException", function(err) {
logger.error("" + err, { err })
})
process.on("beforeExit", code => {
logger.warn("beforeExit event with code: ", { code })
})
process.on("exit", code => {
logger.warn("exit event with code: ", { code })
})
/*
* TODO ideas:
*
* ✅ Allow changing desired temperature by time of day
* - Allow preferring fan instead of off
* ✅ Turn off the status LED after changing
* ✅ Reconfigure everything every hour or two in case someone changed it
* ✅ Log to ELK or some logging service
* ✅ Improve logging (log full room state periodically)
* - Text to speech to announce major changes
* - Text owner when there are incompatible changes
* ✅ Alert owner on exit (done via CloudWatch alerts)
* ✅ Be more resilient to errors
* - Reset connection to Broadlink and Hue every few hours
* - Use getSensorById instead of enumerating every time
* - Publish the change to index.js and then make this *use* broadlinkjs instead of forking it
* ✅ Turn devices off before switching from heat to cool or vice versa
* ✅ Pause between IR signals
* ✅ Turn all devices off and back on every few hours, in case one got stuck
*/
let mousepad = new home.Home(
new home.Room({
name: "Living Room",
sensorPrefix: "00:17:88:01:04:b6:75:75-02",
blasterMacAddress: "9d277b00a8c0",
schedule: {
"5am": {
temp: {
ideal: 74,
min: 66,
max: 80,
},
priority: 0.5,
},
"8am": { priority: 1 },
"7pm": { priority: 0.5 },
"11pm": { priority: 0.1 },
},
}),
new home.Room({
name: "Nursery",
sensorPrefix: "00:17:88:01:04:b6:89:68-02",
blasterMacAddress: "9d279200a8c0",
schedule: {
"6am": {
temp: {
ideal: 73,
min: 68,
max: 78,
},
priority: 1,
},
"7am": {
turnOffStatusLight: false,
},
"6pm": {
temp: {
ideal: 68,
min: 65,
max: 74,
},
priority: 2,
turnOffStatusLight: true,
},
},
}),
new home.Room({
name: "Bedroom",
sensorPrefix: "00:17:88:01:06:f5:f1:d5-02",
blasterMacAddress: "9d278500a8c0",
schedule: {
"7am": {
temp: {
ideal: 76,
min: 68,
max: 78,
},
// fanSetting: "auto",
turnOffStatusLight: false,
priority: 1,
},
"8pm": {
temp: {
ideal: 67,
min: 62,
max: 72,
},
// fanSetting: "high",
turnOffStatusLight: true,
priority: 1,
},
// "9pm": {
// priority: 0.1,
// },
},
}),
new home.Room({
name: "Office",
sensorPrefix: "00:17:88:01:02:01:2e:d5-02",
blasterMacAddress: "9d27b700a8c0",
changeCost: 2,
schedule: {
"8pm": {
turnOffStatusLight: true,
priority: 1,
copyModeFromRoomName: "Bedroom",
},
"7am": {
temp: {
ideal: 76,
min: 70,
max: 80,
},
copyModeFromRoomName: null,
turnOffStatusLight: false,
priority: 1,
},
},
})
)
function scanForDevices() {
logger.info("Looking for Broadlink devices on the LAN...")
let devices = new broadlink()
devices.on("deviceReady", dev => {
let mac = dev.mac.toString("hex")
for (let room of mousepad.rooms)
if (room.blasterMacAddress == mac) {
room.blaster = dev
logger.info(
"Device ready: " + room.name + " - " + dev.getType() + " " + dev.host.address + " " + mac,
{ name: room.name, type: dev.getType(), address: dev.host.address, mac }
)
return
}
logger.info("Unknown device ready: " + dev.getType() + " " + dev.host.address + " " + mac)
})
devices.discover()
}
// Each call seeems to only pick up 3 devices out of the 4 in the house, so let's run it until we get them all
function scanForDevicesAgain(resolve, reject) {
if (!mousepad.rooms.some(r => !r.blaster)) {
logger.info("Found all devices")
resolve()
return
}
scanForDevices()
setTimeout(() => scanForDevicesAgain(resolve, reject), 400)
}
function scanForDevicesUntilAll() {
return new Promise(scanForDevicesAgain)
}
function waitForTemperaturesAgain(resolve, reject) {
if (!mousepad.rooms.some(r => !r.temp.current)) {
logger.info("Got all temperatures")
resolve()
return
}
setTimeout(() => waitForTemperaturesAgain(resolve, reject), 400)
}
function waitForAllTemperatures() {
return new Promise(waitForTemperaturesAgain)
}
logger.info("Starting Hue sensor polling...")
temps.startPollingSensors(HUE_USERNAME, async sensor => {
for (let room of mousepad.rooms) {
if (sensor.uniqueId.startsWith(room.sensorPrefix)) {
const attributes = sensor.state.attributes.attributes
if ("temperature" in attributes) {
room.sensor = sensor
let temp = ((attributes.temperature / 100) * 9) / 5 + 32
let oldTemp = room.temp.current
room.temp.current = temp
logger.info(room.name + " is now " + temp + " F", { oldTemp, ...room.getLogFields() })
}
if ("lightlevel" in attributes) {
room.lightLevel = attributes.lightlevel
}
return
}
}
logger.info("there's a sensor we're not using: " + sensor.uniqueId, {
uniqueId: sensor.uniqueId,
attributes: sensor.state.attributes.attributes
})
})
function logAllRooms() {
for (let room of mousepad.rooms) {
logger.info("Current status", { periodicStatus: true, ...room.getLogFields() })
}
}
function startTimers() {
mousepad.computeOptimalState()
mousepad.applyOptimalState()
logAllRooms()
logger.info("Starting timers")
setInterval(() => {
mousepad.computeOptimalState()
mousepad.applyOptimalState()
logAllRooms()
}, 30 * 1000)
}
async function main() {
await scanForDevicesUntilAll()
await waitForAllTemperatures()
startTimers()
}
main()
.then(console.log)
.catch(console.error)
module.exports = { mousepad }