ble-glucose
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -5,4 +5,5 @@ { | ||
"no-bitwise": 0, | ||
"no-undef": 0, | ||
"max-len": "warn" | ||
} | ||
} |
79
index.js
@@ -19,6 +19,16 @@ /* | ||
/* global navigator, BluetoothUUID */ | ||
/* eslint-disable global-require, no-global-assign */ | ||
const EventEmitter = require('events'); | ||
const sundial = require('sundial'); | ||
import sundial from 'sundial'; | ||
import isElectron from 'is-electron'; | ||
import bows from 'bows'; | ||
const isBrowser = typeof window !== 'undefined'; | ||
const debug = isElectron() ? bows('ble-glucose') : console.log; | ||
if (isElectron() || !isBrowser) { | ||
// For Node.js and Electron | ||
EventTarget = require('events'); | ||
} | ||
const options = { | ||
@@ -52,3 +62,3 @@ filters: [{ | ||
class bluetoothLE extends EventEmitter { | ||
class bluetoothLE extends EventTarget { | ||
constructor() { | ||
@@ -66,4 +76,4 @@ super(); | ||
async scan() { | ||
console.log('Requesting Bluetooth Device...'); | ||
console.log(`with ${JSON.stringify(options)}`); | ||
debug('Requesting Bluetooth Device...'); | ||
debug(`with ${JSON.stringify(options)}`); | ||
@@ -76,5 +86,5 @@ if (typeof navigator !== 'undefined') { | ||
console.log(`Name: ${this.device.name}`); | ||
console.log(`Id: ${this.device.id}`); | ||
console.log(`Connected: ${this.device.gatt.connected}`); | ||
debug(`Name: ${this.device.name}`); | ||
debug(`Id: ${this.device.id}`); | ||
debug(`Connected: ${this.device.gatt.connected}`); | ||
} else { | ||
@@ -90,3 +100,3 @@ throw new Error('navigator not available.'); | ||
]).catch((err) => { | ||
console.log('Error:', err); | ||
debug('Error:', err); | ||
throw err; | ||
@@ -99,11 +109,11 @@ }); | ||
this.server = await this.device.gatt.connect(); | ||
console.log('Connected.'); | ||
debug('Connected.'); | ||
this.deviceInfoService = await this.server.getPrimaryService('device_information'); | ||
this.glucoseService = await this.server.getPrimaryService('glucose'); | ||
console.log('Retrieved services.'); | ||
debug('Retrieved services.'); | ||
const glucoseFeature = await this.glucoseService.getCharacteristic('glucose_feature'); | ||
const features = await glucoseFeature.readValue(); | ||
console.log('Glucose features:', features.getUint16().toString(2).padStart(16, '0')); | ||
debug('Glucose features:', features.getUint16().toString(2).padStart(16, '0')); | ||
@@ -116,3 +126,3 @@ this.glucoseMeasurement = await this.glucoseService.getCharacteristic('glucose_measurement'); | ||
await this.racp.startNotifications(); | ||
console.log('Notifications started.'); | ||
debug('Notifications started.'); | ||
@@ -122,5 +132,5 @@ this.glucoseMeasurementContext.addEventListener('characteristicvaluechanged', bluetoothLE.handleContextNotifications); | ||
this.racp.addEventListener('characteristicvaluechanged', this.handleRACP); | ||
console.log('Event listeners added.'); | ||
debug('Event listeners added.'); | ||
} catch (error) { | ||
console.log(`Argh! ${error}`); | ||
debug(`Argh! ${error}`); | ||
throw error; | ||
@@ -134,4 +144,4 @@ } | ||
} | ||
console.log('Stopping notifications and removing event listeners...'); | ||
if (this.glucoseMeasurement) { | ||
debug('Stopping notifications and removing event listeners...'); | ||
try { | ||
await this.glucoseMeasurement.stopNotifications(); | ||
@@ -143,4 +153,6 @@ this.glucoseMeasurement.removeEventListener( | ||
this.glucoseMeasurement = null; | ||
} catch (err) { | ||
debug('Could not stop glucose measurement'); | ||
} | ||
if (this.glucoseMeasurementContext) { | ||
try { | ||
await this.glucoseMeasurementContext.stopNotifications(); | ||
@@ -152,4 +164,6 @@ this.glucoseMeasurementContext.removeEventListener( | ||
this.glucoseMeasurementContext = null; | ||
} catch (err) { | ||
debug('Could not stop glucose measurement context'); | ||
} | ||
if (this.racp) { | ||
try { | ||
await this.racp.stopNotifications(); | ||
@@ -161,9 +175,10 @@ this.racp.removeEventListener( | ||
this.racp = null; | ||
} catch (err) { | ||
debug('Could not stop RACP'); | ||
} | ||
console.log('Notifications and event listeners stopped.'); | ||
console.log('Disconnecting from Bluetooth Device...'); | ||
if (this.device.gatt.connected) { | ||
debug('Disconnecting from Bluetooth Device...'); | ||
if (this.device && this.device.gatt && this.device.gatt.connected) { | ||
this.device.gatt.disconnect(); | ||
} else { | ||
console.log('Bluetooth Device is already disconnected'); | ||
debug('Bluetooth Device is already disconnected'); | ||
} | ||
@@ -173,3 +188,3 @@ } | ||
async getDeviceInfo() { | ||
console.log('Getting Device Information Characteristics...'); | ||
debug('Getting Device Information Characteristics...'); | ||
const characteristics = await this.deviceInfoService.getCharacteristics(); | ||
@@ -180,3 +195,3 @@ self.deviceInfo = {}; | ||
/* eslint-disable no-await-in-loop, requests to devices are sequential */ | ||
/* eslint-disable no-await-in-loop */ | ||
for (let i = 0; i < characteristics.length; i += 1) { | ||
@@ -203,3 +218,3 @@ switch (characteristics[i].uuid) { | ||
await this.racp.writeValue(new Uint8Array(cmd)); | ||
console.log('Sent command.'); | ||
debug('Sent command.'); | ||
} | ||
@@ -217,3 +232,3 @@ | ||
const { value } = event.target; | ||
console.log('Received context:', bluetoothLE.buf2hex(value.buffer)); | ||
debug('Received context:', bluetoothLE.buf2hex(value.buffer)); | ||
this.parsed = bluetoothLE.parseMeasurementContext(value); | ||
@@ -226,3 +241,3 @@ self.contextRecords.push(this.parsed); | ||
console.log('Received:', bluetoothLE.buf2hex(value.buffer)); | ||
debug('Received:', bluetoothLE.buf2hex(value.buffer)); | ||
this.parsed = bluetoothLE.parseGlucoseMeasurement(value); | ||
@@ -239,3 +254,3 @@ self.records.push(this.parsed); | ||
}; | ||
console.log('RACP Event:', this.racpObject); | ||
debug('RACP Event:', this.racpObject); | ||
@@ -248,3 +263,3 @@ switch (this.racpObject.opCode) { | ||
if (this.racpObject.operand === 0x0101) { | ||
console.log('Success.'); | ||
debug('Success.'); | ||
self.emit('data', { | ||
@@ -334,3 +349,3 @@ records: self.records, | ||
} else { | ||
console.log('No glucose value present for ', sundial.formatDeviceTime(record.timestamp)); | ||
debug('No glucose value present for ', sundial.formatDeviceTime(record.timestamp)); | ||
} | ||
@@ -390,3 +405,3 @@ | ||
return Array.from(new Uint8Array(buffer)) | ||
.map(b => b.toString(16).padStart(2, '0')) | ||
.map((b) => b.toString(16).padStart(2, '0')) | ||
.join(' '); | ||
@@ -393,0 +408,0 @@ } |
{ | ||
"name": "ble-glucose", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Reads blood glucose values from Bluetooth LE enabled meters", | ||
@@ -34,6 +34,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"bows": "1.7.2", | ||
"core-js": "2.6.10", | ||
"lodash": "4.17.15", | ||
"is-electron": "2.2.1", | ||
"lodash": "4.17.21", | ||
"sundial": "1.6.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14425
343
5
+ Addedbows@1.7.2
+ Addedis-electron@2.2.1
+ Addedandlog@1.0.2(transitive)
+ Addedbows@1.7.2(transitive)
+ Addedis-electron@2.2.1(transitive)
+ Addedlodash@4.17.21(transitive)
- Removedlodash@4.17.15(transitive)
Updatedlodash@4.17.21