`}function II(a,b,c,d,e,f){if(b)$(a).innerHTML+=`
${S(b)}
`}function B3(a,b,c){return``}function B2(a,g,b,c,d,e,f){$(a).innerHTML+=`${S(g)}
${B3(`${b}L`,c,e)}${B3(`${b}D`,d,f)}
`}function Set2(a,b){const c=$(`${a}D`),d=$(`${a}L`);c.disabled=0==b;c.className=0==b?"BD":"B";d.disabled=1==b;d.className=1==b?"BL":"B"}function Set2A(a,e,b){const c=$(`${a}D`),d=$(`${a}L`);c.disabled=!e||!b;c.className=b?"B":"BD";d.disabled=!e||b;d.className=b?"BL":"B"}const Gs=[99,1,2,3,4,5];Gs.forEach(D);Gs.push(0);B("G99","conectar","Conectar","connect()");B("G1","desativar","Desativar","desativar()");const divdesativar=$("G1");divdesativar.style.display="none";IH("G2","Alarme Hoje","AlarmeHoje","mudaalarmehoje()");IH("G2","Alarme Amanhã","AlarmeAmanha","mudaalarmeamanha()");B2("G3","Bypass","bypass","bypass(true)","bypass(false)","Sim","Não");B2("G3","Alarme","alarme","mudaalarme(true)","mudaalarme(false)","Sim","Não");II("G3","Luz","LuzInt",0,2047,"mudaluz()");II("G4","Luz","LuzInt2",0,2047,"mudaluz2()");B2("G5","Aquecedor","aquecedor","Aquecedor(true)","Aquecedor(false)","Liga","Desliga");const output=$("output");const hora=$("Hora");function log(msg){output.innerHTML+=`${msg}
`;output.style.display="block"}const decoder=new TextDecoder("utf-8");let deviceCache=null;let conectado=false;let ByPassCharacteristic=null;let DesativarCharacteristic=null;let AlarmeCharacteristic=null;let LuzCharacteristic=null;let Luz2Characteristic=null;let bypassGeral=false;let alarmeGeral=false;let AlarmeHojeCharacteristic=null;let AlarmeAmanhaCharacteristic=null;let AquecedorLigaCharacteristic=null;let AquecedorDesligaCharacteristic=null;function connect(){if(conectado){deviceCache.gatt.disconnect();return}return(deviceCache?Promise.resolve(deviceCache):requestBluetoothDevice()).then(device=>GetCharacteristics(device)).catch(error=>log(error))}const elementIDs=["bypassL","bypassD","AlarmeHoje","AlarmeAmanha","LuzInt2","aquecedorL","aquecedorD"];var temp=null;function GetCharacteristics(device){return device.gatt.connect().then(server=>{const botao=$("conectar");botao.innerText="Desconectar";conectado=true;elementIDs.forEach(id=>{$(id).disabled=false});return server.getPrimaryService("4f1023d0-32ca-4560-877d-9f49b2d46670")}).then(service=>{return service.getCharacteristics()}).then(characteristics=>{let queue=Promise.resolve();characteristics.forEach(characteristic=>{switch(characteristic.uuid){case BluetoothUUID.getCharacteristic(65280):DesativarCharacteristic=characteristic;break;case BluetoothUUID.getCharacteristic(65281):characteristic.addEventListener("characteristicvaluechanged",handlePodeDesativar);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65282):ByPassCharacteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleBypass);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65283):AlarmeCharacteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleAlarmeBool);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65284):LuzCharacteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleLuz);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65285):characteristic.addEventListener("characteristicvaluechanged",handleClock);queue=queue.then(_=>characteristic.startNotifications());break;case BluetoothUUID.getCharacteristic(65286):AlarmeHojeCharacteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleAlarmeHoje);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65287):AlarmeAmanhaCharacteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleAlarmeAmanha);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65288):Luz2Characteristic=characteristic;characteristic.addEventListener("characteristicvaluechanged",handleLuz2);queue=queue.then(_=>characteristic.startNotifications()).then(_=>characteristic.readValue());break;case BluetoothUUID.getCharacteristic(65393):AquecedorLigaCharacteristic=characteristic;break;case BluetoothUUID.getCharacteristic(65394):AquecedorDesligaCharacteristic=characteristic;break;default:}});return queue}).catch(error=>{log("Argh! "+error)})}function handleClock(event){let valor=event.target.value;hora.innerText=decoder.decode(valor)}function handleAlarmeBool(event){let valor=event.target.value;alarmeGeral=valor.getInt8()!=0;Set2A("alarme",bypassGeral,alarmeGeral)}function handleLuz3(event,id){const valor=event.target.value;$(id).value=valor.getInt32(0,true)}function handleLuz(event){handleLuz3(event,"LuzInt")}function handleLuz2(event){handleLuz3(event,"LuzInt2")}function handleAlarme(event,tag){const valor=event.target.value;const valor2=decoder.decode(valor);if(valor2=="0000"||valor2=="99:00")$(tag).value=null;else{$(tag).value=valor2;if(tag=="AlarmeHoje"){const[inputHours,inputMinutes]=valor2.split(":").map(Number);const now=new Date;const currentHours=now.getHours();const currentMinutes=now.getMinutes();const passou=currentHours>inputHours||currentHours===inputHours&¤tMinutes>inputMinutes;$(tag).style.color=passou?"red":"black"}}}function handleAlarmeHoje(event){handleAlarme(event,"AlarmeHoje")}function handleAlarmeAmanha(event){handleAlarme(event,"AlarmeAmanha")}function desativar(){DesativarCharacteristic.writeValueWithResponse(new Uint8Array([1]))}function Aquecedor(a){if(a)AquecedorLigaCharacteristic.writeValueWithResponse(new Uint8Array([1]));else AquecedorDesligaCharacteristic.writeValueWithResponse(new Uint8Array([1]))}function bypass(valor){ByPassCharacteristic.writeValueWithResponse(new Uint8Array([valor?1:0]))}function mudaalarme(valor){AlarmeCharacteristic.writeValueWithResponse(new Uint8Array([valor?1:0]))}function mudaalarme(a,b){const valor=$(a).value;let saida=valor;if(valor==""){saida="99:00"}var uint8=Uint8Array.from(saida.split("").map(x=>x.charCodeAt()));b.writeValueWithResponse(uint8)}function mudaalarmehoje(){mudaalarme("AlarmeHoje",AlarmeHojeCharacteristic)}function mudaalarmeamanha(){mudaalarme("AlarmeAmanha",AlarmeAmanhaCharacteristic)}function mudaluz3(a,c){const b=$(a);let novovalor=parseInt(b.value);if(novovalorb.max)novovalor=b.max;let arr=new Uint8Array(8);for(let i=0;i<2;i++){arr[i]=novovalor%256;novovalor=Math.floor(novovalor/256)}c.writeValueWithResponse(arr)}function mudaluz(){mudaluz3("LuzInt",LuzCharacteristic)}function mudaluz2(){mudaluz3("LuzInt2",Luz2Characteristic)}function handlePodeDesativar(event){const valor=event.target.value;divdesativar.style.display=valor.getInt8()!=0?"block":"none"}function handleBypass(event){let valor=event.target.value;bypassGeral=valor.getInt8()!=0;Set2("bypass",bypassGeral);Set2A("alarme",bypassGeral,alarmeGeral);$("LuzInt").disabled=!bypassGeral}function requestBluetoothDevice(){return navigator.bluetooth.requestDevice({filters:[{name:"Despertador",services:["4f1023d0-32ca-4560-877d-9f49b2d46670"]}]}).then(device=>{deviceCache=device;deviceCache.addEventListener("gattserverdisconnected",onDisconnected);return deviceCache})}function onDisconnected(){const botao=$("conectar");botao.innerText="Conectar";conectado=false;elementIDs.forEach(id=>{$(id).disabled=true});divdesativar.style.display="none"}window.addEventListener("load",()=>{if("serviceWorker"in navigator){navigator.serviceWorker.register("/sw.js").catch(error=>{console.warn("Error registering service worker:");console.warn(error)})}});