forked from vpctorr/broadlink-rm-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (119 loc) · 5.21 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const macRegExp = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
const Broadlink = require('./device');
function sendData(device = false, hexData = false) {
if (device === false || hexData === false) {
return console.log('Missing params, sendData failed', typeof device, typeof hexData);
}
const hexDataBuffer = new Buffer(hexData, 'hex');
device.sendData(hexDataBuffer);
}
module.exports = (commands, key, rooms) => {
let app = express();
app.use(helmet());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/learn/:key/:host', (req, res) => { // learn utility
if (req.params.key !== key) {
return res.json({error: `Key "${req.params.key}" not found`});
}
let host = req.params.host.toLowerCase();
let device = Broadlink({ host, learnOnly: true });
if(!device) {
return res.json({error: `No device found at ${host}`});
}
if (!device.enterLearning) {
return res.json({error:`The device at ${host} doesn't support learning IR codes`});
}
if (!device.enterLearning && !device.enterRFSweep) {
return res.json({error:`The device at ${host} doesn't support learning RF codes`});
}
// do this part
(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: "command_name",
group: "group_id",
data: message.toString('hex')
});
};
device.on('rawData', onRawData);
// Start learning:
(device.enterLearning ? device.enterLearning() : device.enterRFSweep());
});
app.get('/execute/:key/:room/:name', (req, res) => { // execute command
if (req.params.key !== key) {
console.log(`Error while performing command "${req.params.name}": Key "${req.params.key}" not found`);
return res.sendStatus(403);
}
if (!rooms[req.params.room]) {
console.log(`Error while performing command "${req.params.name}": Room "${req.params.room}" not found`);
return res.sendStatus(404);
}
let command = commands.find(o => o.command === req.params.name && rooms[req.params.room]["groups"].indexOf(o.group) > -1);
if (!command) {
console.log(`Error while performing command "${req.params.name}": Command not found`);
return res.sendStatus(404);
}
let host = rooms[req.params.room]["host"].toLowerCase();
let device = Broadlink({ host });
if (!device) {
console.log(`Error while performing command "${req.params.name}": No device found at ${host}`);
return res.sendStatus(404);
}
if (!device.sendData) {
console.log(`Error while performing command "${req.params.name}": The device at ${host} doesn't support sending IR or RF codes`);
return res.sendStatus(501);
}
if (command.data && command.data.includes('5aa5aa555')) {
console.log(`Error while performing command "${req.params.name}": Outdated code type, please use the Learn utility to get a new code`);
return res.sendStatus(501);
}
if ('sequence' in command) {
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(`Sequence "${req.params.name}": Sending command "${send.command}"...`)
sendData(device, send.data);
}, 1000 * i);
} else {
console.log(`Error while performing sequence "${req.params.name}": No command found`);
return res.sendStatus(404);
}
}
} else {
console.log(`Command "${req.params.name}": Sending command...`)
sendData(device, command.data);
}
return res.sendStatus(200);
});
return app;
}