-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaster.js
executable file
·300 lines (278 loc) · 9.27 KB
/
master.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
// Copyright (C) 2017 [email protected]
var http = require('http')
var fs = require('fs')
const cp = require('child_process')
// https://www.npmjs.com/package/node-cron because it has destroy()
var cron = require('node-cron')
// https://github.com/simonlast/node-persist
var storage = require('node-persist')
// https://github.com/plasticrake/tplink-smarthome-api
const { Client } = require('tplink-smarthome-api')
// https://github.com/winstonjs/winston
//const winston = require('winston');
//const logger = winston.createLogger({
// level: 'info',
// format: winston.format.combine(
// winston.format.timestamp({
// format: 'YYYY-MM-DD HH:mm:ss'
// }),
// winston.format.json()
// ),
// defaultMeta: { service: 'user-service' },
// transports: [
// new winston.transports.File({ filename: 'error.log', level: 'error' }),
// new winston.transports.File({ filename: 'combined.log' })
// ]
//});
//if (process.env.NODE_ENV !== 'production') {
// logger.add(new winston.transports.Console({
// format: winston.format.simple()
// }));
//}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function bulbPage (res) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.write('<!DOCTYPE html>\n')
res.write('<html><head><title>Color Bulbs</title>\n')
res.write('<link rel="stylesheet" href="demo.css">\n')
res.write('<link rel="stylesheet" href="form-basic.css">\n')
res.write('<script>\nfunction ajax_get(payload){\n') // no response considered
res.write(' var xhttp = new XMLHttpRequest();\n')
res.write(' xhttp.open("GET", payload, true);\n')
res.write(' xhttp.send();\n')
res.write(' document.activeElement.blur();\n') // remove focus from button
res.write('}\n</script>\n')
res.write('</head><body>\n')
res.write('<header><h1>Color lightbulbs on local network</h1></header>\n')
res.write('<div class="main-content"><div class="form-basic">\n')
var client = new Client()
let sleeper = sleep(400)
var bulbCount = 0
client.startDiscovery()
client.on('bulb-new', (bulb) => {
if (bulb._sysInfo.is_color === 1) {
bulbCount++
res.write('<h3>' + bulb.alias +
' <a href="sunrise.html?bulb=' + encodeURIComponent(bulb.alias).replace(/'/g, '%27') +
'">Sunrise Settings</a> <button onclick="ajax_get(\'off?' + bulb.host +
'\')">Off</button> <button onclick="ajax_get(\'demo?' + encodeURIComponent(bulb.alias).replace(/'/g, '%27') +
'\')">Demo</button></h3>\n')
}
})
await sleeper
client.stopDiscovery()
if (bulbCount === 0) res.write('<h3>No color bulbs found!</h3>\n')
res.write('<div style="text-align: left; margin-top: 2em;"><a href="https://github.com/alwynallan/sunrise">Source on GitHub</a></div>\n')
res.write('</div></div>\n</body>\n</html>\n')
res.end()
}
function indexPage (res) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.write('<!DOCTYPE html>\n')
res.write('<html><head><title>Index</title>\n')
res.write('<link rel="stylesheet" href="demo.css">\n')
res.write('<link rel="stylesheet" href="form-basic.css">\n')
res.write('</head>\n<body>\n')
res.write('<header><h1>Index of node subsystem</h1></header>\n')
res.write('<div class="main-content"><div class="form-basic">\n')
res.write('<h3><a href="bulbs.html">bulbs.html</a></h3><br>\n')
res.write('<p>whoami: ' + cp.execSync('whoami') + '</p>\n')
res.write('</div></div>\n')
res.write('</body></html>')
res.end()
}
function cssFile (filename, res) {
fs.readFile(filename, function (error, content) {
if (error) {
res.writeHead(500)
res.end()
} else {
res.writeHead(200, {'Content-Type': 'text/css'})
res.end(content, 'utf-8')
}
})
}
function htmlFile (filename, res) {
fs.readFile(filename, function (error, content) {
if (error) {
res.writeHead(500)
res.end()
} else {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(content, 'utf-8')
}
})
}
function doOff (hostIP) {
var client = new Client()
client.getDevice({host: hostIP}).then((device) => {
device.lighting.setLightState({
transition_period: 500,
on_off: false
})
})
}
function doOff2 (bulbName) {
const client = new Client();
client.startDiscovery().on('device-new', (device) => {
//device.getSysInfo().then(console.log);
if(device.alias === bulbName) {
//console.log('found ' + bulbName + ' and turning it off')
device.setPowerState(false);
}
});
}
async function doDemo (bulbName) {
let key = 'bulb_' + bulbName + '.json'
let value = await storage.getItem(key)
cp.fork('sunrise-tp.js', [bulbName, '2', value.color1, value.color2, value.final])
}
function doBackend (req, res) {
// replaces sunrise_backend.php, assumes POST
var jsonString = ''
req.on('data', function (data) {
jsonString += data
})
req.on('end', async function () {
let obj = JSON.parse(jsonString)
obj.bulb = decodeURIComponent(obj.bulb)
let key = 'bulb_' + obj.bulb + '.json'
switch (obj.command) {
case 'save':
delete obj.command
delete obj.default
await storage.setItem(key, obj)
// console.log("The config was saved!");
if (tasks.hasOwnProperty(obj.bulb + '_off')) {
tasks[obj.bulb + '_off'].destroy()
delete tasks[obj.bulb + '_off']
}
if (tasks.hasOwnProperty(obj.bulb)) {
tasks[obj.bulb].destroy()
delete tasks[obj.bulb]
}
addCron(obj) // checks if it is active and adds it back if it is
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('File saved: ' + key)
break
case 'load':
if ((await storage.keys()).indexOf(key) === -1) {
let def = {default: true,
active: true,
start: '05:30',
duration: '30',
turn_off: false,
off_time: '08:30',
mon: true,
tue: true,
wed: true,
thu: true,
fri: true,
sat: false,
sun: false,
color1: '#be45be',
color2: '#f3ae52',
final: '100'}
def.bulb = obj.bulb
let deftxt = JSON.stringify(def)
await storage.setItem(key, def)
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end(deftxt)
} else {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end(JSON.stringify(await storage.getItem(key)))
}
break
default:
console.log('error: invalid command')
console.log(obj)
break
}
})
}
async function responer (req, res) {
let [page, args] = req.url.split('?', 2)
// console.log('page: ' + page + ' args: ' + args);
switch (page) {
case '/bulbs.html':
bulbPage(res)
break
case '/':
case '/index.html':
indexPage(res)
break
case '/sunrise.html':
htmlFile(page.substr(1), res)
break
case '/demo.css':
case '/form-basic.css':
cssFile(page.substr(1), res)
break
case '/off':
doOff(args)
res.end()
break
case '/demo':
doDemo(decodeURIComponent(args))
res.end()
break
case '/backend':
doBackend(req, res)
break
case '/wipe.i.know.what.im.doing':
await storage.clear()
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Your storage is wiped out!')
break
default:
res.writeHead(404)
res.end()
break
}
}
async function sunriseSim (bulbName) {
//logger.info('Doing sim for ' + bulbName);
let key = 'bulb_' + bulbName + '.json'
let value = await storage.getItem(key)
cp.fork('sunrise-tp.js', [bulbName, value.duration, value.color1, value.color2, value.final])
}
function addCron (v) {
// console.log(v);
if (v.active) {
let parts = v.start.split(':', 2)
if (isNaN(parts[0]) || parts[0] === '') parts[0] = 0 // cron is very picky
if (isNaN(parts[1]) || parts[1] === '') parts[1] = 0
let hour = parts[0]
let min = parts[1]
let wday = (v.sun ? '0,' : '') + (v.mon ? '1,' : '') + (v.tue ? '2,' : '') + (v.wed ? '3,' : '') + (v.thu ? '4,' : '') + (v.fri ? '5,' : '') + (v.sat ? '6,' : '')
wday = wday.replace(/,$/, '')
if (wday === '') wday = '*' // none is all
let cronStr = '0 ' + parseInt(min) + ' ' + parseInt(hour) + ' * * ' + wday
//logger.info('scheduling ' + v.bulb + ' at ' + cronStr);
tasks[v.bulb] = cron.schedule(cronStr, function () { sunriseSim(v.bulb) })
if(v.turn_off) {
let parts = v.off_time.split(':', 2)
if (isNaN(parts[0]) || parts[0] === '') parts[0] = 0 // cron is very picky
if (isNaN(parts[1]) || parts[1] === '') parts[1] = 0
let hour = parts[0]
let min = parts[1]
// inherit wday from previous
let cronStr = '0 ' + parseInt(min) + ' ' + parseInt(hour) + ' * * ' + wday
//logger.info('scheduling ' + v.bulb + ' at ' + cronStr);
tasks[v.bulb + '_off'] = cron.schedule(cronStr, function () { doOff2(v.bulb) })
}
}
}
async function kickIt () {
await storage.init()
let all = await storage.values()
//logger.info('All stored ' + JSON.stringify(all));
for (var i = 0; i < all.length; i++) addCron(all[i])
var server = http.createServer(responer)
server.listen(8000)
//logger.info('kickIt called');
}
var tasks = {}
kickIt()