-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
105 lines (92 loc) · 2.71 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-Smartthings
*
* By BuzzKC
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
const smartthings = require("smartthings-node");
let st;
let config;
let capabilities = [];
let deviceStatuses = [];
/*
Capabilities statuses implemented:
"switch"
"contactSensor"
"lock"
"temperatureMeasurement"
"relativeHumidityMeasurement"
"motionSensor"
Other capabilities reference: https://docs.smartthings.com/en/latest/capabilities-reference.html
*/
module.exports = NodeHelper.create({
socketNotificationReceived: function(notification, payload) {
if (notification === 'SEND_CONFIG') {
this.config = payload;
this.st = new smartthings.SmartThings(this.config.personalAccessToken);
this.capabilities = this.config.capabilities;
}
if (notification === "GET_DEVICES") {
if (this.capabilities) {
this.deviceStatuses = [];
for (let i = 0; i < this.capabilities.length; i++) {
let capability = this.capabilities[i];
this.getDevicesByCapability(capability);
}
}
}
},
getDevicesByCapability: function(capability) {
this.st.devices.listDevicesByCapability(capability).then(deviceList => {
for (let i = 0; i < deviceList.items.length; i++) {
let device = deviceList.items[i];
this.getDeviceStatus(device, capability);
}
}, reason => {this.sendSocketNotification('ConsoleOutput', reason)}
);
},
getDeviceStatus: function(device, capability) {
this.st.devices.getDeviceCapabilityStatus(device.deviceId, "main", capability).then(deviceStatus => {
let statusType = null;
switch (capability) {
case 'switch':
statusType = deviceStatus.switch.value;
break;
case 'contactSensor':
statusType = deviceStatus.contact.value;
break;
case 'lock':
statusType = deviceStatus.lock.value;
break;
case 'temperatureMeasurement':
statusType = deviceStatus.temperature.value;
break;
case 'relativeHumidityMeasurement':
statusType = deviceStatus.humidity.value;
break;
case 'motionSensor':
statusType = deviceStatus.motion.value;
break;
}
if (!this.isDeviceNameExcluded(device.label)) {
this.deviceStatuses.push({
"id": device.deviceId,
"deviceName": device.label,
"deviceTypeNAME": device.deviceTypeName,
"deviceType": capability,
"value": statusType
});
this.sendSocketNotification('DEVICE_STATUS_FOUND', this.deviceStatuses);
}
}, reason => {this.sendSocketNotification('ConsoleOutput', reason)});
},
isDeviceNameExcluded: function(deviceName) {
for (let i = 0; i < this.config.excludedDeviceNames.length; i++) {
if (deviceName.includes(this.config.excludedDeviceNames[i])) {
return true;
}
}
return false;
}
});