-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
136 lines (110 loc) · 4.83 KB
/
index.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
"use strict";
/* Modules */
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const helmet = require('helmet');
//const uuid = require('uuid/v4');
const { v4: uuid } = require('uuid');
const request = require('request');
const macRegExp = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
/* Setup */
const Broadlink = require('./device');
function sendData(device = false, hexData = false) {
if(device === false || hexData === false) {
console.log('Missing params, sendData failed', typeof device, typeof hexData);
return;
}
const hexDataBuffer = new Buffer(hexData, 'hex');
device.sendData(hexDataBuffer);
}
module.exports = (commands, learnEnabled = false) => {
/* Server */
let app = express();
app.use(helmet());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
(learnEnabled && app.get('/learn/:command/:mac', (req, res) => {
let host = req.params.mac;
let device = Broadlink({ host, learnOnly: true });
if(device) {
if (!device.enterLearning) return es.json({error: `Learn Code (IR learning not supported for device at ${host})`});
if (!device.enterLearning && !device.enterRFSweep) return res.json({error:`Scan RF (RF learning not supported for device at ${host})`});
(device.cancelRFSweep && device.cancelRFSweep());
let cancelLearning = () => {
(device.cancelRFSweep && device.cancelRFSweep());
device.removeListener('rawData', onRawData);
clearTimeout(getTimeout);
clearTimeout(getDataTimeout);
};
let getTimeout = setTimeout(() => {
cancelLearning();
res.json({error: 'Timeout.'});
}, 20000);
let getDataTimeout = setTimeout(() => {
getData(device);
}, 1000);
const getData = (device) => {
if (getDataTimeout) clearTimeout(getDataTimeout);
device.checkData()
getDataTimeout = setTimeout(() => {
getData(device);
}, 1000);
}
let onRawData = (message) => {
cancelLearning();
return res.json({
command: req.params.command,
secret: Math.random().toString(36).substring(3),
mac: (macRegExp.test(host)) ? host : false,
ip: (macRegExp.test(host)) ? false : host,
data: message.toString('hex')
});
};
device.on('rawData', onRawData);
// Start learning:
(device.enterLearning ? device.enterLearning() : device.enterRFSweep());
} else {
res.json({error: `Device ${host} not found`});
}
}));
app.post('/command/:name', (req, res) => {
const command = commands.find((e) => { return e.command == req.params.name; });
if (command && req.body.secret && req.body.secret == command.secret) {
let host = command.mac || command.ip;
let device = Broadlink({ host });
if (!device) {
console.log(`${req.params.name} sendData(no device found at ${host})`);
} else if (!device.sendData) {
console.log(`[ERROR] The device at ${device.host.address} (${device.host.macAddress}) doesn't support the sending of IR or RF codes.`);
} else if (command.data && command.data.includes('5aa5aa555')) {
console.log('[ERROR] This type of hex code (5aa5aa555...) is no longer valid. Use the included "Learn Code" accessory to find new (decrypted) codes.');
} else {
if('sequence' in command) {
console.log('Sending sequence..');
for(var i in command.sequence) {
let find = command.sequence[i];
let send = commands.find((e) => { return e.command == find; });
if(send) {
setTimeout(() => {
console.log(`Sending command ${send.command}`)
sendData(device, send.data);
}, 1000 * i);
} else {
console.log(`Sequence command ${find} not found`);
}
}
} else {
sendData(device, command.data);
}
return res.sendStatus(200);
}
res.sendStatus(501);
} else {
console.log(`Command not found: ${req.params.name}`);
res.sendStatus(404);
}
});
return app;
}