alexa-remote
alexa-remote copied to clipboard
Promise-ing the lib
As enhancement, That would be nice to promise-ing the commands to alexa, that would be easier for command chaining ;-)
I'm happy to take PRs :-)
This may be helpful (or not) to others, I wrap some of the commands.
/**
* How often to refresh the routines cache, when the are requested.
* @type {number}
*/
const ROUTINE_CACHE_LENGTH = 1000 * 60;
class Helper {
constructor(gateway) {
this.gateway = gateway;
this.cacheRoutineUntil = 0;
}
/**
* Pulls SmarthomeDevices into one array and converts the callback into a promise.
*
* @returns {Promise<unknown>}
*/
async getSmarthomeDevices() {
return new Promise((resolve, reject) => {
this.gateway.getSmarthomeDevices((err, data) => {
if (err) return reject(err);
let info = [];
try {
if (this.isSmarthomeDevicesResponseValid(data)) {
let skills = data.locationDetails.Default_Location.amazonBridgeDetails.amazonBridgeDetails;
for (let i in skills) {
let appliances = skills[i].applianceDetails.applianceDetails;
for (let x in appliances) {
info.push(appliances[x]);
}
}
resolve(info)
} else {
return reject(new Error('Smart Home Device Response Invalid'))
}
} catch (e) {
return reject(e)
}
})
});
}
isSmarthomeDevicesResponseValid (data) {
return (
data &&
data.hasOwnProperty('locationDetails') &&
data.locationDetails.hasOwnProperty('Default_Location') &&
data.locationDetails.Default_Location.hasOwnProperty('amazonBridgeDetails') &&
data.locationDetails.Default_Location.amazonBridgeDetails.hasOwnProperty('amazonBridgeDetails')
);
}
/**
* Pulls SmarthomeEntities and converts the callback into a promise.
* @returns {Promise<unknown>}
*/
async getSmarthomeEntities() {
return new Promise((resolve, reject) => {
this.gateway.getSmarthomeEntities((err, data) => {
if (err) return reject(err);
resolve(data)
})
});
}
/**
* Pulls AutomationRoutines and caches the result. allows filtering of the result and converts the callback into a promise.
* @param lens
* @returns {Promise<unknown>}
*/
async parseRoutines(lens) {
return new Promise((resolve, reject) => {
if (Date.now() > this.cacheRoutineUntil) {
this.cacheRoutineUntil = Date.now() + ROUTINE_CACHE_LENGTH;
this.gateway.getAutomationRoutines((err, result) => {
if (err) return reject(err);
this.routines = result;
resolve(this.routines.filter(lens));
});
} else {
resolve(this.routines.filter(lens));
}
});
}
/**
* Converts executeSmarthomeDeviceAction into a promise
* @param entityIds
* @param parameters
* @param entityType
* @returns {Promise<unknown>}
*/
async executeSmarthomeDeviceAction(entityIds, parameters, entityType) {
return new Promise((resolve, reject) => {
this.gateway.executeSmarthomeDeviceAction(entityIds, parameters, entityType, (err, data) => {
if (err) return reject(err);
resolve(data)
})
});
}
/**
* Coverts querySmarthomeDevices into a promise
*
* NOTE : this uses an applianceId or applianceIds
*
* @param applicanceIds
* @param entityType
* @returns {Promise<unknown>}
*/
async querySmarthomeDevices(applicanceIds, entityType = "APPLIANCE") {
return new Promise((resolve, reject) => {
this.gateway.querySmarthomeDevices(applicanceIds, entityType, (err, data) => {
if (err) return reject(err);
resolve(data)
})
});
}
/**
* Promisified wrapper for sequenceCommand
* @param serial
* @param command
* @param message
* @returns {Promise<unknown>}
*/
async sequenceCommand(serial, command, message = false) {
return new Promise((resolve, reject) => {
this.gateway.sendSequenceCommand(serial, command, message, (err, data) => {
if (err) return reject(err);
resolve(data)
})
});
}
/**
* Promisified wrapper for command
* @param serial
* @param command
* @param message
* @returns {Promise<unknown>}
*/
async command(serial, command, message = false) {
return new Promise((resolve, reject) => {
this.gateway.sendCommand(serial, command, message, (err, data) => {
if (err) return reject(err);
resolve(data)
})
});
}
async init(options) {
return new Promise((resolve, reject) => {
this.gateway.init(options, (result) => {
if (result) return reject(result);
else return resolve(true);
});
});
}
async stopProxyServer () {
return new Promise((resolve, reject) => {
this.gateway.stopProxyServer( (result) => {
if (result) return reject(result);
else return resolve(true);
});
});
}
}
module.exports = Helper;