"use strict";var e=require("events"),t=require("usb");function s(e,t,s,i){return new(s||(s=Promise))((function(n,a){function r(e){try{o(i.next(e))}catch(e){a(e)}}function c(e){try{o(i.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?n(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,c)}o((i=i.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class i{}i.MESSAGE_RF=1,i.MESSAGE_TX_SYNC=164,i.DEFAULT_NETWORK_NUMBER=0,i.MESSAGE_CHANNEL_UNASSIGN=65,i.MESSAGE_CHANNEL_ASSIGN=66,i.MESSAGE_CHANNEL_ID=81,i.MESSAGE_CHANNEL_PERIOD=67,i.MESSAGE_CHANNEL_SEARCH_TIMEOUT=68,i.MESSAGE_CHANNEL_FREQUENCY=69,i.MESSAGE_CHANNEL_TX_POWER=96,i.MESSAGE_NETWORK_KEY=70,i.MESSAGE_TX_POWER=71,i.MESSAGE_PROXIMITY_SEARCH=113,i.MESSAGE_ENABLE_RX_EXT=102,i.MESSAGE_LIB_CONFIG=110,i.MESSAGE_CHANNEL_OPEN_RX_SCAN=91,i.MESSAGE_STARTUP=111,i.MESSAGE_SYSTEM_RESET=74,i.MESSAGE_CHANNEL_OPEN=75,i.MESSAGE_CHANNEL_CLOSE=76,i.MESSAGE_CHANNEL_REQUEST=77,i.MESSAGE_CHANNEL_BROADCAST_DATA=78,i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA=79,i.MESSAGE_CHANNEL_BURST_DATA=80,i.MESSAGE_CHANNEL_EVENT=64,i.MESSAGE_CHANNEL_STATUS=82,i.MESSAGE_VERSION=62,i.MESSAGE_CAPABILITIES=84,i.MESSAGE_SERIAL_NUMBER=97,i.CHANNEL_TYPE_TWOWAY_RECEIVE=0,i.CHANNEL_TYPE_TWOWAY_TRANSMIT=16,i.CHANNEL_TYPE_SHARED_RECEIVE=32,i.CHANNEL_TYPE_SHARED_TRANSMIT=48,i.CHANNEL_TYPE_ONEWAY_RECEIVE=64,i.CHANNEL_TYPE_ONEWAY_TRANSMIT=80,i.RADIO_TX_POWER_MINUS20DB=0,i.RADIO_TX_POWER_MINUS10DB=1,i.RADIO_TX_POWER_0DB=2,i.RADIO_TX_POWER_PLUS4DB=3,i.RESPONSE_NO_ERROR=0,i.EVENT_RX_SEARCH_TIMEOUT=1,i.EVENT_RX_FAIL=2,i.EVENT_TX=3,i.EVENT_TRANSFER_RX_FAILED=4,i.EVENT_TRANSFER_TX_COMPLETED=5,i.EVENT_TRANSFER_TX_FAILED=6,i.EVENT_CHANNEL_CLOSED=7,i.EVENT_RX_FAIL_GO_TO_SEARCH=8,i.EVENT_CHANNEL_COLLISION=9,i.EVENT_TRANSFER_TX_START=10,i.CHANNEL_IN_WRONG_STATE=21,i.CHANNEL_NOT_OPENED=22,i.CHANNEL_ID_NOT_SET=24,i.CLOSE_ALL_CHANNELS=25,i.TRANSFER_IN_PROGRESS=31,i.TRANSFER_SEQUENCE_NUMBER_ERROR=32,i.TRANSFER_IN_ERROR=33,i.MESSAGE_SIZE_EXCEEDS_LIMIT=39,i.INVALID_MESSAGE=40,i.INVALID_NETWORK_NUMBER=41,i.INVALID_LIST_ID=48,i.INVALID_SCAN_TX_CHANNEL=49,i.INVALID_PARAMETER_PROVIDED=51,i.EVENT_QUEUE_OVERFLOW=53,i.USB_STRING_WRITE_FAIL=112,i.CHANNEL_STATE_UNASSIGNED=0,i.CHANNEL_STATE_ASSIGNED=1,i.CHANNEL_STATE_SEARCHING=2,i.CHANNEL_STATE_TRACKING=3,i.CAPABILITIES_NO_RECEIVE_CHANNELS=1,i.CAPABILITIES_NO_TRANSMIT_CHANNELS=2,i.CAPABILITIES_NO_RECEIVE_MESSAGES=4,i.CAPABILITIES_NO_TRANSMIT_MESSAGES=8,i.CAPABILITIES_NO_ACKNOWLEDGED_MESSAGES=16,i.CAPABILITIES_NO_BURST_MESSAGES=32,i.CAPABILITIES_NETWORK_ENABLED=2,i.CAPABILITIES_SERIAL_NUMBER_ENABLED=8,i.CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED=16,i.CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED=32,i.CAPABILITIES_SCRIPT_ENABLED=64,i.CAPABILITIES_SEARCH_LIST_ENABLED=128,i.CAPABILITIES_LED_ENABLED=1,i.CAPABILITIES_EXT_MESSAGE_ENABLED=2,i.CAPABILITIES_SCAN_MODE_ENABLED=4,i.CAPABILITIES_PROX_SEARCH_ENABLED=16,i.CAPABILITIES_EXT_ASSIGN_ENABLED=32,i.CAPABILITIES_FS_ANTFS_ENABLED=64,i.TIMEOUT_NEVER=255;class n{static resetSystem(){const e=[];return e.push(0),this.buildMessage(e,i.MESSAGE_SYSTEM_RESET)}static requestMessage(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s.push(t),this.buildMessage(s,i.MESSAGE_CHANNEL_REQUEST)}static setNetworkKey(){const e=[];return e.push(i.DEFAULT_NETWORK_NUMBER),e.push(185),e.push(165),e.push(33),e.push(251),e.push(189),e.push(114),e.push(195),e.push(69),this.buildMessage(e,i.MESSAGE_NETWORK_KEY)}static assignChannel(e,t="receive"){let s=[];if(s=s.concat(this.intToLEHexArray(e)),"receive"===t)s.push(i.CHANNEL_TYPE_TWOWAY_RECEIVE);else if("receive_only"===t)s.push(i.CHANNEL_TYPE_ONEWAY_RECEIVE);else if("receive_shared"===t)s.push(i.CHANNEL_TYPE_SHARED_RECEIVE);else if("transmit"===t)s.push(i.CHANNEL_TYPE_TWOWAY_TRANSMIT);else if("transmit_only"===t)s.push(i.CHANNEL_TYPE_ONEWAY_TRANSMIT);else{if("transmit_shared"!==t)throw new Error("type not allowed");s.push(i.CHANNEL_TYPE_SHARED_TRANSMIT)}return s.push(i.DEFAULT_NETWORK_NUMBER),this.buildMessage(s,i.MESSAGE_CHANNEL_ASSIGN)}static setDevice(e,t,s,n){let a=[];return a=a.concat(this.intToLEHexArray(e)),a=a.concat(this.intToLEHexArray(t,2)),a=a.concat(this.intToLEHexArray(s)),a=a.concat(this.intToLEHexArray(n)),this.buildMessage(a,i.MESSAGE_CHANNEL_ID)}static searchChannel(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s=s.concat(this.intToLEHexArray(t)),this.buildMessage(s,i.MESSAGE_CHANNEL_SEARCH_TIMEOUT)}static setPeriod(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s=s.concat(this.intToLEHexArray(t)),this.buildMessage(s,i.MESSAGE_CHANNEL_PERIOD)}static setFrequency(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s=s.concat(this.intToLEHexArray(t)),this.buildMessage(s,i.MESSAGE_CHANNEL_FREQUENCY)}static setRxExt(){let e=[];return e=e.concat(this.intToLEHexArray(0)),e=e.concat(this.intToLEHexArray(1)),this.buildMessage(e,i.MESSAGE_ENABLE_RX_EXT)}static libConfig(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s=s.concat(this.intToLEHexArray(t)),this.buildMessage(s,i.MESSAGE_LIB_CONFIG)}static openRxScan(){let e=[];return e=e.concat(this.intToLEHexArray(0)),e=e.concat(this.intToLEHexArray(1)),this.buildMessage(e,i.MESSAGE_CHANNEL_OPEN_RX_SCAN)}static openChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,i.MESSAGE_CHANNEL_OPEN)}static closeChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,i.MESSAGE_CHANNEL_CLOSE)}static unassignChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,i.MESSAGE_CHANNEL_UNASSIGN)}static acknowledgedData(e,t){return t=this.intToLEHexArray(e).concat(t),this.buildMessage(t,i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA)}static broadcastData(e,t){return t=this.intToLEHexArray(e).concat(t),this.buildMessage(t,i.MESSAGE_CHANNEL_BROADCAST_DATA)}static buildMessage(e=[],t=0){const s=[];return s.push(i.MESSAGE_TX_SYNC),s.push(e.length),s.push(t),e.forEach((e=>{s.push(e)})),s.push(this.getChecksum(s)),new Uint8Array(s)}static intToLEHexArray(e,t=1){t=t||1;const s=[],i=this.decimalToHex(e,2*t);for(let e=i.length-2;e>=0;e-=2)s.push(parseInt(i.substr(e,2),16));return s}static decimalToHex(e,t){let s=Number(e).toString(16);for(t=t||2;s.length{t=(t^e)%255})),t}}n.BUFFER_INDEX_MSG_LEN=1,n.BUFFER_INDEX_MSG_TYPE=2,n.BUFFER_INDEX_CHANNEL_NUM=3,n.BUFFER_INDEX_MSG_DATA=4,n.BUFFER_INDEX_EXT_MSG_BEGIN=12;class a extends e{constructor(e,s,i={}){super(),this.idVendor=e,this.idProduct=s,this.detachedKernelDriver=!1,this.usedChannels=0,this.attachedSensors=[],this.maxChannels=0,this._canScan=!1,this.throwLibUSBException=!1,this.setMaxListeners(50),t.usb.setDebugLevel(i.usbDebugLevel||0),this.throwLibUSBException=i.throwLibUSBException||!1}canAttach(){return s(this,void 0,void 0,(function*(){return Promise.resolve(this.usedChannels{this.onData(e).catch((e=>{console.error(e)}))})),this.inEndpoint.on("error",(e=>{console.error("ERROR RECV: ",e)})),this.inEndpoint.on("end",(()=>{})),this.inEndpoint.startPoll(),this.outEndpoint=this.iface.endpoints[1],yield this.reset(),Promise.resolve(!0)}))}close(){return s(this,void 0,void 0,(function*(){yield this.detachAll(),this.inEndpoint&&this.inEndpoint.stopPoll((()=>{this.iface&&this.iface.release(!0,(()=>{var e;if(this.detachedKernelDriver){this.detachedKernelDriver=!1;try{null===(e=this.iface)||void 0===e||e.attachKernelDriver()}catch(e){}}this.iface=void 0,this.device&&this.device.reset((()=>{var e;null===(e=this.device)||void 0===e||e.close(),this.emit("shutdown");const s=a.deviceInUse.indexOf(this.device);s>=0&&a.deviceInUse.splice(s,1),t.usb.listenerCount("attach")&&t.usb.emit("attach",this.device),this.device=void 0}))}))}))}))}read(e){return s(this,void 0,void 0,(function*(){const t=new DataView(e.buffer),s=t.getUint8(2);s===i.MESSAGE_STARTUP?yield this.write(n.requestMessage(0,i.MESSAGE_CAPABILITIES)):s===i.MESSAGE_CAPABILITIES?(this.maxChannels=t.getUint8(3),this._canScan=!(6&~t.getUint8(7)),yield this.write(n.setNetworkKey())):s===i.MESSAGE_CHANNEL_EVENT&&t.getUint8(4)===i.MESSAGE_NETWORK_KEY?this.emit("startup",e):this.emit("read",e)}))}write(e){return s(this,void 0,void 0,(function*(){yield new Promise(((t,s)=>{this.outEndpoint&&this.outEndpoint.transfer(Buffer.from(e),(e=>{e?(console.error("ERROR SEND: ",e),s(e)):t()}))}))}))}reset(){return s(this,void 0,void 0,(function*(){yield this.detachAll(),this.maxChannels=0,this.usedChannels=0,yield this.write(n.resetSystem())}))}attach(e,t){return s(this,void 0,void 0,(function*(){return this.usedChannels<0||t&&0!==this.usedChannels||!t&&this.maxChannels<=this.usedChannels?Promise.resolve(!1):(this.usedChannels=t?-1:this.usedChannels+1,this.attachedSensors.push(e),Promise.resolve(!0))}))}detach(e){return s(this,void 0,void 0,(function*(){const t=this.attachedSensors.indexOf(e);return t<0?Promise.resolve(!1):(this.usedChannels=this.usedChannels<0?0:this.usedChannels-1,this.attachedSensors.splice(t,1),Promise.resolve(!0))}))}isPresent(){return s(this,void 0,void 0,(function*(){return Promise.resolve(this.getDevices().length>0)}))}isScanning(){return s(this,void 0,void 0,(function*(){return Promise.resolve(-1===this.usedChannels)}))}getDevices(){return t.getDeviceList().filter((e=>e.deviceDescriptor.idVendor===this.idVendor&&e.deviceDescriptor.idProduct===this.idProduct)).filter((e=>-1===a.deviceInUse.indexOf(e)))}detachAll(){return s(this,void 0,void 0,(function*(){const e=this.attachedSensors;for(const t of e)yield t.detach()}))}onData(e){return s(this,void 0,void 0,(function*(){if(!e.length)return;this.leftover&&(e=this.concatUint8Arrays(this.leftover,e),this.leftover=void 0);const t=new DataView(e.buffer);if(164!==t.getUint8(0))throw new Error("SYNC missing");const s=e.length;let i=0;for(;is){this.leftover=e.slice(i);break}const a=e.slice(i,n);yield this.read(a),i=n}}))}concatUint8Arrays(e,t){const s=new Uint8Array(e.length+t.length);return s.set(e,0),s.set(t,e.length),s}}a.deviceInUse=[];const r=[{vendorId:4047,productId:4104},{vendorId:4047,productId:4105}];class c extends e{constructor(){super(),this.usedChannels=0,this.attachedSensors=[],this.maxChannels=0,this._canScan=!1,this.setMaxListeners(50),this.abortController=new AbortController,this.signal=this.abortController.signal}canAttach(){return s(this,void 0,void 0,(function*(){return Promise.resolve(this.usedChannels"in"===e.direction)),this.outEndpoint=this.iface.alternate.endpoints.find((e=>"out"===e.direction)),!this.inEndpoint||!this.outEndpoint)throw new Error("In or Out endpoint not found.");return yield this.reset(),yield this.readLoop(),!0}catch(e){return console.log(e),yield this.close(),!1}}))}readLoop(){return s(this,void 0,void 0,(function*(){var e;if(!this.signal.aborted&&this.inEndpoint){try{const t=yield null===(e=this.device)||void 0===e?void 0:e.transferIn(this.inEndpoint.endpointNumber,this.inEndpoint.packetSize);if(!t||!t.data)return this.readLoop();let s=new Uint8Array(t.data.buffer);this.leftover&&(s=this.concatUint8Arrays(this.leftover,s),this.leftover=void 0);const i=new DataView(s.buffer);if(164!==i.getUint8(0))return console.error("SYNC missing"),this.readLoop();let n=0;const a=s.length;for(;na){this.leftover=s.slice(n);break}const t=s.slice(n,e);yield this.read(t),n=e}}catch(e){if(!this.signal.aborted)throw e}return this.readLoop()}}))}close(){return s(this,void 0,void 0,(function*(){if(this.cancelReadLoop(),yield this.detachAll(),this.device){try{yield this.device.close()}catch(e){console.error("Error closing device:",e)}const e=c.deviceInUse.indexOf(this.device);e>=0&&c.deviceInUse.splice(e,1),this.emit("shutdown"),this.device=void 0}}))}cancelReadLoop(){this.signal.aborted||(this.abortController.abort(),console.log("Read loop abort requested."))}read(e){return s(this,void 0,void 0,(function*(){const t=new DataView(e.buffer),s=t.getUint8(2);s===i.MESSAGE_STARTUP?yield this.write(n.requestMessage(0,i.MESSAGE_CAPABILITIES)):s===i.MESSAGE_CAPABILITIES?(this.maxChannels=t.getUint8(3),this._canScan=!(6&~t.getUint8(7)),yield this.write(n.setNetworkKey())):s===i.MESSAGE_CHANNEL_EVENT&&t.getUint8(4)===i.MESSAGE_NETWORK_KEY?this.emit("startup",e):this.emit("read",e)}))}write(e){return s(this,void 0,void 0,(function*(){this.device&&this.outEndpoint&&(yield this.device.transferOut(this.outEndpoint.endpointNumber,e))}))}reset(){return s(this,void 0,void 0,(function*(){yield this.detachAll(),this.maxChannels=0,this.usedChannels=0,yield this.write(n.resetSystem())}))}attach(e,t){return s(this,void 0,void 0,(function*(){return this.usedChannels<0||t&&0!==this.usedChannels||!t&&this.maxChannels<=this.usedChannels?Promise.resolve(!1):(this.usedChannels=t?-1:this.usedChannels+1,this.attachedSensors.push(e),Promise.resolve(!0))}))}detach(e){return s(this,void 0,void 0,(function*(){const t=this.attachedSensors.indexOf(e);return t<0?Promise.resolve(!1):(this.usedChannels=this.usedChannels<0?0:this.usedChannels-1,this.attachedSensors.splice(t,1),Promise.resolve(!0))}))}isPresent(){return s(this,void 0,void 0,(function*(){return Promise.resolve(!!this.device)}))}isScanning(){return s(this,void 0,void 0,(function*(){return Promise.resolve(-1===this.usedChannels)}))}detachAll(){return s(this,void 0,void 0,(function*(){for(const e of this.attachedSensors.slice())yield e.detach();this.attachedSensors=[]}))}concatUint8Arrays(e,t){const s=new Uint8Array(e.length+t.length);return s.set(e,0),s.set(t,e.length),s}}c.deviceInUse=[];function o(e){"undefined"!=typeof process&&"function"==typeof process.nextTick?process.nextTick(e):"function"==typeof setImmediate?setImmediate(e):setTimeout(e,0)}class E extends e.EventEmitter{constructor(e){super(),this.stick=e,this.messageQueue=[],e.on("read",(e=>{this.handleEventMessages(e).catch((e=>{console.error(e)}))}))}scan(e,t){return s(this,void 0,void 0,(function*(){if(void 0!==this.channel)throw new Error("Already attached");if(!this.stick.canScan)throw new Error("Stick cannot scan");const a=e=>s(this,void 0,void 0,(function*(){switch(e.msg){case i.MESSAGE_RF:switch(e.code){case i.EVENT_CHANNEL_CLOSED:case i.EVENT_RX_FAIL_GO_TO_SEARCH:return yield this.write(n.unassignChannel(0)),!0;case i.EVENT_TRANSFER_TX_COMPLETED:case i.EVENT_TRANSFER_TX_FAILED:case i.EVENT_RX_FAIL:case i.INVALID_SCAN_TX_CHANNEL:{const t=this.messageQueue.shift();return t&&t.cbk&&t.cbk(e.code===i.EVENT_TRANSFER_TX_COMPLETED),this.messageQueue.length&&(yield this.write(this.messageQueue[0].msg)),!0}}break;case i.MESSAGE_CHANNEL_ASSIGN:return yield this.write(n.setDevice(0,0,0,0)),!0;case i.MESSAGE_CHANNEL_ID:return yield this.write(n.setFrequency(0,t)),!0;case i.MESSAGE_CHANNEL_FREQUENCY:return yield this.write(n.setRxExt()),!0;case i.MESSAGE_ENABLE_RX_EXT:return yield this.write(n.libConfig(0,224)),!0;case i.MESSAGE_LIB_CONFIG:return yield this.write(n.openRxScan()),!0;case i.MESSAGE_CHANNEL_OPEN_RX_SCAN:return o((()=>this.emit("attached"))),!0;case i.MESSAGE_CHANNEL_CLOSE:return!0;case i.MESSAGE_CHANNEL_UNASSIGN:return this.statusCbk=void 0,this.channel=void 0,o((()=>this.emit("detached"))),!0;case i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:return e.code===i.TRANSFER_IN_PROGRESS}return!1}));if(yield this.stick.isScanning())this.channel=0,this.deviceId=0,this.transmissionType=0,this.statusCbk=a,o((()=>this.emit("attached")));else{if(!(yield this.stick.attach(this,!0))){const e=(yield this.stick.canAttach())?"Cannot attach: Device is not scanning, and an unknown error occurred during attachment.":"Cannot attach: Maximum number of channels reached, unable to attach more sensors.";throw new Error(e)}this.channel=0,this.deviceId=0,this.transmissionType=0,this.statusCbk=a,yield this.write(n.assignChannel(0,e))}}))}attachSensor(e,t,a,r,c,E,_,d){return s(this,void 0,void 0,(function*(){if(void 0!==this.channel)throw new Error("Already attached");if(!(yield this.stick.attach(this,!1))){const e=(yield this.stick.canAttach())?"Cannot attach: Device is not in scanning mode or an unknown error occurred.":"Cannot attach: Maximum number of channels reached, unable to attach more sensors.";throw new Error(e)}this.channel=e,this.deviceId=a,this.transmissionType=c;this.statusCbk=t=>s(this,void 0,void 0,(function*(){switch(t.msg){case i.MESSAGE_RF:switch(t.code){case i.EVENT_CHANNEL_CLOSED:case i.EVENT_RX_FAIL_GO_TO_SEARCH:return yield this.write(n.unassignChannel(e)),!0;case i.EVENT_TRANSFER_TX_COMPLETED:case i.EVENT_TRANSFER_TX_FAILED:case i.EVENT_RX_FAIL:case i.INVALID_SCAN_TX_CHANNEL:{const e=this.messageQueue.shift();return e&&e.cbk&&e.cbk(t.code===i.EVENT_TRANSFER_TX_COMPLETED),this.messageQueue.length&&(yield this.write(this.messageQueue[0].msg)),!0}case i.EVENT_CHANNEL_COLLISION:return!0}break;case i.MESSAGE_CHANNEL_ASSIGN:return yield this.write(n.setDevice(e,a,r,c)),!0;case i.MESSAGE_CHANNEL_ID:return yield this.write(n.searchChannel(e,E)),!0;case i.MESSAGE_CHANNEL_SEARCH_TIMEOUT:return yield this.write(n.setFrequency(e,d)),!0;case i.MESSAGE_CHANNEL_FREQUENCY:return yield this.write(n.setPeriod(e,_)),!0;case i.MESSAGE_CHANNEL_PERIOD:return yield this.write(n.libConfig(e,224)),!0;case i.MESSAGE_LIB_CONFIG:return yield this.write(n.openChannel(e)),!0;case i.MESSAGE_CHANNEL_OPEN:return o((()=>this.emit("attached"))),!0;case i.MESSAGE_CHANNEL_CLOSE:return!0;case i.MESSAGE_CHANNEL_UNASSIGN:return this.statusCbk=void 0,this.channel=void 0,o((()=>this.emit("detached"))),!0;case i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:return t.code===i.TRANSFER_IN_PROGRESS}return!1})),yield this.write(n.assignChannel(e,t))}))}detach(){return s(this,void 0,void 0,(function*(){if(void 0===this.channel)return;yield this.write(n.closeChannel(this.channel));if(!(yield this.stick.detach(this)))throw new Error("Error on detaching")}))}write(e){return s(this,void 0,void 0,(function*(){yield this.stick.write(e)}))}handleEventMessages(e){return s(this,void 0,void 0,(function*(){const t=new DataView(e.buffer),s=t.getUint8(n.BUFFER_INDEX_MSG_TYPE);if(t.getUint8(n.BUFFER_INDEX_CHANNEL_NUM)===this.channel)if(s===i.MESSAGE_CHANNEL_EVENT){const s={msg:t.getUint8(n.BUFFER_INDEX_MSG_DATA),code:t.getUint8(n.BUFFER_INDEX_MSG_DATA+1)};this.statusCbk&&this.statusCbk(s)||(console.log("Unhandled event: "+Array.from(e).map((e=>e.toString(16))).join(" ")),this.emit("eventData",{message:t.getUint8(n.BUFFER_INDEX_MSG_DATA),code:t.getUint8(n.BUFFER_INDEX_MSG_DATA+1)}))}else this.decodeDataCbk&&(yield this.decodeDataCbk(t))}))}send(e,t){return s(this,void 0,void 0,(function*(){this.messageQueue.push({msg:e,cbk:t}),1===this.messageQueue.length&&(yield this.write(e))}))}}var _;!function(e){e[e.INIT_PAGE=0]="INIT_PAGE",e[e.STD_PAGE=1]="STD_PAGE",e[e.EXT_PAGE=2]="EXT_PAGE"}(_||(_={}));function d(e,t,s,i){const a=i.getUint8(n.BUFFER_INDEX_MSG_DATA);if(s.pageState===_.INIT_PAGE)s.pageState=_.STD_PAGE;else if(a!==s.oldPage||s.pageState===_.EXT_PAGE)switch(s.pageState=_.EXT_PAGE,-129&a){case 1:t.OperatingTime=i.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=i.getUint8(n.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=i.getUint8(n.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=i.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=i.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=i.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=i.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=i.getUint8(n.BUFFER_INDEX_MSG_DATA+3);break;case 4:t.PreviousBeat=i.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0);break;case 5:t.IntervalAverage=i.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.IntervalMax=i.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.SessionAverage=i.getUint8(n.BUFFER_INDEX_MSG_DATA+3);break;case 6:t.SupportedFeatures=i.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.EnabledFeatures=i.getUint8(n.BUFFER_INDEX_MSG_DATA+3);break;case 7:{const e=i.getUint8(n.BUFFER_INDEX_MSG_DATA+1),s=i.getUint8(n.BUFFER_INDEX_MSG_DATA+2),a=i.getUint8(n.BUFFER_INDEX_MSG_DATA+3);255!==e&&(t.BatteryLevel=e),t.BatteryVoltage=(15&a)+s/256;const r=(112&a)>>>4;switch(t.BatteryStatusBit=r,r){case 1:t.BatteryStatus="New";break;case 2:t.BatteryStatus="Good";break;case 3:t.BatteryStatus="Ok";break;case 4:t.BatteryStatus="Low";break;case 5:t.BatteryStatus="Critical";break;default:t.BatteryVoltage=void 0,t.BatteryStatus="Invalid"}break}}!function(e,t){e.BeatTime=t.getUint16(0,!0),e.BeatCount=t.getUint8(2),e.ComputedHeartRate=t.getUint8(3)}(t,new DataView(i.buffer.slice(n.BUFFER_INDEX_MSG_DATA+4))),s.oldPage=a,e.emit("heartRateData",t)}class h{constructor(e){this.DeviceId=e}}class S extends h{}class A extends E{scan(e){const t=Object.create(null,{scan:{get:()=>super.scan}});return s(this,void 0,void 0,(function*(){return yield t.scan.call(this,e,57)}))}attachSensor(e,t,i,n,a,r,c){const o=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){return yield o.attachSensor.call(this,e,t,i,n,a,r,c,57)}))}}class u extends A{constructor(e){super(e),this.decodeDataCbk=this.decodeData.bind(this)}scan(){throw new Error("scanning unsupported")}attachSensor(e,t,i,n,a,r,c){const o=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){return yield o.attachSensor.call(this,e,t,i,n,a,r,c)}))}decodeData(e){return s(this,void 0,void 0,(function*(){switch(e.getUint8(n.BUFFER_INDEX_MSG_TYPE)){case i.MESSAGE_CHANNEL_BROADCAST_DATA:case i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:case i.MESSAGE_CHANNEL_BURST_DATA:0===this.deviceId&&null!=this.channel&&(yield this.write(n.requestMessage(this.channel,i.MESSAGE_CHANNEL_ID))),this.updateState(this.deviceId,e);break;case i.MESSAGE_CHANNEL_ID:this.deviceId=e.getUint16(n.BUFFER_INDEX_MSG_DATA,!0),this.transmissionType=e.getUint8(n.BUFFER_INDEX_MSG_DATA+3)}}))}}class l extends u{constructor(){super(...arguments),this.page={oldPage:-1,pageState:_.INIT_PAGE}}attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,l.deviceType,0,255,8070),this.state=new h(t)}))}updateState(e,t){this.state.DeviceId=e,d(this,this.state,this.page,t)}}l.deviceType=120;class N extends A{constructor(e){super(e),this.decodeDataCbk=this.decodeData.bind(this)}scan(){const e=Object.create(null,{scan:{get:()=>super.scan}});return s(this,void 0,void 0,(function*(){return yield e.scan.call(this,"receive")}))}attach(){throw new Error("attach unsupported")}send(){throw new Error("send unsupported")}decodeData(e){return s(this,void 0,void 0,(function*(){if(e.byteLength<=n.BUFFER_INDEX_EXT_MSG_BEGIN+3||!(128&e.getUint8(n.BUFFER_INDEX_EXT_MSG_BEGIN))){const t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return void console.log("wrong message format",Array.from(t).map((e=>e.toString(16))).join(" "))}const t=e.getUint16(n.BUFFER_INDEX_EXT_MSG_BEGIN+1,!0);console.log(this.deviceId);if(e.getUint8(n.BUFFER_INDEX_EXT_MSG_BEGIN+3)===this.deviceType()){if(this.createStateIfNew(t),64&e.getUint8(n.BUFFER_INDEX_EXT_MSG_BEGIN)&&32===e.getUint8(n.BUFFER_INDEX_EXT_MSG_BEGIN+5)){const s=e.getInt8(n.BUFFER_INDEX_EXT_MSG_BEGIN+6),i=e.getInt8(n.BUFFER_INDEX_EXT_MSG_BEGIN+7);this.updateRssiAndThreshold(t,s,i)}switch(e.getUint8(n.BUFFER_INDEX_MSG_TYPE)){case i.MESSAGE_CHANNEL_BROADCAST_DATA:case i.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:case i.MESSAGE_CHANNEL_BURST_DATA:this.updateState(t,e)}}}))}}function T(e,t,s){const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA);if(1===i)t.TimeFractional=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.TimeInteger=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.DistanceInteger=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),t.DistanceFractional=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4)>>>4,t.SpeedInteger=15&s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),t.SpeedFractional=s.getUint8(n.BUFFER_INDEX_MSG_DATA+5),t.StrideCount=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6),t.UpdateLatency=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);else if(i>=2&&i<=15&&(t.CadenceInteger=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),t.CadenceFractional=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4)>>>4,t.SpeedInteger=15&s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),t.SpeedFractional=s.getUint8(n.BUFFER_INDEX_MSG_DATA+5),t.Status=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7),3===i))t.Calories=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6);e.emit("ssddata",t),e.emit("ssdData",t)}class D{constructor(e){this.DeviceId=e}}class I extends D{}class U extends u{attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,U.deviceType,0,255,8134),this.state=new D(t)}))}updateState(e,t){this.state.DeviceId=e,T(this,this.state,t)}}U.deviceType=124;function R(e,t,s){var i,a,r,c;const o=null!==(i=t.CadenceEventTime)&&void 0!==i?i:0,E=null!==(a=t.CumulativeCadenceRevolutionCount)&&void 0!==a?a:0,_=null!==(r=t.SpeedEventTime)&&void 0!==r?r:0,d=null!==(c=t.CumulativeSpeedRevolutionCount)&&void 0!==c?c:0;let h=s.getUint16(n.BUFFER_INDEX_MSG_DATA,!0),S=s.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0),A=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),u=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);if(h!==o){t.CadenceEventTime=h,t.CumulativeCadenceRevolutionCount=S,o>h&&(h+=65536),E>S&&(S+=65536);const s=60*(S-E)*1024/(h-o);isNaN(s)||(t.CalculatedCadence=s,e.emit("cadenceData",t))}if(A!==_){t.SpeedEventTime=A,t.CumulativeSpeedRevolutionCount=u,_>A&&(A+=65536),d>u&&(u+=65536);const s=e.wheelCircumference*(u-d);t.CalculatedDistance=s;const i=1024*s/(A-_);isNaN(i)||(t.CalculatedSpeed=i,e.emit("speedData",t))}}class F{constructor(e){this.DeviceId=e}}class v extends F{}class g extends u{constructor(){super(...arguments),this.wheelCircumference=2.199}setWheelCircumference(e){this.wheelCircumference=e}attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,g.deviceType,0,255,8086),this.state=new F(t)}))}updateState(e,t){this.state.DeviceId=e,R(this,this.state,t)}}g.deviceType=121;function M(e,t,s){var i,a;switch(-129&s.getUint8(n.BUFFER_INDEX_MSG_DATA)){case 1:t.OperatingTime=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=s.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);break;case 4:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);t.BatteryVoltage=(15&i)+e/256;const a=(112&i)>>>4;switch(t.BatteryStatusBit=a,a){case 1:t.BatteryStatus="New";break;case 2:t.BatteryStatus="Good";break;case 3:t.BatteryStatus="Ok";break;case 4:t.BatteryStatus="Low";break;case 5:t.BatteryStatus="Critical";break;default:t.BatteryVoltage=void 0,t.BatteryStatus="Invalid"}break}case 5:t.Motion=!(1&~s.getUint8(n.BUFFER_INDEX_MSG_DATA+1))}const r=null!==(i=t.SpeedEventTime)&&void 0!==i?i:0,c=null!==(a=t.CumulativeSpeedRevolutionCount)&&void 0!==a?a:0;let o=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),E=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);if(o!==r){t.SpeedEventTime=o,t.CumulativeSpeedRevolutionCount=E,r>o&&(o+=65536),c>E&&(E+=65536);const s=e.wheelCircumference*(E-c);t.CalculatedDistance=s;const i=1024*s/(o-r);isNaN(i)||(t.CalculatedSpeed=i,e.emit("speedData",t))}}class C{constructor(e){this.DeviceId=e}}class p extends C{}class f extends u{constructor(){super(...arguments),this.wheelCircumference=2.199}setWheelCircumference(e){this.wheelCircumference=e}attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,f.deviceType,0,255,8118),this.state=new C(t)}))}updateState(e,t){this.state.DeviceId=e,M(this,this.state,t)}}f.deviceType=123;function G(e,t,s){var i,a;switch(-129&s.getUint8(n.BUFFER_INDEX_MSG_DATA)){case 1:t.OperatingTime=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=s.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);break;case 4:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);t.BatteryVoltage=(15&i)+e/256;const a=(112&i)>>>4;switch(t.BatteryStatusBit=a,a){case 1:t.BatteryStatus="New";break;case 2:t.BatteryStatus="Good";break;case 3:t.BatteryStatus="Ok";break;case 4:t.BatteryStatus="Low";break;case 5:t.BatteryStatus="Critical";break;default:t.BatteryVoltage=void 0,t.BatteryStatus="Invalid"}break}case 5:t.Motion=!(1&~s.getUint8(n.BUFFER_INDEX_MSG_DATA+1))}const r=null!==(i=t.CadenceEventTime)&&void 0!==i?i:0,c=null!==(a=t.CumulativeCadenceRevolutionCount)&&void 0!==a?a:0;let o=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),E=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);if(o!==r){t.CadenceEventTime=o,t.CumulativeCadenceRevolutionCount=E,r>o&&(o+=65536),c>E&&(E+=65536);const s=60*(E-c)*1024/(o-r);isNaN(s)||(t.CalculatedCadence=s,e.emit("cadenceData",t))}}class B{constructor(e){this.DeviceId=e}}class m extends B{}class y extends u{constructor(){super(...arguments),this.wheelCircumference=2.199}attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,y.deviceType,0,255,8102),this.state=new B(t)}))}setWheelCircumference(e){this.wheelCircumference=e}updateState(e,t){this.state.DeviceId=e,G(this,this.state,t)}}y.deviceType=122;function b(e,t,s){var i,a,r;switch(s.getUint8(n.BUFFER_INDEX_MSG_DATA)){case 1:if(16===s.getUint8(n.BUFFER_INDEX_MSG_DATA+1)){1===s.getUint8(n.BUFFER_INDEX_MSG_DATA+2)&&(t.offset=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0))}break;case 16:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2);255!==e?128&e?(t.PedalPower=127&e,t.RightPedalPower=t.PedalPower,t.LeftPedalPower=100-t.RightPedalPower):(t.PedalPower=127&e,t.RightPedalPower=void 0,t.LeftPedalPower=void 0):(t.PedalPower=void 0,t.RightPedalPower=void 0,t.LeftPedalPower=void 0);const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);t.Cadence=255!==i?i:void 0,t.AccumulatedPower=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),t.Power=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);break}case 32:{const e=null!==(i=t.EventCount)&&void 0!==i?i:0,c=null!==(a=t.TimeStamp)&&void 0!==a?a:0,o=null!==(r=t.TorqueTicksStamp)&&void 0!==r?r:0;let E=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1);const _=s.getUint16(n.BUFFER_INDEX_MSG_DATA+3,!0);let d=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),h=s.getUint16(n.BUFFER_INDEX_MSG_DATA+7,!0);if(d!==c&&E!==e){t.EventCount=E,e>E&&(E+=255),t.TimeStamp=d,c>d&&(d+=65400),t.Slope=_,t.TorqueTicksStamp=h,o>h&&(h+=65535);const s=5e-4*(d-c),i=h-o,n=s/(E-e),a=Math.round(60/n);t.CalculatedCadence=a;const r=(1/(s/i)-t.offset)/(_/10);t.CalculatedTorque=r,t.CalculatedPower=r*a*Math.PI/30}break}default:return}e.emit("powerData",t)}class L{constructor(e){this.offset=0,this.DeviceId=e}}class w extends L{}class X extends u{attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,X.deviceType,0,255,8182),this.state=new L(t)}))}updateState(e,t){this.state.DeviceId=e,b(this,this.state,t)}}X.deviceType=11;function k(e){delete e.ElapsedTime,delete e.Distance,delete e.RealSpeed,delete e.VirtualSpeed,delete e.HeartRate,delete e.HeartRateSource,delete e.CycleLength,delete e.Incline,delete e.Resistance,delete e.METs,delete e.CaloricBurnRate,delete e.Calories,delete e._EventCount0x19,delete e._EventCount0x1A,delete e.Cadence,delete e.AccumulatedPower,delete e.InstantaneousPower,delete e.AveragePower,delete e.TrainerStatus,delete e.TargetStatus,delete e.AscendedDistance,delete e.DescendedDistance,delete e.Strides,delete e.Strokes,delete e.WheelTicks,delete e.WheelPeriod,delete e.Torque}function P(e,t,s){switch(s.getUint8(n.BUFFER_INDEX_MSG_DATA)){case 1:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);255!==e&&(t.Temperature=.5*e-25);const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1);64&i&&(t.ZeroOffset=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0)),128&i&&(t.SpinDownTime=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0));break}case 16:{switch(31&s.getUint8(n.BUFFER_INDEX_MSG_DATA+1)){case 19:t.EquipmentType="Treadmill";break;case 20:t.EquipmentType="Elliptical";break;case 21:t.EquipmentType="Reserved";break;case 22:t.EquipmentType="Rower";break;case 23:t.EquipmentType="Climber";break;case 24:t.EquipmentType="NordicSkier";break;case 25:t.EquipmentType="Trainer/StationaryBike";break;default:t.EquipmentType="General"}let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);const a=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6),c=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==r)switch(3&c){case 3:t.HeartRate=r,t.HeartRateSource="HandContact";break;case 2:t.HeartRate=r,t.HeartRateSource="EM";break;case 1:t.HeartRate=r,t.HeartRateSource="ANT+";break;default:delete t.HeartRate,delete t.HeartRateSource}e/=4;const o=(t.ElapsedTime||0)%64;if(e!==o&&o>e&&(e+=64),t.ElapsedTime=(t.ElapsedTime||0)+e-o,4&c){const e=(t.Distance||0)%256;i!==e&&e>i&&(i+=256),t.Distance=(t.Distance||0)+i-e}else delete t.Distance;switch(8&c?(t.VirtualSpeed=a/1e3,delete t.RealSpeed):(delete t.VirtualSpeed,t.RealSpeed=a/1e3),(112&c)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 17:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),i=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);switch(255!==e&&(t.CycleLength=e/100),i>=-1e4&&i<=1e4&&(t.Incline=i/100),255!==a&&(t.Resistance=a),(112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 18:{const e=s.getUint16(n.BUFFER_INDEX_MSG_DATA+2,!0),i=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);switch(65535!==e&&(t.METs=e/100),65535!==i&&(t.CaloricBurnRate=i/10),1&r&&(t.Calories=a),(112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 19:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4);let i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+5),a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6);const r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==e&&(t.Cadence=e),2&r){const e=(t.DescendedDistance||0)%256;i!==e&&e>i&&(i+=256),t.DescendedDistance=(t.DescendedDistance||0)+i-e}if(1&r){const e=(t.AscendedDistance||0)%256;a!==e&&e>a&&(a+=256),t.AscendedDistance=(t.AscendedDistance||0)+a-e}switch((112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 20:{let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);const a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),r=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),c=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==a&&(t.Cadence=a),65535!==r&&(t.InstantaneousPower=r),2&c){const s=(t.AscendedDistance||0)%256;e!==s&&s>e&&(e+=256),t.AscendedDistance=(t.AscendedDistance||0)+e-s}if(1&c){const e=(t.Strides||0)%256;i!==e&&e>i&&(i+=256),t.Strides=(t.Strides||0)+i-e}switch((112&c)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 22:{let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),a=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==a&&(t.InstantaneousPower=a),1&r){const s=(t.Strokes||0)%256;e!==s&&s>e&&(e+=256),t.Strokes=(t.Strokes||0)+e-s}switch((112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 23:{let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),a=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==a&&(t.InstantaneousPower=a),1&r){const s=(t.Strides||0)%256;e!==s&&s>e&&(e+=256),t.Strides=(t.Strides||0)+e-s}switch((112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 24:{let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+4),a=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==a&&(t.InstantaneousPower=a),1&r){const s=(t.Strides||0)%256;e!==s&&s>e&&(e+=256),t.Strides=(t.Strides||0)+e-s}switch((112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 25:{const e=t._EventCount0x19||0;let i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1);const a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2);let r=s.getUint16(n.BUFFER_INDEX_MSG_DATA+3,!0);const c=4095&s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0),o=s.getUint8(n.BUFFER_INDEX_MSG_DATA+6)>>4,E=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);if(i!==e&&(t._EventCount0x19=i,e>i&&(i+=255)),255!==a&&(t.Cadence=a),4095!==c){t.InstantaneousPower=c;const s=(t.AccumulatedPower||0)%65536;r!==s&&s>r&&(r+=65536),t.AccumulatedPower=(t.AccumulatedPower||0)+r-s,t.AveragePower=(r-s)/(i-e)}switch(t.TrainerStatus=o,3&E){case 0:t.TargetStatus="OnTarget";break;case 1:t.TargetStatus="LowSpeed";break;case 2:t.TargetStatus="HighSpeed";break;default:delete t.TargetStatus}switch((112&E)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 26:{const e=t._EventCount0x1A||0;let i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),r=s.getUint16(n.BUFFER_INDEX_MSG_DATA+3,!0),c=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0);const o=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);i!==e&&(t._EventCount0x1A=i,e>i&&(i+=255));const E=(t.WheelTicks||0)%256;a!==E&&E>a&&(a+=65536),t.WheelTicks=(t.WheelTicks||0)+a-E;const _=(t.WheelPeriod||0)%256;r!==_&&_>r&&(r+=65536),t.WheelPeriod=(t.WheelPeriod||0)+r-_;const d=(t.Torque||0)%256;switch(c!==d&&d>c&&(c+=65536),t.Torque=(t.Torque||0)+c-d,(112&o)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",k(t);break;case 3:t.State="IN_USE";break;case 4:t.State="FINISHED";break;default:delete t.State}break}case 80:t.HwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),t.ManId=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),t.ModelNum=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);break;case 81:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),a=s.getUint32(n.BUFFER_INDEX_MSG_DATA+4,!0);t.SwVersion=i,255!==e&&(t.SwVersion+=e/1e3),4294967295!==a&&(t.SerialNumber=a);break}case 86:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),r=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0);s.getUint8(n.BUFFER_INDEX_MSG_DATA+6);const c=s.getUint8(n.BUFFER_INDEX_MSG_DATA+7);0===e&&(t.PairedDevices=[]),i>0&&t.PairedDevices.push({id:r,type:c,paired:!!(128&a)});break}default:return}e.emit("fitnessData",t)}class H{constructor(e){this.PairedDevices=[],this.DeviceId=e}}class O extends H{}class x extends u{attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,x.deviceType,0,255,8192),this.state=new H(t)}))}updateState(e,t){this.state.DeviceId=e,P(this,this.state,t)}setUserConfigurationInternal(e,t,i,a,r){return s(this,void 0,void 0,(function*(){const s=null==e?65535:Math.max(0,Math.min(65534,Math.round(100*e))),c=null==i?255:Math.round(10*i)%10,o=null==t?4095:Math.max(0,Math.min(1e3,Math.round(20*t))),E=[55,255&s,s>>8&255,255,15&c|(15&o)<<4,o>>4&15,255&(null==i?255:Math.max(0,Math.min(254,Math.round(i)))),255&(null==a?0:Math.max(1,Math.min(255,Math.round(a/.03))))],_=n.acknowledgedData(this.channel,E);yield this.send(_,r)}))}setUserConfiguration(e,t,i,n,a){return s(this,void 0,void 0,(function*(){"function"==typeof e?yield this.setUserConfigurationInternal(void 0,void 0,void 0,void 0,e):"function"==typeof t?yield this.setUserConfigurationInternal(e,void 0,void 0,void 0,t):"function"==typeof i?yield this.setUserConfigurationInternal(e,t,void 0,void 0,i):"function"==typeof n?yield this.setUserConfigurationInternal(e,t,i,void 0,n):yield this.setUserConfigurationInternal(e,t,i,n,a)}))}setBasicResistance(e,t){return s(this,void 0,void 0,(function*(){const s=[48,255,255,255,255,255,255,255&Math.max(0,Math.min(200,Math.round(2*e)))],i=n.acknowledgedData(this.channel,s);yield this.send(i,t)}))}setTargetPower(e,t){return s(this,void 0,void 0,(function*(){const s=Math.max(0,Math.min(4e3,Math.round(4*e))),i=[49,255,255,255,255,255,255&s,s>>8&255],a=n.acknowledgedData(this.channel,i);yield this.send(a,t)}))}setWindResistanceInternal(e,t,i,a){return s(this,void 0,void 0,(function*(){const s=[50,255,255,255,255,255&(null==e?255:Math.max(0,Math.min(186,Math.round(100*e)))),255&(null==t?255:Math.max(0,Math.min(254,Math.round(t+127)))),255&(null==i?255:Math.max(0,Math.min(100,Math.round(100*i))))],r=n.acknowledgedData(this.channel,s);yield this.send(r,a)}))}setWindResistance(e,t,i,n){return s(this,void 0,void 0,(function*(){"function"==typeof e?yield this.setWindResistanceInternal(void 0,void 0,void 0,e):"function"==typeof t?yield this.setWindResistanceInternal(e,void 0,void 0,t):"function"==typeof i?yield this.setWindResistanceInternal(e,t,void 0,i):yield this.setWindResistanceInternal(e,t,i,n)}))}setTrackResistanceInternal(e,t,i){return s(this,void 0,void 0,(function*(){const s=null==e?65535:Math.max(0,Math.min(4e4,Math.round(100*(e+200)))),a=[51,255,255,255,255,255&s,s>>8&255,255&(null==t?255:Math.max(0,Math.min(254,Math.round(2e4*t))))],r=n.acknowledgedData(this.channel,a);yield this.send(r,i)}))}setTrackResistance(e,t,i){return s(this,void 0,void 0,(function*(){"function"==typeof e?yield this.setTrackResistanceInternal(void 0,void 0,e):"function"==typeof t?yield this.setTrackResistanceInternal(e,void 0,t):yield this.setTrackResistanceInternal(e,t,i)}))}}x.deviceType=17;function V(e,t,s){const i=t._EventCount||0,a=s.getUint8(n.BUFFER_INDEX_MSG_DATA);switch(a){case 1:{let e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+1);const a=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),r=s.getUint16(n.BUFFER_INDEX_MSG_DATA+3,!0),c=4095&s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),o=s.getUint16(n.BUFFER_INDEX_MSG_DATA+5,!0)>>4&1023,E=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0)>>6&1023;switch(e!==i&&(t._EventCount=e,i>e&&(e+=255)),t.UTCTimeRequired=!(1&~a),t.SupportANTFS=!(1&~r),r>>1&7){case 1:t.MeasurementInterval=.25;break;case 2:t.MeasurementInterval=.5;break;case 3:t.MeasurementInterval=1;break;case 4:t.MeasurementInterval=2;break;default:delete t.MeasurementInterval}switch(c){case 4094:t.TotalHemoglobinConcentration="AmbientLightTooHigh";break;case 4095:t.TotalHemoglobinConcentration="Invalid";break;default:t.TotalHemoglobinConcentration=c}switch(o){case 1022:t.PreviousSaturatedHemoglobinPercentage="AmbientLightTooHigh";break;case 1023:t.PreviousSaturatedHemoglobinPercentage="Invalid";break;default:t.PreviousSaturatedHemoglobinPercentage=o}switch(E){case 1022:t.CurrentSaturatedHemoglobinPercentage="AmbientLightTooHigh";break;case 1023:t.CurrentSaturatedHemoglobinPercentage="Invalid";break;default:t.CurrentSaturatedHemoglobinPercentage=E}break}case 80:t.HwVersion=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),t.ManId=s.getUint16(n.BUFFER_INDEX_MSG_DATA+4,!0),t.ModelNum=s.getUint16(n.BUFFER_INDEX_MSG_DATA+6,!0);break;case 81:{const e=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(n.BUFFER_INDEX_MSG_DATA+3),a=s.getUint32(n.BUFFER_INDEX_MSG_DATA+4,!0);t.SwVersion=i,255!==e&&(t.SwVersion+=e/1e3),4294967295!==a&&(t.SerialNumber=a);break}case 82:{s.getUint8(n.BUFFER_INDEX_MSG_DATA+2);const e=16777215&s.getUint32(n.BUFFER_INDEX_MSG_DATA+3,!0),i=s.getUint32(n.BUFFER_INDEX_MSG_DATA+6,!0),a=s.getUint32(n.BUFFER_INDEX_MSG_DATA+7,!0);t.OperatingTime=e*(128&~a?16:2),t.BatteryVoltage=(15&a)+i/256;const r=(112&a)>>>4;switch(t.BatteryStatusBit=r,r){case 1:t.BatteryStatus="New";break;case 2:t.BatteryStatus="Good";break;case 3:t.BatteryStatus="Ok";break;case 4:t.BatteryStatus="Low";break;case 5:t.BatteryStatus="Critical";break;default:t.BatteryVoltage=void 0,t.BatteryStatus="Invalid"}break}default:return}1===a&&t._EventCount===i||e.emit("oxygenData",t)}class W{constructor(e){this.DeviceId=e}}class Y extends W{}class q extends u{attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,q.deviceType,0,255,8192),this.state=new W(t)}))}updateState(e,t){this.state.DeviceId=e,V(this,this.state,t)}_sendTimeCmd(e,t){return s(this,void 0,void 0,(function*(){const s=new Date,i=Math.round((s.getTime()-Date.UTC(1989,11,31,0,0,0,0))/1e3),a=-Math.round(s.getTimezoneOffset()/15),r=[16,255&e,255,255&a,255&i,i>>8&255,i>>16&255,i>>24&255],c=n.acknowledgedData(this.channel,r);yield this.send(c,t)}))}setUTCTime(e){return s(this,void 0,void 0,(function*(){yield this._sendTimeCmd(0,e)}))}startSession(e){return s(this,void 0,void 0,(function*(){yield this._sendTimeCmd(1,e)}))}stopSession(e){return s(this,void 0,void 0,(function*(){yield this._sendTimeCmd(2,e)}))}setLap(e){return s(this,void 0,void 0,(function*(){yield this._sendTimeCmd(3,e)}))}}q.deviceType=31;function K(e,t,s){1===s.getUint8(n.BUFFER_INDEX_MSG_DATA)&&(t.EventCount=s.getUint8(n.BUFFER_INDEX_MSG_DATA+2),t.Temperature=s.getInt16(n.BUFFER_INDEX_MSG_DATA+6,!0)/100),e.emit("envdata",t),e.emit("envData",t)}class Q{constructor(e){this.DeviceId=e}}class j extends Q{}class z extends u{attach(e,t){const i=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return s(this,void 0,void 0,(function*(){yield i.attachSensor.call(this,e,"receive",t,z.deviceType,0,255,8192),this.state=new Q(t)}))}updateState(e,t){this.state.DeviceId=e,K(this,this.state,t)}}z.deviceType=25;exports.BaseSensor=E,exports.BicyclePowerScanState=w,exports.BicyclePowerScanner=class extends N{constructor(){super(...arguments),this.states={}}deviceType(){return X.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new w(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){b(this,this.states[e],t)}},exports.BicyclePowerSensor=X,exports.BicyclePowerSensorState=L,exports.CadenceScanState=m,exports.CadenceScanner=class extends N{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return y.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new m(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){G(this,this.states[e],t)}},exports.CadenceSensor=y,exports.CadenceSensorState=B,exports.EnvironmentScanState=j,exports.EnvironmentScanner=class extends N{constructor(){super(...arguments),this.states={}}deviceType(){return z.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new j(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){K(this,this.states[e],t)}},exports.EnvironmentSensor=z,exports.EnvironmentSensorState=Q,exports.FitnessEquipmentScanState=O,exports.FitnessEquipmentScanner=class extends N{constructor(){super(...arguments),this.states={}}deviceType(){return x.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new O(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){P(this,this.states[e],t)}},exports.FitnessEquipmentSensor=x,exports.FitnessEquipmentSensorState=H,exports.GarminStick2=class extends a{constructor(e={}){super(4047,4104,e)}},exports.GarminStick3=class extends a{constructor(e={}){super(4047,4105,e)}},exports.HeartRateScanState=S,exports.HeartRateScanner=class extends N{constructor(){super(...arguments),this.states={},this.pages={}}deviceType(){return l.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new S(e)),this.pages[e]||(this.pages[e]={oldPage:-1,pageState:_.INIT_PAGE})}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){d(this,this.states[e],this.pages[e],t)}},exports.HeartRateSensor=l,exports.HeartRateSensorState=h,exports.MuscleOxygenScanState=Y,exports.MuscleOxygenScanner=class extends N{constructor(){super(...arguments),this.states={}}deviceType(){return q.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new Y(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){V(this,this.states[e],t)}},exports.MuscleOxygenSensor=q,exports.MuscleOxygenSensorState=W,exports.SpeedCadenceScanState=v,exports.SpeedCadenceScanner=class extends N{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return g.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new v(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){R(this,this.states[e],t)}},exports.SpeedCadenceSensor=g,exports.SpeedCadenceSensorState=F,exports.SpeedScanState=p,exports.SpeedScanner=class extends N{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return f.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new p(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){M(this,this.states[e],t)}},exports.SpeedSensor=f,exports.SpeedSensorState=C,exports.StrideSpeedDistanceScanState=I,exports.StrideSpeedDistanceScanner=class extends N{constructor(){super(...arguments),this.states={}}deviceType(){return U.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new I(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){T(this,this.states[e],t)}},exports.StrideSpeedDistanceSensor=U,exports.StrideSpeedDistanceSensorState=D,exports.WebUsbStick=class extends c{constructor(){if("undefined"==typeof navigator||void 0===navigator.usb)throw new Error("WebUSB API is not available in this environment.");super()}}; //# sourceMappingURL=index.cjs.map