-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
134 lines (120 loc) · 5.01 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
/* eslint-disable max-lines-per-function,new-cap,consistent-this */
/**
The MIT License (MIT)
Copyright (c) 2016 @biddster
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
module.exports = function (RED) {
const Wemore = require('wemore');
const Domain = require('domain');
const _ = require('lodash');
const Crypto = require('crypto');
const uuidFromSerial = function (serial) {
// Many thanks to https://github.com/lspiehler/node-fauxmo/blob/master/src/deviceSerial.js
const rawserial = Crypto.createHash('md5').update(serial).digest('hex');
return (
// eslint-disable-next-line prefer-template
rawserial.substring(0, 8) +
'-' +
rawserial.substring(8, 12) +
'-' +
rawserial.substring(12, 16) +
'-' +
rawserial.substring(16, 20) +
'-' +
rawserial.substring(20, 32)
);
};
// For each wemore.Emulate we create, wemore registers a process exit listener. By default, node
// only supports 10 exit listeners and we are likely to want to emulate many more devices than that.
// https://github.com/biddster/node-red-contrib-wemo-emulator/issues/8
process.setMaxListeners(0);
RED.nodes.registerType('wemo-emulator', function (config) {
RED.nodes.createNode(this, config);
const node = this;
const globalConfig = { debug: false };
const getGlobalConfig = function () {
return _.assign(globalConfig, node.context().global.get('wemo-emulator'));
};
const debug = function (args) {
if (getGlobalConfig().debug) {
node.log(...args);
}
};
// Address in use errors occur when ports clash. They stop node dead so we use a domain to notify the user.
// Otherwise NodeRED won't start and that's hard to debug.
// Note that domains are deprecated in v7. So we'll have to port to whatever replaces them in the future.
const d = Domain.create();
d.on('error', (e) => {
node.error(`Emulation error: ${e.message}`, e);
node.status({
fill: 'red',
shape: 'dot',
text: e.message,
});
});
let connection = null;
d.run(() => {
config.uuid = uuidFromSerial(config.serial);
debug(`UUID [${config.serial}] => [${config.uuid}]`);
// console.log(config.uuid);
// {friendlyName: "TV", port: 9001, serial: 'a unique id'}
connection = Wemore.Emulate(config)
.on('listening', function () {
node.status({
fill: 'yellow',
shape: 'dot',
text: `Listen on ${this.port}`,
});
debug(`Listening on: ${this.port}`);
})
.on('on', (_self, sender) => {
node.send({
topic: config.onTopic,
payload: config.onPayload,
sender,
});
node.status({
fill: 'green',
shape: 'dot',
text: 'on',
});
debug('Turning on');
})
.on('off', (_self, sender) => {
node.send({
topic: config.offTopic,
payload: config.offPayload,
sender,
});
node.status({
fill: 'green',
shape: 'circle',
text: 'off',
});
debug('Turning off');
});
});
node.on('close', () => {
debug('Closing connection');
connection.close();
// debug('Closing domain');
// d.dispose();
debug('Closed');
});
});
};