-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
325 lines (296 loc) · 11.4 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const fs = require('fs')
var https = require('https');
var http = require('http');
var bodyParser = require('body-parser')
var express = require('express')
var request = require('request')
var schedule = require('node-schedule')
var async = require('async')
var parseString = require('xml2js').parseString;
var pool_controller = require('./pool_controller')
var harmony = require('harmonyhubjs-client')
var logger = require('./log')
var app = express()
var host = '127.0.0.1'
var http_port = 8081
var https_port = 8082
var harmonyHub = 'harmonyhub.usner.net'
var tunerName = 'tuner.usner.net'
var tuner = require('./yamaha_controller')
tuner.connect(tunerName)
var key = "diV6_8ZybVoRS4Czv2JrkM"
const privateKey = fs.readFileSync('musner-key.pem'); //Private Key for validation (server uses only)
const certificate = fs.readFileSync('musner-cert.pem'); //Certificate, to provide to connecting host.
const options = {
key: privateKey,
cert: certificate
};
//E8DE27067F01
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.get('/harmony/activities', function (req, res) {
ret = ''
harmony(harmonyHub).then(function(harmonyClient) {
harmonyClient.getActivities().then(function(activities) {
activities.some(function(activity) {
console.log(activity.label)
ret += activity.label + '<br/>'
})
res.send(ret)
})
})
})
app.get('/harmony/plex', function (req, res) {
harmony(harmonyHub)
.then(function(harmonyClient) {
harmonyClient.getActivities()
.then(function(activities) {
activities.some(function(activity) {
if (activity.label === 'Plex') {
console.log('Watch Plex...')
var id = activity.id;
//tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>Standby</Power></Power_Control></Zone_2></YAMAHA_AV>')
harmonyClient.startActivity(id)
harmonyClient.end()
res.sendStatus(200)
}
});
});
});
})
app.get('/harmony/tv', function (req, res) {
harmony(harmonyHub)
.then(function(harmonyClient) {
harmonyClient.getActivities()
.then(function(activities) {
activities.some(function(activity) {
if (activity.label === 'Watch TV') {
console.log('Watch TV...')
var id = activity.id;
//tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>Standby</Power></Power_Control></Zone_2></YAMAHA_AV>')
harmonyClient.startActivity(id)
harmonyClient.end()
res.sendStatus(200)
}
});
});
});
})
app.get('/harmony/off', function (req, res) {
harmony(harmonyHub).then(function(harmonyClient) {
console.log('Turning Harmony off...')
harmonyClient.turnOff()
harmonyClient.end()
res.sendStatus(200)
})
})
function harmonyTransportAction(device, action, done) {
harmony(harmonyHub).then(function(harmonyClient) {
return harmonyClient.getAvailableCommands()
.then(function (commands) {
device = commands.device.filter(function(group) {return group.label.toLowerCase() === device}).pop()
transport=device.controlGroup.filter(function (group) { return group.name.toLowerCase() === 'transportbasic' }).pop()
action=transport["function"].filter(function(group) {return group.name.toLowerCase() === action}).pop()
var encodedAction = action.action.replace(/\:/g, '::')
return harmonyClient.send('holdAction', 'action=' + encodedAction + ':status=press')
})
.finally(function () {
harmonyClient.end()
done()
})
})
}
app.get('/harmony/tv/pause/' + key, function (req, res) {
console.log("/harmony/tv/pause")
//curl -v -s -k --key musner-key.pem -l https://192.168.1.2:8082/harmony
harmonyTransportAction("tv", "pause", function(d) { res.status(200).send("Paused") });
})
app.get('/harmony/tv/play/' + key, function (req, res) {
console.log("/harmony/tv/pause")
harmonyTransportAction("tv", "play", function(d) { res.status(200).send("Paused") });
})
// Returns a JSON encoded body containing the status of all pool components
// Example:
// http://yoururl:8181/pool
// {
// "time":"14:40",
// "spa":0,
// "cleaner":0,
// "blower":0,
// "spaLight":0,
// "poolLight":0,
// "pool":1,
// "waterFeature":0,
// "spillway":0,
// "aux7":0,
// "waterTemp":85,
// "airTemp":92
// }
//app.get('/pool', function (req, res) {
// logger.info("Getting pool status")
// pool_controller.getPoolStatus(res)
//});
// Returns a JSON encoded body containing the pump status
// Note that this only supports one pump at the moment but could easily be expanded
app.get('/pump', function (req, res) {
logger.info("Getting pump status")
pool_controller.getPumpStatus(res)
});
// Turn a feature on or off
// Usage:
// Action:
// POST
// Headers:
// Content-Type: application/json
// Parameters:
// feature=status
// Valid feature strings are:
// "spa", "cleaner", "blower", "spaLight", "poolLight", "pool", "waterFeature", "spillway", "aux7"
// Example line to turn pool and lights on:
// {
// http://localhost:80901/pool?pool=on&poolLight=on&spaLight=on
// }
app.get('/pool', function (req, res) {
console.log(req.query)
params = []
for (var param in req.query) {
params.push(param)
}
pool_controller.setFeature(param, req.query[param], function(err, obj) {
if (err) {
res.status(500).send("Failed to set feature state", param, req.query[param])
} else {
res.status(200).send(obj)
}
})
})
var callback = 'http://192.168.1.10:39500/';
var x = 1
var state = {}
setInterval(function() {
pool_controller.getPoolStatus(function(obj) {
//obj.waterTemp = x
//obj.airTemp = x+5
//x += 1
// if the time is valid and there's been a state change
if (state.waterTemp != obj.waterTemp ||
state.waterTemp != obj.waterTemp ||
state.airTemp != obj.airTemp ||
state.pool != obj.pool ||
state.spa != obj.spa ||
state.blower != obj.blower ||
state.poolLight != obj.poolLight ||
state.spaLight != obj.spaLight ||
state.cleaner != obj.cleaner ||
state.spillway != obj.spillway ||
state.waterFeature != obj.waterFeature) {
state = obj
logger.info("Sending status")
logger.info(obj)
request.post({
localAddress: '192.168.1.2',
url: callback,
json: obj
}, function (error, resp) {
if (error != null) {
console.log('response', error, resp);
}
})
}
})
}, 10000)
app.get('/pool/lights/on', function (req, res) {
pool_controller.setLights('on', res)
})
app.get('/pool/lights/off', function (req, res) {
pool_controller.setLights('off', res)
})
app.get('/pool/status', function (req, res) {
pool_controller.getPoolStatus(function(obj) {
logger.info(obj)
res.status(200).send(obj)
})
})
app.get('/tuner/patio/pandora', function (req, res) {
console.log('Patio tuner on: Pandora')
error = ""
logger.info("Tuner power on")
tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>On</Power></Power_Control></Zone_2></YAMAHA_AV>', function(result) {
if (!result) error += "Failed to power on"
})
logger.info("Tuner input Pandora")
tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Input><Input_Sel>Pandora</Input_Sel></Input></Zone_2></YAMAHA_AV>', function(result) {
if (!result) error += 'Failed to set to Pandora'
})
logger.info("Tuner volume -28")
tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Volume><Lvl><Val>-28</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume></Zone_2></YAMAHA_AV>', function(result) {
if (!result) error += 'Failed to set volume level to -28'
})
logger.info("Tuner sleep 120")
tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Sleep>120 min</Sleep></Power_Control></Zone_2></YAMAHA_AV>', function(result) {
if (!result) error += 'Failed to set sleep timer'
})
if (error != '') {
logger.error("Failed to turn on patio")
res.status(500).send(error)
} else {
res.status(200).send("Patio tuner on: Pandora")
}
})
app.get('/tuner/patio/off', function (req, res) {
console.log('Patio tuner off')
tuner.command('<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>Standby</Power></Power_Control></Zone_2></YAMAHA_AV>', function(result) {
if (result) {
res.status(200).send("Patio tuner off")
} else {
res.status(500).send("Failed to turn patio tuner off")
}
})
})
app.get('/tuner/pandora/thumbup', function (req, res) {
console.log('Pandora thumb dup')
tuner.command('<YAMAHA_AV cmd="PUT"><Pandora><Play_Control><Feedback>Thumb Up</Feedback></Play_Control></Pandora></YAMAHA_AV>', function(result) {
if (result) {
res.status(200).send("Pandora thumb up")
} else {
res.status(500).send("Failed to Pandora thumb up")
}
})
})
app.get('/tuner/pandora/thumbdown', function (req, res) {
console.log('Pandora thumb dup')
tuner.command('<YAMAHA_AV cmd="PUT"><Pandora><Play_Control><Feedback>Thumb Down</Feedback></Play_Control></Pandora></YAMAHA_AV>', function(result) {
if (result) {
res.status(200).send("Pandora thumb down")
} else {
res.status(500).send("Failed to Pandora thumb down")
}
})
})
app.get('/tuner/pandora/next', function (req, res) {
console.log('Pandora next')
tuner.command('<YAMAHA_AV cmd="PUT"><Pandora><List_Control><Cursor>Sel</Cursor></List_Control></Pandora></YAMAHA_AV>', function(result) {
if (result) {
res.status(200).send("Pandora next")
} else {
res.status(500).send("Failed to Pandora next")
}
})
})
app.get('/patio/off', function(req, res) {
pool_controller.setAll('off', res)
})
// the server entry point
//var server = app.listen(port, function () {
// var host = '127.0.0.1'
// var port = server.address().port
// logger.info("Home-Device-Controller listening at http://%s:%s", host, port)
//})
app.listen = function() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
}
http.createServer(app).listen(http_port)
logger.info("Home-Device-Controller listening at http://%s:%s", host, http_port)
https.createServer(options, app).listen(https_port)
logger.info("Home-Device-Controller listening at http://%s:%s", host, https_port)