forked from thomasloven/plejd2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
348 lines (297 loc) · 9.8 KB
/
api.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const axios = require('axios');
const EventEmitter = require('events');
const _ = require('lodash');
API_APP_ID = 'zHtVqXt8k4yFyk2QGmgp48D9xZr2G94xWYnF4dak';
API_BASE_URL = 'https://cloud.plejd.com/parse/';
API_LOGIN_URL = 'login';
API_SITE_LIST_URL = 'functions/getSiteList';
API_SITE_DETAILS_URL = 'functions/getSiteById';
// #region logging
let debug = '';
const getLogger = () => {
const consoleLogger = msg => console.log('plejd-api', msg);
if (debug === 'console') {
return consoleLogger;
}
return _.noop;
};
const logger = getLogger();
// #endregion
class PlejdApi extends EventEmitter {
constructor(siteName, username, password, includeRoomsAsLights) {
super();
this.includeRoomsAsLights = includeRoomsAsLights;
this.siteName = siteName;
this.username = username;
this.password = password;
this.sessionToken = '';
this.site = null;
}
updateSettings(settings) {
if (settings.debug) {
debug = 'console';
}
else {
debug = '';
}
}
login() {
console.log('plejd-api: login()');
console.log('plejd-api: logging into ' + this.siteName);
const self = this;
const instance = axios.create({
baseURL: API_BASE_URL,
headers: {
'X-Parse-Application-Id': API_APP_ID,
'Content-Type': 'application/json'
}
});
return new Promise((resolve, reject) => {
logger('sending POST to ' + API_BASE_URL + API_LOGIN_URL);
instance.post(
API_LOGIN_URL,
{
'username': this.username,
'password': this.password
})
.then((response) => {
console.log('plejd-api: got session token response');
self.sessionToken = response.data.sessionToken;
if (!self.sessionToken) {
console.log('plejd-api: error: no session token received');
reject('no session token received.');
}
resolve();
})
.catch((error) => {
if (error.response.status === 400) {
console.log('error: server returned status 400. probably invalid credentials, please verify.');
}
else {
console.log('error: unable to retrieve session token response: ' + error);
}
reject('unable to retrieve session token response: ' + error);
});
});
}
getSites() {
console.log('plejd-api: getSites()');
const self = this;
const instance = axios.create({
baseURL: API_BASE_URL,
headers: {
'X-Parse-Application-Id': API_APP_ID,
'X-Parse-Session-Token': this.sessionToken,
'Content-Type': 'application/json'
}
});
return new Promise((resolve, reject) => {
logger('sending POST to ' + API_BASE_URL + API_SITE_LIST_URL);
instance.post(API_SITE_LIST_URL)
.then((response) => {
console.log('plejd-api: got site list response');
const site = response.data.result.find(x => x.site.title == self.siteName);
if (!site) {
console.log('plejd-api: error: failed to find a site named ' + self.siteName);
reject('failed to find a site named ' + self.siteName);
return;
}
resolve(site);
})
.catch((error) => {
console.log('plejd-api: error: unable to retrieve list of sites. error: ' + error);
return reject('plejd-api: unable to retrieve list of sites. error: ' + error);
});
});
}
getSite(siteId) {
console.log('plejd-api: getSite(...)');
const self = this;
const instance = axios.create({
baseURL: API_BASE_URL,
headers: {
'X-Parse-Application-Id': API_APP_ID,
'X-Parse-Session-Token': this.sessionToken,
'Content-Type': 'application/json'
}
});
return new Promise((resolve, reject) => {
logger('sending POST to ' + API_BASE_URL + API_SITE_DETAILS_URL);
instance.post(API_SITE_DETAILS_URL, { siteId: siteId })
.then((response) => {
console.log('plejd-api: got site details response');
if (response.data.result.length === 0) {
const msg = 'no site with ID ' + siteId + ' was found.';
console.log('plejd-api: error: ' + msg);
reject(msg);
return;
}
self.site = response.data.result[0];
self.cryptoKey = self.site.plejdMesh.cryptoKey;
resolve(self.cryptoKey);
})
.catch((error) => {
console.log('plejd-api: error: unable to retrieve the crypto key. error: ' + error);
return reject('plejd-api: unable to retrieve the crypto key. error: ' + error);
});
});
}
getDevices() {
let devices = [];
// Just log the devices if debug logging enabled
if (debug) {
logger(JSON.stringify(this.site));
}
const roomDevices = {};
for (let i = 0; i < this.site.devices.length; i++) {
const device = this.site.devices[i];
const deviceId = device.deviceId;
const settings = this.site.outputSettings.find(x => x.deviceParseId == device.objectId);
let deviceNum = this.site.deviceAddress[deviceId];
if (settings) {
const outputs = this.site.outputAddress[deviceId];
deviceNum = outputs[settings.output];
}
// check if device is dimmable
const plejdDevice = this.site.plejdDevices.find(x => x.deviceId == deviceId);
let { name, type, dimmable } = this._getDeviceType(plejdDevice.hardwareId);
if (settings) {
dimmable = settings.dimCurve != 'NonDimmable';
}
const newDevice = {
id: deviceNum,
name: device.title,
type: type,
typeName: name,
dimmable: dimmable,
version: plejdDevice.firmware.version,
serialNumber: plejdDevice.deviceId
};
if (newDevice.typeName === 'WPH-01') {
// WPH-01 is special, it has two buttons which needs to be
// registered separately.
const inputs = this.site.inputAddress[deviceId];
const first = inputs[0];
const second = inputs[1];
let switchDevice = {
id: first,
name: device.title + ' knapp vä',
type: type,
typeName: name,
dimmable: dimmable,
version: plejdDevice.firmware.version,
serialNumber: plejdDevice.deviceId
};
if (roomDevices[device.roomId]) {
roomDevices[device.roomId].push(switchDevice);
}
else {
roomDevices[device.roomId] = [switchDevice];
}
devices.push(switchDevice);
switchDevice = {
id: second,
name: device.title + ' knapp hö',
type: type,
typeName: name,
dimmable: dimmable,
version: plejdDevice.firmware.version,
serialNumber: plejdDevice.deviceId
};
if (roomDevices[device.roomId]) {
roomDevices[device.roomId].push(switchDevice);
}
else {
roomDevices[device.roomId] = [switchDevice];
}
devices.push(switchDevice);
}
else {
if (roomDevices[device.roomId]) {
roomDevices[device.roomId].push(newDevice);
}
else {
roomDevices[device.roomId] = [newDevice];
}
devices.push(newDevice);
}
}
if (this.includeRoomsAsLights) {
logger('includeRoomsAsLights is set to true, adding rooms too.');
for (let i = 0; i < this.site.rooms.length; i++) {
const room = this.site.rooms[i];
const roomId = room.roomId;
const roomAddress = this.site.roomAddress[roomId];
const newDevice = {
id: roomAddress,
name: room.title,
type: 'light',
typeName: 'Room',
dimmable: roomDevices[roomId].find(x => x.dimmable).length > 0
};
devices.push(newDevice);
}
}
// add scenes as switches
const scenes = this.site.scenes.filter(x => x.hiddenFromSceneList == false);
for (const scene of scenes) {
const sceneNum = this.site.sceneIndex[scene.sceneId];
const newScene = {
id: sceneNum,
name: scene.title,
type: 'switch',
typeName: 'Scene',
dimmable: false,
version: '1.0',
serialNumber: scene.objectId
};
devices.push(newScene);
}
return devices;
}
_getDeviceType(hardwareId) {
switch (parseInt(hardwareId)) {
case 1:
case 11:
return { name: "DIM-01", type: 'light', dimmable: true };
case 2:
return { name: "DIM-02", type: 'light', dimmable: true };
case 3:
return { name: "CTR-01", type: 'light', dimmable: false };
case 4:
return { name: "GWY-01", type: 'sensor', dimmable: false };
case 5:
return { name: "LED-10", type: 'light', dimmable: true };
case 6:
return { name: "WPH-01", type: 'switch', dimmable: false };
case 7:
return { name: "REL-01", type: 'switch', dimmable: false };
case 8:
case 9:
// Unknown
return { name: "-unknown-", type: 'light', dimmable: false };
case 10:
return { name: "-unknown-", type: 'light', dimmable: false };
case 12:
// Unknown
return { name: "-unknown-", type: 'light', dimmable: false };
case 13:
return { name: "Generic", type: 'light', dimmable: false };
case 14:
case 15:
case 16:
// Unknown
return { name: "-unknown-", type: 'light', dimmable: false };
case 17:
return { name: "REL-01", type: 'switch', dimmable: false };
case 18:
return { name: "REL-02", type: 'switch', dimmable: false };
case 19:
// Unknown
return { name: "-unknown-", type: 'light', dimmable: false };
case 20:
return { name: "SPR-01", type: 'switch', dimmable: false };
}
}
}
module.exports = { PlejdApi };