-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbtscale.min.js
1 lines (1 loc) · 10.5 KB
/
btscale.min.js
1
!function t(e,n,r){function i(c,s){if(!n[c]){if(!e[c]){var a="function"==typeof require&&require;if(!s&&a)return a(c,!0);if(o)return o(c,!0);var u=new Error("Cannot find module '"+c+"'");throw u.code="MODULE_NOT_FOUND",u}var h=n[c]={exports:{}};e[c][0].call(h.exports,function(t){var n=e[c][1][t];return i(n?n:t)},h,h.exports,t,e,n,r)}return n[c].exports}for(var o="function"==typeof require&&require,c=0;c<r.length;c++)i(r[c]);return i}({1:[function(t,e,n){"use strict";n.SCALE_SERVICE_UUID="00001820-0000-1000-8000-00805f9b34fb",n.SCALE_CHARACTERISTIC_UUID="00002a80-0000-1000-8000-00805f9b34fb",n.MAGIC1=223,n.MAGIC2=120,n.TABLE1=[0,118,132,80,219,228,111,178,250,251,77,79,142,87,140,95,158,174,176,181,93,150,21,185,15,252,253,112,27,128,187,244,147,254,255,105,104,131,207,167,210,235,60,100,65,119,198,134,203,211,221,72,238,240,30,88,76,138,143,164,2,75,6,36,141,183,191,40,99,173,184,86,137,160,196,81,197,82,39,61,201,214,220,66,44,215,230,239,249,53,217,188,122,31,67,108,54,56,7,148,152,216,227,182,83,63,12,146,154,194,209,213,52,29,98,169,32,126,172,9,94,89,49,156,163,151,179,116,193,237,242,16,46,74,225,35,43,129,247,97,25,8,26,57,101,62,115,59,123,11,103,4,106,34,70,14,85,102,84,1,69,107,50,139,171,24,186,204,212,38,226,231,28,68,20,149,153,133,218,78,110,224,232,55,190,243,127,223,246,248,45,48,33,19,23,13,22,37,91,51,17,92,124,135,161,189,5,144,159,166,109,180,199,202,195,18,3,229,222,233,155,136,47,234,236,200,41,113,73,90,114,71,125,162,165,145,175,177,10,205,96,192,157,120,206,208,121,58,170,168,42,64,241,117,245,130],n.TABLE2=[0,159,60,216,151,206,62,98,141,119,238,149,106,195,155,24,131,200,215,193,174,22,196,194,165,140,142,28,172,113,54,93,116,192,153,135,63,197,169,78,67,226,250,136,84,190,132,222,191,122,162,199,112,89,96,183,97,143,247,147,42,79,145,105,251,44,83,94,173,160,154,231,51,228,133,61,56,10,179,11,3,75,77,104,158,156,71,13,55,121,229,198,201,20,120,15,240,139,114,68,43,144,157,150,36,35,152,161,95,210,180,6,27,227,230,146,127,253,1,45,243,246,92,148,202,232,117,186,29,137,255,37,2,177,47,203,221,72,57,163,14,64,12,58,207,235,107,32,99,175,21,125,100,176,108,220,123,242,16,208,73,204,233,124,59,234,209,39,249,115,248,164,118,69,17,236,18,237,7,126,211,19,103,65,70,23,166,30,91,205,184,66,241,128,109,214,74,76,46,212,225,80,213,48,167,239,244,38,245,110,40,49,168,111,81,85,101,90,178,4,82,50,218,187,181,134,170,102,5,217,86,171,182,219,223,41,224,129,52,87,53,252,130,185,31,254,188,138,189,88,8,9,25,26,33,34]},{}],2:[function(t,e,n){"use strict";var r=function(){function t(){}return t.prototype.addEventListener=function(t,e){if(this.listeners_||(this.listeners_=Object.create(null)),t in this.listeners_){var n=this.listeners_[t];n.indexOf(e)<0&&n.push(e)}else this.listeners_[t]=[e]},t.prototype.removeEventListener=function(t,e){if(this.listeners_&&t in this.listeners_){var n=this.listeners_[t],r=n.indexOf(e);r>=0&&(1===n.length?delete this.listeners_[t]:n.splice(r,1))}},t.prototype.dispatchEvent=function(t){if(!this.listeners_)return!0;var e=this;t.__defineGetter__("target",function(){return e});var n=t.type,r=0;if(n in this.listeners_)for(var i=this.listeners_[n].concat(),o=0,c=void 0;c=i[o];o++)r|=c.handleEvent?c.handleEvent.call(c,t)===!1:c.call(this,t)===!1;return!r&&!t.defaultPrevented},t}();n.BTSEventTarget=r},{}],3:[function(t,e,n){"use strict";function r(){var t=E++;return E&=255,t}function i(t){E=255&t}function o(t,e,n){for(var r=0;r<t.byteLength;r++){var i=e[r]+n&255;t[r]=y.TABLE1[i]}}function c(t,e){for(var n=new Uint8Array(t.byteLength),r=0;r<t.byteLength;r++){var i=255&t[r];n[r]=y.TABLE2[i]-e&255}return n}function s(t){for(var e=0,n=0;n<t.length;n++)e+=t[n];return 255&e}function a(t,e,n){if(n.length>w)throw"payload too long: "+n.length;var i=new ArrayBuffer(8+n.length),c=new Uint8Array(i),a=r();c[0]=y.MAGIC1,c[1]=y.MAGIC2,c[2]=5+n.length,c[3]=t,c[4]=a,c[5]=e,c[6]=255&n.length;var u=new Uint8Array(i,7,n.length);o(u,n,a);var h=new Uint8Array(i,3,n.length+4);return c[7+n.length]=s(h),i}function u(t){var e=t.byteLength;if(e){var n=new Uint8Array(t);if(8>e)throw"data too short: "+e;if(n[0]!==y.MAGIC1&&n[1]!==y.MAGIC2)throw"don't have the magic";var r=n[2],i=new Uint8Array(t.slice(3,e-1)),o=s(i);if(n[e-1]!==o)throw"checksum mismatch "+n[e-1]+" !== "+o;var a=n[3],u=n[4],h=n[5],l=n[6];if(r!==e-3)throw"length mismatch 1 "+r+" !== "+(e-3);if(l!==e-8)throw"length mismatch 2";var d=new Uint8Array(t.slice(7,e-1)),f=c(d,u);return new C(a,h,f)}}function h(t,e,n){void 0===t&&(t=1),void 0===e&&(e=100),void 0===n&&(n=1);var r=[255&t,255&e,255&n];return a(4,0,r)}function l(){var t=[0,0];return a(12,0,t)}function d(){var t=[5];return a(12,0,t)}function f(){var t=[6];return a(12,0,t)}function v(){var t=[7];return a(12,0,t)}function p(t){void 0===t&&(t=20);var e=[8,255&t];return a(12,0,e)}function g(){return a(2,0,[])}var y=t("./constants"),w=10,E=0;n.setSequenceId=i;var C=function(){function t(t,e,n){if(this.type=t,this.id=e,this.payload=n,this.value=null,5===t){for(var r=((255&n[1])<<8)+(255&n[0]),i=0;i<n[4];i++)r/=10;2===(2&n[6])&&(r*=-1),this.value=r}}return t}();n.Message=C,n.decode=u,n.encodeWeight=h,n.encodeTare=l,n.encodeStartTimer=d,n.encodePauseTimer=f,n.encodeStopTimer=v,n.encodeGetTimer=p,n.encodeGetBattery=g},{"./constants":1}],4:[function(t,e,n){"use strict";var r=function(){function t(t){this.start=Date.now()/1e3,this.series=[],this.scale=t,this.recordCb=this.record.bind(this),this.record(),t.addEventListener("weightMeasured",this.recordCb)}return t.prototype.stop=function(){return this.record(),this.scale.removeEventListener("weightMeasured",this.recordCb),this.scale=null,this.recordCb=null,this.series},t.prototype.record=function(){var t=Date.now()/1e3-this.start;this.series.push([t,this.scale.weight])},t}();n.Recorder=r},{}],5:[function(t,e,n){"use strict";var r=this&&this.__extends||function(t,e){function n(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)},i=t("./constants"),o=t("./event_target"),c=t("./recorder"),s=t("./packet"),a=function(t){function e(e){t.call(this),this.connected=!1,this.service=null,this.characteristic=null,this.weight=null,this.recorder=null,this.batteryCb=null,this.series=null,this.device=e,this.name=this.device.name,console.log("created scale for "+this.device.address+" ("+this.device.name+")"),this.connect()}return r(e,t),e.prototype.connect=function(){var t=this;if(!this.connected){var e=console.log.bind(console);this.device.gatt.connect().then(function(e){return t.device.gatt.getPrimaryService(i.SCALE_SERVICE_UUID)},function(t){return console.log("error connecting - "+t),null}).then(function(e){return t.service=e,console.log("primary services "),e.getCharacteristic(i.SCALE_CHARACTERISTIC_UUID)},function(t){console.log("primary services ERR - "+t)}).then(function(n){return e("Starting notifications..."),t.characteristic=n,n.startNotifications()},function(t){console.log("err getting characteristic")}).then(function(e){e.addEventListener("characteristicvaluechanged",t.characteristicValueChanged.bind(t)),t.notificationsReady()},function(t){e("FAILED: "+t)})}},e.prototype.characteristicValueChanged=function(t){var e=s.decode(t.target.value.buffer);if(!e)return void console.log("characteristic value update, but no message");if(5===e.type){var n=this.weight,r=this.weight!==e.value;this.weight=e.value;var i={detail:{value:e.value,previous:n}};this.dispatchEvent(new CustomEvent("weightMeasured",i)),r&&this.dispatchEvent(new CustomEvent("weightChanged",i))}else if(3===e.type){var o=this.batteryCb;this.batteryCb=null,o&&o(e.payload[0]/100)}else console.log("non-weight response"),console.log(e)},e.prototype.disconnect=function(){this.connected=!1,this.device&&this.device.gatt.connect()},e.prototype.notificationsReady=function(){console.log("scale ready"),this.connected=!0,this.poll(),setInterval(this.poll.bind(this),1e3),this.dispatchEvent(new CustomEvent("ready",{detail:{scale:this}}))},e.prototype.tare=function(){if(!this.connected)return!1;var t=s.encodeTare();return this.characteristic.writeValue(t).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.startTimer=function(){if(!this.connected)return!1;var t=s.encodeStartTimer();return this.characteristic.writeValue(t).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.pauseTimer=function(){if(!this.connected)return!1;var t=s.encodePauseTimer();return this.characteristic.writeValue(t).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.stopTimer=function(){if(!this.connected)return!1;var t=s.encodeStopTimer();return this.characteristic.writeValue(t).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.getTimer=function(t){if(!this.connected)return!1;t||(t=1);var e=s.encodeGetTimer(t);return this.characteristic.writeValue(e).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.getBattery=function(t){if(!this.connected)return!1;this.batteryCb=t;var e=s.encodeGetBattery();return this.characteristic.writeValue(e).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.poll=function(){if(!this.connected)return!1;var t=s.encodeWeight();return this.characteristic.writeValue(t).then(function(){},function(t){console.log("write failed: "+t)}),!0},e.prototype.startRecording=function(){this.recorder||(this.recorder=new c.Recorder(this))},e.prototype.stopRecording=function(){return this.series=this.recorder.stop(),this.recorder=null,this.series},e}(o.BTSEventTarget);n.Scale=a},{"./constants":1,"./event_target":2,"./packet":3,"./recorder":4}],6:[function(t,e,n){"use strict";var r=this&&this.__extends||function(t,e){function n(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)},i=t("./constants"),o=t("./event_target"),c=t("./scale"),s=navigator.bluetooth,a=function(t){function e(){t.call(this),this.ready=!1,this.devices={},this.scales=[],this.adapterState=null,this.failed=!1,console.log("new ScaleFinder")}return r(e,t),e.prototype.deviceAdded=function(t){if(t.address in this.devices)return void console.log("WARN: device added that is already known "+t.address);var e=new c.Scale(t);this.devices[t.address]=e,this.scales.push(e)},e.prototype.startDiscovery=function(){var t=this;this.failed||s.requestDevice({filters:[{services:[i.SCALE_SERVICE_UUID]}]}).then(function(e){t.deviceAdded(e)})},e.prototype.stopDiscovery=function(){this.failed},e}(o.BTSEventTarget);n.ScaleFinder=a,"undefined"!=typeof window&&(window.ScaleFinder=a)},{"./constants":1,"./event_target":2,"./scale":5}]},{},[6]);