From 7109e6c52e19606a8a502805805f8cf5a333547a Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Wed, 22 Nov 2017 09:58:45 -0500 Subject: [PATCH] Add general purpose characteristic for intval3 --- lib/ble/Readme.md | 38 +++++++++++++++++++++++++----- lib/ble/index.js | 60 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/lib/ble/Readme.md b/lib/ble/Readme.md index a96ceb5..711a7c2 100644 --- a/lib/ble/Readme.md +++ b/lib/ble/Readme.md @@ -1,18 +1,44 @@ - + -## Blootstrap +## ble + +* [ble](#module_ble) + * [~BLE](#module_ble..BLE) + * [new BLE()](#new_module_ble..BLE_new) + * [.on(eventName, callback)](#module_ble..BLE+on) + * [~os](#module_ble..os) + + + +### ble~BLE Class representing the bluetooth interface -**Kind**: global class - +**Kind**: inner class of [ble](#module_ble) -### blootstrap.on(eventName, callback) +* [~BLE](#module_ble..BLE) + * [new BLE()](#new_module_ble..BLE_new) + * [.on(eventName, callback)](#module_ble..BLE+on) + + + +#### new BLE() +Establishes Bluetooth Low Energy services, accessible to process through this class + + + +#### blE.on(eventName, callback) Binds functions to events that are triggered by BLE messages -**Kind**: instance method of [Blootstrap](#Blootstrap) +**Kind**: instance method of [BLE](#module_ble..BLE) | Param | Type | Description | | --- | --- | --- | | eventName | string | Name of the event to to bind | | callback | function | Invoked when the event is triggered | + + +### ble~os +Bluetooth Low Energy module + +**Kind**: inner constant of [ble](#module_ble) diff --git a/lib/ble/index.js b/lib/ble/index.js index f05744e..2349bbd 100644 --- a/lib/ble/index.js +++ b/lib/ble/index.js @@ -1,17 +1,20 @@ 'use strict' +/** @module ble */ +/** Bluetooth Low Energy module */ + const os = require('os') const bleno = require('bleno') const util = require('util') const log = require('../log')('ble') -const wifi = require('../../lib/wifi') +const wifi = require('../wifi') const BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'intval3' const DEVICE_ID = process.env.DEVICE_ID || 'intval3' -const SERVICE_ID = process.env.SERVICE_ID || 'blootstrap' -const CHAR_ID = process.env.CHAR_ID || 'blootstrapchar' -const WIFI_ID = process.env.WIFI_ID || 'blootstrapwifi' +const SERVICE_ID = process.env.SERVICE_ID || 'intval3_ble' +const CHAR_ID = process.env.CHAR_ID || 'intval3char' +const WIFI_ID = process.env.WIFI_ID || 'wifichar' const NETWORK = os.networkInterfaces() const MAC = getMac() || spoofMac() @@ -19,7 +22,6 @@ let currentWifi = 'disconnected' const chars = [] - function createChar(name, uuid, prop, write, read) { function characteristic () { bleno.Characteristic.call(this, { @@ -38,7 +40,8 @@ function createChar(name, uuid, prop, write, read) { chars.push(new characteristic()) } -function createChars () { +function createChars (onWrite, onRead) { + createChar('intval3', CHAR_ID, ['read', 'write'], onWrite, onRead) createChar('wifi', WIFI_ID, ['read', 'write'], onWifiWrite, onWifiRead) } @@ -49,7 +52,7 @@ function onWifiWrite (data, offset, withoutResponse, callback) { let ssid let pwd if (offset) { - console.warn(`Offset scenario`) + log.warn(`Offset scenario`) result = bleno.Characteristic.RESULT_ATTR_NOT_LONG return callback(result) } @@ -57,15 +60,15 @@ function onWifiWrite (data, offset, withoutResponse, callback) { obj = JSON.parse(utf8) ssid = obj.ssid pwd = obj.pwd - console.log(`Connecting to AP: ${ssid}...`) + log.info(`connecting to AP`, { ssid : ssid }) return wifi.setNetwork(ssid, pwd, (err, data) => { if (err) { - console.error('Error configuring wifi', err) + log.error('Error configuring wifi', err) result = bleno.Characteristic.RESULT_UNLIKELY_ERROR return callback(result) } currentWifi = ssid - console.log(`Connected to ${ssid}`) + log.info(`Connected to AP`, { ssid : ssid }) result = bleno.Characteristic.RESULT_SUCCESS return callback(result) }) @@ -115,14 +118,19 @@ function capitalize (s) { /** Class representing the bluetooth interface */ class BLE { + /** + * Establishes Bluetooth Low Energy services, accessible to process through this class + * + * @constructor + */ constructor () { log.info('Starting bluetooth service') bleno.on('stateChange', state => { const BLE_ID = `${DEVICE_ID}_${MAC}` - console.log(`on -> stateChange: ${state}`) + log.info('stateChange', { state : state }) if (state === 'poweredOn') { - console.log(`Started advertising BLE serveses as ${BLE_ID}`) + log.info('Starting advertising', { BLE_ID : BLE_ID }) bleno.startAdvertising(BLENO_DEVICE_NAME, [BLE_ID]) } else { bleno.stopAdvertising() @@ -130,8 +138,8 @@ class BLE { }) bleno.on('advertisingStart', err => { - console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success')) - createChars() + log.info('advertisingStart', { res : (err ? 'error ' + err : 'success') }) + createChars(this._onWrite.bind(this), this._onRead.bind(this)) if (!err) { bleno.setServices([ new bleno.PrimaryService({ @@ -143,13 +151,33 @@ class BLE { }) bleno.on('accept', clientAddress => { - console.log(`${clientAddress} accepted`) + log.info('accept', { clientAddress : clientAddress }) }) bleno.on('disconnect', clientAddress => { - console.log(`${clientAddress} disconnected`) + log.info('disconnect', { clientAddress : clientAddress }) }) } + _onWrite (data, offset, withoutResponse, callback) { + let result + let utf8 + let obj + if (offset) { + log.warn(`Offset scenario`) + result = bleno.Characteristic.RESULT_ATTR_NOT_LONG + return callback(result) + } + utf8 = data.toString('utf8') + obj = JSON.parse(utf8) + console.dir(obj) + result = bleno.Characteristic.RESULT_SUCCESS + return callback(result) + } + _onRead (offset, callback) { + const result = bleno.Characteristic.RESULT_SUCCESS + const data = new Buffer(JSON.stringify( { success : true } )) + callback(result, data.slice(offset, data.length)) + } /** * Binds functions to events that are triggered by BLE messages *