Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught SyntaxError #307

Open
richierockskool opened this issue Apr 26, 2023 · 0 comments
Open

Uncaught SyntaxError #307

richierockskool opened this issue Apr 26, 2023 · 0 comments

Comments

@richierockskool
Copy link

Uncaught SyntaxError file:///Users/richjagger/Documents/GitHub/homebridge-inkbird-wifi-gateway/src/BleScanner.js:2
import { once, startScanning, on, stopScanning } from '@abandonware/noble';
^^
SyntaxError: Named export 'on' not found. The requested module '@abandonware/noble' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@abandonware/noble';
const { once, startScanning, on, stopScanning } = pkg;
at instantiate (internal/modules/esm/module_job:124:21)
--- await ---
at processTicksAndRejections (node:internal/process/task_queues:95:5)
--- await ---
at runMainESM (internal/modules/run_main:55:21)
at executeUserEntryPoint (internal/modules/run_main:78:5)
at (internal/main/run_main_module:23:47)
module

Here is my code

import Emitter from 'events';
import { once, startScanning, on, stopScanning } from '@abandonware/noble';

class BleScanner extends Emitter {

constructor(log) {
super();
this.log = log;
this.devices = new Set();
once('scanStart', () => {
log('Started BLE scanning.');
});
once('scanStop', () => {
//Notify?
log('Stopped BLE scanning.');
});
once('stateChange', (state) => {
log('BLE State %s', state);
if (state === 'poweredOn') {
startScanning([], true);
}
});

// eslint-disable-next-line no-undef
on('discover', (peripheral) => {
  if (this.devices.has(peripheral.address)) {
    let buffer= peripheral.advertisement.manufacturerData;
    const expectedCrc16 = buffer[6] * 256 + buffer[5];
    // eslint-disable-next-line eqeqeq
    if (expectedCrc16 != this._getCrc16(buffer.slice(0, 5))) {
      this.log('CRC Error', buffer.toString('hex'));
      return this;
    } else {
      const temperature_raw_value = buffer[1] * 256 + buffer[0];
      let data = {
        temperature: temperature_raw_value >= 0x8000 ?
          (temperature_raw_value - 0x10000) / 100 : temperature_raw_value / 100,
        humidity: (buffer[3] * 256 + buffer[2]) / 100,
        battery: buffer[7],
      };
      this.emit(peripheral.address, data);
    }
  }
  return this;
});

}

destroy() {
stopScanning();
}

addDevice(bleMac) {
this.devices.add(bleMac);
}

/**
* @param {Buffer} buffer
*/
_getCrc16(buffer) {
let crc16 = 0xffff;
for (let byte of buffer) {
crc16 ^= byte;
for (let i = 0; i < 8; i++) {
const tmp = crc16 & 0x1;
crc16 >>= 1;
if (tmp) {
crc16 ^= 0xa001;
}
}
}
return crc16;
}
}

export default BleScanner;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant