Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise-ing the lib #33

Open
NebzHB opened this issue Apr 5, 2019 · 2 comments
Open

Promise-ing the lib #33

NebzHB opened this issue Apr 5, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@NebzHB
Copy link

NebzHB commented Apr 5, 2019

As enhancement, That would be nice to promise-ing the commands to alexa, that would be easier for command chaining ;-)

@Apollon77 Apollon77 added the enhancement New feature or request label Apr 5, 2019
@Apollon77
Copy link
Owner

I'm happy to take PRs :-)

@hive
Copy link

hive commented Feb 18, 2023

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants