UNPKG

remote-uploader

Version:
2 lines 29.4 kB
��!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof module&&module.exports?module.exports=t():e.UART=t()}("undefined"!=typeof self?self:this,(function(){if("undefined"!=typeof navigator){var e,t,n=[],i=[],o={name:"Web Bluetooth",description:"Bluetooth LE devices",svg:'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17.71 7.71L12 2h-1v7.59L6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 11 14.41V22h1l5.71-5.71-4.3-4.29 4.3-4.29zM13 5.83l1.88 1.88L13 9.59V5.83zm1.88 10.46L13 18.17v-3.76l1.88 1.88z" fill="#ffffff"/></svg>',isSupported:function(){return navigator.platform.indexOf("Win")>=0&&(navigator.userAgent.indexOf("Chrome/54")>=0||navigator.userAgent.indexOf("Chrome/55")>=0||navigator.userAgent.indexOf("Chrome/56")>=0)?"Chrome <56 in Windows has navigator.bluetooth but it's not implemented properly":window&&window.location&&"http:"==window.location.protocol&&"localhost"!=window.location.hostname?"Serving off HTTP (not HTTPS) - Web Bluetooth not enabled":!!navigator.bluetooth||(/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream?"To use Web Bluetooth on iOS you'll need the WebBLE App.\nPlease go to https://itunes.apple.com/us/app/webble/id1193531073 to download it.":"This Web Browser doesn't support Web Bluetooth.\nPlease see https://www.espruino.com/Puck.js+Quick+Start")},connect:function(t,i){var o,r,d,l="6e400001-b5a3-f393-e0a9-e50e24dcca9e",f=void 0,h=[],p=!1,v=20;return t.close=function(e){t.isOpening=!1,t.isOpen?(t.isOpen=!1,t.emit("close")):e&&e(null),f&&(f.disconnect(),f=void 0,r=void 0,d=void 0)},t.write=function(e,n){e&&h.push({data:e,callback:n,maxLength:e.length}),t.isOpen&&!t.txInProgress&&function e(){if(p)return void setTimeout(e,50);var n;if(!h.length)return void a.writeProgress();var i=h[0];a.writeProgress(i.maxLength-i.data.length,i.maxLength),i.data.length<=v?(n=i.data,i.data=void 0):(n=i.data.substr(0,v),i.data=i.data.substr(v));t.txInProgress=!0,u(2,"Sending "+JSON.stringify(n)),r.writeValue(s(n)).then((function(){u(3,"Sent"),i.data||(h.shift(),i.callback&&i.callback()),t.txInProgress=!1,e()})).catch((function(e){u(1,"SEND ERROR: "+e),h=[],t.close()}))}()},navigator.bluetooth.requestDevice({filters:[{namePrefix:"Puck.js"},{namePrefix:"Pixl.js"},{namePrefix:"MDBT42Q"},{namePrefix:"Bangle"},{namePrefix:"RuuviTag"},{namePrefix:"iTracker"},{namePrefix:"Thingy"},{namePrefix:"Espruino"},{services:[l]}],optionalServices:[l]}).then((function(e){return u(1,"Device Name: "+e.name),u(1,"Device ID: "+e.id),e.addEventListener("gattserverdisconnected",(function(){u(1,"Disconnected (gattserverdisconnected)"),t.close()})),e.gatt.connect()})).then((function(e){return u(1,"Connected"),f=e,e.getPrimaryService(l)})).then((function(e){return u(2,"Got service"),(o=e).getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e")})).then((function(e){return d=e,u(2,"RX characteristic:"+JSON.stringify(d)),d.addEventListener("characteristicvaluechanged",(function(e){var n=e.target.value;if(n.byteLength>v&&(u(2,"Received packet of length "+n.byteLength+", increasing chunk size"),v=n.byteLength),a.flowControl)for(var i=0;i<n.byteLength;i++){var o=n.getUint8(i);17==o&&(u(2,"XON received => resume upload"),p=!1),19==o&&(u(2,"XOFF received => pause upload"),p=!0)}var r=c(n.buffer);u(3,"Received "+JSON.stringify(r)),t.emit("data",r)})),d.startNotifications()})).then((function(){return o.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e")})).then((function(e){r=e,u(2,"TX characteristic:"+JSON.stringify(r))})).then((function(){t.txInProgress=!1,t.isOpen=!0,t.isOpening=!1,e=!1,n=[],i(t),t.emit("open"),t.write()})).catch((function(e){u(1,"ERROR: "+e),t.close()})),t}},r={name:"Web Serial",description:"USB connected devices",svg:'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M15 7v4h1v2h-3V5h2l-3-4-3 4h2v8H8v-2.07c.7-.37 1.2-1.08 1.2-1.93 0-1.21-.99-2.2-2.2-2.2-1.21 0-2.2.99-2.2 2.2 0 .85.5 1.56 1.2 1.93V13c0 1.11.89 2 2 2h3v3.05c-.71.37-1.2 1.1-1.2 1.95 0 1.22.99 2.2 2.2 2.2 1.21 0 2.2-.98 2.2-2.2 0-.85-.49-1.58-1.2-1.95V15h3c1.11 0 2-.89 2-2v-2h1V7h-4z" fill="#ffffff"/></svg>',isSupported:function(){return navigator.serial?!window||!window.location||"http:"!=window.location.protocol||"localhost"==window.location.hostname||"Serving off HTTP (not HTTPS) - Web Serial not enabled":"No navigator.serial - Web Serial not enabled"},connect:function(e,t){var n;function i(){e.isOpening=!1,e.isOpen&&(u(1,"Disconnected"),e.isOpen=!1,e.emit("close"))}return navigator.serial.requestPort({}).then((function(e){return u(1,"Connecting to serial port"),n=e,e.open({baudRate:115200})})).then((function(){!function t(){var o=n.readable.getReader();o.read().then((function({value:n,done:r}){if(o.releaseLock(),n){var a=c(n.buffer);u(3,"Received "+JSON.stringify(a)),e.emit("data",a)}r?i():t()}))}(),u(1,"Serial connected. Receiving data..."),e.txInProgress=!1,e.isOpen=!0,e.isOpening=!1,t(e)})).catch((function(e){u(0,"ERROR: "+e),i()})),e.close=function(e){n&&(n.close(),n=void 0),i()},e.write=function(e,t){var i=n.writable.getWriter();i.write(s(e)).then((function(){t()})).catch((function(e){u(0,"SEND ERROR: "+e),closeSerial()})),i.releaseLock()},e}};i.push(o),i.push(r);var a={debug:1,flowControl:!0,log:function(e,t){e<=this.debug&&console.log("<UART> "+t)},writeProgress:function(e,t){},connect:l,write:h,eval:function(t,i){if(f())return e?(u(3,"Busy - adding eval to queue"),void n.push({type:"eval",expr:t,cb:i})):void h("eval(process.env.CONSOLE).println(JSON.stringify("+t+"))\n",(function(e){try{var t=JSON.parse(e.trim());i(t)}catch(t){u(1,"Unable to decode "+JSON.stringify(e)+", got "+t.toString()),i(null,"Unable to decode "+JSON.stringify(e)+", got "+t.toString())}}),!0)},setTime:function(e){var t=new Date,n="setTime("+t.getTime()/1e3+");";h(n+="if (E.setTimeZone) E.setTimeZone("+t.getTimezoneOffset()/-60+");\n",e)},isConnected:function(){return void 0!==t},getConnection:function(){return t},close:function(){t&&t.close()},modal:function(e){var t=document.createElement("div");t.style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;opacity:0.5;z-index:100;background:black;",t.innerHTML='<div style="position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);font-family: Sans-Serif;font-size:400%;color:white;">Click to Continue...</div>',t.onclick=function(n){e(),n.preventDefault(),document.body.removeChild(t)},document.body.appendChild(t)}};return a}function c(e){return String.fromCharCode.apply(null,new Uint8Array(e))}function s(e){for(var t=new ArrayBuffer(e.length),n=new Uint8Array(t),i=0,o=e.length;i<o;i++)n[i]=e.charCodeAt(i);return t}function d(){if(n.length){var e=n.shift();u(3,"Executing "+JSON.stringify(e)+" from queue"),"eval"==e.type?a.eval(e.expr,e.cb):"write"==e.type?a.write(e.data,e.callback,e.callbackNewline):u(1,"Unknown queue item "+JSON.stringify(e))}}function u(e,t){a.log&&a.log(e,t)}function l(e){var t={on:function(e,t){this["on"+e]=t},emit:function(e,t){this["on"+e]&&this["on"+e](t)},isOpen:!1,isOpening:!0,txInProgress:!1},n=document.createElement("div");n.style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;opacity:0.5;z-index:100;background:black;";var o=document.createElement("div");o.style="position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);font-family: Sans-Serif;z-index:101;";var r=document.createElement("div");r.innerText="SELECT A PORT...",r.style="color:#fff;background:#000;padding:8px 8px 4px 8px;font-weight:bold;",o.appendChild(r);var a=document.createElement("div");return a.style="color:#000;background:#fff;padding:4px 8px 4px 8px;",o.appendChild(a),i.forEach((function(i){var r=i.isSupported();!0!==r&&u(0,i.name+" not supported, "+r);var c=document.createElement("div");c.style="width:300px;height:60px;background:#ccc;margin:4px 0px 4px 0px;padding:0px 0px 0px 68px;cursor:pointer;",c.innerHTML='<div style="position:absolute;left:8px;width:48px;height:48px;background:#999;padding:6px;cursor:pointer;">'+i.svg+'</div><div style="font-size:150%;padding-top:8px;">'+i.name+'</div><div style="font-size:80%;color:#666">'+i.description+"</div>",c.onclick=function(r){t=i.connect(t,e),r.preventDefault(),document.body.removeChild(o),document.body.removeChild(n)},a.appendChild(c)})),document.body.appendChild(n),document.body.appendChild(o),t}function f(){var e=!1;return i.forEach((function(t){var n=t.isSupported();!0===n?e=!0:u(0,t.name+" not supported, "+n)})),e}function h(i,o,r){if(f()){if(e)return u(3,"Busy - adding write to queue"),void n.push({type:"write",data:i,callback:o,callbackNewline:r});var a;if(t&&(t.isOpen||t.isOpening))return t.txInProgress||(t.received=""),e=!0,t.write(i,c);t=l((function(n){if(!n)return t=void 0,void(o&&o(null));t.received="",t.on("data",(function(e){t.received+=e,t.hadData=!0,t.cb&&t.cb(e)})),t.on("close",(function(e){t=void 0})),e=!0,t.write(i,c)}))}function c(){r&&(t.cb=function(n){var i=t.received.indexOf("\n");if(i>=0){var r=t.received.substr(0,i);t.received=t.received.substr(i+1),t.cb=void 0,a&&clearTimeout(a),a=void 0,o&&o(r),e=!1,d()}});var n=300,i=r?100:3,c=i;a=setTimeout((function s(){a=void 0,n&&n--,c&&c--,t.hadData&&(c=i),c&&n?a=setTimeout(s,100):(t.cb=void 0,r&&u(2,"write waiting for newline timed out"),o&&o(t.received),e=!1,d(),t.received=""),t.hadData=!1}),100)}}})),function(e){"use strict";function t(e,t){var n=(65535&e)+(65535&t);return(e>>16)+(t>>16)+(n>>16)<<16|65535&n}function n(e,n,i,o,r,a){return t((c=t(t(n,e),t(o,a)))<<(s=r)|c>>>32-s,i);var c,s}function i(e,t,i,o,r,a,c){return n(t&i|~t&o,e,t,r,a,c)}function o(e,t,i,o,r,a,c){return n(t&o|i&~o,e,t,r,a,c)}function r(e,t,i,o,r,a,c){return n(t^i^o,e,t,r,a,c)}function a(e,t,i,o,r,a,c){return n(i^(t|~o),e,t,r,a,c)}function c(e,n){var c,s,d,u,l;e[n>>5]|=128<<n%32,e[14+(n+64>>>9<<4)]=n;var f=1732584193,h=-271733879,p=-1732584194,v=271733878;for(c=0;c<e.length;c+=16)s=f,d=h,u=p,l=v,f=i(f,h,p,v,e[c],7,-680876936),v=i(v,f,h,p,e[c+1],12,-389564586),p=i(p,v,f,h,e[c+2],17,606105819),h=i(h,p,v,f,e[c+3],22,-1044525330),f=i(f,h,p,v,e[c+4],7,-176418897),v=i(v,f,h,p,e[c+5],12,1200080426),p=i(p,v,f,h,e[c+6],17,-1473231341),h=i(h,p,v,f,e[c+7],22,-45705983),f=i(f,h,p,v,e[c+8],7,1770035416),v=i(v,f,h,p,e[c+9],12,-1958414417),p=i(p,v,f,h,e[c+10],17,-42063),h=i(h,p,v,f,e[c+11],22,-1990404162),f=i(f,h,p,v,e[c+12],7,1804603682),v=i(v,f,h,p,e[c+13],12,-40341101),p=i(p,v,f,h,e[c+14],17,-1502002290),f=o(f,h=i(h,p,v,f,e[c+15],22,1236535329),p,v,e[c+1],5,-165796510),v=o(v,f,h,p,e[c+6],9,-1069501632),p=o(p,v,f,h,e[c+11],14,643717713),h=o(h,p,v,f,e[c],20,-373897302),f=o(f,h,p,v,e[c+5],5,-701558691),v=o(v,f,h,p,e[c+10],9,38016083),p=o(p,v,f,h,e[c+15],14,-660478335),h=o(h,p,v,f,e[c+4],20,-405537848),f=o(f,h,p,v,e[c+9],5,568446438),v=o(v,f,h,p,e[c+14],9,-1019803690),p=o(p,v,f,h,e[c+3],14,-187363961),h=o(h,p,v,f,e[c+8],20,1163531501),f=o(f,h,p,v,e[c+13],5,-1444681467),v=o(v,f,h,p,e[c+2],9,-51403784),p=o(p,v,f,h,e[c+7],14,1735328473),f=r(f,h=o(h,p,v,f,e[c+12],20,-1926607734),p,v,e[c+5],4,-378558),v=r(v,f,h,p,e[c+8],11,-2022574463),p=r(p,v,f,h,e[c+11],16,1839030562),h=r(h,p,v,f,e[c+14],23,-35309556),f=r(f,h,p,v,e[c+1],4,-1530992060),v=r(v,f,h,p,e[c+4],11,1272893353),p=r(p,v,f,h,e[c+7],16,-155497632),h=r(h,p,v,f,e[c+10],23,-1094730640),f=r(f,h,p,v,e[c+13],4,681279174),v=r(v,f,h,p,e[c],11,-358537222),p=r(p,v,f,h,e[c+3],16,-722521979),h=r(h,p,v,f,e[c+6],23,76029189),f=r(f,h,p,v,e[c+9],4,-640364487),v=r(v,f,h,p,e[c+12],11,-421815835),p=r(p,v,f,h,e[c+15],16,530742520),f=a(f,h=r(h,p,v,f,e[c+2],23,-995338651),p,v,e[c],6,-198630844),v=a(v,f,h,p,e[c+7],10,1126891415),p=a(p,v,f,h,e[c+14],15,-1416354905),h=a(h,p,v,f,e[c+5],21,-57434055),f=a(f,h,p,v,e[c+12],6,1700485571),v=a(v,f,h,p,e[c+3],10,-1894986606),p=a(p,v,f,h,e[c+10],15,-1051523),h=a(h,p,v,f,e[c+1],21,-2054922799),f=a(f,h,p,v,e[c+8],6,1873313359),v=a(v,f,h,p,e[c+15],10,-30611744),p=a(p,v,f,h,e[c+6],15,-1560198380),h=a(h,p,v,f,e[c+13],21,1309151649),f=a(f,h,p,v,e[c+4],6,-145523070),v=a(v,f,h,p,e[c+11],10,-1120210379),p=a(p,v,f,h,e[c+2],15,718787259),h=a(h,p,v,f,e[c+9],21,-343485551),f=t(f,s),h=t(h,d),p=t(p,u),v=t(v,l);return[f,h,p,v]}function s(e){var t,n="",i=32*e.length;for(t=0;t<i;t+=8)n+=String.fromCharCode(e[t>>5]>>>t%32&255);return n}function d(e){var t,n=[];for(n[(e.length>>2)-1]=void 0,t=0;t<n.length;t+=1)n[t]=0;var i=8*e.length;for(t=0;t<i;t+=8)n[t>>5]|=(255&e.charCodeAt(t/8))<<t%32;return n}function u(e){var t,n,i="0123456789abcdef",o="";for(n=0;n<e.length;n+=1)t=e.charCodeAt(n),o+=i.charAt(t>>>4&15)+i.charAt(15&t);return o}function l(e){return unescape(encodeURIComponent(e))}function f(e){return function(e){return s(c(d(e),8*e.length))}(l(e))}function h(e,t){return function(e,t){var n,i,o=d(e),r=[],a=[];for(r[15]=a[15]=void 0,o.length>16&&(o=c(o,8*e.length)),n=0;n<16;n+=1)r[n]=909522486^o[n],a[n]=1549556828^o[n];return i=c(r.concat(d(t)),512+8*t.length),s(c(a.concat(i),640))}(l(e),l(t))}function p(e,t,n){return t?n?h(t,e):u(h(t,e)):n?f(e):u(f(e))}"function"==typeof define&&define.amd?define((function(){return p})):"object"==typeof module&&module.exports?module.exports=p:e.md5=p}(this);class Remote{constructor(){this.UART=UART,this.connected=!1}connect(){this.UART.write("",(()=>{this.connected=!0}))}async upload(e,t){this.connected||this.connect();let n=await this.getDeviceType();"BANGLEJS"==n&&(t=!1);let i=!1;return await this.#e(e).then((async e=>{this.dump().then((t=>{t=t.split("// Code saved with E.setBootCode"),md5(e)!=md5(t[1])||(i=!0)})),this.reset(),t||1==i?1!=i&&"PIXLJS"!=n?(this.UART.write(`E.setBootCode(\`${e}\`,true);\n`),this.UART.write("load();\n")):1!=i&&(this.UART.write(e),i=await this.#t(),this.UART.write("save();\n"),this.UART.write("load();\n")):this.UART.write(e)})),1!=i&&await this.#t().then((e=>{i=e})),i}reset(){this.connected||this.connect(),this.UART.write("reset(true);\n")}disconnect(){this.connected||this.connect(),this.UART.close(),this.connected=!1}isConnected(){return this.connected}async getDeviceType(){return this.connected||this.connect(),await this.eval("process.env.BOARD")}async dump(){return this.connected||this.connect(),await this.eval("E.dumpStr()")}async#e(e){const t=await fetch(e).then((e=>{if(!e.ok)throw new Error(e.status);return e}));if(!t)throw new Error("Fetch failed!");let n=await t.text();return n+="\n",n}#n(){let e=Math.floor(100*Math.random()),t=`function checkUploadStatus(){return '${e}';}\n`;return this.UART.write(t),e}async#t(){let e=this.#n();return await this.eval("checkUploadStatus()")==e}call(e){e+="\n",this.UART.write(e)}eval(e){return new Promise(((t,n)=>{this.UART.eval(e,(e=>{t(e)}))})).catch((e=>{throw Error(e)}))}}