/
SoRusV
/
src_xendil
Обзор
Документация
Войти
/
SoRusV
/
src_xendil
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
html/src/SCORD.js
2 592 строки
94 KB
ruslan
Initial commit
29 окт 2024, 11:18
29 окт 2024, 11:18
5c4dcf5
Код
Авторство
О чём код?
/// @T - in table, Add TBody to response /// @A - AutoRefresh. Take this elements at AutoRefresh() (not send/save), example time, wifi status /// @AT - AutoRefresh in table, /// @S - only get from server, but not send to server; /// @R - this elements refresh at call Refresh() and always save/send to server; ///-- @G - this elements send to server GetElementsToServer() //let VerFree=false; let ShowAqua=""; let ShowName=""; let dTManualOffMs=90000; let JsonMaxLength=2000; /// max size of json string send to esp-server let bBreakXMLResponse=-1; let TimeWokerLastSend=0; let dTimeWokerLastSendMax=30000; /// if error server not responce let _ip="192.168.4.1"; let esp=32; /// or =8266 //var JJS; //if(JJS){ /// get from android //let _ip=JJS.GetIP(); // // _ip="192.168.1.7"; //} function GetShowAqua(){ return {ShowAqua: ShowAqua, ShowName:ShowName, _ip:_ip}; } function parseGET() { /// file:///C:/Oleg/Scord/html/src/MAbout.htm?param=val&data=ololo /// Обработка GET-запросов var tmp = new Array(); // два вспомогательных var tmp2 = new Array(); // массива get = new Array(); // var url = location.search; // строка GET запроса var url = window.location.href; // строка GET запроса if(url != '') { tmp = (url.substr(1)).split('&'); // разделяем переменные for(var i=0; i < tmp.length; i++) { tmp2 = tmp[i].split('='); // массив param будет содержать get[tmp2[0]] = tmp2[1]; // пары ключ(имя переменной)->значение } } // if(get["ip"]) // _ip=get["ip"]; if(get["AquaName"]) ShowAqua=get["AquaName"]; if(get["Name"]) ShowName=get["Name"]; if(get["IP"]) _ip=get["IP"]; } parseGET(); //console.log("+"); // function myhref(web){ // window.location.href = web;} // function get_rest(b, d) // { // console.log(b); // let c; // let a; // // if (window.XMLHttpRequest) // a = new XMLHttpRequest(); // // else // // a = new ActiveXObject("Microsoft.XMLHTTP"); // a.onreadystatechange = function() // { // // console.log("--------------------"); // // console.log(a.readyState); // // console.log(a.responseText); // if (a.readyState == 4) // { // if(a.status == 200) // c = JSON.parse(a.responseText); // else if(a.status == 201) // c = a.responseText; // return c; // } // }; // if ((d == undefined) || (d == "")) // a.open("GET", "/get?" + b, false); // else // a.open("GET", "http://" + d + "/get?" + b, false); // a.send(); // delete a; // return c // } // function set_rest(b, c) { // let a; // // if (window.XMLHttpRequest) // a = new XMLHttpRequest(); // // else // // a = new ActiveXObject("Microsoft.XMLHTTP"); // if ((c == undefined) || (c == "")) // a.open("GET", "/set?" + b, false); // else // a.open("GET", "http://" + c + "/set?" + b, false); // a.send(); // delete a; // } function post_rest(b, c) { let a; // if (window.XMLHttpRequest) a = new XMLHttpRequest(); // else // a = new ActiveXObject("Microsoft.XMLHTTP"); if ((c == undefined) || (c == "")) a.open("POST", "/set?", false); else a.open("POST", "http://" + c + "/set?", false); a.send(b); delete a; } // let JLPckt; // function GetJLPckt() // { // JLPckt=get_rest1("Json={\"NetUdp\":{\"All\":0}}", _ip); // }; let FuncRetGetJNetUdpT; function GetJNetUdp(JAddGetParam, FuncRetGetJNetUdpT_) { /// JAddGetParam [{...}]; function FuncRetGET(d, J){ let b=true; while(b){ b=false; for(let i=0; i<J.NetUdp.Count-1; i++) if(J.NetUdp.Data[String(i)].IndexNet > J.NetUdp.Data[String(i+1)].IndexNet){ let T=J.NetUdp.Data[String(i)]; J.NetUdp.Data[String(i)]=J.NetUdp.Data[String(i+1)]; J.NetUdp.Data[String(i+1)]=T; b=true; } } if(FuncRetGetJNetUdpT) FuncRetGetJNetUdpT(J); }; // let J=get_rest("Json={\"NetUdp\":{\"All\":0}}", _ip); // JsonAdd(JAddGetParam, {"NetUdp":{"All":0}}); FuncRetGetJNetUdpT=FuncRetGetJNetUdpT_; if(!JAddGetParam) JAddGetParam=[]; JAddGetParam[JAddGetParam.length]={"NetUdp":{"All":0}}; // let J=[JAddGetParam]; // let JNetUdp1={"NetUdp":{"Count":1,"Data":{"0":{"IP":_ip, FuncRet:FuncRetGET}}}}; let JNetUdp1={"Count":1,"Data":{"0":{"IP":_ip, FuncRet:FuncRetGET}}}; WokerSend(JAddGetParam, JNetUdp1, undefined, "GET"); // let J = { // "NetUdp": { // "Count": 10, "Data": { // "0": {"ID": 1186570, "IndexNet": 5, "Name": "Name_1186570", "AquaName": "Aq0", "CloneName": "", "IP": "192.168.1.100", "TabLight": false, "TabTimer": false, "TabTemperature": false}, // "1": {"ID": 1186571, "IndexNet": 4, "Name": "Name_1186571", "AquaName": "Aq0", "CloneName": "", "IP": "192.168.1.101", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "2": {"ID": 1186572, "IndexNet": 3, "Name": "Name_1186572", "AquaName": "Aq1", "CloneName": "Cl1", "IP": "192.168.1.102", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "3": {"ID": 1186573, "IndexNet": 2, "Name": "Name_1186573", "AquaName": "Aq1", "CloneName": "Cl1", "IP": "192.168.1.103", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "4": {"ID": 1186574, "IndexNet": 1, "Name": "Name_1186574", "AquaName": "Aq1", "CloneName": "", "IP": "192.168.1.104", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "5": {"ID": 1186575, "IndexNet": 0, "Name": "Name_1186575", "AquaName": "Aq1", "CloneName": "", "IP": "192.168.1.105", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "6": {"ID": 1186572, "IndexNet": 3, "Name": "Name_1186576", "AquaName": "Aq1", "CloneName": "Cl2", "IP": "192.168.1.106", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "7": {"ID": 1186573, "IndexNet": 2, "Name": "Name_1186577", "AquaName": "Aq1", "CloneName": "Cl2", "IP": "192.168.1.107", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "8": {"ID": 1186572, "IndexNet": 3, "Name": "Name_1186576", "AquaName": "", "CloneName": "", "IP": "192.168.1.108", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // "9": {"ID": 1186573, "IndexNet": 2, "Name": "Name_1186577", "AquaName": "", "CloneName": "", "IP": "192.168.1.109", "TabLight": true, "TabTimer": true, "TabTemperature": true} // } // // // "Count": 2, "Data": { // // "0": {"ID": 1186572, "IndexNet": 3, "Name": "Name_1186576", "AquaName": "", "CloneName": "", "IP": "192.168.1.102", "TabLight": true, "TabTimer": true, "TabTemperature": true}, // // "1": {"ID": 1186573, "IndexNet": 2, "Name": "Name_1186577", "AquaName": "", "CloneName": "", "IP": "192.168.1.103", "TabLight": true, "TabTimer": true, "TabTemperature": true} // // } // } // } // J=J.NetUdp; } function WaitShow(bShowCloseBtn) { document.getElementById('idDivWait').style.display = "block"; if(bShowCloseBtn==false) document.getElementById("idWaitClose").style.display = "none"; else document.getElementById("idWaitClose").style.display = "block"; }; // function ShowAlert() // { // if(sFRetSend!=""){ // alert(sFRetSend); // sFRetSend=""; // } // }; function WaitHide() { document.getElementById('idDivWait').style.display = "none"; // if(sFRetSend!="") { // alert(sFRetSend); // sFRetSend = ""; // } // setTimeout(ShowAlert, 1); }; function WaitClose(bShow) { bBreakXMLResponse=PostCount; LockWokerSend=false; JJob={}; if(bShow==undefined || bShow) { ShowMainMenu('none'); WaitHide(); } } function ShowWait(F) { WaitShow(); // document.getElementById('idDivWait').style.display = "block"; // document.getElementById("BtnSend").value="Sending..."; // F(); setTimeout(F, 1); }; function DEC2Color(Value) { // console.log(Value); r="#" + pad(Value.toString(16).toUpperCase(),6); return r; } function onInputEditTime(E, sP) { function GetCaretPos(E) { if (document.selection) { let sel = document.selection.createRange(); // IE let clone = sel.duplicate(); // IE sel.collapse(true); clone.moveToElementText(E); clone.setEndPoint('EndToEnd', sel); return clone.text.length; } else if (E.selectionStart>=0) return E.selectionStart; return E.value.length - 1; } function SetSelectionRange(E, selectionStart, selectionEnd) { if (E.setSelectionRange) { E.focus(); E.setSelectionRange(selectionStart, selectionEnd); } else if (E.createTextRange) { let range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); } }; function SetCaretToPos (input, pos) { if(pos>=0) SetSelectionRange(input, pos, pos); }; function Replace(StrS, Index, StrT) { let S=StrS.substring(0, Index); S+=StrT; S+=StrS.substring(Index+StrT.length, StrS.length); return S; }; function IsDigit(s){ let r=parseInt(s); return (r==r); } // let CK=event.keyCode; // if(CK==37 || CK==39 || CK==16)// || CK==16) // return true; let p=GetCaretPos(E); E.value=E.value.substring(1,3); if(E.value.length>sP.length) E.value=E.value.substring(1,sP.length); // E.value.length=sP.length; SetCaretToPos(E, p); // if(p==sP.length && CK!=8) /// Key!=backspace // return false; // let SE=E.value.charAt(p);// substring(p, p+1); // let SK=String.fromCharCode(CK); /// char -> String // let CE = SE.charCodeAt(0); /// Sring -> char // if(p==E.value.length) { // let rr=sP.substring(p,p+1); // if (SK == " " || (CK==186 && sP.substring(p,p+1)==":") || (CK==190 && sP.substring(p,p+1)==".") )//!=8) /// Key!=backspace // p += 1; // else if(CK!=8) /// Key!=backspace // return false; // } // let p1=p-1; /// position of before not digital // for(;p1>=0; p1--) { // let s = sP.charAt(p1); //substring(p1, p1 + 1); // let c=s.charCodeAt(0); // if(c<48 || 57<c) // break; // } // p1++; // // let p2=p; /// position of next not digital // for(;p2<sP.length; p2++) { // let s = sP.charAt(p2); //substring(p2, p2 + 1); // let c=s.charCodeAt(0); // if(c<48 || 57<c) // break; // } // p2--; // // if(p>E.value.length){ // E.value+=sP.substring(E.value.length, p2+1); // SetCaretToPos(E, p); // return false; // } // // if(CK==46) { /// del // E.value=Replace(E.value, p, E.value.substring(p+1, p2+1)); // if(p<=p2) // E.value=Replace(E.value, p2, "0"); // SetCaretToPos(E, p); // return false; // } // if(CK==8) { /// backspace // if(p1<p) // E.value=Replace(E.value, p-1, E.value.substring(p, p2+1)); // if(p-1<=p2) // E.value=Replace(E.value, p2, "0"); // SetCaretToPos(E, p-1); // return false; // } // if((CE<48 || 57<CE) && (CK<48 || 57<CK) && (SK=" " || SK.trim()!="") ) { // SetCaretToPos(E, GetCaretPos(E)+1); // return false; // } // if(p==E.value.length) // return false; // if(CK<48 || 57<CK) /// edit isn't digital // return false; // if(CE<48 || 57<CE) /// key isn't digital // return false; // let sN=Replace(E.value, p, SK); // if(parseInt(sN.substring(p1,p2+1))<=parseInt(sP.substring(p1, p2+1))) // E.value=sN; // else // return false; // if(p<E.value.length-1){ // let CE1=E.value[p+1].charCodeAt(0); // if(CE1<48 || 57<CE1) // SetCaretToPos(E, p+2); // else // SetCaretToPos(E, p+1); // } return;// false; }; let EditTimeValue=""; let EditTimePos=-1; function GetCaretPos(E) { if (document.selection) { let sel = document.selection.createRange(); // IE let clone = sel.duplicate(); // IE sel.collapse(true); clone.moveToElementText(E); clone.setEndPoint('EndToEnd', sel); return clone.text.length; } else if (E.selectionStart>=0) return E.selectionStart; return E.value.length - 1; }; function SetSelectionRange(E, selectionStart, selectionEnd) { if (E.setSelectionRange) { E.focus(); E.setSelectionRange(selectionStart, selectionEnd); } else if (E.createTextRange) { let range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); } }; function SetCaretToPos (input, pos) { EditTimePos=pos; if(pos>=0) SetSelectionRange(input, pos, pos); }; function Replace(StrS, Index, StrT) { let S=StrS.substring(0, Index); S+=StrT; S+=StrS.substring(Index+StrT.length, StrS.length); return S; }; function IsDigit(s){ let r=parseInt(s); return (r==r); }; // function onKeyPressEditTime(E, sP) { // let r=event.keyCode; // }; function onKeyUpEditTime(E, sP) { /// !!! bug - in onKeyDown cann't disable key by return false - in mobile gadget (Chrome) not work /// therefore we use onkeydown for comp and on keyup for mobile gadget (Chrome) // return; if(E.value==EditTimeValue) return; EditTimePos=GetCaretPos(E); let Ev=E.value; if(Ev.length > sP.length) Ev=Ev.substring(0,sP.length); else if(Ev.length < sP.length){ let ssP=sP.substr(Ev.length-1, 1); let ssE=Ev.substr(Ev.length-1, 1); if(!IsDigit(ssP) && ssP!=ssE) Ev=Ev.substring(0,Ev.length-1); if(!IsDigit(ssP) && ssP==ssE){ let p2=Ev.length; for(; p2<sP.length; p2++) { let ssP = sP.substring(p2, p2 + 1); if (!IsDigit(ssP)) break; } Ev=Ev+sP.substring(Ev.length, p2); } } for(let i=0; i<Ev.length; i++){ let ssP=sP.substring(i,i+1); let ssE=Ev.substring(i,i+1); if(!IsDigit(ssP) && ssP!=ssE) { let a=Ev.substring(0, i); let b=EditTimeValue.substring(i + 1, Ev.length); Ev = a+ ssP + b; } if( IsDigit(ssP) && !IsDigit(ssE) ) { Ev = Ev.substring(0, i) + EditTimeValue.substring(i, i + 1) + EditTimeValue.substring(i + 1, Ev.length); EditTimePos--; } if( IsDigit(ssP) && !IsDigit(Ev.substring(i,i+1) ) ) { Ev = Ev.substring(0, i) + ssP + EditTimeValue.substring(i + 1, Ev.length); EditTimePos--; } } while (Ev.length<EditTimeValue.length) Ev+="0"; let p1, p2; for(p1=-1; p1<Ev.length; p1++) { // let S=Ev.substring(p1, p1 + 1); if( p1<0 || !IsDigit(Ev.substring(p1, p1 + 1)) ) { while ( p1 < Ev.length && !IsDigit(Ev.substring(p1, p1 + 1)) ) p1++; p2=p1; while( p2 < Ev.length && IsDigit(Ev.substring(p2, p2 + 1)) ) p2++; //S=Ev.substring(p1,p2); // console.log (S); VE=Ev.substring(p1,p2); VP=sP.substring(p1,p2); if(VE*1>VP*1) { let a=Ev.substring(0, p1); let b=Ev.substring(p2, Ev.length); Ev = a + VP + b; } } } E.value=Ev; // let CK=event.keyCode; // if(EditTimeValue!="") { // if(EditTimePos!=-1) // EditTimePos=GetCaretPos(E); // E.value = EditTimeValue; SetCaretToPos(E, EditTimePos); // } EditTimeValue=""; EditTimePos=-1; // EditTimeValue=E.value; }; function onKeyDownEditTime(E, sP, FEnter) { /// !!! bug - in onKeyDown cann't disable key by return false - in mobile gadget (Chrome) not work /// therefore we use onkeydown for comp and on keyup for mobile gadget (Chrome) EditTimeValue=E.value; // return true; // let CK=e.charCode;//event.keyCode; let CK=event.keyCode; if(CK==13 && FEnter) /// Key = Enter { FEnter(); return; } if(96<=CK && CK<=105) CK-=48; if(CK==37 || CK==39 || CK==16)// || CK==16) return true; EditTimeValue=E.value; let p=GetCaretPos(E); EditTimePos=p;//GetCaretPos(E); if(p==sP.length && CK!=8) /// Key!=backspace { // E.value = EditTimeValue; return false; } let SE=E.value.charAt(p);// substring(p, p+1); let SK=String.fromCharCode(CK); /// char -> String let CE = SE.charCodeAt(0); /// Sring -> char if(p==E.value.length) { if (SK == " " || (CK==186 && sP.substring(p,p+1)==":") || (CK==190 && sP.substring(p,p+1)==".") )//!=8) /// Key!=backspace p += 1; else if(CK!=8) /// Key!=backspace { // E.value = EditTimeValue; return false; } } let c57=57; let p1=p-1; /// position of before not digital for(;p1>=0; p1--) { let s = sP.charAt(p1); //substring(p1, p1 + 1); let c=s.charCodeAt(0); if(c<48 || c57<c) break; } p1++; let p2=p; /// position of next not digital for(;p2<sP.length; p2++) { let s = sP.charAt(p2); //substring(p2, p2 + 1); let c=s.charCodeAt(0); if(c<48 || c57<c) break; } p2--; if(p>E.value.length){ E.value+=sP.substring(E.value.length, p2+1); SetCaretToPos(E, p); // E.value = EditTimeValue; return false; } if(CK==46) { /// del E.value=Replace(E.value, p, E.value.substring(p+1, p2+1)); if(p<=p2) E.value=Replace(E.value, p2, "0"); SetCaretToPos(E, p); // E.value = EditTimeValue; return false; } if(CK==8) { /// backspace if(p1<p) E.value=Replace(E.value, p-1, E.value.substring(p, p2+1)); if(p-1<=p2) E.value=Replace(E.value, p2, "0"); SetCaretToPos(E, p-1); // E.value = EditTimeValue; return false; } if((CE<48 || c57<CE) && (CK<48 || c57<CK) && (SK=" " || SK.trim()!="") ) { SetCaretToPos(E, GetCaretPos(E)+1); // E.value = EditTimeValue; return false; } if(p==E.value.length){ // E.value = EditTimeValue; return false; } if(CK<48 || c57<CK) /// edit isn't digital { // event.preventDefault(); // event.stopPropagation(); // E.value = EditTimeValue; return false; } if(CE<48 || 57<CE) { /// key isn't digital // E.value = EditTimeValue; return false; } let sN=Replace(E.value, p, SK); let SP=sP.substring(p,p+1); // if(parseInt(SK)>parseInt(SP)) // return false; if(parseInt(sN.substring(p1,p2+1))<=parseInt(sP.substring(p1, p2+1))) E.value=sN; else{ E.value=E.value.substring(0, p1)+sP.substring(p1, p2+1)+E.value.substring(p2+1, E.value.length); SetCaretToPos(E, p+1); //E.value = EditTimeValue; return false; } if(p<E.value.length-1){ let CE1=E.value[p+1].charCodeAt(0); if(CE1<48 || c57<CE1) SetCaretToPos(E, p+2); else SetCaretToPos(E, p+1); } // E.value = EditTimeValue; EditTimeValue=E.value; return false; } // Array.prototype.unique = function() { // return this.filter(function(value, index, _this) {return _this.indexOf(value) === index;}); // } //массив должен быть отсортирован function ArrayDelRepet(A) { let result = []; for(let i = 0; i < A.length; i++) { if(A[i] == result[result.length-1]) continue; result.push(A[i]) } return result; } function pad(num, size) { let s = String(num); while (s.length < size) s = "0" + s; if(s.length > size) s=s.substring(s.length-size, s.length); return s; } function onkeyupM(obj, n) { if(n==undefined) n=2; obj.value=obj.value.replace(/[^0-9]+/g,''); if (obj.value>Number(obj.max)) obj.value=obj.max; if (obj.value<0) obj.value=0; obj.value=pad(obj.value, n) } function Reboot() { alert("Please wait when reboot finished"); post_rest("Json={\"Main\":{\"Reboot\":0}}", _ip); } function SaveConfigToFile() { post_rest("Json={\"Main\":{\"SaveConfig\":0}}", _ip); } //function SaveTimerToFile() //{ // post_rest("Json={\"Main\":{\"SaveTimer\":0}}", _ip); //} function GetElementByID(E, id) { let Es = E.getElementsByTagName("*"); for (let i = 0; i < Es.length; i++) if(Es.item(i).id==id) return Es.item(i); return undefined; }; function JsonAdd(JsonTo, JsonFrom){ /// Add JsonFrom to JsonTo. /// example JsonFrom = { "Blink": { "Count": 0 } } for (x in JsonFrom) { // console.log(x) if (typeof JsonFrom[x] == 'object') { // if (JsonFrom[x].length && JsonFrom[x].length == 2) /// Json is array // { // if (!JsonTo[x]) // JsonTo[x]=[]; // JsonTo[x][JsonFrom[x][0]] = JsonFrom[x][1] // } if (JsonFrom[x].length) /// Json is array { if (!JsonTo[x]) JsonTo[x]=[]; for(let p=0; p<JsonFrom[x].length; p++) JsonTo[x][p]=JsonFrom[x][p]; } else { if (!JsonTo[x]) JsonTo[x] = {}; JsonAdd(JsonTo[x], JsonFrom[x]) } } else JsonTo[x] = JsonFrom[x]; } } function JsonSet(JTo, JPath, V) /// set value V in JTo, by path in JPath. { for (x in JPath){ if (typeof JPath[x] == 'object') { /// array if(JPath[x].length){ if (!JTo[x]) JTo[x] = []; // console.log(JSON.stringify(JPath)); // console.log(JSON.stringify(JTo)); JTo[x][JPath[x][0]]=V; } // if(!JTo[x]) // JTo[x]=[]; // JTo[x][JPath[x][0]]=V; else{ if (!JTo[x]) JTo[x] = {}; JsonSet(JTo[x], JPath[x], V); } // else // Json[x] = V; // JsonSet(Json[x], V); } else JTo[x] = V; } // if (Json.length && Json.length == 1) // { // Json[1] = V; // } // else // for (x in Json) // if (typeof Json[x] == 'object') // JsonSet(Json[x], V); // else // Json[x] = V; } function ElementGetValue(E) /// E - element { if (E) { if (E.type == "checkbox"){ if(E.checked) return 1; else return 0; } // return Number(E.checked); // else if (E.tagName == "SELECT") // return E.selectedIndex; else if (E.tagName == "DIV") return E.innerHTML; else if (E.tagName == "INPUT" && E.type == "color") { return Number("0x"+((E.value.charAt(0) == "#") ? E.value.substring(1, 7) : E.value)); } else if (E.tagName == "INPUT" && E.id.indexOf("%")>0) return Number(E.value/100.);//"#" + Value.toString(16).toUpperCase(); else if(E.tagName == "INPUT" && E.type == "number") return Number(E.value); else //if (E.tagName == "INPUT") return E.value; } } function ElementSetValue(E, Value) /// E - element { //console.log("===============") //console.log(E.tagName); //console.log(E.type); //console.log("Value=" + Value) if (E && typeof (Value) != "undefined") { // console.log(E.id) if (E.tagName == "DIV" || E.tagName == "SPAN") E.innerHTML = Value; else if (E.type == "checkbox") E.checked = Value; // else if (E.tagName == "SELECT") // E.selectedIndex = Value; else if (E.tagName == "INPUT" && E.type == "color") { // console.log("Value="+Value) E.value = DEC2Color(Value);//"#" + Value.toString(16).toUpperCase(); // console.log("E.Value=" + E.Value) } else if (E.tagName == "INPUT" && E.id.indexOf("%")>0) { // console.log("Value="+Value) E.value = Value*100;//"#" + Value.toString(16).toUpperCase(); // console.log("E.Value=" + E.Value) } else //if (E.tagName == "INPUT") { if (E.className == "cHour" || E.className == "cMin" || E.className == "cSec") E.value = pad(Value, 2); else if (E.className == "cMSec") E.value = pad(Value, 3); else E.value = Value; } } } function GetKeyFromString(S) /// S - String /// return [Key0, Key1, ...] { let Key = []; let IndexMax = S.indexOf("{"); if(IndexMax<0) IndexMax = S.length; let Index = S.indexOf("@")+1; let iK = 0; let IndexT = Index; while(IndexT<IndexMax) { IndexT++; let S0 = S.substring(IndexT, IndexT + 1); if (S0 != "@" && S0!="{" && S0!="%") continue; if(IndexT>IndexMax) break; let S1 = S.substring(Index, IndexT); if(S1!="") { /// take place at "%" in id Key[iK] = S1; iK++; Index = IndexT + 1 } } return Key } function ElementsToJson(Doc, JTo, Key)//, bAddDIV) /// add elements with .id like this "@Key{@Main@:{@GPIO_SDA@:0}}" to JsonTo /// Key is Key in elements.id like this "@Key{@Main@:{@GPIO_SDA@:0}}" /// if(bAddDIV==true) then add DIV to JsonTo, else not add DIV to JsonTo /// return count of elements { //console.log(Key) let CountE = 0; let elements = Doc.getElementsByTagName("*"); for (i = 0; i < elements.length; i++) { let E = elements.item(i); if (E.id && (E.id && E.id.substring(0, 1) == "@")) { let KeyE = GetKeyFromString(E.id); // console.log(KeyE) if (KeyE.length > 1 && KeyE[1].length == 1 && Key[1] != KeyE[1]) continue; // console.log(1) //if (E.id == "@T{Index}") // continue if (KeyE[0] == Key[0]) { //if (!bAddDIV && E.tagName == "DIV") // continue let S = E.id.substring(E.id.indexOf("{"), E.id.length); if (S == "{Index}") continue; S = S.replace(new RegExp("@", 'g'), "\""); // console.log(S) let JPath = JSON.parse(S); let V=ElementGetValue(E); // if(E.id=="@T{@WDay@:[0]}") { // let aa = 45; // } // if(E.id=="@T{@WDay@:[1]}") { // let aa = 45; // } JsonSet(JTo, JPath, V); //console.log(JSON.stringify(JsonFrom)) // JsonAdd(JTo, JFrom); CountE++; } } } return CountE; } function JsonGet(JsonFrom, JsonE) /// find value of elements JsonE in big JsonFrom /// example JsonE = { "Blink": { "Count": 0 } } /// return value in Json { // console.log("+++++++++++++++") // console.log(JSON.stringify(JsonFrom)) // console.log(JSON.stringify(JsonE)) // console.log(JsonFrom) // console.log(JsonE) //console.log(typeof (JsonFrom)) //console.log(typeof JsonFrom[x]) //console.log(JsonFrom.length) //console.log(JsonE.length) // if(!JsonFrom || !JsonE) { // let aa = 0; // } // if (JsonFrom.length && JsonE.length) /// Json is array // return JsonFrom[JsonE[0]]; // for (x in JsonE) // { // // console.log("+") // if (typeof JsonFrom[x] == 'object' && typeof JsonE[x] == 'object') // return JsonGet(JsonFrom[x], JsonE[x]); // else // return JsonFrom[x]; // } // return JsonFrom if (JsonFrom.length && JsonE.length) /// Json is array return JsonFrom[JsonE[0]]; for (x in JsonE) { // console.log("+") if (typeof JsonFrom[x] == 'object' && typeof JsonE[x] == 'object') return JsonGet(JsonFrom[x], JsonE[x]); else return JsonFrom[x]; } return JsonFrom } function ElementsFromJson(Doc, JsonFrom, Index) /// get value of elements with .id like this "{@Main@:{@GPIO_SDA@:0}}" from Json and set in value /// Index - only need for table { // console.log("ElementsFromJson(Doc, JsonFrom, Index)") let elements = Doc.getElementsByTagName("*"); for (i = 0; i < elements.length; i++) { let E = elements.item(i); if (E.id && E.id.substring(0, 1) == "@") { //let KeyE = GetKeyFromString(E.id) //// console.log(KeyE) //if (KeyE.length > 1 && KeyE[1].length == 1 && KeyE[1] == "G") // continue; let S = E.id.substring(E.id.indexOf("{"), E.id.length); // console.log("S="+S) if (S == "{Index}") { // console.log("-----S == {Index})--------") ElementSetValue(E, Index); continue } S = S.replace(new RegExp("@", 'g'), "\""); let JsonE = JSON.parse(S); let V = JsonGet(JsonFrom, JsonE); // console.log(V) // console.log("-------------") ElementSetValue(E, V) } } } function GetElementsFromFaceByKey(Key) { let JsonTo = {}; Key[1]="S"; if (ElementsToJson(document, JsonTo, Key) < 1) return; // let sJ = "Json=" + JSON.stringify(JsonTo); //console.log(S) return JsonTo; // return JSON.stringify(JsonTo); //let JsonR = get_rest(sJ, _ip); //console.log("JsonR="+JSON.stringify(JsonR)) //let JsonR = JSON.parse("{\"Main\":{\"GPIO_SDA\":\"2\",\"GPIO_SCL\":\"3\"},\"PWM\":{\"Frequency\":\"2800\"},\"LTemperature\":{\"GPIO\":\"1\"},\"Blink\":{\"Enabled\":true,\"GPIO\":\"4\",\"TimeOn\":\"30\",\"TimeOff\":\"40\",\"Count\":\"6\",\"TimePause\":\"1500\"}}") // ElementsFromJson(document, JsonR) } function GetElementsFromFace() { return GetElementsFromFaceByKey(["R", "S"]) } //let sJAutoE; /// string of json function GetElementsFromFaceAuto() /// for auto refresh { // console.log("GetElementsFromServerAuto()") // if (!sJAutoE) // { let JsonTo = {}; if (ElementsToJson(document, JsonTo, ["A", "S"]) <1) return; return JsonTo; // let sJ = "Json=" + JSON.stringify(JTo); // // } // let JsonR = get_rest(sJ, _ip); // //let JsonR = JSON.parse("{\"Main\":{\"GPIO_SDA\":\"2\",\"GPIO_SCL\":\"3\"},\"PWM\":{\"Frequency\":\"2800\"},\"LTemperature\":{\"GPIO\":\"1\"},\"Blink\":{\"Enabled\":true,\"GPIO\":\"4\",\"TimeOn\":\"30\",\"TimeOff\":\"40\",\"Count\":\"6\",\"TimePause\":\"1500\"}}") // ElementsFromJson(document, JsonR) } function GetElementsToServer() { let JsonTo = {}; ElementsToJson(document, JsonTo, ["R"]);//, "G"]) //if (ElementsToJson(document, JsonTo, ["R", "G"]) > 0) { //let S = "Json=" + JSON.stringify(JsonTo); //console.log(S); //post_rest(S, _ip); //console.log(JSON.stringify(JsonTo)) //} return JsonTo; } function GetJsonObjectLast(Json) /// return last object. Json must have only 1 branch { for (x in Json) if (typeof Json[x] == 'object') { let r = GetJsonObjectLast(Json[x]); if (r == false) return Json[x]; else return r } else return false; return false } function TBodyToJson(TBody, Key) /// Key is Key in elements.id like this "@Key{@Main@:{@GPIO_SDA@:0}}" { let S = TBody.id.substring(TBody.id.indexOf("{"), TBody.id.length); S = S.replace(new RegExp("@", 'g'), "\""); let JsonTo = JSON.parse(S); let J = GetJsonObjectLast(JsonTo); J["Count"] = GetDynamicTBodyRowCount(TBody); //TBody.rows.length J["Data"] = {}; J = J["Data"]; let RC=GetDynamicTBodyRowCount(TBody); for (let iR = 0; iR < RC; iR++)// TBody.rows.length; iR++) { J[String(iR)] = {}; let JR = J[String(iR)]; let R = TBody.rows[iR]; // let elements = R.getElementsByTagName("*"); ElementsToJson(R, JR, Key) } return JsonTo } function GetTBodyToSever(idTBody)//, JNetUdpSend) /// send table to server { // if(JNetUdpSend==undefined) // JNetUdpSend=JNetUdpSelect; let TBody = document.getElementById(idTBody); let JsonTo = TBodyToJson(TBody, ["T","G"]); return JsonTo; // let S = "Json=" + JSON.stringify(JsonTo); // console.log(S); // post_rest(S);//, JNetUdpSend); //console.log(JSON.stringify(JsonTo)) }; function GetNameFromTableName(S) /// "{@LPWMChanelLED@:{}}" -> "LPWMChanelLED" { return S.substring(S.indexOf("@")+1, S.indexOf(":")-1); }; // function GetTBodyFromFace(idTBody)//, JsonFrom) // /// load table from server // ///-- return json from server JsonFrom // { // let TBody = document.getElementById(idTBody); // let S = TBody.id.substring(TBody.id.indexOf("{"), TBody.id.length); // S = S.replace(new RegExp("@", 'g'), "\""); // // if(!JsonFrom) { // let JsonTBodyGet = JSON.parse(S); // // console.log(JSON.stringify(JsonTBodyGet)); // let JGet = GetJsonObjectLast(JsonTBodyGet); // JGet["All"] = 0; // return JsonTBodyGet; // }; function SetTBodyFromJson(Key, JsonFrom, LockRowCount){ // let TBody = document.getElementById(idTBody); if(LockRowCount==undefined) LockRowCount=false; let TBody = document.getElementById("{@"+Key+"@:{}}"); let JsonTBody = {};//JSON.parse(S); JsonTBody[Key]={}; let J = JsonGet(JsonFrom, JsonTBody); let Count = J["Count"]; if(!LockRowCount) TBodyRowCountSet(TBody, Count); if (Count > 0) { J = J["Data"]; for (let iR = 0; iR < Math.min(Count, TBody.rows.length); iR++) { if (iR >= J.length) break; ElementsFromJson(TBody.rows[iR], J[String(iR)], iR) } } // JLPWMChanelTimer=JsonFrom["LPWMChanelTimer"] // TBody= document.getElementById(idIable); if (typeof MakeRow !== "undefined") for(let iR=0; iR< TBody.rows.length; iR++) MakeRow(TBody.rows[iR], iR, TBody.id);//, iR); // let SJ = "Json=" + JSON.stringify(JsonTBodyGet); // console.log(SJ); // // SJ="Json={\"LTimer\":{\"Count\":0, \"Data\":{\"All\":{\"Count\":0, \"LP\":0}}}}"; // // SJ="Json={\"LPWMChanel\":{\"Count\":0, \"Data\":{\"All\":{\"GPIO_PWM\":0, \"LP\":0}}}}"; // JsonFrom = get_rest(SJ, _ip); // // } // // let JsonFrom = JSON.parse("{\"LTimerChanel\":{\"Count\":3,\"Data\":{\"0\":{\"Name\":\"Name_0a\",\"GPIO\":3,\"Regime\":0,\"YE\":55,\"Dimension\":\"ml1\",\"ID\":0},\"1\":{\"Name\":\"Name_1\",\"GPIO\":2,\"Regime\":1,\"YE\":-1,\"Dimension\":\"ml\",\"ID\":1},\"2\":{\"Name\":\"Name_2\",\"GPIO\":3,\"Regime\":2,\"YE\":5,\"Dimension\":\"m2l\",\"ID\":2}}}}"); // //let JsonFrom = JSON.parse("{\"LTimer\":{\"Count\":3,\"Data\":{\"0\":{\"Enabled\":true,\"Name\":\"Name_01\",\"TimerChanelID\":-1,\"YE\":10,\"WDay\":[0,1,0,1,0,1,0],\"TimeStart\":{\"H\":1,\"M\":2,\"S\":3,\"MS\":4},\"IntervalOn\":{\"H\":5,\"M\":6,\"S\":7,\"MS\":8},\"IntervalOff\":{\"H\":9,\"M\":10,\"S\":11,\"MS\":12},\"Count\":13},\"1\":{\"Enabled\":false,\"Name\":\"Name_11\",\"TimerChanelID\":1,\"YE\":22,\"WDay\":[1,0,0,1,0,0,1],\"TimeStart\":{\"H\":21,\"M\":22,\"S\":23,\"MS\":24},\"IntervalOn\":{\"H\":20,\"M\":26,\"S\":27,\"MS\":28},\"IntervalOff\":{\"H\":19,\"M\":30,\"S\":31,\"MS\":32},\"Count\":33},\"2\":{\"Enabled\":true,\"Name\":\"Name_2s\",\"TimerChanelID\":2,\"YE\":41,\"WDay\":[0,0,1,1,0,0,0],\"TimeStart\":{\"H\":12,\"M\":43,\"S\":44,\"MS\":45},\"IntervalOn\":{\"H\":16,\"M\":47,\"S\":48,\"MS\":49},\"IntervalOff\":{\"H\":11,\"M\":50,\"S\":51,\"MS\":555},\"Count\":60}}}}"); // console.log("JsonFrom="+JSON.stringify(JsonFrom)); // // console.log(S) // let JsonTBody = JSON.parse(S); // let J = JsonGet(JsonFrom, JsonTBody); // // console.log(JSON.stringify(J)) // let Count = J["Count"]; // // console.log("Count="+Count) // TBodyRowCountSet(TBody, Count); // // console.log(1) // if (Count > 0) // { // // console.log("+") // J = J["Data"]; // for (let iR = 0; iR < TBody.rows.length; iR++) // { // if (iR >= J.length) // break; // // console.log("iR=" + iR) // ElementsFromJson(TBody.rows[iR], J[String(iR)], iR) // } // } // return JsonFrom; // // console.log(2) }; // let sJAutoT; /// string of json TBody function GetTBodyFromServerAuto(idTBody) /// load table from server { let TBody = document.getElementById(idTBody); // if (!sJAutoT) // { let JTo=TBodyToJson(TBody, ["AT", "S"]); // ElementsToJson(document, JTo, "A", true) // sJAuto = "Json=" + JSON.stringify(JTo) // } // console.log("sJAuto=" + sJAuto); return JTo; // let JsonFrom = get_rest(sJAuto, _ip); // // let S = TBody.id.substring(TBody.id.indexOf("{"), TBody.id.length); // S = S.replace(new RegExp("@", 'g'), "\""); // // // let JsonFrom = JSON.parse("{\"LTimerChanel\":{\"Count\":3,\"Data\":{\"0\":{\"Name\":\"Name_0a\",\"GPIO\":3,\"Regime\":0,\"YE\":55,\"Dimension\":\"ml1\",\"ID\":0},\"1\":{\"Name\":\"Name_1\",\"GPIO\":2,\"Regime\":1,\"YE\":-1,\"Dimension\":\"ml\",\"ID\":1},\"2\":{\"Name\":\"Name_2\",\"GPIO\":3,\"Regime\":2,\"YE\":5,\"Dimension\":\"m2l\",\"ID\":2}}}}"); // //let JsonFrom = JSON.parse("{\"LTimer\":{\"Count\":3,\"Data\":{\"0\":{\"Enabled\":true,\"Name\":\"Name_01\",\"TimerChanelID\":-1,\"YE\":10,\"WDay\":[0,1,0,1,0,1,0],\"TimeStart\":{\"H\":1,\"M\":2,\"S\":3,\"MS\":4},\"IntervalOn\":{\"H\":5,\"M\":6,\"S\":7,\"MS\":8},\"IntervalOff\":{\"H\":9,\"M\":10,\"S\":11,\"MS\":12},\"Count\":13},\"1\":{\"Enabled\":false,\"Name\":\"Name_11\",\"TimerChanelID\":1,\"YE\":22,\"WDay\":[1,0,0,1,0,0,1],\"TimeStart\":{\"H\":21,\"M\":22,\"S\":23,\"MS\":24},\"IntervalOn\":{\"H\":20,\"M\":26,\"S\":27,\"MS\":28},\"IntervalOff\":{\"H\":19,\"M\":30,\"S\":31,\"MS\":32},\"Count\":33},\"2\":{\"Enabled\":true,\"Name\":\"Name_2s\",\"TimerChanelID\":2,\"YE\":41,\"WDay\":[0,0,1,1,0,0,0],\"TimeStart\":{\"H\":12,\"M\":43,\"S\":44,\"MS\":45},\"IntervalOn\":{\"H\":16,\"M\":47,\"S\":48,\"MS\":49},\"IntervalOff\":{\"H\":11,\"M\":50,\"S\":51,\"MS\":555},\"Count\":60}}}}"); // //console.log("JsonFrom="+JSON.stringify(JsonFrom)) // // console.log(S) // let JsonTBody = JSON.parse(S); // let J = JsonGet(JsonFrom, JsonTBody); // // console.log(JSON.stringify(J)) // let Count = J["Count"]; // // console.log("Count=" + Count) // TBodyRowCountSet(TBody, Count); // // console.log(1) // if (Count > 0) { // // console.log("+") // J = J["Data"]; // for (let iR = 0; iR < TBody.rows.length; iR++) // { // if (iR >= J.length) // break; // // console.log("iR=" + iR) // ElementsFromJson(TBody.rows[iR], J[String(iR)], iR) // } // } // // console.log(2) } //var Dv=0; let ns = 'http://www.w3.org/2000/svg'; //let ButtonColor = '#303030'; bLightEdit=true; function sleep2(delay) { var start = new Date().getTime(); while (new Date().getTime() < start + delay); }; function SetNode(Node, JParam) { for (let p in JParam) { // console.log(p) // console.log(JParam[p]) // if(JParam[p]!=JParam[p]) { // console.log(JParam[p]); // console.log("Error"); // } Node.setAttributeNS(null, p, JParam[p]); } }; function GetNode(Parent, NodeType, JParam) { Node = document.createElementNS(ns, NodeType); SetNode(Node, JParam); if(Parent) Parent.appendChild(Node); return Node; }; function SetVisible(Element, Visible) { if (Visible) Element.style.display = 'inline'; else Element.style.display='none'; }; function ValueUnit2Text(Value, Unit, K){ if(Unit=="%") return Unit+" "+Value.toFixed(Log10(K) + 1); if(Unit=="Lm") return Unit+" "+Value.toFixed(1); if(Unit=="Lux") return Unit+" "+Value.toFixed(0); if(Unit=="W") return Unit+" "+Value.toFixed(1); return T; }; function SvgToCoordinate(svg, x, y) { let m = svg.getScreenCTM(); let p = svg.createSVGPoint(); p.x = x;//event.pageX; p.y = y;//event.pageY; p = p.matrixTransform(m); return {x: Number(p.x), y: Number(p.y)}; }; // GetSvgCoordinates = function (svg, event) { // let m = svg.getScreenCTM(); // let p = svg.createSVGPoint(); // p.x = event.pageX; // p.y = event.pageY; // p = p.matrixTransform(m.inverse()); // return {x: Number(p.x), y: Number(p.y)}; // }; function Log10(V) { // return Math.log10(V); return Math.log(V) / Math.LN10; }; var DaySec=24 * 3600; function Time2Sec(sTime){ /// "12:34" || "23:03:45" let i=0; let sgn=1; // console.log(sTime); if(sTime.substring(0,1)=='-'){ i=1; sgn=-1; } let p=sTime.indexOf(':'); let h=sTime.substring(i,p); let sTime0=sTime.substring(p+1,sTime.length); p=sTime0.indexOf(':'); if(p<0) p=sTime0.length; let m=sTime0.substring(0,p); let s=sTime0.substring(p+1,sTime0.length); return sgn*(h*3600+m*60+1*s); }; function Time2MSec(sTime){ /// "12:34" || "23:03:45" || "23:03:45.678" let i=0; let sgn=1; // console.log(sTime); if(sTime.substring(0,1)=='-'){ i=1; sgn=-1; } let p=sTime.indexOf(':'); let h=sTime.substring(i,p); let sTime0=sTime.substring(p+1,sTime.length); p=sTime0.indexOf(':'); if(p<0) p=sTime0.length; let m=sTime0.substring(0,p); sTime0=sTime0.substring(p+1,sTime.length); p=sTime0.indexOf('.'); if(p<0) p=sTime0.length; let s=sTime0.substring(0,p); let ms=sTime0.substring(p+1,sTime0.length); return sgn*(h*3600000+m*60000+s*1000+1*ms); }; function MButton(JParam) { let th = this;//MChartBar.prototype; th.ButtonDownColor = '#007000'; if(JParam.ButtonDownColor) th.ButtonDownColor=JParam.ButtonDownColor; th.Value = JParam.Value; th.Index = JParam.Index; th.Enabled=true; th.bDown = false; // let ns = 'http://www.w3.org/2000/svg'; th.Fon = GetNode(JParam.svg, "rect", {width: JParam.W, height: JParam.H, rx:5, ry:5, x:JParam.L, y:JParam.T, fill: '#303030', stroke:'#BBBBBB', 'stroke-width': 2}); // JParam.svg.appendChild(B); // th.Text = GetNode(null,"text", {x:JParam.L + 5, y:JParam.T + 15, 'font-size': 12, // fill: '#DDDDDD', 'text-anchor': 'middle'}); th.Text = GetNode(null,"text", {x:JParam.L+JParam.W/2, y:JParam.T+JParam.H/2, 'font-size': 12, fill: '#DDDDDD', 'text-anchor': 'middle', 'dominant-baseline': 'middle'}); th.Text.textContent = JParam.Text; JParam.svg.appendChild(th.Text); th.onBeforeDown = JParam.onBeforeDown; addEventListener('mousedown', mousedown, false); th.Down = function (bDown_, bNeedCall) { // console.log(Sender2); if(bNeedCall==undefined) bNeedCall=true; if (bNeedCall && th.onBeforeDown) if(!th.onBeforeDown(th, bDown_))//, Sender2)) return; th.bDown = bDown_; if (th.bDown) th.Fon.setAttributeNS(null, 'fill', th.ButtonDownColor); else th.Fon.setAttributeNS(null, 'fill', '#303030'); }; th.Show=function() { th.Fon.style.display='block'; th.Text.style.display='block'; th.Text.style.display='block'; }; th.Hide=function() { th.Fon.style.display='none'; th.Text.style.display='none'; }; function mousedown(event) { //function mousedown(event) { if(!th.Enabled) return; selectedElement = event.target; // console.log(selectedElement) // console.log(Text) if (selectedElement == th.Text || selectedElement == th.Fon)// && th.onDown) { th.Down(!th.bDown); // } } th.Enable=function (bEnabled){ th.Enabled=bEnabled; if(th.Enabled) th.Text.style.cursor = 'pointer'; //'default'; else th.Text.style.cursor = 'default'; //'default'; th.Fon.style.cursor=th.Text.style.cursor; }; th.Enable(th.Enabled); } function GetJCount(J){ let Count=0; for(p in J) Count++; return Count; } // var Req = new Worker('Request.js'); // let bRLock=false; // Req.addEventListener('message', function(e) { // console.log(bRLock = false); // bRLock = false; // // document.getElementById('result').textContent = e.data; // }, false); let JSendPWM; let bNewDataExist=false; function SendPWMValue(J){ /// J=[[iPwm,Value]�] if(LockWokerSend) return; function FuncRet(){ // console.log("auto"); if(bNewDataExist) /// while we sent data, we get new data SendPWM(); JSendPWM = undefined; }; function SendPWM(){ if(JSendPWM) { // console.log("SendPWM"); let J = []; J[0] = JSendPWM; // console.log(JSON.stringify(J[0])); bNewDataExist = false; WokerSend(J, JNetUdpDSelect, FuncRet, "POST"); } }; for(p in J.Data){ /// else example: at JSON.stringify(...) = ... 6.777777777E-7 not correct translate to float in ESP if(0<J.Data[p].VManual.V && J.Data[p].VManual.V<0.000001) J.Data[p].VManual.V=0; } bNewDataExist=true; let dT=new Date().getTime()-TimeWokerLastSend; let b=(!JSendPWM || dT>dTimeWokerLastSendMax); // console.log("b="+b); JSendPWM= {LPWMChanelLED: J}; JSendPWM.LPWMChanelLED.Group=1; // console.log("+++"+JSON.stringify(JSendPWM)); if(b){ /// send data now // console.log("Manual"); SendPWM(); } // else /// now sending data, will send late // console.log("--------------"); // let J=[]; // J[0]=JSendPWM; // WokerSend(J, JNetUdpDSelect, FuncRet); // JSendPWM=undefined; // console.log("SendPWMValue():"+JSON.stringify(J)); //SendJSONToServer({"LPWMChanel":J}); // console.log("PostPWM"); //+ post_rest("Json="+JSON.stringify({LPWMChanelLED:J}), _ip); // let RType = false; // if (window.XMLHttpRequest) // RType = true; //+ Req.postMessage({sJson: "Json=" + JSON.stringify(J), ip: _ip);//, type: RType}); } function GetJPWMParam() { // JFrom = get_rest("{\"LPWMChanel\":{\"All\":0}}", _ip); // return JFrom.LPWMChanel; return JLightAll["LPWMChanelLED"]; // return GetJSONFromServer({"LPWMChanelLED":{"All":0}}); // return GetJSONFromServer({"LPWMChanel":{"All":0}}); } function PathAdd(path){ return function(){ if (xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ if(path.lastIndexOf('/') < 1) path = '/'; else path = path.substring(0, path.lastIndexOf('/')); } else alert("ERROR["+xmlHttp.status+"]: "+xmlHttp.responseText); } } } function HttpDelete(filename){ xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = PathAdd(filename); var formData = new FormData(); formData.append("path", filename); if(_ip=="") xmlHttp.open("DELETE", "/edit"); else xmlHttp.open("DELETE", "http://" + _ip +"/edit"); xmlHttp.send(formData); delete xmlHttp; } function HttpLoad (filename) { let oRequest = new XMLHttpRequest(); // console.log("5"); if(_ip=="") oRequest.open('GET', filename, false); else oRequest.open('GET', "http://" + _ip +"/"+filename, false); // console.log("6"); //oRequest.setRequestHeader("User-Agent", navigator.userAgent); let s=""; try { oRequest.send(null); // console.log("7"); s = oRequest.responseText; } catch(e){}; // console.log("8"); delete oRequest; return s; } // function GetTemperatureParam(){ // return JLTemper;//JLightAll["LTemperature"]; // // let JT=GetJSONFromServer({"LTemperature":{"All":0}}); // // console.log(JSON.stringify(JT)); // // return JT; // // return { // // "Count": 7, "Data": { // // "0": {"Name": "NameT3", "Color": 0xFF5555, "Ch": 0}, // // "1": {"Name": "NameT2", "Color": 0x55FF55, "Ch": 2}, // // "2": {"Name": "NameT1", "Color": 0xFF00FF, "Ch": 1}, // // "3": {"Name": "NameT5", "Color": 0xFFFF00, "Ch": 5} // // } // // } // } // let DIG="0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZabcde"; // function Atoi(s, p) /// int Atoi(char *s, int p) // { // let i, a;//, digit[256] = {0}; // let digit=""; // for(i = 0; DIG[i]; i++) // digit[DIG[i]] = i; // for(i = a = 0; s[i]; i++) // a = a * p + digit[s[i]]; // return a; // }; // function GetTemperatureTime() { // return JTemperTime; //let JT= GetJSONFromServer({"LTemperature":{"Data":{"All":{"Ch":0, "LT":0}}}}); //return JT; // return { // '0': {"Ch": 0, LP: [['02:23', 51.5], ['04:23', 40.2], ['05:23', 55.2], ['06:23', 50.2], ['11:23', 55.4]]}, // '1': {"Ch": 2, LP: [['12:23', 49.5], ['14:23', 19.2], ['16:23', 49.2], ['21:23', 51.4]]}, // '2': {"Ch": 1, LP: [['03:23', 49.5], ['16:23', 1.2], ['23:23', 50.4]]} // }; // // return GetJSONFromServer({"CTmpr":{}}); // }; function HttpUpload(filename, file){ xmlHttp = new XMLHttpRequest(); //++ xmlHttp.onreadystatechange = PathAdd(filename); let formData = new FormData(); formData.append("path", filename); formData.append('name', file.name); formData.append('type', file.type); formData.append('size', file.size); formData.append('file', file); //console.log("1"); if(_ip=="") xmlHttp.open("POST", "/edit"); else xmlHttp.open("POST", "http://" + _ip +"/edit"); // console.log("1"); xmlHttp.send(formData); // console.log("1"); delete formData; }; // let JGalleryCBar="[{\"Name\": \"Name0\", \"Data\": {\"2\": 1, \"1\": 1, \"3\": 0.002636, \"7\": 0.001636, \"4\": 0.00663}}, \ // {\"Name\": \"Name4\", \"Data\": {\"2\": 0.005, \"1\": 0.7636, \"3\": 0.2636, \"7\": 0.001636, \"4\": 0.00663}}]"; // { \"Name\": \"Name1\", \"Data\": { \"3\": [[\"02:23\", 0.8], [\"05:23\", 0.14], [\"14:03\", 0.142], [\"21:03\", 0.13]],\ // \"2\": [[\"02:23\", 0.37], [\"04:23\", 0.42], [\"06:23\", 0.4], [\"14:23\", 0.52], [\"21:23\", 0.23]],\ // \"8\": [[\"02:23\", 0.47], [\"04:23\", 0.39], [\"06:23\", 0.8], [\"14:23\", 0.24], [\"21:23\", 0.3]],\ // \"4\": [[\"02:43\", 0.57], [\"03:43\", 0.52], [\"05:43\", 0.7], [\"08:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]\ // }},\ // let JGalleryCTime="[ \ // { \"Name\": \"Name0\", \"Data\": \ // {\ // \"3\": [[\"01:23\", 0.7], [\"04:23\", 0.14], [\"09:03\", 0.142], [\"21:03\", 0.13]],\ // \"2\": [[\"01:23\", 0.77], [\"04:23\", 0.22], [\"05:23\", 0.4], [\"09:23\", 0.52], [\"21:23\", 0.23]],\ // \"1\": [[\"01:23\", 0.87], [\"04:23\", 0.29], [\"05:23\", 0.8], [\"09:23\", 0.24], [\"21:23\", 0.3]],\ // \"4\": [[\"01:43\", 0.77], [\"02:43\", 0.32], [\"04:43\", 0.7], [\"05:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]\ // }\ // },\ // {\"Name\": \"Name2\", \"Data\": \ // {\ // \"3\": [[\"03:23\", 0.9], [\"07:23\", 0.14], [\"19:03\", 0.142], [\"21:03\", 0.13]],\ // \"2\": [[\"03:23\", 0.17], [\"04:23\", 0.62], [\"08:23\", 0.4], [\"19:23\", 0.52], [\"21:23\", 0.23]],\ // \"8\": [[\"03:23\", 0.27], [\"04:23\", 0.79], [\"08:23\", 0.8], [\"19:23\", 0.24], [\"21:23\", 0.3]],\ // \"4\": [[\"03:43\", 0.37], [\"04:13\", 0.82], [\"4:43\", 0.7], [\"05:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]\ // }\ // }\ // ]"; // JFrom= {"Data": { // '0': {PWM:3, LP:[['01:23', 0.7], ['04:23', 0.14], ['09:03', 0.142], ['21:03', 0.13]]}, // '1': {PWM:2, LP:[['01:23', 0.37], ['04:23', 0.22], ['05:23', 0.4], ['09:23', 0.52], ['21:23', 0.23]]}, // '2': {PWM:8, LP:[['01:23', 0.47], ['04:23', 0.29], ['05:23', 0.8], ['09:23', 0.24], ['21:23', 0.3]]}, // '3': {PWM:4, LP:[['01:43', 0.57], ['02:43', 0.32], ['04:43', 0.7], ['05:43', 0.34], ['09:43', 0.342], ['21:43', 0.73]]} // }}; // let JGalleryCTime="[ \ // { \"Name\": \"Name0\", \"Data\": \ // {\ // \"0\": {\"PWM\":3, \"LP\":[[\"01:23\", 0.7], [\"04:23\", 0.14], [\"09:03\", 0.142], [\"21:03\", 0.13]]},\ // \"1\": {\"PWM\":2, \"LP\":[[\"01:23\", 0.77], [\"04:23\", 0.22], [\"05:23\", 0.4], [\"09:23\", 0.52], [\"21:23\", 0.23]]},\ // \"2\": {\"PWM\":1, \"LP\":[[\"01:23\", 0.87], [\"04:23\", 0.29], [\"05:23\", 0.8], [\"09:23\", 0.24], [\"21:23\", 0.3]]},\ // \"3\": {\"PWM\":5, \"LP\":[[\"01:43\", 0.77], [\"02:43\", 0.32], [\"04:43\", 0.7], [\"05:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]}\ // }\ // },\ // {\"Name\": \"Name2\", \"Data\": \ // {\ // \"0\": {\"PWM\":3, \"LP\":[[\"03:23\", 0.9], [\"07:23\", 0.14], [\"19:03\", 0.142], [\"21:03\", 0.13]]},\ // \"1\": {\"PWM\":2, \"LP\":[[\"03:23\", 0.17], [\"04:23\", 0.62], [\"08:23\", 0.4], [\"19:23\", 0.52], [\"21:23\", 0.23]]},\ // \"2\": {\"PWM\":8, \"LP\":[[\"03:23\", 0.27], [\"04:23\", 0.79], [\"08:23\", 0.8], [\"19:23\", 0.24], [\"21:23\", 0.3]]},\ // \"3\": {\"PWM\":4, \"LP\":[[\"03:43\", 0.37], [\"04:13\", 0.82], [\"4:43\", 0.7], [\"05:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]}\ // }\ // }\ // ]"; // let JGalleryCTime="[ \ // { \"Name\": \"Name0\", \"Data\": \ // {\ // \"3\": {\"PWM\":2, LP:[[\"01:43\", 0.77], [\"02:43\", 0.32], [\"04:43\", 0.7], [\"05:43\", 0.34], [\"09:43\", 0.342], [\"21:43\", 0.73]]}\ // }\ // }\ // ]"; function GetJSONFromFile(FileName){ // let SJ; // if(FileName=="GalleryCBar.txt") { /// it is emulate need del // SJ = JGalleryCBar; // } // else if(FileName=="GalleryCTime.txt"){ // SJ=JGalleryCTime; // } // let J0=JSON.parse(SJ); // return J0; ///-- todo download file to SJ let SJ=HttpLoad(FileName); // console.log("4"); if(SJ=="") SJ="[]"; let J0=[]; try { J0 = JSON.parse(SJ); } catch(e){}; return J0; }; function SaveJSONToFile(FileName, J){ // let SJ = JSON.stringify(J); // if(FileName=="GalleryCBar.txt") { /// it is emulate need del // JGalleryCBar=SJ; // } // else if(FileName=="GalleryCTime.txt"){ // JGalleryCTime=SJ; // } ///--- todo SJ to file and upload it let SJ = JSON.stringify(J, null, '\t'); var file = new File([SJ], FileName); HttpUpload(FileName, file); }; //var bLoaded=false; // function SendJSONToServer(JTo) { // if(bLightEdit)//bLoaded) // for(p in JTo){ // if(p=="PWMValue") // console.log("PWMValue="+JSON.stringify(JTo)); // } // post_rest("Json={\"LTemperature\":{\"ScanSensor\":0}}", _ip); // }; let DIG="0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; let it0;//, digit[256] = {0}; let Digit=[]; for(it0 = 0; it0<DIG.length; it0++) Digit[DIG[it0].charCodeAt(0)] = it0; function Atoi(S, p) /// int Atoi(char *s, int p) { let i, a; for(i = a = 0; i<S.length; i++) a = a * p + Digit[S[i].charCodeAt(0)]; return a; }; function UnpackTemper(JTo){ for (p in JTo) /// it is emulate need del { // JFrom = get_rest(SJ, _ip); JFrom = JTo[p]; let b=false; if(p=="LTemperature") {// && JFrom.Data && JFrom.Data[0] && JFrom.Data[0].LT) { for (t in JFrom.Data) if (JFrom.Data[t].LT) { b = true; let S = JFrom.Data[t].LT; JFrom.Data[t].LP = []; let C = S.length / 3; dSec = 24 * 3600 / C; for (let c = 0; c < C; c++) { let V = (Atoi(S.substring(c * 3, (c + 1) * 3), 41) - 32768) / 100; if (-100 < V) { let Sec = c * dSec; let H = Math.floor(Sec / 3600); let M = (Sec - H * 3600) / 60; JFrom.Data[t].LP[JFrom.Data[t].LP.length] = [H + ":" + M, V]; } } JFrom.Data[t].LT = ""; // JFrom= { // '0': {"Ch": 0, LP: [['02:23', 51.5], ['04:23', 40.2], ['05:23', 55.2], ['06:23', 50.2], ['11:23', 55.4]]}, // '1': {"Ch": 2, LP: [['12:23', 49.5], ['14:23', 19.2], ['16:23', 49.2], ['21:23', 51.4]]}, // '2': {"Ch": 1, LP: [['03:23', 49.5], ['16:23', 1.2], ['23:23', 50.4]]} // }; p = p; } if(b) return JFrom.Data; } else continue; } return {}; } // function GetJSONFromServer(JTo) { // let SJ = "Json=" + JSON.stringify(JTo); // console.log(SJ); // let JFrom; // //JFrom = get_rest(SJ, _ip); // // console.log("JFrom="+JFrom); // for (p in JTo) /// it is emulate need del // { // JFrom = get_rest(SJ, _ip); // JFrom = JFrom[p]; // let b=false; // if(p=="LTemperature")// && JFrom.Data && JFrom.Data[0] && JFrom.Data[0].LT) { // for(t in JFrom.Data) // if(JFrom.Data[t].LT) // { // b=true; // let S=JFrom.Data[t].LT; // JFrom.Data[t].LP=[]; // let C=S.length/3; // dSec=24*3600/C; // for(let c=0; c<C; c++) { // let V=(Atoi(S.substring(c*3, (c+1)*3), 41)-32768)/100; // if(-100<V) { // let Sec = c * dSec; // let H = Math.floor(Sec / 3600); // let M = (Sec - H * 3600) / 60; // JFrom.Data[t].LP[JFrom.Data[t].LP.length] = [H + ":" + M, V]; // } // } // JFrom.Data[t].LT=""; // // JFrom= { // // '0': {"Ch": 0, LP: [['02:23', 51.5], ['04:23', 40.2], ['05:23', 55.2], ['06:23', 50.2], ['11:23', 55.4]]}, // // '1': {"Ch": 2, LP: [['12:23', 49.5], ['14:23', 19.2], ['16:23', 49.2], ['21:23', 51.4]]}, // // '2': {"Ch": 1, LP: [['03:23', 49.5], ['16:23', 1.2], ['23:23', 50.4]]} // // }; // p=p; // } // if(b) // JFrom=JFrom.Data; // } // // console.log("JFrom="+ JSON.stringify(JFrom)); // return JFrom; // }; //получение cookie по имени function GetCookie(name) { return null; /// cockies is break; // получение cookie по заданному смещению function GetCookieOffset (offset) { var strEnd = document.cookie.indexOf (";", offset); if (strEnd == -1) strEnd = document.cookie.length; return unescape(document.cookie.substring(offset, strEnd)); } var key = name + "="; var i = 0; while (i < document.cookie.length) { var j = i + key.length; if (document.cookie.substring(i, j) == key) return GetCookieOffset (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return undefined; } // установка cookie по имени function SetCookie (name, value) { return; var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; //var expires = (argc > 2) ? argv[2] : null; var expires = new Date(); expires.setTime(expires.getTime() + (30 * 24 * 60 * 60 * 1000)); var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path));// + // ((domain == null) ? "" : ("; domain=" + domain)) + // ((secure == true) ? "; secure" : ""); } // удаление одного cookie function DeleteCookie(strName) { document.cookie = strName + "=0; expires=" + (new Date(0)).toGMTString(); } ///---------net menu ---------------- let ulMenu; let AliMenu=[]; let JNetUdp; /// all esp /// ={NetUdp:{Data:{0:{IP:_, Name:_, ...:..., Worker:_}}}} let JNetUdpDSelect; /// only selected clone esp (The data send all of them)/// {Data:{0:{IP:_, Worker:_}}} var JNetUdpDShow; /// only 1 esp (The data get from this esp) /// need var for FrameMenu; ={Data:{0:{IP:_, Name:_, Tab, ...:..., Worker:_}}} // var tmpCookie = "testCookieForCheck"; // SetCookie(tmpCookie, "122"); let LockShowPage=false; function ShowPage(){ if(LockShowPage) return; _ip=JNetUdpDSelect.Data[0].IP; ShowWait(FRefresh); }; // let iMenuLock=0; function ShowMainMenu(st){ // console.log("call ShowMainMenu"); let f=document.getElementById("idMenuTab"); //idMenuMainTab"); let w=document.getElementById("idDivWait"); let wH=document.getElementById("idDivWaitH"); if(st=="none" && f.style.display != "block") return; //f.setAttribute('hidden', "false"); // f.hidden=false; // if(f.src=="") // f.src="menu.htm"; if(st) f.style.display =st; else if (f.style.display == "block") st = "none"; else st = "block"; let stH=""; if(st=="block") stH="none"; f.style.display=st; // f.style.position="fixed";//st; let gg=window.innerHeight-30;//document.documentElement.clientHeight-30;//document.body.height; f.style.height= gg+"px";//st; f.style.overflow= "auto";//st; // f.style.display="inline-block";//st; w.style.display=st; wH.style.display=stH; // nFrameMenu.aa=789; //nFrameMenu.sss(521); }; function MakeMenuNet(E, sKeyTab, FuncRetMakeMenuNet, JAddGetParam) { // return; /// sKeyTab="TabTemperature", "TabTimer", "TabLight" /// JAddGetParam additinal param for get from server JAddGetParam [{...}]; function FuncRetGetJNetUdp(J) { JNetUdp=J; let JNetUdpD = JNetUdp.NetUdp.Data; ulMenu = document.createElement('ul'); ulMenu.className = "MenuNet"; E.appendChild(ulMenu); ///button - main menu if (!parent.parent.FrameMain) { let M = document.createElement('li'); M.style.width = "30px"; ulMenu.appendChild(M); let a = document.createElement('a'); a.className = "aMainMenu"; a.innerHTML = "."; a.setAttribute('onclick', "ShowMainMenu()"); M.appendChild(a); let w = document.getElementById("idDivWait"); w.setAttribute('onclick', "ShowMainMenu('none')"); } if(ShowAqua=="" && ShowName=="") for (p in JNetUdpD) if (!sKeyTab || JNetUdpD[p][sKeyTab]) { for (p in JNetUdpD) if(JNetUdpD[p].IP==JNetUdp.IP) { ShowAqua=JNetUdpD[p].AquaName; ShowName=JNetUdpD[p].Name; break; } } let LA = []; let i; for (p in JNetUdpD) if (!sKeyTab || JNetUdpD[p][sKeyTab]) { for (i = 0; i < LA.length; i++) if (JNetUdpD[p].AquaName == LA[i]) break; if (i == LA.length) LA[LA.length] = JNetUdpD[p].AquaName; } // let ShowAqua = GetCookie("ShowAqua"); let aAqua0; //if(LA.length>1 || LA[0]!="") { let liL = document.createElement('li'); ulMenu.appendChild(liL); let a = document.createElement('a'); a.className = "ListM"; // a.innerHTML="-----"; liL.appendChild(a); let ulL = document.createElement('ul'); ulL.className = "sub-menu"; liL.appendChild(ulL); for (i = 0; i < LA.length; i++) { let li = document.createElement('li'); ulL.appendChild(li); a = document.createElement('a'); a.setAttribute('onclick', "onClickListNetItem(this," + sKeyTab + ")"); if (LA[i] != "" || LA.length == 1) a.innerHTML = LA[i]; else a.innerHTML = "-"; a.className = "List"; if ((i == 0 && !aAqua0) || ShowAqua == a.innerHTML) aAqua0 = a; li.appendChild(a); } LockShowPage=true; onClickListNetItem(aAqua0, sKeyTab); LockShowPage=false; } if(FuncRetMakeMenuNet) FuncRetMakeMenuNet(J); //return J; }; MakeMenuMainTab(document.getElementById('idMenuTab')); while(E.firstChild) E.removeChild(E.firstChild); GetJNetUdp(JAddGetParam, FuncRetGetJNetUdp); } function onClickListNetItem(aE, sKeyTab){ /// sKeyTab="TabTemperature", "TabTimer", "TabLight" if(!aE) return; // SetCookie ("ShowAqua", aE.innerHTML); ShowAqua=aE.innerHTML; //let aE0=aE; aE.parentNode.parentNode.parentNode.childNodes[0].innerHTML=aE.innerHTML; for(let i=0; i<AliMenu.length; i++) AliMenu[i].remove(); let JNetUdpD=JNetUdp.NetUdp.Data; let AClone=[]; // let ShowName=GetCookie("ShowName"); let aESP0, a0, i; function SetItem(){ let li = document.createElement('li'); AClone[i][1].appendChild(li); a=document.createElement('a'); a.setAttribute('onclick', "onClicMenuNetItem(this)"); // let J=["1.2.3.4", "2.5.6.7", "8.9.0.i"]; a.id="{\"Data\":{\"0\":{\"IP\":\""+JNetUdpD[p].IP+"\"}}}";//JSON.stringify([JNetUdpD[p].IP]); a.innerHTML=JNetUdpD[p].Name; //let p0=JSON.parse(a.id); AClone[i][AClone[i].length]=a; li.appendChild(a); if(!a0) a0=a; if(JNetUdpD[p].Name==ShowName) aESP0=a; } for(p in JNetUdpD) if(!sKeyTab || JNetUdpD[p][sKeyTab]) if (JNetUdpD[p].AquaName == aE.innerHTML || (JNetUdpD[p].AquaName=="" && aE.innerHTML=="-")){ if(JNetUdpD[p].CloneName==""){ let liL = document.createElement('li'); ulMenu.appendChild(liL); AliMenu[AliMenu.length]=liL; let a=document.createElement('a'); a.setAttribute('onclick', "onClicMenuNetItem(this)"); a.id="{\"Data\":{\"0\":{\"IP\":\""+JNetUdpD[p].IP+"\"}}}";//JSON.stringify([JNetUdpD[p].IP]); a.innerHTML=JNetUdpD[p].Name; // AClone[i][AClone[i].length]=a; liL.appendChild(a); // if(!a0) // a0=a; // if((!ShowIP && !aESP0) ||JNetUdpD[p].IP==ShowIP) // aESP0=a; if(!a0) a0=a; if(JNetUdpD[p].Name==ShowName) aESP0=a; } else{ for(i=0; i<AClone.length; i++) if(AClone[i][0].innerHTML==JNetUdpD[p].CloneName) { SetItem(); break; } if(i==AClone.length){ let liL = document.createElement('li'); ulMenu.appendChild(liL); AliMenu[AliMenu.length]=liL; let a=document.createElement('a'); a.innerHTML=JNetUdpD[p].CloneName; liL.appendChild(a); AClone[i]=[]; AClone[i][0]=a; let ulL = document.createElement('ul'); ulL.className="sub-menu"; liL.appendChild(ulL); AClone[i][1]=ulL; SetItem(); } } } //} if(!aESP0) aESP0=a0; for(let i0=0; i0<AClone.length; i0++) { // let J; let L=1; for (let i1 = 2; i1 < AClone[i0].length; i1++) { let J=JSON.parse(AClone[i0][i1].id); for (let i2 = 2; i2 < AClone[i0].length; i2++) if (i1 != i2) J.Data[L++]=JSON.parse(AClone[i0][i2].id).Data["0"]; AClone[i0][i1].id=JSON.stringify(J); } } onClicMenuNetItem(aE); onClicMenuNetItem(aESP0); // aE.style.display = "none"; } function onClicMenuNetItem(aE) { if (!aE) return; //if(E.id!="") let JIP = []; /// [0] - select IP, [1,...] - clone IP if (aE.id != ""){ JNetUdpDSelect= JSON.parse(aE.id); for(p in JNetUdp.NetUdp.Data) if(JNetUdp.NetUdp.Data[p].IP==JNetUdpDSelect.Data[0].IP) { JNetUdpDShow={Data:{0:JNetUdp.NetUdp.Data[p]}}; ShowName=JNetUdp.NetUdp.Data[p].Name; let firmware=JNetUdp.NetUdp.Data[p].FirmwareBuild; firmware=firmware.substring(1, 3); if(firmware=="1.") esp=8266; else esp=32; // SetCookie("ShowName", JNetUdp.NetUdp.Data[p].Name); break; } } else { JNetUdpDSelect = undefined; // SetCookie("ShowName", 0); } let tags=document.getElementsByTagName("a"); let t0; for(i in tags) if (tags[i] != aE && aE.className == "" && tags[i].className == "selected") tags[i].className = ""; else if (tags[i] != aE && aE.className == "List" && tags[i].className == "ListM") tags[i].className = "List"; let bNeedShowPage=false; if(aE.className == "") { aE.className = "selected"; bNeedShowPage=true;//ShowPage(); } else if(aE.className == "List") aE.className = "ListM"; if(aE.parentNode.parentNode.parentNode.tagName=="LI"); t0=aE.parentNode.parentNode.parentNode.childNodes[0]; if(t0 && t0.className == "List") t0.className = "ListM"; else if(t0 && t0.className == "") { t0.className = "selected"; bNeedShowPage=true; //ShowPage(); } if(bNeedShowPage) { ShowHideTab(); // SetCookie("TabLight", JNetUdpDShow["Data"]["0"]["TabLight"]); // SetCookie("TabTimer", JNetUdpDShow["Data"]["0"]["TabTimer"]); // SetCookie("TabTemperature", JNetUdpDShow["Data"]["0"]["TabTemperature"]); ShowPage(); //aE.parentNode.style.display="none"; // aE.parentNode.parentNode.style.display="none"; // aE.parentNode.parentNode.parentNode.style.display="none"; } }; let WokerSendFuncRet; /// !!! not inside in function WokerSend(), else not work correct. Because, I think, when worker /// call to WokerSend::FRet, all values of WokerSend() was delete, or may be woker remember its. let WokerSendJ=[]; let JNetUdpD={}; let LockWokerSend=false; let AXml={}; let WokerGETPOST; let PostCount=0; let JJob={}; /// ={iTh:iJob, ...} example{"0":1, ...} let AFormData=[]; var JLicShowed={}; /// already lic-warning showed {IP1:Error, IP2:Error,...} var JLicNeedShow; /// will need show {IP1:Error, IP2:Error,...} // let LastSendIndexPacket=-1; //let sFRetSend=""; //DeleteCookie("192_168_1_106"); //DeleteCookie("192_168_1_107"); function WokerSend(J, JNetUdpD_, FuncRet, GETPOST){ /// Send json J to IP from JNetUdpD /// J=[{"LPWMChanelLED":{"Data":{"0":{"GPIO_PWM":"P4","VTemp":0.8326879144385024},"1":{"GPIO_PWM":"P2","VTemp":0.6308512670942398},"2":{"GPIO_PWM":"G15","VTemp":0.5373712335381918}}}}, {...}] /// sJNetUdpD={"Data":{"0":{"IP":"192.168.1.102", "Name":Name0, "FuncRet":FuncRet},"2":{"IP":"192.168.1.106", "Name":Name1, "FuncRet":FuncRet}}} /// in finish every thread for every IP call JNetUdpD.Data[p].FuncRet(JNetUdpD.Data[p]); /// GETPOST = "Get" or ="POST" in Request.js :: xmlHttp.open( GETPOST, "http://" + IP+ "/edit"); if(LockWokerSend) { if(FuncRet) FuncRet(); return; } if((new Date().getTime()-TimeWokerLastSend)>dTimeWokerLastSendMax) JJob={}; //bBreakXMLResponse=false; JLicNeedShow={}; WokerGETPOST=GETPOST; WokerSendJ=J; JNetUdpD=JNetUdpD_; // if(!JNetUdpD){ // var aa=0; // } // for(p in JNetUdpD.Data) // if(JNetUdpD.Data[p].FuncRet) // JNetUdpD.Data[p].bFuncRet=1; // else // JNetUdpD.Data[p].bFuncRet=0; // console.log(JSON.stringify(JNetUdpD)); // console.log(JSON.stringify(J)); // if(JJS){ /// send via android // console.log("++@@@@@@@@@@@@"); // console.log(JSON.stringify(J)); // console.log("JNetUdpD="+JSON.stringify(JNetUdpD)); // JJS.SendJSON(JSON.stringify(J), JSON.stringify(JNetUdpD), GETPOST); // // FuncRet(); // WokerSendJ=[]; // if(WokerSendFuncRet && !bBreakXMLResponse) // WokerSendFuncRet(); /// not call direct FuncRet(). Not work in Mlight.htm!!! // return; // } // console.log("--@@@@@@@@@@@@"); let Wrap=document.getElementById('idDivWait'); // var RType = false; // if (window.XMLHttpRequest) // RType = true; WokerSendFuncRet=FuncRet; function FRetSend(iTh, sResponse){ console.log(sResponse); // console.log("FRetSend iTh="+iTh) // if(sResponse=="") // return false; let JR={}; if(sResponse!=""){ /// at file send sResponse="" console.log(sResponse); //if(sResponse.length) // UPLOADED JR=JSON.parse(sResponse); let iPostCount=JR["sRet"]["iPostCount"]; if(iPostCount<=bBreakXMLResponse) return; } // if(JR["sRet"]==undefined) { // let weea = 0; // } // console.log("FRet"); // console.log(e.data.Response); // var iTh=e.data.Index; let iJob=JJob[iTh]; let Div = document.createElement('div'); // if(!JNetUdpD){ // var aa=0; // } let Name=JNetUdpD.Data[iTh].Name; if(!Name); Name=""; Div.innerHTML="Sent "+Name+"IP:"+JNetUdpD.Data[iTh].IP +" J["+(iJob)+"]"; if(iJob>=0 && WokerSendJ[iJob] && WokerSendJ[iJob].path) Div.innerHTML+=J[iJob].path; Div.id="idDivWaitLog"; Wrap.appendChild(Div); // console.log("-----1------"); // console.log(sResponse); // let sJRIP=JR["IP"]; // if(aaa!="") { // console.log("sResponse="+sResponse); // console.log("-----------------sJRIP="+sJRIP); // sJRIP=sJRIP.replace(/[.]/g, "_");//replace(new RegExp("\.", 'g'), "_"); // for(p in JR) /// test Lic error // console.log("-----------------sJRIP="+sJRIP); // console.log("-----------------GetCookie(sJRIP)="+GetCookie(sJRIP)); // let DateTime = new Date(); // if (JR["Lic"] && JR["Lic"] > 0 && JR["IP"] && !JLicShowed[JR["IP"]] && GetCookie(sJRIP) != DateTime.getDate()) { if (JR["Lic"] && JR["Lic"] > 0 && JR["IP"] && !JLicShowed[JR["IP"]] ) { JLicShowed[JR["IP"]] = JR["Lic"]; JLicNeedShow[JR["IP"]] = JR["Lic"]; // SetCookie(sJRIP, DateTime.getDate()); // console.log("-----------------GetCookie(sJRIP)="+GetCookie(sJRIP)); /// !!! bug. Ahow aleert only onece at 1 day else: /// if the android off (in this time arduino reload page and show alert) and then on, it show white page } // } if(iJob>=0 && JNetUdpD.Data[iTh].FuncRet) { if(sResponse.substring(0,1)=='{' || sResponse.substring(0,1)=='[') JsonAdd(JNetUdpD.Data[iTh].J, JR); // else if(!JNetUdpD.Data[Index].J) // JNetUdpD.Data[Index].J={}; } // console.log("-----2------"); iJob++; JJob[iTh]=iJob; if(iJob<WokerSendJ.length) { // console.log("----- next iTh="+iTh+" iJob="+iJob); if(WokerSendJ[iJob].file && WokerSendJ[iJob].path) /// upload file to server SendJ(iTh, {file: WokerSendJ[iJob].file, path: WokerSendJ[iJob].path, IP:JNetUdpD.Data[iTh].IP, GETPOST:WokerGETPOST}, JNetUdpD.Data[iTh].FRet, iJob);//, RType: RType}); else /// send json-parametr to server SendJ(iTh, {sJson: "Json=" + JSON.stringify(WokerSendJ[iJob]), IP: JNetUdpD.Data[iTh].IP, GETPOST:WokerGETPOST}, JNetUdpD.Data[iTh].FRet, iJob);//, RType: RType}); } else { // console.log("*******************************Th end iTh="+iTh) JJob[iTh]=-1; /// ThEnd = true; let bAllEnd=true; delete AXml[iTh]; delete AFormData[iTh]; for(iTh_ in JNetUdpD.Data) bAllEnd=bAllEnd && JJob[iTh_]<0; // ThEnd; if(JNetUdpD.Data[iTh].FuncRet){//&& !bBreakXMLResponse) { //setTimeout(JNetUdpD.Data[p].FuncRet(JNetUdpD.Data[p]), 1); // console.log(e.data.Response); JNetUdpD.Data[iTh].FuncRet(JNetUdpD.Data[iTh], JNetUdpD.Data[iTh].J);//e.data.JResponse); } if(bAllEnd) { // console.log("===================================Ok all"); while(true){ let wl = document.getElementById('idDivWaitLog'); if(wl) wl.remove(); else break; } // if(JLicNeedShow.length>0) { let S=""; for(p in JLicNeedShow){ // let DevName=JNetUdpD.Data[p]; S+=p+": " if(JLicNeedShow[p]==1) S+="the license time expires\n\r"; else if(JLicNeedShow[p]==2 || JLicNeedShow[p]==3) S+="the license time has expired\n\r"; else if(JLicNeedShow[p]==4) S+="the license is absent\n\r"; else S+="the license error "+JLicNeedShow[p]+"\n\r"; } // sFRetSend+=S; if(S!="") alert(S); } // for(iTh in JNetUdpD.Data) // if(window["ThPostFRet"+p]) // for(let iTh in AXml) // delete AXml[iTh]; // for(let iTh in AFormData) // delete AFormData[iTh]; // AXml.length=0; // console.log("=============AXml.length=0;"); WokerSendJ=[]; if(WokerSendFuncRet)// && !bBreakXMLResponse) WokerSendFuncRet(); /// not call direct FuncRet(). Not work in Mlight.htm!!! /// !!! Else always call to function with was sent to worker at it created (SendPWMValue::FuncRet() ), /// but to Mlignt.htm::FuncRet never call. } } // if(sFRetSend!="") // alert(sFRetSend); }; function SendJ(iTh, J, FRetSend, iJob) { if(AXml[iTh]) delete AXml[iTh]; //if(!AXml[iTh]) { AXml[iTh] = new XMLHttpRequest(); // console.log("+ new XMLHttpRequest()++iTh="+iTh); //} xmlHttp =AXml[iTh]; // function () { // if (xmlHttp.readyState == 4) { // if (xmlHttp.status == 200) { // FRetSend(xmlHttp.responseText);//e.data.Index); // // FRetSend({Index: J.Index, JIndex: J.JIndex, Response: xmlHttp.responseText});//e.data.Index); // } // } // }; if (J.GETPOST == "GET") { // xmlHttp.addEventListener("loadend", undefined); xmlHttp.onreadystatechange = FRetSend; PostCount++; let S="&sRet={\"iPostCount\":"+PostCount+"}"; if(J.IP=="") xmlHttp.open("GET", "/get?" + J.sJson+S, true); else xmlHttp.open("GET", "http://" + J.IP + "/get?" + J.sJson+S, true); xmlHttp.send(); } else /// GETPOST = "POST" { // console.log("***POST*** iTh="+iTh); xmlHttp.onreadystatechange = undefined; xmlHttp.addEventListener("loadend", FRetSend); // function (evt) { // ///The transfer is compvare. // FRetSend(xmlHttp.responseText); // }); if (J.file && J.path) { /// upload file to server AFormData[iTh] = new FormData(); AFormData[iTh].append("data", J.file, J.path); if(J.IP=="") xmlHttp.open(J.GETPOST, "/edit", true); else xmlHttp.open(J.GETPOST, "http://" + J.IP + "/edit", true); xmlHttp.send(AFormData[iTh]); // delete formData; } else { /// send json-parametr to server if(J.IP=="") xmlHttp.open(J.GETPOST, "/set?", true); else xmlHttp.open(J.GETPOST, "http://" + J.IP + "/set?", true); PostCount++; // console.log("POST PostCount="+PostCount); xmlHttp.send(J.sJson+"&sRet={\"iPostCount\":"+PostCount+"}");//Th_"+iTh+"_Job_"+iJob+"_PostCount_"+PostCount); } } TimeWokerLastSend = new Date().getTime(); // delete xmlHttp; }; for(let iTh in JNetUdpD.Data){ // let iTh=iTh; let ThPostFRet="ThPostFRet iTh="+iTh; if(GETPOST=="GET") { window[ThPostFRet + iTh] = function (sResponse) { // delete window[ThPostFRet]; var xmlHttp=AXml[iTh]; if (xmlHttp && xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { // console.log("!!!!!!!!!!! Get Ret") // console.log("iTh="+iTh+ " responseText="+AXml[iTh].responseText ); FRetSend(iTh, xmlHttp.responseText);//e.data.Index); // FRe,tSend({Index: J.Index, JIndex: J.JIndex, Response: xmlHttp.responseText});//e.data.Index); } } }; } else { /// GETPOST=="POST" // var aa="console.log(\"99999999999999=\"+"+iTh+")"; window[ThPostFRet + iTh] = function (sResponse) { // console.log("sR0="+JSON.stringify(sResponse)); // console.log("sR="+JSON.stringify(sResponse)); // console.log("Post Ret "+"iTh="+iTh); // if(AXml[iTh]==undefined) // var a=0; // console.log("iTh="+iTh+ " responseText="+AXml[iTh].responseText ); // var J0=JSON. // console.log("AXml="+JSON.stringify(AXml) ); // console.log("AXml.length="+AXml.length) // for(var iX=0; iX<AXml.length; iX++) for(iX in AXml) { // console.log("iX="+iX) // console.log("AXml[iTh]=" + AXml[iX]); // console.log("AXml[iTh].responseText=" + AXml[iX].responseText); // console.log(AXml[iX].status) /// !=0 // console.log(AXml[iX].statusText) /// !="" if(AXml[iX] && AXml[iX].status!=0){// && AXml[iX].responseText!="") { FRetSend(iX, AXml[iX].responseText); break; } } // console.log("---------------") // FRetSend(iTh, AXml[iTh].responseText); }; // if(iTh==0) { // window[ThPostFRet + iTh] = function (sResponse) { // FRetSend(0, AXml[0].responseText); // }; // } // else if(iTh==1) { // window[ThPostFRet + iTh] = function (sResponse) { // FRetSend(1, AXml[1].responseText); // }; // } // else if(iTh==2) { // window[ThPostFRet + iTh] = function (sResponse) { // FRetSend(2, AXml[2].responseText); // }; // } } JNetUdpD.Data[iTh].J={}; JNetUdpD.Data[iTh].FRet=window[ThPostFRet + iTh]; // JNetUdpD.Data[iTh].WokerEnd=false; if(JJob[iTh]==undefined || JJob[iTh]<0) { /// first send // console.log("0000000 New iTh="+iTh); // console.log(JSON.stringify(JJob)) JJob[iTh] = 0; // Req.postMessage({sJson: "Json=" + JSON.stringify(J), ip: _ip, type: RType}); if (J[0].file && J[0].path) /// upload file to server SendJ(iTh, {file: J[0].file, path: J[0].path, IP: JNetUdpD.Data[iTh].IP, GETPOST: WokerGETPOST}, JNetUdpD.Data[iTh].FRet, 0);//, RType: RType}); // JNetUdpD.Data[p].Woker.postMessage({Index:p, file:J[0].file, path: J[0].path, JIndex:0, IP:JNetUdpD.Data[p].IP, GETPOST:GETPOST});//, RType: RType}); else{ /// send json-parametr to server // if(!J[0]["LTime"]) // J[0]["LTime"]={}; J[0]["TimeL"]=GetTime(); SendJ(iTh, {sJson: "Json=" + JSON.stringify(J[0]), IP: JNetUdpD.Data[iTh].IP, GETPOST: WokerGETPOST}, JNetUdpD.Data[iTh].FRet, 0);//, RType: RType}); // JNetUdpD.Data[p].Woker.postMessage({Index:p, sJson: "Json=" +JSON.stringify(J[0]), JIndex:0, IP:JNetUdpD.Data[p].IP, GETPOST:GETPOST});//, RType: RType}); } } else /// now sending { // console.log("0000000 Reset Job iTh="+iTh); JJob[iTh] = 0; } } } function ClearElement(E){ let Es = E.getElementsByTagName("*"); for (let i = Es.length-1; i>=0 ; i--) Es.item(i).remove(); } var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; let TimeCurrent; /// we can take time from esp and save it here, and late take it (example for slider) {H:_, M:_, S_:} function GetTimeCurrent() { if(TimeCurrent==undefined) return undefined; return{H:TimeCurrent.H, M:TimeCurrent.M, S:TimeCurrent.S}; } function GetTime() { // return {Y:2017, Mn:11, D:21, WD:1, H:2, M:3, S:4}; let a = new Date(); return {Y:a.getFullYear(), Mn:a.getMonth()+1, D:a.getDate(), WD:a.getDay(), H:a.getHours(), M:a.getMinutes(), S:a.getSeconds() + 1}; }; function AddDigits2String(V, S, length) { let S0 = ""+V; while(S0.length<length) S0="0"+S0; return S+S0; } function Millis2TimeStr(ms, bShowMs) { if(bShowMs==undefined) bShowMs=true; if(ms<0) return "00:00"; let H=Math.floor(ms/3600000); ms-=H*3600000; let M = Math.floor(ms / 60000); ms -= M * 60000; let Sec = Math.floor(ms / 1000); ms = Math.floor(ms-Sec * 1000); let S=""; S=AddDigits2String(H, S, 2); S += ":"; S=AddDigits2String(M, S, 2); if(Sec>0 || ms>0) { S += ":"; S=AddDigits2String(Sec, S, 2); if (ms>0 && bShowMs) { S += "."; S=AddDigits2String(ms, S, 3); } } return S; } function Str2Time(S) /// "__:__:__.____" -> {H:_, M:_, S_, MS:_} { // let ms = 0; let r= {H:0, M:0, S:0, MS:0}; let p = 0; let iLast = 0; let i=0; for (i = 0; i < S.length; i++) { if(S[i]==" ") /// teime is end next date break; if (S[i] == ':') { // let r=STime.substring(iLast, i); if (p == 0) r.H = S.substring(iLast, i)*1; else if (p == 1) r.M = S.substring(iLast, i)*1; iLast = i + 1; p++; } else if (S[i] == '.' && p == 2) { // let r=STime.substring(iLast, i); r.S = S.substring(iLast, i); iLast = i + 1; p++; } } if (p == 1) r.M = S.substring(iLast, i)*1; else if (p == 2) r.S = S.substring(iLast, i)*1; else if (p == 3) r.MS = S.substring(iLast, i)*1; return r; }; function TimeStr2Millis(STime) { let T=Str2Time(STime); return T.H*3600000 + T.M*60000 + T.S*1000 + T.MS*1; }; function GetGPIO(GPIO_PWM) /// return GPIO from like it "G23" or like it "G23|1|16" always return G23 { let i=GPIO_PWM.indexOf("|"); if(i>=0) return GPIO_PWM.substr(0, i); return GPIO_PWM; }