Skip to content
Snippets Groups Projects
api-mqtt-in.js 2.66 KiB
"use strict";
import Luci from "../luci.js";
import Api from "./api.js";

class ApiMqttIn {

    static readAllRPCLuci = payloadJSON => {
        Api.sendDataLuciWeb(payloadJSON);
    }

    static luciReceived = [];
    static readStatusLuci = (outVal, payloadJSON) => {
        // TODO find true connection IN-OUT
        const index = ApiMqttIn.luciReceived.findIndex(luce => luce.out === outVal);
        if(index >= 0)//se ho gia' ricevuto la luce, non la conto
            return;
        const l = Luci.createLight(payloadJSON.luogo != null ? payloadJSON.luogo : 'test' + Date.now(), payloadJSON.stato, 'in' + ApiMqttIn.luciReceived.length, outVal);
        // add the light in the correct position in the lights array
        // the array should be sorted by out0, out1, out2, ... out7
        let i = 0;
        while (i < ApiMqttIn.luciReceived.length && ApiMqttIn.luciReceived[i].out < outVal) {
            i++;
        }
        if(i < ApiMqttIn.luciReceived.length)
            ApiMqttIn.luciReceived.splice(i, 0, l);
        else
            ApiMqttIn.luciReceived.push(l);

        if (ApiMqttIn.luciReceived.length >= 8) {
            const toBeSent = ApiMqttIn.luciReceived.splice(0, 8);
            Api.sendLuciWeb(toBeSent);
        }
    }

    static outForToggleLuciStato = null;//luce con "luce-out" = out0/.../out7
    /**
     * 
     * @param {*} outVal out0/.../out7
     * @param {*} payloadJSON {event:true/false} luce accesa o spenta
     */
    static triggerLuce = (outVal, payloadJSON) => {
        if (Api.outForToggleLuciStato == null)
            return;
            // send to luci to figure out what is the connection
            // Api.sendLuciStatoWeb({ luce: ApiMqttIn.outForToggleLuciStato, nome: outVal, evento: payloadJSON.evento });
        if (ApiMqttIn.outForToggleLuciStato["luce-out"] === outVal && ApiMqttIn.outForToggleLuciStato.stato === payloadJSON.evento) {
            Api.sendLuciStatoWeb();
            ApiMqttIn.outForToggleLuciStato = null;
        }
    }

    static newLuce = payloadJSON => {
        Api.sendWebNewLuci(payloadJSON);
    }

    static cambioStatoScenario = payloadJSON => {
        Api.sendWebNewScenarioStatus(payloadJSON);
    }

    static readStatoAutomaScenario = stato => {
        Api.sendWebScenarioAutoma(stato);
    }

    static renamedSavedScenario = payloadJSON => {
        Api.sendWebSavedScenario(JSON.parse(payloadJSON));
    }

    static readStatoAutomaAntifurto = stato => {
        Api.sendWebAntifurtoAutoma(stato);
    }

    static readAllRPCScenari = payloadJSON => {
        if (payloadJSON.status != null)//escludo le luci
            return;
        Api.sendScenariWeb(payloadJSON);
    }

}

export default ApiMqttIn;