Add general purpose characteristic for intval3

This commit is contained in:
mmcw-dev 2017-11-22 09:58:45 -05:00
parent e2b3d86f31
commit 7109e6c52e
2 changed files with 76 additions and 22 deletions

View File

@ -1,18 +1,44 @@
<a name="Blootstrap"></a>
<a name="module_ble"></a>
## 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)
<a name="module_ble..BLE"></a>
### ble~BLE
Class representing the bluetooth interface
**Kind**: global class
<a name="Blootstrap+on"></a>
**Kind**: inner class of [<code>ble</code>](#module_ble)
### blootstrap.on(eventName, callback)
* [~BLE](#module_ble..BLE)
* [new BLE()](#new_module_ble..BLE_new)
* [.on(eventName, callback)](#module_ble..BLE+on)
<a name="new_module_ble..BLE_new"></a>
#### new BLE()
Establishes Bluetooth Low Energy services, accessible to process through this class
<a name="module_ble..BLE+on"></a>
#### blE.on(eventName, callback)
Binds functions to events that are triggered by BLE messages
**Kind**: instance method of [<code>Blootstrap</code>](#Blootstrap)
**Kind**: instance method of [<code>BLE</code>](#module_ble..BLE)
| Param | Type | Description |
| --- | --- | --- |
| eventName | <code>string</code> | Name of the event to to bind |
| callback | <code>function</code> | Invoked when the event is triggered |
<a name="module_ble..os"></a>
### ble~os
Bluetooth Low Energy module
**Kind**: inner constant of [<code>ble</code>](#module_ble)

View File

@ -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
*