OpenHausIO / backend

HTTP API for the OpenHaus SmartHome/IoT solution
https://docs.open-haus.io
6 stars 2 forks source link

Emit `state` event on component when state changes #482

Closed mStirner closed 1 month ago

mStirner commented 4 months ago

In the endpoint item construction (class.endpoint.js), add a .emit(...) inside the state update callback: https://github.com/OpenHausIO/backend/blob/dbf499ec68eb4ab7d4204ab21efea51133fe824c/components/endpoints/class.endpoint.js#L66-L83

to

        // see 407 & 420
        this.states = obj.states.map((item) => {
            return new State(item, () => {
                try {

                    // feedback 
                    scope.logger.verbose(`Value in endpoint ("${obj._id}") state ("${item._id}") changed: ${item.alias}=${item.value}`);

                    // notify listener event changed
                    scope.emit("state", item);

                    // update item in database 
                    //await scope.update(this._id, this); 
                    updater();

                } catch (err) {

                    scope.logger.warn(err, `Could not update item (${obj._id}) state ("${item._id}") in database: ${item.alias}=${item.value}`);

                }
            });
        });