Commit 1159af26 authored by Donald Chen's avatar Donald Chen
Browse files

code formatter, dont make duplicate devices

parent 55845c3c
......@@ -4,89 +4,89 @@
* A Device object manages an edge device and its bluetooth communication
*/
class Device {
constructor(bleDevice) {
this.id = bleDevice.address.replace(/:/g, '');
this.bleDevice = bleDevice;
this.fwVersion = null;
}
constructor(bleDevice) {
this.id = bleDevice.address.replace(/:/g, '');
this.bleDevice = bleDevice;
this.fwVersion = null;
}
/**
* Returns a Promise that will resolve with the ble device's firmware revision,
* or reject with an error
*/
readFirmware() {
return new Promise((resolve, reject) => {
console.log(`${this.id}: readFirmwareRevision`);
this.bleDevice.readFirmwareRevision((error, firmwareRevision) => {
if (error) {
reject(error);
return;
}
/**
* Returns a Promise that will resolve with the ble device's firmware revision,
* or reject with an error
*/
readFirmware() {
return new Promise((resolve, reject) => {
console.log(`${this.id}: readFirmwareRevision`);
this.bleDevice.readFirmwareRevision((error, firmwareRevision) => {
if (error) {
reject(error);
return;
}
console.log(`${this.id}: firmware revision = ${firmwareRevision}`);
this.fwVersion = firmwareRevision;
console.log(`${this.id}: firmware revision = ${firmwareRevision}`);
this.fwVersion = firmwareRevision;
resolve();
});
});
}
resolve();
});
});
}
/**
* Returns a Promise that will connect and set up the ble device with noble.
* Once set up, it will read the device's firmware and enable its luxometer.
*/
setUp() {
return new Promise((resolve, reject) => {
this.bleDevice.connectAndSetUp((setupError) => {
if (setupError) {
reject(new Error(setupError));
}
/**
* Returns a Promise that will connect and set up the ble device with noble.
* Once set up, it will read the device's firmware and enable its luxometer.
*/
setUp() {
return new Promise((resolve, reject) => {
this.bleDevice.connectAndSetUp((setupError) => {
if (setupError) {
reject(new Error(setupError));
}
this.readFirmware()
.then(this.enableLuxometer.bind(this))
.then(() => {
resolve(this);
})
.catch((err) => {
console.log(`DEVICE SETUP ERROR: ${err}`);
throw err;
});
});
});
}
this.readFirmware()
.then(this.enableLuxometer.bind(this))
.then(() => {
resolve(this);
})
.catch((err) => {
console.log(`DEVICE SETUP ERROR: ${err}`);
throw err;
});
});
});
}
/**
* Returns a Promise that will enable the device's luxometer
*/
enableLuxometer() {
return new Promise((resolve) => {
console.log(`${this.id}: enableLuxometer`);
this.bleDevice.enableLuxometer(resolve);
});
}
/**
* Returns a Promise that will enable the device's luxometer
*/
enableLuxometer() {
return new Promise((resolve) => {
console.log(`${this.id}: enableLuxometer`);
this.bleDevice.enableLuxometer(resolve);
});
}
/**
* Returns a Promise that will notify the device's luxometer
*/
notifyLuxometer() {
return new Promise((resolve) => {
console.log(`${this.id}: notifyLuxometer`);
this.bleDevice.notifyLuxometer(resolve);
});
}
/**
* Returns a Promise that will notify the device's luxometer
*/
notifyLuxometer() {
return new Promise((resolve) => {
console.log(`${this.id}: notifyLuxometer`);
this.bleDevice.notifyLuxometer(resolve);
});
}
/**
* This function will bind its given callback to run when the ble device
* triggers a 'luxometerChange' event.
*/
onLuxometerChange(callback) {
console.log(`${this.id}: set up onluxchange`);
this.bleDevice.on('luxometerChange', (lux) => {
callback(this, lux);
});
/**
* This function will bind its given callback to run when the ble device
* triggers a 'luxometerChange' event.
*/
onLuxometerChange(callback) {
console.log(`${this.id}: set up onluxchange`);
this.bleDevice.on('luxometerChange', (lux) => {
callback(this, lux);
});
return this.notifyLuxometer();
}
return this.notifyLuxometer();
}
}
module.exports = Device;
module.exports = Device;
\ No newline at end of file
......@@ -20,74 +20,74 @@ const devices = {};
const provider = new CloudProvider(config);
function startScanning() {
// Restart scanning
// https://github.com/sandeepmistry/noble/issues/223
if (noble.state === 'poweredOn') {
noble.startScanning();
} else {
throw new Error('BLE poweredOff');
}
// Restart scanning
// https://github.com/sandeepmistry/noble/issues/223
if (noble.state === 'poweredOn') {
noble.startScanning();
} else {
throw new Error('BLE poweredOff');
}
}
function stopScanning() {
noble.stopScanning();
noble.stopScanning();
}
/**
* Called when a new device is discovered
*/
function onDiscover(beaconInst) {
const device = new Device(beaconInst);
// Ignore duplicates
if (devices[device.id]) {
console.log(`${device.id}: Duplicate device found, ignoring`);
return;
}
else {
console.log(`Discovered: ${device.id}`);
//must stop scanning to initiate a connection
stopScanning()
// Connect to device and setup
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
const telemetry = {
deviceID: d.id,
light: lux.toFixed(1),
};
provider.sendTelemetry(telemetry);
});
// Store in list of known devices
devices[device.id] = device;
// Set disconnect handler
device.bleDevice.on('disconnect', function disconnectHandler() {
console.log(`${device.id}: disconnected`);
delete devices[device.id];
device.bleDevice.removeListener('disconnect', disconnectHandler);
});
// Wait for as long as possible before scanning again to avoid race
// conditions
startScanning();
})
.catch((error) => {
console.log(error)
process.exit(1)
});
const id = beaconInst.address.replace(/:/g, '');
// Ignore duplicates
if (devices[id]) {
console.log(`${id}: Duplicate device found, ignoring`);
return;
} else {
const device = new Device(beaconInst);
console.log(`Discovered: ${device.id}`);
//must stop scanning to initiate a connection
stopScanning()
// Connect to device and setup
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
const telemetry = {
deviceID: d.id,
light: lux.toFixed(1),
};
provider.sendTelemetry(telemetry);
});
// Store in list of known devices
devices[device.id] = device;
// Set disconnect handler
device.bleDevice.on('disconnect', function disconnectHandler() {
console.log(`${device.id}: disconnected`);
delete devices[device.id];
device.bleDevice.removeListener('disconnect', disconnectHandler);
});
// Wait for as long as possible before scanning again to avoid race
// conditions
startScanning();
})
.catch((error) => {
console.log(error)
process.exit(1)
});
}
}
}
noble.on('scanStop', () => {
console.log('Scanning stopped');
console.log('Scanning stopped');
});
noble.on('scanStart', () => {
console.log('Scanning started');
console.log('Scanning started');
});
rigadoSensorBeacon.discoverAll(onDiscover);
rigadoSensorBeacon.discoverAll(onDiscover);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment