iobroker autostart script

The following script is always running but executes given commands only once after iobroker restarted (either via ‘iobroker restart’-command or via a system’s reboot). It does not need any additional objects etc. In this special case it checks in intervals if the Zigbee adapter is running, then restarts the adapter once (because of an issue that’s not part of this post). Please keep in mind: The script also executes every time it is saved. 

 

/*
    To run once after iob start / restart / system reboot.
    By using an interval with a simple logic
    the script can run infinetely but only executes the given actions once.
*/
const CHECKINTERVAL_ZIGBEE = 7500; //minimum. Query-command is SLOW
var bRunOnce = false;

let waitforZigbeeRunning;
let waitforZigbeeRestart;

let mainInterval = setInterval(function(){
    if(!bRunOnce){
        bRunOnce = true;
        waitforZigbeeRunning = setInterval(function(){
            //exec is SLOW 
            exec("iob status zigbee", function (error, stdout, stderr) {
                if(stdout.includes('is running')){
                    console.log('zigbee adapter detected as running. clearing checker-interval');
                    clearInterval(waitforZigbeeRunning);
                    waitforZigbeeRestart = setTimeout(function(){
                        console.log('run command 10 sec after clearing checker interval');
                        exec("iob restart zigbee", function (error, stdout, stderr) {});
                        sendTo('whatsapp-cmb.0', 'send', {
                            text: 'zigbee restartet 120 sec after clearing checker interval', 
                        });
                    }, 120000);
                }else{
                    //console.log('zigbee adapter not running yet');
                }
            });
        }, CHECKINTERVAL_ZIGBEE);
    }  
}, 1000);
Tagged , , , , , .Speichere in deinen Favoriten diesen permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert