2021-02-01 21:22:53 +01:00
|
|
|
const axios = require('axios').default;
|
|
|
|
|
|
|
|
|
|
const Configuration = require('./Configuration');
|
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
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
class PlejdApi {
|
|
|
|
|
config;
|
|
|
|
|
deviceRegistry;
|
|
|
|
|
sessionToken;
|
|
|
|
|
siteId;
|
|
|
|
|
siteDetails;
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
constructor(deviceRegistry) {
|
|
|
|
|
this.config = Configuration.getOptions();
|
|
|
|
|
this.deviceRegistry = deviceRegistry;
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
async init() {
|
|
|
|
|
logger.info('init()');
|
|
|
|
|
await this.login();
|
|
|
|
|
await this.getSites();
|
|
|
|
|
await this.getSiteDetails();
|
|
|
|
|
this.getDevices();
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
async login() {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('login()');
|
2021-02-01 21:22:53 +01:00
|
|
|
logger.info(`logging into ${this.config.site}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_LOGIN_URL}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
try {
|
|
|
|
|
const response = await this._getAxiosInstance().post(API_LOGIN_URL, {
|
|
|
|
|
username: this.config.username,
|
|
|
|
|
password: this.config.password,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.info('got session token response');
|
|
|
|
|
this.sessionToken = response.data.sessionToken;
|
|
|
|
|
|
|
|
|
|
if (!this.sessionToken) {
|
|
|
|
|
logger.error('No session token received');
|
|
|
|
|
throw new Error('API: No session token received.');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.response.status === 400) {
|
|
|
|
|
logger.error('Server returned status 400. probably invalid credentials, please verify.');
|
|
|
|
|
} else if (error.response.status === 403) {
|
|
|
|
|
logger.error(
|
|
|
|
|
'Server returned status 403, forbidden. Plejd seems to do this sometimes, despite correct credentials. Possibly waiting a long time will fix this.',
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
logger.error('Unable to retrieve session token response: ', error);
|
|
|
|
|
}
|
|
|
|
|
logger.verbose(`Error details: ${JSON.stringify(error.response, null, 2)}`);
|
|
|
|
|
|
|
|
|
|
throw new Error(`API: Unable to retrieve session token response: ${error}`);
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
async 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
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_SITE_LIST_URL}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
try {
|
|
|
|
|
const response = await this._getAxiosInstance().post(API_SITE_LIST_URL);
|
|
|
|
|
|
|
|
|
|
const sites = response.data.result;
|
|
|
|
|
logger.info(
|
|
|
|
|
`Got site list response with ${sites.length}: ${sites.map((s) => s.site.title).join(', ')}`,
|
|
|
|
|
);
|
|
|
|
|
logger.silly('All sites found:');
|
|
|
|
|
logger.silly(JSON.stringify(sites, null, 2));
|
|
|
|
|
|
|
|
|
|
const site = sites.find((x) => x.site.title === this.config.site);
|
|
|
|
|
|
|
|
|
|
if (!site) {
|
|
|
|
|
logger.error(`Failed to find a site named ${this.config.site}`);
|
|
|
|
|
throw new Error(`API: Failed to find a site named ${this.config.site}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info(`Site found matching configuration name ${this.config.site}`);
|
|
|
|
|
logger.silly(JSON.stringify(site, null, 2));
|
|
|
|
|
this.siteId = site.site.siteId;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('error: unable to retrieve list of sites. error: ', error);
|
|
|
|
|
throw new Error(`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
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
async getSiteDetails() {
|
|
|
|
|
logger.info(`Get site details for ${this.siteId}...`);
|
2020-03-13 11:58:15 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
logger.debug(`sending POST to ${API_BASE_URL}${API_SITE_DETAILS_URL}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
try {
|
|
|
|
|
const response = await this._getAxiosInstance().post(API_SITE_DETAILS_URL, {
|
|
|
|
|
siteId: this.siteId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.info('got site details response');
|
|
|
|
|
|
|
|
|
|
if (response.data.result.length === 0) {
|
|
|
|
|
logger.error(`No site with ID ${this.siteId} was found.`);
|
|
|
|
|
throw new Error(`API: No site with ID ${this.siteId} was found.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.siteDetails = response.data.result[0];
|
|
|
|
|
this.deviceRegistry.setApiSite(this.siteDetails);
|
|
|
|
|
|
|
|
|
|
logger.info(`Site details for site id ${this.siteId} found`);
|
|
|
|
|
logger.silly(JSON.stringify(this.siteDetails, null, 2));
|
|
|
|
|
|
|
|
|
|
this.deviceRegistry.cryptoKey = this.siteDetails.plejdMesh.cryptoKey;
|
|
|
|
|
if (!this.deviceRegistry.cryptoKey) {
|
|
|
|
|
throw new Error('API: No crypto key set for site');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`Unable to retrieve site details for ${this.siteId}. error: `, error);
|
|
|
|
|
throw new Error(`API: Unable to retrieve site details. error: ${error}`);
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDevices() {
|
2021-02-01 21:22:53 +01:00
|
|
|
logger.info('Getting devices from site details response...');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
this._getPlejdDevices();
|
|
|
|
|
this._getRoomDevices();
|
|
|
|
|
this._getSceneDevices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_getAxiosInstance() {
|
|
|
|
|
const headers = {
|
|
|
|
|
'X-Parse-Application-Id': API_APP_ID,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (this.sessionToken) {
|
|
|
|
|
headers['X-Parse-Session-Token'] = this.sessionToken;
|
|
|
|
|
}
|
2019-12-10 19:15:38 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
return axios.create({
|
|
|
|
|
baseURL: API_BASE_URL,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-12-13 17:59:48 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
|
_getDeviceType(hardwareId) {
|
|
|
|
|
switch (parseInt(hardwareId, 10)) {
|
|
|
|
|
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 };
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unknown device type with id ${hardwareId}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_getPlejdDevices() {
|
|
|
|
|
this.deviceRegistry.clearPlejdDevices();
|
|
|
|
|
|
|
|
|
|
this.siteDetails.devices.forEach((device) => {
|
2021-01-22 15:49:02 +01:00
|
|
|
const { deviceId } = device;
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
const settings = this.siteDetails.outputSettings.find(
|
|
|
|
|
(x) => x.deviceParseId === device.objectId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let deviceNum = this.siteDetails.deviceAddress[deviceId];
|
2019-12-10 22:01:12 +01:00
|
|
|
|
|
|
|
|
if (settings) {
|
2021-02-01 21:22:53 +01:00
|
|
|
const outputs = this.siteDetails.outputAddress[deviceId];
|
2019-12-10 22:01:12 +01:00
|
|
|
deviceNum = outputs[settings.output];
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 15:49:02 +01:00
|
|
|
// check if device is dimmable
|
2021-02-01 21:22:53 +01:00
|
|
|
const plejdDevice = this.siteDetails.plejdDevices.find((x) => x.deviceId === deviceId);
|
2021-01-22 15:49:02 +01:00
|
|
|
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,
|
2021-02-01 21:22:53 +01:00
|
|
|
roomId: device.roomId,
|
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.
|
2021-02-01 21:22:53 +01:00
|
|
|
const inputs = this.siteDetails.inputAddress[deviceId];
|
2020-02-20 13:02:47 +01:00
|
|
|
const first = inputs[0];
|
|
|
|
|
const second = inputs[1];
|
|
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
this.deviceRegistry.addPlejdDevice({
|
|
|
|
|
...newDevice,
|
2020-02-20 13:04:25 +01:00
|
|
|
id: first,
|
2021-02-01 21:22:53 +01:00
|
|
|
name: `${device.title} left`,
|
|
|
|
|
});
|
2020-02-20 13:04:25 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
this.deviceRegistry.addPlejdDevice({
|
|
|
|
|
...newDevice,
|
2020-02-20 13:04:25 +01:00
|
|
|
id: second,
|
2021-02-01 21:22:53 +01:00
|
|
|
name: `${device.title} right`,
|
|
|
|
|
});
|
2021-01-22 15:49:02 +01:00
|
|
|
} else {
|
2021-02-01 21:22:53 +01:00
|
|
|
this.deviceRegistry.addPlejdDevice(newDevice);
|
2020-02-20 13:04:25 +01:00
|
|
|
}
|
2021-02-01 21:22:53 +01:00
|
|
|
});
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
_getRoomDevices() {
|
|
|
|
|
if (this.config.includeRoomsAsLights) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug('includeRoomsAsLights is set to true, adding rooms too.');
|
2021-02-01 21:22:53 +01:00
|
|
|
this.siteDetails.rooms.forEach((room) => {
|
2021-01-22 15:49:02 +01:00
|
|
|
const { roomId } = room;
|
2021-02-01 21:22:53 +01:00
|
|
|
const roomAddress = this.siteDetails.roomAddress[roomId];
|
2019-12-13 17:59:48 +01:00
|
|
|
|
|
|
|
|
const newDevice = {
|
|
|
|
|
id: roomAddress,
|
|
|
|
|
name: room.title,
|
|
|
|
|
type: 'light',
|
|
|
|
|
typeName: 'Room',
|
2021-02-01 21:22:53 +01:00
|
|
|
dimmable: this.deviceIdsByRoom[roomId].some(
|
|
|
|
|
(deviceId) => this.plejdDevices[deviceId].dimmable,
|
|
|
|
|
),
|
2019-12-13 17:59:48 +01:00
|
|
|
};
|
2020-01-17 15:00:54 +00:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
this.deviceRegistry.addRoomDevice(newDevice);
|
|
|
|
|
});
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug('includeRoomsAsLights done.');
|
2019-12-13 17:59:48 +01:00
|
|
|
}
|
2021-02-01 21:22:53 +01:00
|
|
|
}
|
2019-12-13 17:59:48 +01:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
_getSceneDevices() {
|
2020-02-29 15:54:08 +00:00
|
|
|
// add scenes as switches
|
2021-02-01 21:22:53 +01:00
|
|
|
const scenes = this.siteDetails.scenes.filter((x) => x.hiddenFromSceneList === false);
|
2020-02-29 15:54:08 +00:00
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
scenes.forEach((scene) => {
|
|
|
|
|
const sceneNum = this.siteDetails.sceneIndex[scene.sceneId];
|
2020-02-29 15:54:08 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2021-02-01 21:22:53 +01:00
|
|
|
this.deviceRegistry.addScene(newScene);
|
|
|
|
|
});
|
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;
|