ant-plus-next
Version:
A modern Node.js module for working with ANT+ USB sticks and sensors.
1 lines • 52.8 kB
JavaScript
import e,{EventEmitter as t}from"events";import s from"usb";function i(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 n{}n.MESSAGE_RF=1,n.MESSAGE_TX_SYNC=164,n.DEFAULT_NETWORK_NUMBER=0,n.MESSAGE_CHANNEL_UNASSIGN=65,n.MESSAGE_CHANNEL_ASSIGN=66,n.MESSAGE_CHANNEL_ID=81,n.MESSAGE_CHANNEL_PERIOD=67,n.MESSAGE_CHANNEL_SEARCH_TIMEOUT=68,n.MESSAGE_CHANNEL_FREQUENCY=69,n.MESSAGE_CHANNEL_TX_POWER=96,n.MESSAGE_NETWORK_KEY=70,n.MESSAGE_TX_POWER=71,n.MESSAGE_PROXIMITY_SEARCH=113,n.MESSAGE_ENABLE_RX_EXT=102,n.MESSAGE_LIB_CONFIG=110,n.MESSAGE_CHANNEL_OPEN_RX_SCAN=91,n.MESSAGE_STARTUP=111,n.MESSAGE_SYSTEM_RESET=74,n.MESSAGE_CHANNEL_OPEN=75,n.MESSAGE_CHANNEL_CLOSE=76,n.MESSAGE_CHANNEL_REQUEST=77,n.MESSAGE_CHANNEL_BROADCAST_DATA=78,n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA=79,n.MESSAGE_CHANNEL_BURST_DATA=80,n.MESSAGE_CHANNEL_EVENT=64,n.MESSAGE_CHANNEL_STATUS=82,n.MESSAGE_VERSION=62,n.MESSAGE_CAPABILITIES=84,n.MESSAGE_SERIAL_NUMBER=97,n.CHANNEL_TYPE_TWOWAY_RECEIVE=0,n.CHANNEL_TYPE_TWOWAY_TRANSMIT=16,n.CHANNEL_TYPE_SHARED_RECEIVE=32,n.CHANNEL_TYPE_SHARED_TRANSMIT=48,n.CHANNEL_TYPE_ONEWAY_RECEIVE=64,n.CHANNEL_TYPE_ONEWAY_TRANSMIT=80,n.RADIO_TX_POWER_MINUS20DB=0,n.RADIO_TX_POWER_MINUS10DB=1,n.RADIO_TX_POWER_0DB=2,n.RADIO_TX_POWER_PLUS4DB=3,n.RESPONSE_NO_ERROR=0,n.EVENT_RX_SEARCH_TIMEOUT=1,n.EVENT_RX_FAIL=2,n.EVENT_TX=3,n.EVENT_TRANSFER_RX_FAILED=4,n.EVENT_TRANSFER_TX_COMPLETED=5,n.EVENT_TRANSFER_TX_FAILED=6,n.EVENT_CHANNEL_CLOSED=7,n.EVENT_RX_FAIL_GO_TO_SEARCH=8,n.EVENT_CHANNEL_COLLISION=9,n.EVENT_TRANSFER_TX_START=10,n.CHANNEL_IN_WRONG_STATE=21,n.CHANNEL_NOT_OPENED=22,n.CHANNEL_ID_NOT_SET=24,n.CLOSE_ALL_CHANNELS=25,n.TRANSFER_IN_PROGRESS=31,n.TRANSFER_SEQUENCE_NUMBER_ERROR=32,n.TRANSFER_IN_ERROR=33,n.MESSAGE_SIZE_EXCEEDS_LIMIT=39,n.INVALID_MESSAGE=40,n.INVALID_NETWORK_NUMBER=41,n.INVALID_LIST_ID=48,n.INVALID_SCAN_TX_CHANNEL=49,n.INVALID_PARAMETER_PROVIDED=51,n.EVENT_QUEUE_OVERFLOW=53,n.USB_STRING_WRITE_FAIL=112,n.CHANNEL_STATE_UNASSIGNED=0,n.CHANNEL_STATE_ASSIGNED=1,n.CHANNEL_STATE_SEARCHING=2,n.CHANNEL_STATE_TRACKING=3,n.CAPABILITIES_NO_RECEIVE_CHANNELS=1,n.CAPABILITIES_NO_TRANSMIT_CHANNELS=2,n.CAPABILITIES_NO_RECEIVE_MESSAGES=4,n.CAPABILITIES_NO_TRANSMIT_MESSAGES=8,n.CAPABILITIES_NO_ACKNOWLEDGED_MESSAGES=16,n.CAPABILITIES_NO_BURST_MESSAGES=32,n.CAPABILITIES_NETWORK_ENABLED=2,n.CAPABILITIES_SERIAL_NUMBER_ENABLED=8,n.CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED=16,n.CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED=32,n.CAPABILITIES_SCRIPT_ENABLED=64,n.CAPABILITIES_SEARCH_LIST_ENABLED=128,n.CAPABILITIES_LED_ENABLED=1,n.CAPABILITIES_EXT_MESSAGE_ENABLED=2,n.CAPABILITIES_SCAN_MODE_ENABLED=4,n.CAPABILITIES_PROX_SEARCH_ENABLED=16,n.CAPABILITIES_EXT_ASSIGN_ENABLED=32,n.CAPABILITIES_FS_ANTFS_ENABLED=64,n.TIMEOUT_NEVER=255;class a{static resetSystem(){const e=[];return e.push(0),this.buildMessage(e,n.MESSAGE_SYSTEM_RESET)}static requestMessage(e,t){let s=[];return s=s.concat(this.intToLEHexArray(e)),s.push(t),this.buildMessage(s,n.MESSAGE_CHANNEL_REQUEST)}static setNetworkKey(){const e=[];return e.push(n.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,n.MESSAGE_NETWORK_KEY)}static assignChannel(e,t="receive"){let s=[];if(s=s.concat(this.intToLEHexArray(e)),"receive"===t)s.push(n.CHANNEL_TYPE_TWOWAY_RECEIVE);else if("receive_only"===t)s.push(n.CHANNEL_TYPE_ONEWAY_RECEIVE);else if("receive_shared"===t)s.push(n.CHANNEL_TYPE_SHARED_RECEIVE);else if("transmit"===t)s.push(n.CHANNEL_TYPE_TWOWAY_TRANSMIT);else if("transmit_only"===t)s.push(n.CHANNEL_TYPE_ONEWAY_TRANSMIT);else{if("transmit_shared"!==t)throw new Error("type not allowed");s.push(n.CHANNEL_TYPE_SHARED_TRANSMIT)}return s.push(n.DEFAULT_NETWORK_NUMBER),this.buildMessage(s,n.MESSAGE_CHANNEL_ASSIGN)}static setDevice(e,t,s,i){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(i)),this.buildMessage(a,n.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,n.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,n.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,n.MESSAGE_CHANNEL_FREQUENCY)}static setRxExt(){let e=[];return e=e.concat(this.intToLEHexArray(0)),e=e.concat(this.intToLEHexArray(1)),this.buildMessage(e,n.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,n.MESSAGE_LIB_CONFIG)}static openRxScan(){let e=[];return e=e.concat(this.intToLEHexArray(0)),e=e.concat(this.intToLEHexArray(1)),this.buildMessage(e,n.MESSAGE_CHANNEL_OPEN_RX_SCAN)}static openChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,n.MESSAGE_CHANNEL_OPEN)}static closeChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,n.MESSAGE_CHANNEL_CLOSE)}static unassignChannel(e){let t=[];return t=t.concat(this.intToLEHexArray(e)),this.buildMessage(t,n.MESSAGE_CHANNEL_UNASSIGN)}static acknowledgedData(e,t){return t=this.intToLEHexArray(e).concat(t),this.buildMessage(t,n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA)}static broadcastData(e,t){return t=this.intToLEHexArray(e).concat(t),this.buildMessage(t,n.MESSAGE_CHANNEL_BROADCAST_DATA)}static buildMessage(e=[],t=0){const s=[];return s.push(n.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;)s="0"+s;return s}static getChecksum(e){let t=0;return e.forEach(e=>{t=(t^e)%255}),t}}a.BUFFER_INDEX_MSG_LEN=1,a.BUFFER_INDEX_MSG_TYPE=2,a.BUFFER_INDEX_CHANNEL_NUM=3,a.BUFFER_INDEX_MSG_DATA=4,a.BUFFER_INDEX_EXT_MSG_BEGIN=12;class r extends e{constructor(e,t,i={}){super(),this.idVendor=e,this.idProduct=t,this.detachedKernelDriver=!1,this.usedChannels=0,this.attachedSensors=[],this.maxChannels=0,this._canScan=!1,this.throwLibUSBException=!1,this.setMaxListeners(50),s.usb.setDebugLevel(i.usbDebugLevel||0),this.throwLibUSBException=i.throwLibUSBException||!1}canAttach(){return i(this,void 0,void 0,function*(){return Promise.resolve(this.usedChannels<this.maxChannels)})}canScan(){return i(this,void 0,void 0,function*(){return Promise.resolve(this._canScan)})}open(){return i(this,void 0,void 0,function*(){const e=this.getDevices();for(;e.length;)try{const t=e.shift();if(!t)continue;this.device=t,this.device.open(),this.iface=this.device.interfaces[0];try{this.iface&&this.iface.isKernelDriverActive()&&(this.detachedKernelDriver=!0,this.iface.detachKernelDriver())}catch(e){}this.iface.claim();break}catch(e){if(e&&this.throwLibUSBException){switch(e.errno){case s.usb.LIBUSB_ERROR_ACCESS:throw new Error("LIBUSB_ERROR_ACCESS: Access denied (insufficient permissions)");case s.usb.LIBUSB_ERROR_NO_DEVICE:throw new Error("LIBUSB_ERROR_NO_DEVICE: Device has been disconnected");case s.usb.LIBUSB_ERROR_BUSY:throw new Error("LIBUSB_ERROR_BUSY: Resource busy");default:console.error("Unknown LIBUSB error:",e)}}this.device&&this.device.close(),this.device=void 0,this.iface=void 0}if(!this.device)return Promise.resolve(!1);if(r.deviceInUse.push(this.device),!this.iface)throw new Error("Interface not initialized.");return this.inEndpoint=this.iface.endpoints[0],this.inEndpoint.on("data",e=>{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 i(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 t=r.deviceInUse.indexOf(this.device);t>=0&&r.deviceInUse.splice(t,1),s.usb.listenerCount("attach")&&s.usb.emit("attach",this.device),this.device=void 0})})})})}read(e){return i(this,void 0,void 0,function*(){const t=new DataView(e.buffer),s=t.getUint8(2);s===n.MESSAGE_STARTUP?yield this.write(a.requestMessage(0,n.MESSAGE_CAPABILITIES)):s===n.MESSAGE_CAPABILITIES?(this.maxChannels=t.getUint8(3),this._canScan=!(6&~t.getUint8(7)),yield this.write(a.setNetworkKey())):s===n.MESSAGE_CHANNEL_EVENT&&t.getUint8(4)===n.MESSAGE_NETWORK_KEY?this.emit("startup",e):this.emit("read",e)})}write(e){return i(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 i(this,void 0,void 0,function*(){yield this.detachAll(),this.maxChannels=0,this.usedChannels=0,yield this.write(a.resetSystem())})}attach(e,t){return i(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 i(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 i(this,void 0,void 0,function*(){return Promise.resolve(this.getDevices().length>0)})}isScanning(){return i(this,void 0,void 0,function*(){return Promise.resolve(-1===this.usedChannels)})}getDevices(){return s.getDeviceList().filter(e=>e.deviceDescriptor.idVendor===this.idVendor&&e.deviceDescriptor.idProduct===this.idProduct).filter(e=>-1===r.deviceInUse.indexOf(e))}detachAll(){return i(this,void 0,void 0,function*(){const e=this.attachedSensors;for(const t of e)yield t.detach()})}onData(e){return i(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(;i<s;){if(i+1===s){this.leftover=e.slice(i);break}const n=i+t.getUint8(i+1)+4;if(n>s){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}}r.deviceInUse=[];class c extends r{constructor(e={}){super(4047,4104,e)}}class o extends r{constructor(e={}){super(4047,4105,e)}}const E=[{vendorId:4047,productId:4104},{vendorId:4047,productId:4105}];class _ 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 i(this,void 0,void 0,function*(){return Promise.resolve(this.usedChannels<this.maxChannels)})}canScan(){return i(this,void 0,void 0,function*(){return Promise.resolve(this._canScan)})}open(){return i(this,void 0,void 0,function*(){var e;try{if(this.device||(this.device=yield navigator.usb.requestDevice({filters:E})),yield this.device.open(),this.iface=null===(e=this.device.configuration)||void 0===e?void 0:e.interfaces[0],!this.iface)throw new Error("No interface configuration found.");if(yield this.device.claimInterface(this.iface.interfaceNumber),_.deviceInUse.push(this.device),this.inEndpoint=this.iface.alternate.endpoints.find(e=>"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 i(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(;n<a;){if(n+1===a){this.leftover=s.slice(n);break}const e=n+i.getUint8(n+1)+4;if(e>a){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 i(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=_.deviceInUse.indexOf(this.device);e>=0&&_.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 i(this,void 0,void 0,function*(){const t=new DataView(e.buffer),s=t.getUint8(2);s===n.MESSAGE_STARTUP?yield this.write(a.requestMessage(0,n.MESSAGE_CAPABILITIES)):s===n.MESSAGE_CAPABILITIES?(this.maxChannels=t.getUint8(3),this._canScan=!(6&~t.getUint8(7)),yield this.write(a.setNetworkKey())):s===n.MESSAGE_CHANNEL_EVENT&&t.getUint8(4)===n.MESSAGE_NETWORK_KEY?this.emit("startup",e):this.emit("read",e)})}write(e){return i(this,void 0,void 0,function*(){this.device&&this.outEndpoint&&(yield this.device.transferOut(this.outEndpoint.endpointNumber,e))})}reset(){return i(this,void 0,void 0,function*(){yield this.detachAll(),this.maxChannels=0,this.usedChannels=0,yield this.write(a.resetSystem())})}attach(e,t){return i(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 i(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 i(this,void 0,void 0,function*(){return Promise.resolve(!!this.device)})}isScanning(){return i(this,void 0,void 0,function*(){return Promise.resolve(-1===this.usedChannels)})}detachAll(){return i(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}}_.deviceInUse=[];class d extends _{constructor(){if("undefined"==typeof navigator||void 0===navigator.usb)throw new Error("WebUSB API is not available in this environment.");super()}}function h(e){"undefined"!=typeof process&&"function"==typeof process.nextTick?process.nextTick(e):"function"==typeof setImmediate?setImmediate(e):setTimeout(e,0)}class S extends t{constructor(e){super(),this.stick=e,this.messageQueue=[],e.on("read",e=>{this.handleEventMessages(e).catch(e=>{console.error(e)})})}scan(e,t){return i(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 s=e=>i(this,void 0,void 0,function*(){switch(e.msg){case n.MESSAGE_RF:switch(e.code){case n.EVENT_CHANNEL_CLOSED:case n.EVENT_RX_FAIL_GO_TO_SEARCH:return yield this.write(a.unassignChannel(0)),!0;case n.EVENT_TRANSFER_TX_COMPLETED:case n.EVENT_TRANSFER_TX_FAILED:case n.EVENT_RX_FAIL:case n.INVALID_SCAN_TX_CHANNEL:{const t=this.messageQueue.shift();return t&&t.cbk&&t.cbk(e.code===n.EVENT_TRANSFER_TX_COMPLETED),this.messageQueue.length&&(yield this.write(this.messageQueue[0].msg)),!0}}break;case n.MESSAGE_CHANNEL_ASSIGN:return yield this.write(a.setDevice(0,0,0,0)),!0;case n.MESSAGE_CHANNEL_ID:return yield this.write(a.setFrequency(0,t)),!0;case n.MESSAGE_CHANNEL_FREQUENCY:return yield this.write(a.setRxExt()),!0;case n.MESSAGE_ENABLE_RX_EXT:return yield this.write(a.libConfig(0,224)),!0;case n.MESSAGE_LIB_CONFIG:return yield this.write(a.openRxScan()),!0;case n.MESSAGE_CHANNEL_OPEN_RX_SCAN:return h(()=>this.emit("attached")),!0;case n.MESSAGE_CHANNEL_CLOSE:return!0;case n.MESSAGE_CHANNEL_UNASSIGN:return this.statusCbk=void 0,this.channel=void 0,h(()=>this.emit("detached")),!0;case n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:return e.code===n.TRANSFER_IN_PROGRESS}return!1});if(yield this.stick.isScanning())this.channel=0,this.deviceId=0,this.transmissionType=0,this.statusCbk=s,h(()=>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=s,yield this.write(a.assignChannel(0,e))}})}attachSensor(e,t,s,r,c,o,E,_){return i(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=s,this.transmissionType=c;this.statusCbk=t=>i(this,void 0,void 0,function*(){switch(t.msg){case n.MESSAGE_RF:switch(t.code){case n.EVENT_CHANNEL_CLOSED:case n.EVENT_RX_FAIL_GO_TO_SEARCH:return yield this.write(a.unassignChannel(e)),!0;case n.EVENT_TRANSFER_TX_COMPLETED:case n.EVENT_TRANSFER_TX_FAILED:case n.EVENT_RX_FAIL:case n.INVALID_SCAN_TX_CHANNEL:{const e=this.messageQueue.shift();return e&&e.cbk&&e.cbk(t.code===n.EVENT_TRANSFER_TX_COMPLETED),this.messageQueue.length&&(yield this.write(this.messageQueue[0].msg)),!0}case n.EVENT_CHANNEL_COLLISION:return!0}break;case n.MESSAGE_CHANNEL_ASSIGN:return yield this.write(a.setDevice(e,s,r,c)),!0;case n.MESSAGE_CHANNEL_ID:return yield this.write(a.searchChannel(e,o)),!0;case n.MESSAGE_CHANNEL_SEARCH_TIMEOUT:return yield this.write(a.setFrequency(e,_)),!0;case n.MESSAGE_CHANNEL_FREQUENCY:return yield this.write(a.setPeriod(e,E)),!0;case n.MESSAGE_CHANNEL_PERIOD:return yield this.write(a.libConfig(e,224)),!0;case n.MESSAGE_LIB_CONFIG:return yield this.write(a.openChannel(e)),!0;case n.MESSAGE_CHANNEL_OPEN:return h(()=>this.emit("attached")),!0;case n.MESSAGE_CHANNEL_CLOSE:return!0;case n.MESSAGE_CHANNEL_UNASSIGN:return this.statusCbk=void 0,this.channel=void 0,h(()=>this.emit("detached")),!0;case n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:return t.code===n.TRANSFER_IN_PROGRESS}return!1}),yield this.write(a.assignChannel(e,t))})}detach(){return i(this,void 0,void 0,function*(){if(void 0===this.channel)return;yield this.write(a.closeChannel(this.channel));if(!(yield this.stick.detach(this)))throw new Error("Error on detaching")})}write(e){return i(this,void 0,void 0,function*(){yield this.stick.write(e)})}handleEventMessages(e){return i(this,void 0,void 0,function*(){const t=new DataView(e.buffer),s=t.getUint8(a.BUFFER_INDEX_MSG_TYPE);if(t.getUint8(a.BUFFER_INDEX_CHANNEL_NUM)===this.channel)if(s===n.MESSAGE_CHANNEL_EVENT){const s={msg:t.getUint8(a.BUFFER_INDEX_MSG_DATA),code:t.getUint8(a.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(a.BUFFER_INDEX_MSG_DATA),code:t.getUint8(a.BUFFER_INDEX_MSG_DATA+1)}))}else this.decodeDataCbk&&(yield this.decodeDataCbk(t))})}send(e,t){return i(this,void 0,void 0,function*(){this.messageQueue.push({msg:e,cbk:t}),1===this.messageQueue.length&&(yield this.write(e))})}}var A;function u(e,t,s,i){const n=i.getUint8(a.BUFFER_INDEX_MSG_DATA);if(s.pageState===A.INIT_PAGE)s.pageState=A.STD_PAGE;else if(n!==s.oldPage||s.pageState===A.EXT_PAGE)switch(s.pageState=A.EXT_PAGE,-129&n){case 1:t.OperatingTime=i.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=i.getUint8(a.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=i.getUint8(a.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=i.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=i.getUint16(a.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=i.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=i.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=i.getUint8(a.BUFFER_INDEX_MSG_DATA+3);break;case 4:t.PreviousBeat=i.getUint16(a.BUFFER_INDEX_MSG_DATA+2,!0);break;case 5:t.IntervalAverage=i.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.IntervalMax=i.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.SessionAverage=i.getUint8(a.BUFFER_INDEX_MSG_DATA+3);break;case 6:t.SupportedFeatures=i.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.EnabledFeatures=i.getUint8(a.BUFFER_INDEX_MSG_DATA+3);break;case 7:{const e=i.getUint8(a.BUFFER_INDEX_MSG_DATA+1),s=i.getUint8(a.BUFFER_INDEX_MSG_DATA+2),n=i.getUint8(a.BUFFER_INDEX_MSG_DATA+3);255!==e&&(t.BatteryLevel=e),t.BatteryVoltage=(15&n)+s/256;const r=(112&n)>>>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(a.BUFFER_INDEX_MSG_DATA+4))),s.oldPage=n,e.emit("heartRateData",t)}!function(e){e[e.INIT_PAGE=0]="INIT_PAGE",e[e.STD_PAGE=1]="STD_PAGE",e[e.EXT_PAGE=2]="EXT_PAGE"}(A||(A={}));class l{constructor(e){this.DeviceId=e}}class N extends l{}class T extends S{scan(e){const t=Object.create(null,{scan:{get:()=>super.scan}});return i(this,void 0,void 0,function*(){return yield t.scan.call(this,e,57)})}attachSensor(e,t,s,n,a,r,c){const o=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){return yield o.attachSensor.call(this,e,t,s,n,a,r,c,57)})}}class D extends T{constructor(e){super(e),this.decodeDataCbk=this.decodeData.bind(this)}scan(){throw new Error("scanning unsupported")}attachSensor(e,t,s,n,a,r,c){const o=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){return yield o.attachSensor.call(this,e,t,s,n,a,r,c)})}decodeData(e){return i(this,void 0,void 0,function*(){switch(e.getUint8(a.BUFFER_INDEX_MSG_TYPE)){case n.MESSAGE_CHANNEL_BROADCAST_DATA:case n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:case n.MESSAGE_CHANNEL_BURST_DATA:0===this.deviceId&&null!=this.channel&&(yield this.write(a.requestMessage(this.channel,n.MESSAGE_CHANNEL_ID))),this.updateState(this.deviceId,e);break;case n.MESSAGE_CHANNEL_ID:this.deviceId=e.getUint16(a.BUFFER_INDEX_MSG_DATA,!0),this.transmissionType=e.getUint8(a.BUFFER_INDEX_MSG_DATA+3)}})}}class I extends D{constructor(){super(...arguments),this.page={oldPage:-1,pageState:A.INIT_PAGE}}attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,I.deviceType,0,255,8070),this.state=new l(t)})}updateState(e,t){this.state.DeviceId=e,u(this,this.state,this.page,t)}}I.deviceType=120;class U extends T{constructor(e){super(e),this.decodeDataCbk=this.decodeData.bind(this)}scan(){const e=Object.create(null,{scan:{get:()=>super.scan}});return i(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 i(this,void 0,void 0,function*(){if(e.byteLength<=a.BUFFER_INDEX_EXT_MSG_BEGIN+3||!(128&e.getUint8(a.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(a.BUFFER_INDEX_EXT_MSG_BEGIN+1,!0);console.log(this.deviceId);if(e.getUint8(a.BUFFER_INDEX_EXT_MSG_BEGIN+3)===this.deviceType()){if(this.createStateIfNew(t),64&e.getUint8(a.BUFFER_INDEX_EXT_MSG_BEGIN)&&32===e.getUint8(a.BUFFER_INDEX_EXT_MSG_BEGIN+5)){const s=e.getInt8(a.BUFFER_INDEX_EXT_MSG_BEGIN+6),i=e.getInt8(a.BUFFER_INDEX_EXT_MSG_BEGIN+7);this.updateRssiAndThreshold(t,s,i)}switch(e.getUint8(a.BUFFER_INDEX_MSG_TYPE)){case n.MESSAGE_CHANNEL_BROADCAST_DATA:case n.MESSAGE_CHANNEL_ACKNOWLEDGED_DATA:case n.MESSAGE_CHANNEL_BURST_DATA:this.updateState(t,e)}}})}}class R extends U{constructor(){super(...arguments),this.states={},this.pages={}}deviceType(){return I.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new N(e)),this.pages[e]||(this.pages[e]={oldPage:-1,pageState:A.INIT_PAGE})}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){u(this,this.states[e],this.pages[e],t)}}function F(e,t,s){const i=s.getUint8(a.BUFFER_INDEX_MSG_DATA);if(1===i)t.TimeFractional=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.TimeInteger=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.DistanceInteger=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3),t.DistanceFractional=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4)>>>4,t.SpeedInteger=15&s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),t.SpeedFractional=s.getUint8(a.BUFFER_INDEX_MSG_DATA+5),t.StrideCount=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6),t.UpdateLatency=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);else if(i>=2&&i<=15&&(t.CadenceInteger=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3),t.CadenceFractional=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4)>>>4,t.SpeedInteger=15&s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),t.SpeedFractional=s.getUint8(a.BUFFER_INDEX_MSG_DATA+5),t.Status=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7),3===i))t.Calories=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6);e.emit("ssddata",t),e.emit("ssdData",t)}class v{constructor(e){this.DeviceId=e}}class g extends v{}class M extends D{attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,M.deviceType,0,255,8134),this.state=new v(t)})}updateState(e,t){this.state.DeviceId=e,F(this,this.state,t)}}M.deviceType=124;class C extends U{constructor(){super(...arguments),this.states={}}deviceType(){return M.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new g(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){F(this,this.states[e],t)}}function f(e,t,s){var i,n,r,c;const o=null!==(i=t.CadenceEventTime)&&void 0!==i?i:0,E=null!==(n=t.CumulativeCadenceRevolutionCount)&&void 0!==n?n:0,_=null!==(r=t.SpeedEventTime)&&void 0!==r?r:0,d=null!==(c=t.CumulativeSpeedRevolutionCount)&&void 0!==c?c:0;let h=s.getUint16(a.BUFFER_INDEX_MSG_DATA,!0),S=s.getUint16(a.BUFFER_INDEX_MSG_DATA+2,!0),A=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),u=s.getUint16(a.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 G{constructor(e){this.DeviceId=e}}class B extends G{}class p extends D{constructor(){super(...arguments),this.wheelCircumference=2.199}setWheelCircumference(e){this.wheelCircumference=e}attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,p.deviceType,0,255,8086),this.state=new G(t)})}updateState(e,t){this.state.DeviceId=e,f(this,this.state,t)}}p.deviceType=121;class m extends U{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return p.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new B(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){f(this,this.states[e],t)}}function y(e,t,s){var i,n;switch(-129&s.getUint8(a.BUFFER_INDEX_MSG_DATA)){case 1:t.OperatingTime=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=s.getUint16(a.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);break;case 4:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);t.BatteryVoltage=(15&i)+e/256;const n=(112&i)>>>4;switch(t.BatteryStatusBit=n,n){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(a.BUFFER_INDEX_MSG_DATA+1))}const r=null!==(i=t.SpeedEventTime)&&void 0!==i?i:0,c=null!==(n=t.CumulativeSpeedRevolutionCount)&&void 0!==n?n:0;let o=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),E=s.getUint16(a.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 b{constructor(e){this.DeviceId=e}}class L extends b{}class X extends D{constructor(){super(...arguments),this.wheelCircumference=2.199}setWheelCircumference(e){this.wheelCircumference=e}attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,X.deviceType,0,255,8118),this.state=new b(t)})}updateState(e,t){this.state.DeviceId=e,y(this,this.state,t)}}X.deviceType=123;class w extends U{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return X.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new L(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){y(this,this.states[e],t)}}function k(e,t,s){var i,n;switch(-129&s.getUint8(a.BUFFER_INDEX_MSG_DATA)){case 1:t.OperatingTime=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.OperatingTime|=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2)<<8,t.OperatingTime|=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3)<<16,t.OperatingTime*=2;break;case 2:t.ManId=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SerialNumber=t.DeviceId,t.SerialNumber|=s.getUint16(a.BUFFER_INDEX_MSG_DATA+2,!0)<<16,t.SerialNumber>>>=0;break;case 3:t.HwVersion=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),t.SwVersion=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),t.ModelNum=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);break;case 4:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);t.BatteryVoltage=(15&i)+e/256;const n=(112&i)>>>4;switch(t.BatteryStatusBit=n,n){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(a.BUFFER_INDEX_MSG_DATA+1))}const r=null!==(i=t.CadenceEventTime)&&void 0!==i?i:0,c=null!==(n=t.CumulativeCadenceRevolutionCount)&&void 0!==n?n:0;let o=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),E=s.getUint16(a.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 P{constructor(e){this.DeviceId=e}}class H extends P{}class O extends D{constructor(){super(...arguments),this.wheelCircumference=2.199}attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,O.deviceType,0,255,8102),this.state=new P(t)})}setWheelCircumference(e){this.wheelCircumference=e}updateState(e,t){this.state.DeviceId=e,k(this,this.state,t)}}O.deviceType=122;class x extends U{constructor(){super(...arguments),this.wheelCircumference=2.199,this.states={}}deviceType(){return O.deviceType}setWheelCircumference(e){this.wheelCircumference=e}createStateIfNew(e){this.states[e]||(this.states[e]=new H(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){k(this,this.states[e],t)}}function V(e,t,s){var i,n,r;switch(s.getUint8(a.BUFFER_INDEX_MSG_DATA)){case 1:if(16===s.getUint8(a.BUFFER_INDEX_MSG_DATA+1)){1===s.getUint8(a.BUFFER_INDEX_MSG_DATA+2)&&(t.offset=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0))}break;case 16:{const e=s.getUint8(a.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(a.BUFFER_INDEX_MSG_DATA+3);t.Cadence=255!==i?i:void 0,t.AccumulatedPower=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),t.Power=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0);break}case 32:{const e=null!==(i=t.EventCount)&&void 0!==i?i:0,c=null!==(n=t.TimeStamp)&&void 0!==n?n:0,o=null!==(r=t.TorqueTicksStamp)&&void 0!==r?r:0;let E=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1);const _=s.getUint16(a.BUFFER_INDEX_MSG_DATA+3,!0);let d=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),h=s.getUint16(a.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 W{constructor(e){this.offset=0,this.DeviceId=e}}class Y extends W{}class K extends D{attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,K.deviceType,0,255,8182),this.state=new W(t)})}updateState(e,t){this.state.DeviceId=e,V(this,this.state,t)}}K.deviceType=11;class q extends U{constructor(){super(...arguments),this.states={}}deviceType(){return K.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)}}function Q(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 j(e,t,s){switch(s.getUint8(a.BUFFER_INDEX_MSG_DATA)){case 1:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);255!==e&&(t.Temperature=.5*e-25);const i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1);64&i&&(t.ZeroOffset=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0)),128&i&&(t.SpinDownTime=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0));break}case 16:{switch(31&s.getUint8(a.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(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);const n=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6),c=s.getUint8(a.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=n/1e3,delete t.RealSpeed):(delete t.VirtualSpeed,t.RealSpeed=n/1e3),(112&c)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",Q(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(a.BUFFER_INDEX_MSG_DATA+3),i=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);switch(255!==e&&(t.CycleLength=e/100),i>=-1e4&&i<=1e4&&(t.Incline=i/100),255!==n&&(t.Resistance=n),(112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",Q(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(a.BUFFER_INDEX_MSG_DATA+2,!0),i=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);switch(65535!==e&&(t.METs=e/100),65535!==i&&(t.CaloricBurnRate=i/10),1&r&&(t.Calories=n),(112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",Q(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(a.BUFFER_INDEX_MSG_DATA+4);let i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+5),n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6);const r=s.getUint8(a.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;n!==e&&e>n&&(n+=256),t.AscendedDistance=(t.AscendedDistance||0)+n-e}switch((112&r)>>4){case 1:t.State="OFF";break;case 2:t.State="READY",Q(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(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3);const n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),r=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),c=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);if(255!==n&&(t.Cadence=n),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",Q(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(a.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),n=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==n&&(t.InstantaneousPower=n),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",Q(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(a.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),n=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==n&&(t.InstantaneousPower=n),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",Q(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(a.BUFFER_INDEX_MSG_DATA+3);const i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+4),n=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),r=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);if(255!==i&&(t.Cadence=i),65535!==n&&(t.InstantaneousPower=n),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",Q(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(a.BUFFER_INDEX_MSG_DATA+1);const n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2);let r=s.getUint16(a.BUFFER_INDEX_MSG_DATA+3,!0);const c=4095&s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0),o=s.getUint8(a.BUFFER_INDEX_MSG_DATA+6)>>4,E=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);if(i!==e&&(t._EventCount0x19=i,e>i&&(i+=255)),255!==n&&(t.Cadence=n),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",Q(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(a.BUFFER_INDEX_MSG_DATA+1),n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),r=s.getUint16(a.BUFFER_INDEX_MSG_DATA+3,!0),c=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0);const o=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);i!==e&&(t._EventCount0x1A=i,e>i&&(i+=255));const E=(t.WheelTicks||0)%256;n!==E&&E>n&&(n+=65536),t.WheelTicks=(t.WheelTicks||0)+n-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",Q(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(a.BUFFER_INDEX_MSG_DATA+3),t.ManId=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),t.ModelNum=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0);break;case 81:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3),n=s.getUint32(a.BUFFER_INDEX_MSG_DATA+4,!0);t.SwVersion=i,255!==e&&(t.SwVersion+=e/1e3),4294967295!==n&&(t.SerialNumber=n);break}case 86:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3),r=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0);s.getUint8(a.BUFFER_INDEX_MSG_DATA+6);const c=s.getUint8(a.BUFFER_INDEX_MSG_DATA+7);0===e&&(t.PairedDevices=[]),i>0&&t.PairedDevices.push({id:r,type:c,paired:!!(128&n)});break}default:return}e.emit("fitnessData",t)}class z{constructor(e){this.PairedDevices=[],this.DeviceId=e}}class Z extends z{}class J extends D{attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,J.deviceType,0,255,8192),this.state=new z(t)})}updateState(e,t){this.state.DeviceId=e,j(this,this.state,t)}setUserConfigurationInternal(e,t,s,n,r){return i(this,void 0,void 0,function*(){const i=null==e?65535:Math.max(0,Math.min(65534,Math.round(100*e))),c=null==s?255:Math.round(10*s)%10,o=null==t?4095:Math.max(0,Math.min(1e3,Math.round(20*t))),E=[55,255&i,i>>8&255,255,15&c|(15&o)<<4,o>>4&15,255&(null==s?255:Math.max(0,Math.min(254,Math.round(s)))),255&(null==n?0:Math.max(1,Math.min(255,Math.round(n/.03))))],_=a.acknowledgedData(this.channel,E);yield this.send(_,r)})}setUserConfiguration(e,t,s,n,a){return i(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 s?yield this.setUserConfigurationInternal(e,t,void 0,void 0,s):"function"==typeof n?yield this.setUserConfigurationInternal(e,t,s,void 0,n):yield this.setUserConfigurationInternal(e,t,s,n,a)})}setBasicResistance(e,t){return i(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=a.acknowledgedData(this.channel,s);yield this.send(i,t)})}setTargetPower(e,t){return i(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],n=a.acknowledgedData(this.channel,i);yield this.send(n,t)})}setWindResistanceInternal(e,t,s,n){return i(this,void 0,void 0,function*(){const i=[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==s?255:Math.max(0,Math.min(100,Math.round(100*s))))],r=a.acknowledgedData(this.channel,i);yield this.send(r,n)})}setWindResistance(e,t,s,n){return i(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 s?yield this.setWindResistanceInternal(e,t,void 0,s):yield this.setWindResistanceInternal(e,t,s,n)})}setTrackResistanceInternal(e,t,s){return i(this,void 0,void 0,function*(){const i=null==e?65535:Math.max(0,Math.min(4e4,Math.round(100*(e+200)))),n=[51,255,255,255,255,255&i,i>>8&255,255&(null==t?255:Math.max(0,Math.min(254,Math.round(2e4*t))))],r=a.acknowledgedData(this.channel,n);yield this.send(r,s)})}setTrackResistance(e,t,s){return i(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,s)})}}J.deviceType=17;class $ extends U{constructor(){super(...arguments),this.states={}}deviceType(){return J.deviceType}createStateIfNew(e){this.states[e]||(this.states[e]=new Z(e))}updateRssiAndThreshold(e,t,s){this.states[e].Rssi=t,this.states[e].Threshold=s}updateState(e,t){j(this,this.states[e],t)}}function ee(e,t,s){const i=t._EventCount||0,n=s.getUint8(a.BUFFER_INDEX_MSG_DATA);switch(n){case 1:{let e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+1);const n=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),r=s.getUint16(a.BUFFER_INDEX_MSG_DATA+3,!0),c=4095&s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),o=s.getUint16(a.BUFFER_INDEX_MSG_DATA+5,!0)>>4&1023,E=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0)>>6&1023;switch(e!==i&&(t._EventCount=e,i>e&&(e+=255)),t.UTCTimeRequired=!(1&~n),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(a.BUFFER_INDEX_MSG_DATA+3),t.ManId=s.getUint16(a.BUFFER_INDEX_MSG_DATA+4,!0),t.ModelNum=s.getUint16(a.BUFFER_INDEX_MSG_DATA+6,!0);break;case 81:{const e=s.getUint8(a.BUFFER_INDEX_MSG_DATA+2),i=s.getUint8(a.BUFFER_INDEX_MSG_DATA+3),n=s.getUint32(a.BUFFER_INDEX_MSG_DATA+4,!0);t.SwVersion=i,255!==e&&(t.SwVersion+=e/1e3),4294967295!==n&&(t.SerialNumber=n);break}case 82:{s.getUint8(a.BUFFER_INDEX_MSG_DATA+2);const e=16777215&s.getUint32(a.BUFFER_INDEX_MSG_DATA+3,!0),i=s.getUint32(a.BUFFER_INDEX_MSG_DATA+6,!0),n=s.getUint32(a.BUFFER_INDEX_MSG_DATA+7,!0);t.OperatingTime=e*(128&~n?16:2),t.BatteryVoltage=(15&n)+i/256;const r=(112&n)>>>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===n&&t._EventCount===i||e.emit("oxygenData",t)}class te{constructor(e){this.DeviceId=e}}class se extends te{}class ie extends D{attach(e,t){const s=Object.create(null,{attachSensor:{get:()=>super.attachSensor}});return i(this,void 0,void 0,function*(){yield s.attachSensor.call(this,e,"receive",t,ie.deviceType,0,255,8192),this.state=new te(t)})}updateState(e,t){this.state.DeviceId=e,ee(this,this.state,t)}_sendTimeCmd(e,t){return i(this,void 0,void 0,functio