/** * @license * * shelly-porssisahko * * (c) Jussi isotalo - http://jisotalo.fi * https://github.com/jisotalo/shelly-porssisahko * * License: GNU Affero General Public License v3.0 */ let C_HIST=24,C_ERRC=3,C_ERRD=120,C_DEF={mode:0,m0:{cmd:0},m1:{lim:0},m2:{per:24,cnt:0,lim:-999,sq:0,m:999,ps:0,pe:23,ps2:0,pe2:23,cnt2:0},vat:24,day:0,night:0,bk:0,err:0,outs:[0],fh:0,fhCmd:0,inv:0,min:60,oc:0},_={s:{v:"2.12.5",dn:"",st:0,str:"",cmd:-1,chkTs:0,errCnt:0,errTs:0,upTs:0,timeOK:0,configOK:0,fCmdTs:0,fCmd:0,tz:"+02:00",tzh:0,p:[{ts:0,now:0,low:0,high:0,avg:0},{ts:0,now:0,low:0,high:0,avg:0}]},p:[[],[]],h:[],c:C_DEF},m=!1,r=!1;function i(e,t){t-=e;return 0<=t&&t<3600}function c(e,t,s){return Math.min(s,Math.max(e,t))}function l(e){return Math.floor((e?e.getTime():Date.now())/1e3)}function o(e,t,s){let o=e.toString();for(;o.length=C_HIST;)_.h.splice(0,1);_.h.push([l(),r?1:0,_.s.st])}function p(){var e=new Date;_.s.timeOK=2e3=_.c.min&&_.s.cmd+_.c.inv==1}()?e(1)?t(1):m=!1:f():d(!0))}catch(e){u("loop() - virhe:"+e),m=!1}}function e(e){var t=new Date;let s=!1;if(1==e)s=_.s.timeOK&&0===_.s.p[1].ts&&15<=t.getHours();else{let e=n(new Date(1e3*_.s.p[0].ts))!==n(t);e&&0<_.s.p[1].ts&&n(new Date(1e3*_.s.p[1].ts))!==n(t)&&(_.p[0]=_.p[1],_.s.p[0]=Object.assign({},_.s.p[1]),_.s.p[0].ts=l(),_.s.p[1].ts=0,_.p[1]=[],e=!1),s=_.s.timeOK&&(0==_.s.p[0].ts||e)}return _.s.errCnt>=C_ERRC&&l(t)-_.s.errTs=C_ERRC&&(_.s.errCnt=0),s}function t(c){try{let r=new Date;a(r);var t=1==c?new Date(864e5+new Date(r.getFullYear(),r.getMonth(),r.getDate()).getTime()):r;let e=t.getFullYear()+"-"+o(1+t.getMonth(),2,"0")+"-"+o(n(t),2,"0")+"T00:00:00"+_.s.tz.replace("+","%2b");var s=e.replace("T00:00:00","T23:59:59");let i={url:"https://dashboard.elering.ee/api/nps/price/csv?fields=fi&start="+e+"&end="+s,timeout:5,ssl_ca:"*"};r=null,e=null,Shelly.call("HTTP.GET",i,function(t,e,s){i=null;try{if(0!==e||null==t||200!==t.code||!t.body_b64)throw Error("virhe: "+e+"("+s+") - "+JSON.stringify(t));{t.headers=null,s=t.message=null,_.p[c]=[],_.s.p[c].avg=0,_.s.p[c].high=-999,_.s.p[c].low=999,t.body_b64=atob(t.body_b64),t.body_b64=t.body_b64.substring(1+t.body_b64.indexOf("\n"));let e=0;for(;0<=e;){t.body_b64=t.body_b64.substring(e);var o=[e=0,0];if(0===(e=1+t.body_b64.indexOf('"',e)))break;o[0]=+t.body_b64.substring(e,t.body_b64.indexOf('"',e)),e=2+t.body_b64.indexOf('"',e),e=2+t.body_b64.indexOf(';"',e),o[1]=+(""+t.body_b64.substring(e,t.body_b64.indexOf('"',e)).replace(",",".")),o[1]=o[1]/10*(100+(0_.s.p[c].high&&(_.s.p[c].high=o[1]),o[1]<_.s.p[c].low&&(_.s.p[c].low=o[1]),e=t.body_b64.indexOf("\n",e)}if(t=null,_.s.p[c].avg=0<_.p[c].length?_.s.p[c].avg/_.p[c].length:0,_.s.p[c].ts=l(r),1==c&&_.p[c].length<23)throw Error("huomisen hintoja ei saatu")}}catch(e){u("getPrices() - virhe:"+e),_.s.errCnt+=1,_.s.errTs=l(),_.s.p[c].ts=0,_.p[c]=[]}1==c?m=!1:Timer.set(1e3,!1,f)})}catch(e){u("getPrices() - virhe:"+e),_.s.p[c].ts=0,_.p[c]=[],1==c?m=!1:Timer.set(1e3,!1,f)}}function f(){try{"function"==typeof USER_CONFIG&&(_.c=USER_CONFIG(_.c,_,!1)),r=!1;var e,t,s=new Date;function o(e){if(null==e)m=!1;else if(r!=e&&(_.s.st=12),r=e,_.c.inv&&(r=!r),1==_.c.oc&&_.s.cmd==r)u("logic(): lähtö on jo oikeassa tilassa"),h(),_.s.cmd=r?1:0,_.s.chkTs=l(),m=!1;else{let t=0,s=0;for(let e=0;e<_.c.outs.length;e++)!function(n,e){var t="{id:"+n+",on:"+(r?"true":"false")+"}";Shelly.call("Switch.Set",t,function(e,t,s,o){0!=t&&u("setRelay() - ohjaus #"+n+" epäonnistui: "+t+" - "+s),o(0==t)},e)}(_.c.outs[e],function(e){t++,e&&s++,t==_.c.outs.length&&(s==t&&(h(),_.s.cmd=r?1:0,_.s.chkTs=l()),m=!1)})}}a(s),!function(){if(_.s.timeOK&&0!=_.s.p[0].ts){var t=l();for(let e=0;e<_.p[0].length;e++)if(i(_.p[0][e][0],t))return _.s.p[0].now=_.p[0][e][1];return _.p[0].length<24&&(_.s.p[0].ts=0),_.s.p[0].now=0}_.s.p[0].now=0}(),0===_.c.mode?(r=1===_.c.m0.cmd,_.s.st=1):_.s.timeOK&&0<_.s.p[0].ts&&n(new Date(1e3*_.s.p[0].ts))===n(s)?1===_.c.mode?(r=_.s.p[0].now<=("avg"==_.c.m1.lim?_.s.p[0].avg:_.c.m1.lim),_.s.st=r?2:3):2===_.c.mode&&(r=function(){_.c.m2.ps=c(0,_.c.m2.ps,23),_.c.m2.pe=c(_.c.m2.ps,_.c.m2.pe,24),_.c.m2.ps2=c(0,_.c.m2.ps2,23),_.c.m2.pe2=c(_.c.m2.ps2,_.c.m2.pe2,24),_.c.m2.cnt=c(0,_.c.m2.cnt,0<_.c.m2.per?_.c.m2.per:_.c.m2.pe-_.c.m2.ps),_.c.m2.cnt2=c(0,_.c.m2.cnt2,_.c.m2.pe2-_.c.m2.ps2);var o=[];for(y=_.c.m2.per<0?1:_.c.m2.per,v=0;v<_.p[0].length;v+=y)if(!((C=-2==_.c.m2.per&&1<=v?_.c.m2.cnt2:_.c.m2.cnt)<=0)){var n=[];for(T=v,w=v+_.c.m2.per,_.c.m2.per<0&&0==v?(T=_.c.m2.ps,w=_.c.m2.pe):-2==_.c.m2.per&&1==v&&(T=_.c.m2.ps2,w=_.c.m2.pe2),b=T;b_.p[0].length-1);b++)n.push(b);if(_.c.m2.sq){let t=999,s=0;for(b=0;b<=n.length-C;b++){let e=0;for(g=b;g("avg"==_.c.m2.m?_.s.p[0].avg:_.c.m2.m)&&(r=!1,_.s.st=11):_.s.timeOK?(_.s.st=7,e=1<=_.c.min&&(_.s.st=13,r=!1),_.s.timeOK&&0<_.s.fCmdTs&&(0<_.s.fCmdTs-l(s)?(r=1==_.s.fCmd,_.s.st=9):_.s.fCmdTs=0),"function"==typeof USER_OVERRIDE?USER_OVERRIDE(r,_,o):o(r)}catch(e){u("logic() - virhe:"+JSON.stringify(e)),m=!1}}let v=0,b=0,g=0,y=0,C=0,T=0,w=0;u("shelly-porssisahko v."+_.s.v),u("URL: http://"+(Shelly.getComponentStatus("wifi").sta_ip??"192.168.33.1")+"/script/"+Shelly.getCurrentScriptId()),HTTPServer.registerEndpoint("",function(s,o){try{if(m)return s=null,o.code=503,void o.send();var n=function(e){var t={},s=e.split("&");for(let e=0;e