-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.js
101 lines (85 loc) · 3.04 KB
/
driver.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
const deviceCache = {};
const activeDevices = [];
const eventCallbackFunctionInstances = {};
const eventsFired = {};
const initDevices = async (devices, commsInterface, wemo, events, createEvent) => {
// To listen for events from the switches the wemo library requires that we initialise each switch as a
// class and attach an event listener to it
// remove existing event listeners
Object.keys(deviceCache).forEach((deviceId) => {
deviceCache[deviceId].removeListener('binaryState', eventCallbackFunctionInstances[deviceId]);
delete deviceCache[deviceId];
});
// get the new callback url
const cbUrl = wemo.getCallbackURL();
// loop through the devices and initialise them
devices.forEach((device) => {
const newDevice = Object.assign(device, {});
// update the callback url
newDevice.specs.additionalInfo.callbackURL = cbUrl;
deviceCache[newDevice._id] = wemo.client(newDevice.specs.additionalInfo);
eventCallbackFunctionInstances[newDevice._id] = (value) => {
// as soon as we setup the event listener this event will fire. We want to ignore this initial event..
if (typeof eventsFired[newDevice._id] === 'undefined') {
eventsFired[newDevice._id] = true;
return;
}
if (value === '1') {
createEvent(events.ON, newDevice._id, {
on: true,
});
} else {
createEvent(events.ON, newDevice._id, {
on: false,
});
}
};
deviceCache[newDevice._id].on('binaryState', eventCallbackFunctionInstances[newDevice._id]);
deviceCache[newDevice._id].on('error', (err) => {
if ((err.code === 'EHOSTUNREACH') || (err.code === 'ECONNREFUSED') || (err.code === 'ETIMEDOUT')) {
// activeDevices = activeDevices.filter(item => item.additionalInfo.host !== err.address);
}
});
});
};
const discover = async () => Promise.resolve(activeDevices);
const commandOn = async (device, wemo) => {
const wemoSwitchInstance = wemo.client(device.specs.additionalInfo);
wemoSwitchInstance.setBinaryState(1);
};
const commandOff = async (device, wemo) => {
const wemoSwitchInstance = wemo.client(device.specs.additionalInfo);
wemoSwitchInstance.setBinaryState(0);
};
module.exports = async (getSettings, updateSettings, commsInterface, Wemo, events, createEvent) => {
const wemo = new Wemo();
wemo.discover((err, info) => {
if (err) {
return;
}
if (info.modelName !== 'Socket') {
return;
}
const device = {
originalId: info.serialNumber,
name: info.friendlyName,
address: info.callbackURL,
commands: {
on: true,
off: true,
},
events: {
[events.ON]: true,
},
additionalInfo: info,
};
activeDevices.push(device);
});
return {
initDevices: async devices => initDevices(devices, commsInterface, wemo, events, createEvent),
authentication_getSteps: [],
discover: async () => discover(),
command_on: async device => commandOn(device, wemo),
command_off: async device => commandOff(device, wemo),
};
};