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

Changed dependency and code to work on Linux #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jsroots-moestuin-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## Installation
```sh
npm install
npm install --no-optional
```
Start the REST server on port 8888.
```sh
Expand Down
148 changes: 84 additions & 64 deletions jsroots-moestuin-node/miflora.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,111 @@ class MiFlora extends EventEmitter {
super();
this.noble = noble;
this._macAddress = macAddress;
noble.on('discover', this.discover.bind(this));
}

discover(peripheral) {
if (this._macAddress !== undefined) {
if (this._macAddress.toLowerCase() === peripheral.address.toLowerCase()) {
// start listening the specific device
this.connectDevice(peripheral);
noble.on('discover', (peripheral) => {
// console.log('noble:onDiscover');

if (this._macAddress !== undefined) {
if (this._macAddress.toLowerCase() === peripheral.address.toLowerCase()) {
console.log('connect to macAdress :' + this._macAddress);
// start listening the specific device
this.connectDevice(peripheral, this);
}
} else if (peripheral.advertisement.localName === DEFAULT_DEVICE_NAME) {
console.log('connect to ' + DEFAULT_DEVICE_NAME);
// start listening found device
this.connectDevice(peripheral, this);
}
} else if (peripheral.advertisement.localName === DEFAULT_DEVICE_NAME) {
// start listening found device
this.connectDevice(peripheral);
}
});

noble.on('scanStart', () => {
console.log('noble:onScanStart');
});
noble.on('scanStop', () => {
console.log('noble:onScanStop');
this.startScanning();
});

noble.on('warning', (message) => {
console.log('noble:onWarning:' + message);
});
}

connectDevice(peripheral) {
connectDevice(peripheral, context) {
// prevent simultanious connection to the same device
console.log('connect to device : ' + peripheral);
if (peripheral.state === 'disconnected') {
peripheral.connect();
peripheral.once('connect', function () {
this.listenDevice(peripheral, this);
}.bind(this));
}
}

listenDevice(peripheral, context) {
peripheral.discoverSomeServicesAndCharacteristics(SERVICE_UUIDS, CHARACTERISTIC_UUIDS, function (error, services, characteristics) {
characteristics.forEach(function (characteristic) {
switch (characteristic.uuid) {
case DATA_CHARACTERISTIC_UUID:
characteristic.read(function (error, data) {
context.parseData(peripheral, data);
});
break;
case FIRMWARE_CHARACTERISTIC_UUID:
characteristic.read(function (error, data) {
context.parseFirmwareData(peripheral, data);
});
break;
case REALTIME_CHARACTERISTIC_UUID:
characteristic.write(REALTIME_META_VALUE, false);
break;
}

peripheral.once('connect', () => {
peripheral.discoverSomeServicesAndCharacteristics(SERVICE_UUIDS, CHARACTERISTIC_UUIDS, function (error, services, characteristics) {
console.log('discover some service - services : ' + services);
console.log('discover some service - error :' + error);
characteristics.forEach((characteristic) => {
switch (characteristic.uuid) {
case DATA_CHARACTERISTIC_UUID:
characteristic.read((error, data) => {
if (error) console.log('data characteristics - error :' + error);

let temperature = data.readUInt16LE(0) / 10;
let lux = data.readUInt32LE(3);
let moisture = data.readUInt16BE(6);
let fertility = data.readUInt16LE(8);
let deviceData = new DeviceData(peripheral.id,
temperature,
lux,
moisture,
fertility);
console.table(deviceData);
context.emit('data', deviceData);
});
break;
case FIRMWARE_CHARACTERISTIC_UUID:
characteristic.read((error, data) => {
if (error) console.log('firmware characteristics - error :' + error);

const firmware = {
deviceId: peripheral.id,
batteryLevel: parseInt(data.toString('hex', 0, 1), 16),
firmwareVersion: data.toString('ascii', 2, data.length)
};
console.table(firmware);
context.emit('firmware', firmware);
});
break;
case REALTIME_CHARACTERISTIC_UUID:
characteristic.write(REALTIME_META_VALUE, false);
break;
}
});
});
});
});
}

parseData(peripheral, data) {
let temperature = data.readUInt16LE(0) / 10;
let lux = data.readUInt32LE(3);
let moisture = data.readUInt16BE(6);
let fertility = data.readUInt16LE(8);
let deviceData = new DeviceData(peripheral.id,
temperature,
lux,
moisture,
fertility);

this.emit('data', deviceData);
}
}

parseFirmwareData(peripheral, data) {
let firmware = {
deviceId: peripheral.id,
batteryLevel: parseInt(data.toString('hex', 0, 1), 16),
firmwareVersion: data.toString('ascii', 2, data.length)
};

this.emit('firmware', firmware);
}

startScanning() {
if (noble.state === 'poweredOn') {
noble.startScanning([], true);
console.log('Lets start scanning.');
noble.startScanning([], true, (e) => {
console.table(e);
});
} else {
// bind event to start scanning
noble.on('stateChange', function (state) {
console.log('Wait for stateChange.');
noble.on('stateChange', (state) => {
console.log('noble state : ' + state);
if (state === 'poweredOn') {
noble.startScanning([], true);
console.log('Ready set! start scanning!...');
noble.startScanning([], true, (e) => {
console.table(e);
});
}
});
}
}

stopScanning() {
console.log("stopScanning...");
noble.stopScanning();
}
}
Expand Down
9 changes: 3 additions & 6 deletions jsroots-moestuin-node/package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
"dependencies": {
"@stencil/core": "^0.15.2",
"@stencil/router": "~0.3.0",
"bluetooth-hci-socket": "^0.5.1",
"body-parser": "^1.18.3",
"debug": "^2.6.0",
"express": "^4.16.3",
"http": "0.0.0",
"noble": "^1.7.0",
"node-bluetooth": "^1.2.4",
"node-mi-flora": "^0.1.1",
"npm": "^6.4.1",
"socket.io": "^2.1.1",
"xpc-connection": "sandeepmistry/node-xpc-connection#pull/26/head"
},
"resolutions": {
"xpc-connection": "sandeepmistry/node-xpc-connection#pull/26/head"
"socket.io": "^2.1.1"
},
"devDependencies": {
"xo": "^0.17.1"
Expand All @@ -43,4 +41,3 @@
"node": ">=6.9"
}
}

11 changes: 7 additions & 4 deletions jsroots-moestuin-node/rest-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MiFlora = require('./miflora');
const express = require('express')
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const io = require('socket.io')(http);
Expand Down Expand Up @@ -31,18 +31,21 @@ flora.on('data', (data) => {
updateDeviceData(data);
});

flora.on('firmware', (data) => {
console.table(data);
});
// Scan for BLE Flora devices
flora.startScanning();


// Update data received
// Update or add device data received
function updateDeviceData(data) {
let deviceIndex = floraDevices.findIndex(x => x.deviceId === data.deviceId);

if (deviceIndex == -1) {
if (deviceIndex === -1) {
floraDevices.push(data);
} else {
floraDevices[deviceIndex] = data;
}
io.local.emit('data', floraDevices)
io.local.emit('data', floraDevices);
}