Push dev work to master #2

Merged
sixteenmillimeter merged 416 commits from dev into master 2018-07-19 15:29:02 +00:00
2 changed files with 76 additions and 22 deletions
Showing only changes of commit 7109e6c52e - Show all commits

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 Class representing the bluetooth interface
**Kind**: global class **Kind**: inner class of [<code>ble</code>](#module_ble)
<a name="Blootstrap+on"></a>
### 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 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 | | Param | Type | Description |
| --- | --- | --- | | --- | --- | --- |
| eventName | <code>string</code> | Name of the event to to bind | | eventName | <code>string</code> | Name of the event to to bind |
| callback | <code>function</code> | Invoked when the event is triggered | | 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' 'use strict'
/** @module ble */
/** Bluetooth Low Energy module */
const os = require('os') const os = require('os')
const bleno = require('bleno') const bleno = require('bleno')
const util = require('util') const util = require('util')
const log = require('../log')('ble') 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 BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'intval3'
const DEVICE_ID = process.env.DEVICE_ID || 'intval3' const DEVICE_ID = process.env.DEVICE_ID || 'intval3'
const SERVICE_ID = process.env.SERVICE_ID || 'blootstrap' const SERVICE_ID = process.env.SERVICE_ID || 'intval3_ble'
const CHAR_ID = process.env.CHAR_ID || 'blootstrapchar' const CHAR_ID = process.env.CHAR_ID || 'intval3char'
const WIFI_ID = process.env.WIFI_ID || 'blootstrapwifi' const WIFI_ID = process.env.WIFI_ID || 'wifichar'
const NETWORK = os.networkInterfaces() const NETWORK = os.networkInterfaces()
const MAC = getMac() || spoofMac() const MAC = getMac() || spoofMac()
@ -19,7 +22,6 @@ let currentWifi = 'disconnected'
const chars = [] const chars = []
function createChar(name, uuid, prop, write, read) { function createChar(name, uuid, prop, write, read) {
function characteristic () { function characteristic () {
bleno.Characteristic.call(this, { bleno.Characteristic.call(this, {
@ -38,7 +40,8 @@ function createChar(name, uuid, prop, write, read) {
chars.push(new characteristic()) 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) createChar('wifi', WIFI_ID, ['read', 'write'], onWifiWrite, onWifiRead)
} }
@ -49,7 +52,7 @@ function onWifiWrite (data, offset, withoutResponse, callback) {
let ssid let ssid
let pwd let pwd
if (offset) { if (offset) {
console.warn(`Offset scenario`) log.warn(`Offset scenario`)
result = bleno.Characteristic.RESULT_ATTR_NOT_LONG result = bleno.Characteristic.RESULT_ATTR_NOT_LONG
return callback(result) return callback(result)
} }
@ -57,15 +60,15 @@ function onWifiWrite (data, offset, withoutResponse, callback) {
obj = JSON.parse(utf8) obj = JSON.parse(utf8)
ssid = obj.ssid ssid = obj.ssid
pwd = obj.pwd pwd = obj.pwd
console.log(`Connecting to AP: ${ssid}...`) log.info(`connecting to AP`, { ssid : ssid })
return wifi.setNetwork(ssid, pwd, (err, data) => { return wifi.setNetwork(ssid, pwd, (err, data) => {
if (err) { if (err) {
console.error('Error configuring wifi', err) log.error('Error configuring wifi', err)
result = bleno.Characteristic.RESULT_UNLIKELY_ERROR result = bleno.Characteristic.RESULT_UNLIKELY_ERROR
return callback(result) return callback(result)
} }
currentWifi = ssid currentWifi = ssid
console.log(`Connected to ${ssid}`) log.info(`Connected to AP`, { ssid : ssid })
result = bleno.Characteristic.RESULT_SUCCESS result = bleno.Characteristic.RESULT_SUCCESS
return callback(result) return callback(result)
}) })
@ -115,14 +118,19 @@ function capitalize (s) {
/** Class representing the bluetooth interface */ /** Class representing the bluetooth interface */
class BLE { class BLE {
/**
* Establishes Bluetooth Low Energy services, accessible to process through this class
*
* @constructor
*/
constructor () { constructor () {
log.info('Starting bluetooth service') log.info('Starting bluetooth service')
bleno.on('stateChange', state => { bleno.on('stateChange', state => {
const BLE_ID = `${DEVICE_ID}_${MAC}` const BLE_ID = `${DEVICE_ID}_${MAC}`
console.log(`on -> stateChange: ${state}`) log.info('stateChange', { state : state })
if (state === 'poweredOn') { 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]) bleno.startAdvertising(BLENO_DEVICE_NAME, [BLE_ID])
} else { } else {
bleno.stopAdvertising() bleno.stopAdvertising()
@ -130,8 +138,8 @@ class BLE {
}) })
bleno.on('advertisingStart', err => { bleno.on('advertisingStart', err => {
console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success')) log.info('advertisingStart', { res : (err ? 'error ' + err : 'success') })
createChars() createChars(this._onWrite.bind(this), this._onRead.bind(this))
if (!err) { if (!err) {
bleno.setServices([ bleno.setServices([
new bleno.PrimaryService({ new bleno.PrimaryService({
@ -143,13 +151,33 @@ class BLE {
}) })
bleno.on('accept', clientAddress => { bleno.on('accept', clientAddress => {
console.log(`${clientAddress} accepted`) log.info('accept', { clientAddress : clientAddress })
}) })
bleno.on('disconnect', 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 * Binds functions to events that are triggered by BLE messages
* *