From ef0d729a581b6007e062ebdd669069bb6d3dfa97 Mon Sep 17 00:00:00 2001 From: Philippe Tjon-A-Hen Date: Fri, 7 Dec 2018 10:29:10 +0100 Subject: [PATCH] Changed dependency and code to work on Linux --- jsroots-moestuin-node/README.md | 2 +- jsroots-moestuin-node/miflora.js | 148 +++++++++++++++------------ jsroots-moestuin-node/package.json | 9 +- jsroots-moestuin-node/rest-server.js | 11 +- 4 files changed, 95 insertions(+), 75 deletions(-) mode change 100755 => 100644 jsroots-moestuin-node/package.json diff --git a/jsroots-moestuin-node/README.md b/jsroots-moestuin-node/README.md index f227ef8..d862714 100755 --- a/jsroots-moestuin-node/README.md +++ b/jsroots-moestuin-node/README.md @@ -7,7 +7,7 @@ ## Installation ```sh -npm install +npm install --no-optional ``` Start the REST server on port 8888. ```sh diff --git a/jsroots-moestuin-node/miflora.js b/jsroots-moestuin-node/miflora.js index d5a14dc..085f96c 100755 --- a/jsroots-moestuin-node/miflora.js +++ b/jsroots-moestuin-node/miflora.js @@ -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(); } } diff --git a/jsroots-moestuin-node/package.json b/jsroots-moestuin-node/package.json old mode 100755 new mode 100644 index 99d0489..d8a9a35 --- a/jsroots-moestuin-node/package.json +++ b/jsroots-moestuin-node/package.json @@ -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" @@ -43,4 +41,3 @@ "node": ">=6.9" } } - diff --git a/jsroots-moestuin-node/rest-server.js b/jsroots-moestuin-node/rest-server.js index 7d7139a..38f9826 100644 --- a/jsroots-moestuin-node/rest-server.js +++ b/jsroots-moestuin-node/rest-server.js @@ -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); @@ -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); } \ No newline at end of file