BangleApps/apps/widancs/ancs.min.js

10 lines
4.4 KiB
JavaScript

(function(){function t(a){function e(){k(0);delete b.gatt;delete b.ancs;NRF.getGattforCentralServer||NRF.disconnect();setTimeout(function(){NRF.wake()},500)}var d;b.gatt=a;k(1);b.gatt.device.on("gattserverdisconnected",function(a){d&&clearInterval(d);c&&clearInterval(c);e()});E.on("kill",function(){b.gatt.disconnect().then(function(){NRF.sleep()})});NRF.setSecurity({passkey:"123456",mitm:1,display:1});var c=setTimeout(function(){d&&clearInterval(d);b.gatt.disconnect().then(e)},1E4);b.gatt.startBonding().then(function(){d=
setInterval(function(){var a=b.gatt.getSecurityStatus();a.connected?a.connected&&a.encrypted&&(clearInterval(d),clearTimeout(c),k(2),v()):(clearInterval(d),clearTimeout(c))},1E3)})["catch"](function(a){Terminal.println("ERROR "+a)})}function v(){b.ancs={primary:null,notify:null,control:null,data:null};b.gatt.getPrimaryService("7905F431-B5CE-4E99-A40F-4B1E122D00D0").then(function(a){b.ancs.primary=a;return a.getCharacteristic("9FBF120D-6301-42D9-8C58-25E699A21DBD")}).then(function(a){b.ancs.notify=
a;return b.ancs.primary.getCharacteristic("69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9")}).then(function(a){b.ancs.control=a;return b.ancs.primary.getCharacteristic("22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB")}).then(function(a){b.ancs.data=a;k(3);b.ancs.notify.on("characteristicvaluechanged",function(a){var e=a.target.value,c=e.getUint8(0);a=e.getUint8(2);e=e.getUint32(4,!0);1<c||(m&&clearTimeout(m),w.includes(a)&&(c=b.notqueue.length,1==a?(f&&(b.notqueue.push(b.current),f=!1),b.notqueue.push({cat:a,uid:e})):
16>c&&(b.notqueue[c]={cat:a,uid:e}),m=setTimeout(n,1E3)))});b.ancs.data.on("characteristicvaluechanged",function(a){b.store(a.target.value.buffer);(a=b.gotmsg())&&x(b.buf,a)});b.ancs.notify.startNotifications().then(function(){b.ancs.data.startNotifications().then(function(){k(4)})})})["catch"](function(a){Terminal.println("ERROR "+a)})}function y(a){a=a.split("\n");for(var b=0;b<a.length;b++){a[b]=a[b].trim();var d=a[b];if(18<d.length){for(var c=18;10<c&&!" \t-_".includes(d[c]);)c--;10==c&&(c=18);
a[b]=d.substr(0,c);a.splice(b+1,0,d.substr(c))}}return a.join("\n")}function z(){l=setTimeout(function(){SCREENACCESS.release();l=void 0;f=!1;n()},500)}function x(a,e){function d(a){var e=new Uint8Array(6),c=DataView(e.buffer);c.setUint8(0,2);c.setUint32(1,b.current.uid,!0);c.setUint8(5,a?0:1);b.ancs.control.writeValue(e).then(z)}b.msgTO&&clearTimeout(b.msgTO);for(var c="",h=8;h<8+e.tlen;++h)c+=String.fromCharCode(a[h]);h="";for(var f=11+e.tlen;f<11+e.tlen+e.mlen;++f)h+=String.fromCharCode(a[f]);
h=y(h);E.showPrompt();l&&clearTimeout(l);Bangle.setLCDPower(!0);SCREENACCESS.request();p||(p=!0,Bangle.buzz(500).then(function(){p=!1}));1!=b.current.cat?E.showAlert(h,c).then(d.bind(null,!1)):E.showPrompt(h,{title:c,buttons:{Accept:!0,Cancel:!1}}).then(d)}function n(){if(0!=b.notqueue.length&&!f){f=!0;b.current=b.notqueue.pop();var a=DataView(b.com.buffer);6==b.current.cat?a.setUint8(8,2):a.setUint8(8,3);a.setUint32(1,b.current.uid,!0);b.inp=0;b.ancs.control.writeValue(b.com).then(function(){b.msgTO=
setTimeout(function(){f=!1;b.msgTO=void 0;n()},1E3)})}}function k(a){q=a;WIDGETS.ancs.draw()}var u=require("Storage").readJSON("widancs.json",1)||{settings:{enabled:!1,category:[1,2,4]}},r=u.settings.enabled,w=u.settings.category,b={gatt:null,ancs:null,current:{cat:0,uid:0},notqueue:[],msgTO:void 0,com:new Uint8Array([0,0,0,0,0,1,20,0,3,100,0]),buf:new Uint8Array(132),inp:0,store:function(a){var b=this.inp;132>=b+a.length&&(this.buf.set(a,b),this.inp+=a.length)},gotmsg:function(){var a=this.inp,b=
DataView(this.buf.buffer);if(8>a)return null;var d=b.getUint16(6,!0);if(a<d+8)return null;b=b.getUint16(9+d,!0);return a<b+d+11?null:{tlen:d,mlen:b}}};if(!NRF.getGattforCentralServer&&r&&"undefined"!=typeof SCREENACCESS)NRF.on("disconnect",function(a){NRF.sleep()});if(r&&"undefined"!=typeof SCREENACCESS)NRF.on("connect",function(a){NRF.getGattforCentralServer?t(NRF.getGattforCentralServer(a)):NRF.connect(a).then(t)});var p=!1,l=void 0,f=!1,m,q=5;WIDGETS.ancs={area:"tl",width:24,draw:function(){var a=
new Uint16Array([50712,63512,1023,65504,2016,0]),b=E.toArrayBuffer(atob("GBgBAAAABAAADgAAHwAAPwAAf4AAP4AAP4AAP4AAHwAAH4AAD8AAB+AAA/AAAfgAAf3gAH/4AD/8AB/+AA/8AAf4AAHwAAAgAAAA"));g.setColor(a[q]);g.drawImage(b,this.x,this.y)}};r&&"undefined"!=typeof SCREENACCESS&&(q=0,NRF.setServices(void 0,{uart:!1}),NRF.sleep(),NRF.wake(),NRF.setAdvertising([2,1,6,17,21,208,0,45,18,30,75,15,164,153,78,206,181,49,244,5,121],{connectable:!0,discoverable:!0,interval:375}))})();