-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
232 lines (196 loc) · 7.21 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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const cp = require('child_process');
const utils = require('@iobroker/adapter-core');
const pcap = require('pcap');
const adapterName = require('./package.json').name.split('.').pop();
let adapter;
let MACs = [
'747548',
'F0D2F1',
'8871E5',
'74C246',
'F0272D',
'34D270',
'0C47C9',
'A002DC',
'AC63BE',
'44650D',
'50F5DA',
'84D6D0',
'B47C9C',
'FCA667',
'18742E',
'78E103',
'6837E9',
'00FC8B',
'40B4CD',
'FC65DE',
'2C3AE8',
'6C5697',
'38F73D',
'6854FD'
];
let pcapSession;
String.prototype.replaceAll = function (search, replacement) {
const target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
function intArrayToHex (intArray) {
return intArray.map(d => d.toString(16).padStart(2, '0')).join(':');
}
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: adapterName
});
adapter = new utils.Adapter(options);
adapter.on('ready', () => main());
adapter.on('unload', callback => {
try {
adapter.log.info('cleaned everything up...');
pcapSession.close();
pcapSession = null;
callback();
} catch (e) {
callback();
}
});
return adapter;
} // endStartAdapter
function checkNetCapRights() {
return new Promise((resolve, reject) => {
const cmd = `sudo setcap 'cap_net_raw,cap_net_admin+eip' ${process.execPath}`;
adapter.log.debug(`Executing command: ${cmd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
const child = cp.exec(cmd);
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(`Cannot set rights: ${code}`);
} else {
// command succeeded
resolve();
}
});
});
}
async function main() {
if (adapter.config.checkRights && process.platform === 'linux') {
await checkNetCapRights();
}
if (adapter.config.devices && adapter.config.devices.length) {
for (let k = 0; k < adapter.config.devices.length; k++) {
const mac = adapter.config.devices[k].mac;
const macOK = mac.replaceAll(':', '');
if (macOK.length > 5) {
MACs.push(macOK.substring(0, 6));
adapter.log.debug(`manual MAC: ${MACs.push(macOK.substring(0, 6))}`);
}
}
}
MACs = removeDuplicates(MACs);
if (typeof adapter.config.interface === 'undefined' || adapter.config.interface === '') {
adapter.config.interface = '';
adapter.log.info('starting pcap session on default interface');
} else {
adapter.log.info(`starting pcap session on interface ${adapter.config.interface}`);
}
pcapSession = pcap.createSession(adapter.config.interface, {filter: 'arp'});
pcapSession.on('packet', async rawPacket => {
const packet = pcap.decode.packet(rawPacket);
if (packet.payload.ethertype === 2054) {
let mac = packet.payload.payload.sender_ha.addr;
mac = intArrayToHex(mac);
const niceMac = mac.replaceAll(':', '-');
const needle = mac.slice(0, 8).toString().toUpperCase().split(':').join('');
adapter.log.debug(`needle MAC: ${needle}`);
if (MACs.includes(needle)) {
try {
const obj = await adapter.getObjectAsync(niceMac);
// if non-existent or not type device
if (!obj || obj.type !== 'device') {
await adapter.setObjectAsync(niceMac, {
type: 'device',
common: {},
native: {}
});
}
} catch (e) {
adapter.log.error(`Cannot create state [${niceMac}]: ${e}`);
}
const id = `${niceMac}.pressed`;
await adapter.setObjectNotExistsAsync(id, {
type: 'state',
common: {
name: 'Dash button pressed',
type: 'boolean',
role: 'switch',
read: true,
write: false
}
});
await adapter.setStateAsync(id, {val: true, ack: true});
// reset press state to false in 5 seconds
setTimeout(() =>
adapter.setState(id, {val: false, ack: true}), 5000);
await adapter.setObjectNotExistsAsync(`${niceMac}.lastPressed`, {
type: 'state',
common: {
name: 'Dash button last pressed date',
type: 'string',
role: 'indicator.date',
read: true,
write: false
}
});
await adapter.setStateAsync(`${niceMac}.lastPressed`, {val: (new Date()).toISOString(), ack: true});
await adapter.setObjectNotExistsAsync(`${niceMac}.switch`, {
type: 'state',
common: {
name: 'Dash button state toggle',
type: 'boolean',
role: 'switch',
read: true,
write: false
}
});
try {
const state = await adapter.getStateAsync(`${niceMac}.switch`);
if (!state) {
await adapter.setStateAsync(`${niceMac}.switch`, {val: false, ack: true});
} else {
if (Date.now() - state.lc > 5000) {
await adapter.setStateAsync(`${niceMac}.switch`, {val: !state.val, ack: true});
}
}
} catch (e) {
adapter.log.error(`Cannot set switch state [${niceMac}.switch]: ${e}`);
}
}
}
});
}
function removeDuplicates(arr) {
const obj = {};
const retArray = [];
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = true;
}
for (const key in obj) {
retArray.push(key);
}
return retArray;
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
} // endElse