2019-12-04 11:17:06 +01:00
|
|
|
const axios = require('axios');
|
|
|
|
|
const EventEmitter = require('events');
|
2021-01-21 21:31:37 +01:00
|
|
|
const Logger = require('./Logger');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
const API_APP_ID = 'zHtVqXt8k4yFyk2QGmgp48D9xZr2G94xWYnF4dak';
|
|
|
|
|
const API_BASE_URL = 'https://cloud.plejd.com/parse/';
|
|
|
|
|
const API_LOGIN_URL = 'login';
|
|
|
|
|
const API_SITE_LIST_URL = 'functions/getSiteList';
|
|
|
|
|
const API_SITE_DETAILS_URL = 'functions/getSiteById';
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
const logger = Logger.getLogger('plejd-api');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
class PlejdApi extends EventEmitter {
|
2019-12-13 17:59:48 +01:00
|
|
|
constructor(siteName, username, password, includeRoomsAsLights) {
|
2019-12-04 11:17:06 +01:00
|
|
|
super();
|
|
|
|
|
|
2019-12-13 17:59:48 +01:00
|
|
|
this.includeRoomsAsLights = includeRoomsAsLights;
|
2019-12-04 11:17:06 +01:00
|
|
|
this.siteName = siteName;
|
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
|
|
|
|
|
|
|
|
|
this.sessionToken = '';
|
|
|
|
|
this.site = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
login() {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('login()');
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.info(`logging into ${this.siteName}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
const instance = axios.create({
|
|
|
|
|
baseURL: API_BASE_URL,
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Parse-Application-Id': API_APP_ID,
|
2021-01-22 15:49:02 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2019-12-04 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_LOGIN_URL}`);
|
2020-03-13 11:58:15 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
instance
|
|
|
|
|
.post(API_LOGIN_URL, {
|
|
|
|
|
username: this.username,
|
|
|
|
|
password: this.password,
|
2020-03-13 11:58:15 +01:00
|
|
|
})
|
|
|
|
|
.then((response) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('got session token response');
|
2020-03-13 11:58:15 +01:00
|
|
|
self.sessionToken = response.data.sessionToken;
|
|
|
|
|
|
|
|
|
|
if (!self.sessionToken) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error('No session token received');
|
2021-01-22 15:49:02 +01:00
|
|
|
reject(new Error('no session token received.'));
|
2020-03-13 11:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (error.response.status === 400) {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.error(
|
|
|
|
|
'Server returned status 400. probably invalid credentials, please verify.',
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error('Unable to retrieve session token response: ', error);
|
2020-03-13 11:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
reject(new Error(`unable to retrieve session token response: ${error}`));
|
2020-03-13 11:58:15 +01:00
|
|
|
});
|
|
|
|
|
});
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
getSites() {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('Get all Plejd sites for account...');
|
2019-12-04 11:17:06 +01:00
|
|
|
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,
|
2021-01-22 15:49:02 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2019-12-04 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_SITE_LIST_URL}`);
|
2020-03-13 11:58:15 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
instance
|
|
|
|
|
.post(API_SITE_LIST_URL)
|
2020-03-13 11:58:15 +01:00
|
|
|
.then((response) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('got site list response');
|
2021-01-22 15:49:02 +01:00
|
|
|
const site = response.data.result.find((x) => x.site.title === self.siteName);
|
2020-03-13 11:58:15 +01:00
|
|
|
|
|
|
|
|
if (!site) {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.error(`error: failed to find a site named ${self.siteName}`);
|
|
|
|
|
reject(new Error(`failed to find a site named ${self.siteName}`));
|
2020-03-13 11:58:15 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(site);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error('error: unable to retrieve list of sites. error: ', error);
|
2021-01-22 15:49:02 +01:00
|
|
|
return reject(new Error(`plejd-api: unable to retrieve list of sites. error: ${error}`));
|
2020-03-13 11:58:15 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-12-14 14:10:10 +01:00
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
getSite(siteId) {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.info('Get site details...');
|
2020-03-13 11:58:15 +01:00
|
|
|
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,
|
2021-01-22 15:49:02 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2020-03-13 11:58:15 +01:00
|
|
|
});
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-22 15:49:02 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_SITE_DETAILS_URL}`);
|
2020-03-13 11:58:15 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
instance
|
|
|
|
|
.post(API_SITE_DETAILS_URL, { siteId })
|
2020-03-13 11:58:15 +01:00
|
|
|
.then((response) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('got site details response');
|
2020-03-13 11:58:15 +01:00
|
|
|
if (response.data.result.length === 0) {
|
2021-01-22 15:49:02 +01:00
|
|
|
const msg = `no site with ID ${siteId} was found.`;
|
|
|
|
|
logger.error(`error: ${msg}`);
|
2020-03-13 11:58:15 +01:00
|
|
|
reject(msg);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.site = response.data.result[0];
|
|
|
|
|
self.cryptoKey = self.site.plejdMesh.cryptoKey;
|
|
|
|
|
|
|
|
|
|
resolve(self.cryptoKey);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error('error: unable to retrieve the crypto key. error: ', error);
|
2021-01-22 15:49:02 +01:00
|
|
|
return reject(new Error(`plejd-api: unable to retrieve the crypto key. error: ${error}`));
|
2020-03-13 11:58:15 +01:00
|
|
|
});
|
|
|
|
|
});
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDevices() {
|
2021-01-22 15:49:02 +01:00
|
|
|
const devices = [];
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.verbose(JSON.stringify(this.site));
|
2019-12-10 19:15:38 +01:00
|
|
|
|
2019-12-13 17:59:48 +01:00
|
|
|
const roomDevices = {};
|
|
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
for (let i = 0; i < this.site.devices.length; i++) {
|
2019-12-10 22:01:12 +01:00
|
|
|
const device = this.site.devices[i];
|
2021-01-22 15:49:02 +01:00
|
|
|
const { deviceId } = device;
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
const settings = this.site.outputSettings.find((x) => x.deviceParseId === device.objectId);
|
2019-12-10 22:01:12 +01:00
|
|
|
let deviceNum = this.site.deviceAddress[deviceId];
|
|
|
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
|
const outputs = this.site.outputAddress[deviceId];
|
|
|
|
|
deviceNum = outputs[settings.output];
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
// check if device is dimmable
|
|
|
|
|
const plejdDevice = this.site.plejdDevices.find((x) => x.deviceId === deviceId);
|
|
|
|
|
const deviceType = this._getDeviceType(plejdDevice.hardwareId);
|
|
|
|
|
const { name, type } = deviceType;
|
|
|
|
|
let { dimmable } = deviceType;
|
2019-12-10 22:01:12 +01:00
|
|
|
|
|
|
|
|
if (settings) {
|
2021-01-22 15:49:02 +01:00
|
|
|
dimmable = settings.dimCurve !== 'NonDimmable';
|
2019-12-10 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newDevice = {
|
|
|
|
|
id: deviceNum,
|
2019-12-04 11:17:06 +01:00
|
|
|
name: device.title,
|
2021-01-22 15:49:02 +01:00
|
|
|
type,
|
2019-12-13 14:13:00 +01:00
|
|
|
typeName: name,
|
2021-01-22 15:49:02 +01:00
|
|
|
dimmable,
|
2020-01-21 14:24:02 +00:00
|
|
|
version: plejdDevice.firmware.version,
|
2021-01-22 15:49:02 +01:00
|
|
|
serialNumber: plejdDevice.deviceId,
|
2019-12-10 22:01:12 +01:00
|
|
|
};
|
|
|
|
|
|
2020-02-20 13:02:47 +01:00
|
|
|
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];
|
|
|
|
|
|
2020-02-20 13:04:25 +01:00
|
|
|
let switchDevice = {
|
|
|
|
|
id: first,
|
2021-01-22 15:49:02 +01:00
|
|
|
name: `${device.title} knapp vä`,
|
|
|
|
|
type,
|
2020-02-20 13:04:25 +01:00
|
|
|
typeName: name,
|
2021-01-22 15:49:02 +01:00
|
|
|
dimmable,
|
2020-02-20 13:04:25 +01:00
|
|
|
version: plejdDevice.firmware.version,
|
2021-01-22 15:49:02 +01:00
|
|
|
serialNumber: plejdDevice.deviceId,
|
2020-02-20 13:04:25 +01:00
|
|
|
};
|
2020-02-20 13:02:47 +01:00
|
|
|
|
|
|
|
|
if (roomDevices[device.roomId]) {
|
2020-02-20 13:04:25 +01:00
|
|
|
roomDevices[device.roomId].push(switchDevice);
|
2021-01-22 15:49:02 +01:00
|
|
|
} else {
|
2020-02-20 13:04:25 +01:00
|
|
|
roomDevices[device.roomId] = [switchDevice];
|
2020-02-20 13:02:47 +01:00
|
|
|
}
|
2020-02-20 13:04:25 +01:00
|
|
|
devices.push(switchDevice);
|
|
|
|
|
|
|
|
|
|
switchDevice = {
|
|
|
|
|
id: second,
|
2021-01-22 15:49:02 +01:00
|
|
|
name: `${device.title} knapp hö`,
|
|
|
|
|
type,
|
2020-02-20 13:04:25 +01:00
|
|
|
typeName: name,
|
2021-01-22 15:49:02 +01:00
|
|
|
dimmable,
|
2020-02-20 13:04:25 +01:00
|
|
|
version: plejdDevice.firmware.version,
|
2021-01-22 15:49:02 +01:00
|
|
|
serialNumber: plejdDevice.deviceId,
|
2020-02-20 13:04:25 +01:00
|
|
|
};
|
2020-02-20 13:02:47 +01:00
|
|
|
|
|
|
|
|
if (roomDevices[device.roomId]) {
|
2020-02-20 13:04:25 +01:00
|
|
|
roomDevices[device.roomId].push(switchDevice);
|
2021-01-22 15:49:02 +01:00
|
|
|
} else {
|
2020-02-20 13:04:25 +01:00
|
|
|
roomDevices[device.roomId] = [switchDevice];
|
2020-02-20 13:02:47 +01:00
|
|
|
}
|
2020-02-20 13:04:25 +01:00
|
|
|
devices.push(switchDevice);
|
2021-01-22 15:49:02 +01:00
|
|
|
} else {
|
2020-02-20 13:02:47 +01:00
|
|
|
if (roomDevices[device.roomId]) {
|
|
|
|
|
roomDevices[device.roomId].push(newDevice);
|
2021-01-22 15:49:02 +01:00
|
|
|
} else {
|
2020-02-20 13:02:47 +01:00
|
|
|
roomDevices[device.roomId] = [newDevice];
|
|
|
|
|
}
|
2019-12-13 17:59:48 +01:00
|
|
|
|
2020-02-20 13:04:25 +01:00
|
|
|
devices.push(newDevice);
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-13 17:59:48 +01:00
|
|
|
if (this.includeRoomsAsLights) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug('includeRoomsAsLights is set to true, adding rooms too.');
|
2019-12-13 17:59:48 +01:00
|
|
|
for (let i = 0; i < this.site.rooms.length; i++) {
|
|
|
|
|
const room = this.site.rooms[i];
|
2021-01-22 15:49:02 +01:00
|
|
|
const { roomId } = room;
|
2019-12-13 17:59:48 +01:00
|
|
|
const roomAddress = this.site.roomAddress[roomId];
|
|
|
|
|
|
|
|
|
|
const newDevice = {
|
|
|
|
|
id: roomAddress,
|
|
|
|
|
name: room.title,
|
|
|
|
|
type: 'light',
|
|
|
|
|
typeName: 'Room',
|
2021-01-22 15:49:02 +01:00
|
|
|
dimmable: roomDevices[roomId].filter((x) => x.dimmable).length > 0,
|
2019-12-13 17:59:48 +01:00
|
|
|
};
|
2020-01-17 15:00:54 +00:00
|
|
|
|
2019-12-13 17:59:48 +01:00
|
|
|
devices.push(newDevice);
|
|
|
|
|
}
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug('includeRoomsAsLights done.');
|
2019-12-13 17:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 15:54:08 +00:00
|
|
|
// add scenes as switches
|
2021-01-22 15:49:02 +01:00
|
|
|
const scenes = this.site.scenes.filter((x) => x.hiddenFromSceneList === false);
|
2020-02-29 15:54:08 +00:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-02-29 15:54:08 +00:00
|
|
|
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',
|
2021-01-22 15:49:02 +01:00
|
|
|
serialNumber: scene.objectId,
|
2020-02-29 15:54:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
devices.push(newScene);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
return devices;
|
|
|
|
|
}
|
2019-12-13 14:13:00 +01:00
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2019-12-13 14:13:00 +01:00
|
|
|
_getDeviceType(hardwareId) {
|
2021-01-22 15:49:02 +01:00
|
|
|
switch (parseInt(hardwareId, 10)) {
|
2019-12-13 14:13:00 +01:00
|
|
|
case 1:
|
|
|
|
|
case 11:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'DIM-01', type: 'light', dimmable: true };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 2:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'DIM-02', type: 'light', dimmable: true };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 3:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'CTR-01', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 4:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'GWY-01', type: 'sensor', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 5:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'LED-10', type: 'light', dimmable: true };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 6:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'WPH-01', type: 'switch', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 7:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'REL-01', type: 'switch', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 8:
|
|
|
|
|
case 9:
|
|
|
|
|
// Unknown
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: '-unknown-', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 10:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: '-unknown-', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 12:
|
|
|
|
|
// Unknown
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: '-unknown-', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 13:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'Generic', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 14:
|
|
|
|
|
case 15:
|
|
|
|
|
case 16:
|
|
|
|
|
// Unknown
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: '-unknown-', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 17:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'REL-01', type: 'switch', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 18:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'REL-02', type: 'switch', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 19:
|
|
|
|
|
// Unknown
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: '-unknown-', type: 'light', dimmable: false };
|
2019-12-13 14:13:00 +01:00
|
|
|
case 20:
|
2021-01-22 15:49:02 +01:00
|
|
|
return { name: 'SPR-01', type: 'switch', dimmable: false };
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unknown device type with id ${hardwareId}`);
|
2019-12-13 14:13:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
module.exports = PlejdApi;
|