//Config
var rgbw_1= '192.168.178.251',
rgbw_2= '192.168.178.252',
dimmer2= '192.168.178.235',
bwm_Unten= 'input:1',
bwm_Oben= 'input:0',
cTimeout= 3,
delay= 15,
nightMode= [23,8],
extraDimmer= 100, //zweiter Dimm Wert
debug= false; //only for short periods(debug)!!! --> default is false
function Treppe_Rauf(bright,bright2){
function Next_A(result,_bright){
callLimit= 2; cSp= 0.6; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/0?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/1?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/2?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/0?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/1?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/2?turn=on&brightness='+_bright, timeout: cTimeout});
}
callLimit= 2; cSp= 0; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + dimmer2 + '/light/0?turn=on&brightness='+bright2, timeout: cTimeout},Next_A,bright);
}
function Treppe_Runter(bright,bright2){
function Next_R(result,_bright){
callLimit= 2; cSp= 0.6; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/0?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/2?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/1?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/1?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/2?turn=on&brightness='+_bright, timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/0?turn=on&brightness='+_bright, timeout: cTimeout});
}
callLimit= 2; cSp= 0; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + dimmer2 + '/light/0?turn=on&brightness='+bright2, timeout: cTimeout},Next_R,bright);
}
function Alles_Aus(){
function Next(){
callLimit= 2; cSp= 0.1; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/0?turn=off', timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/2?turn=off', timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/1?turn=off', timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/1?turn=off', timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_2 + '/white/2?turn=off', timeout: cTimeout});
Call('HTTP.get',{url: 'http://' + rgbw_1 + '/white/0?turn=off', timeout: cTimeout});
}
callLimit= 2; cSp= 0; //Active Call Limit and speed betwinn calls
Call('HTTP.get',{url: 'http://' + dimmer2 + '/light/0?turn=off', timeout: cTimeout},Next);
}
let tH1= 0; //Global Timer
function Event_Loop(data){
try{
let trigger= Efilter(data,{device:[bwm_Unten,bwm_Oben],filterKey:['state']}); //Filter for BWM Event
data= null; if(!trigger) return; //Clear useless Data and exit if useless Data
let timeN= new Date(); timeN= timeN.getHours(); //Get Time in hours
let brightness= 50, brightnessdim= 100; //Default brightniss
if(nightMode[0] <= timeN || nightMode[1] > timeN) brightness= 10, brightnessdim= 50; //Night brightniss
let bwm_U= Status(Cut(bwm_Unten,':',0,true),Number(Cut(bwm_Unten,':',1))).state; //Get Status bwm_Unten
let bwm_O= Status(Cut(bwm_Oben,':',0,true),Number(Cut(bwm_Oben,':',1))).state; //Get Status bwm_Oben
let callsFin= (!aC && !cCache.length); //Set Flag for all calls Finished
if(debug) print('Debug: callsFin->',callsFin,', bwm_Unten->',bwm_U,', bwm_Oben->',bwm_O,', timeN->',timeN); //Debug output
if(bwm_U || bwm_O){Timer.clear(tH1); tH1= 0;} //Reset Timer
if(callsFin && bwm_U && !tH1) Treppe_Rauf(brightness,extraDimmer); //Do Sequenz Rauf
if(callsFin && bwm_O && !tH1) Treppe_Runter(brightness,extraDimmer); //Do Sequenz Runter
if(!bwm_U && !bwm_O && !tH1) tH1= Timer.set(1000*delay,false,Alles_Aus); //Start Timer and Do Sequenz
}catch(e){ErrorMsg(e,'Event_Loop()');} //Error Handler
}
function Main(){
Shelly.addEventHandler(Event_Loop); //Create a Asyncron Event_Loop
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
function Efilter(d,p,deBug) { //Event Filter, d=eventdata, p={device:[], filterKey:[], filterValue:[], noInfo:true, inData:true}->optional_parameter
try{
let fR= {}; //d.info= d.info.data;
if(p.noInfo){fR= d; d= {}; d.info= fR; fR= {};} if(p.inData && d.info.data){Object.assign(d.info,d.info.data) delete d.info.data;}
if(!d.info) fR.useless= true; if(p.device && p.device.length && p.device.indexOf(d.info.component) === -1) fR.useless= true;
if(p.device && p.device.length && !fR.useless && !p.filterKey && !p.filterValue) fR= d.info;
if(p.filterKey && !fR.useless) for(f of p.filterKey) for(k in d.info) if(f === k) fR[k]= d.info[k];
if(p.filterValue && !fR.useless) for(f of p.filterValue) for(v of d.info) if(Str(v) && f === v) fR[Str(v)]= v;
if(deBug) print('\nDebug: EventData-> ', d, '\n\nDebug: Result-> ', fR, '\n');
if(Str(fR) === '{}' || fR.useless){return;} return fR;}catch(e){ErrorMsg(e,'Efilter()');}}
function ErrorChk(r,e,m,d){ //Shelly.call error check
try{
aC--; if(aC<0) aC= 0;
if(d.CB && d.uD) d.CB(r,d.uD); if(d.CB && !d.uD) d.CB(r);
if(!d.CB && d.uD) print('Debug: ',d.uD); if(e) throw new Error(Str(m));
if(Str(r) && Str(r.code) && r.code !== 200) throw new Error(Str(r));
}catch(e){ErrorMsg(e,'ErrorChk(), call Answer');}}
function Cqueue(){ //Shelly.call queue
try{
if(!cCache[0] && !nCall[0]) return;
while(cCache[0] && aC < callLimit){if(cCache[0] && !nCall[0]){nCall= cCache[0]; cCache.splice(0,1);}
if(nCall[0] && aC < callLimit){Call(nCall[0],nCall[1],nCall[2],nCall[3],nCall[4]); nCall= [];}}
if((nCall[0] || cCache[0]) && !tH8)if(cSp <= 0) cSp= 0.1; tH8= Timer.set(1000*cSp,0,function(){tH8= 0; Cqueue();});}catch(e){ErrorMsg(e,'Cqueue()');}}
function Call(m,p,CB,uD,deBug){ //Upgrade Shelly.call
try{
let d= {};
if(deBug) print('Debug: calling:',m,p); if(CB) d.CB= CB; if(Str(uD)) d.uD= uD; if(!m && CB){CB(uD); return;}
if(aC < callLimit){aC++; Shelly.call(m,p,ErrorChk,d);}else if(cCache.length < cacheLimit){
cCache.push([m,p,CB,uD,deBug]); if(deBug) print('Debug: save call:',m,p,', call queue now:',cCache.length); Cqueue();
}else{throw new Error('to many Calls in use, droping call: '+Str(m)+', '+Str(p));}}catch(e){ErrorMsg(e,'Call()');}}
function Str(d){ //Upgrade JSON.stringify
try{
if(d === null || d === undefined) return null; if(typeof d === 'string')return d;
return JSON.stringify(d);}catch(e){ErrorMsg(e,'Str()');}}
function Cut(f,k,o,i){ //Upgrade slice f=fullData, k=key-> where to cut, o=offset->offset behind key, i=invertCut
try{
let s= f.indexOf(k); if(s === -1) return; if(o) s= s+o.length || s+o; if(i) return f.slice(0,s);
return f.slice(s);}catch(e){ErrorMsg(e,'Cut()');}}
function Setup(l){ //Wating 2sek, to avoid a Shelly FW Bug
try{
if(Main && !tH9){tH9= Timer.set(2000,l,function(){print('\nStatus: started Script _[', scriptN,']_');
if(callLimit > 5){callLimit= 5;} try{Main();}catch(e){ErrorMsg(e,'Main()'); Setup();}});}}catch(e){ErrorMsg(e,'Setup()');}}
function ErrorMsg(e,s){ //Toolbox formatted Error Msg
try{
let i= 0; if(Cut(e.message, '-104: Timed out')) i= 'wrong URL or device may be offline';
if(s === 'Main()') i= e.stack; if(Cut(e.message, '"Main" is not')) i= 'define a Main() function before using Setup()';
print('Error:',s || "",'---> ',e.type,e.message); if(i) print('Info: maybe -->',i);}catch(e){print('Error: ErrorMsg() --->',e);}}
var tH7= 0, tH8= 0, tH9= 0, aC= 0, cCache= [], nCall= [], callLimit= 5, cacheLimit= 40, cSp= 0.1; //Toolbox global variable
var Status= Shelly.getComponentStatus, Config= Shelly.getComponentConfig; //Renamed native function
var info= Shelly.getDeviceInfo(), scriptID= Shelly.getCurrentScriptId(), scriptN= Config('script',scriptID).name; //Pseudo const, variabel
//Toolbox v3.2 -Alpha(cut), Shelly FW >1.0.8
Setup();
Alles anzeigen