2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
/** @module ble */
|
|
|
|
/** Bluetooth Low Energy module */
|
|
|
|
|
2017-08-22 03:01:08 +00:00
|
|
|
const os = require('os')
|
2017-08-22 01:11:07 +00:00
|
|
|
const bleno = require('bleno')
|
|
|
|
const util = require('util')
|
2017-08-28 12:49:47 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
const log = require('../log')('ble')
|
2017-11-22 14:58:45 +00:00
|
|
|
const wifi = require('../wifi')
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
const BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'intval3'
|
|
|
|
const DEVICE_ID = process.env.DEVICE_ID || 'intval3'
|
2017-11-22 14:58:45 +00:00
|
|
|
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'
|
2017-08-22 03:01:08 +00:00
|
|
|
const NETWORK = os.networkInterfaces()
|
|
|
|
const MAC = getMac() || spoofMac()
|
|
|
|
|
|
|
|
let currentWifi = 'disconnected'
|
2017-12-11 19:38:46 +00:00
|
|
|
let getState
|
2017-08-22 01:11:07 +00:00
|
|
|
|
|
|
|
const chars = []
|
|
|
|
|
|
|
|
function createChar(name, uuid, prop, write, read) {
|
|
|
|
function characteristic () {
|
|
|
|
bleno.Characteristic.call(this, {
|
|
|
|
uuid : uuid,
|
|
|
|
properties: prop
|
|
|
|
})
|
|
|
|
}
|
|
|
|
util.inherits(characteristic, bleno.Characteristic)
|
2017-08-22 03:45:56 +00:00
|
|
|
if (prop.indexOf('read') !== -1) {
|
2017-08-22 01:11:07 +00:00
|
|
|
//data, offset, withoutResponse, callback
|
|
|
|
characteristic.prototype.onReadRequest = read
|
|
|
|
}
|
2017-08-22 03:45:56 +00:00
|
|
|
if (prop.indexOf('write') !== -1) {
|
2017-08-22 01:11:07 +00:00
|
|
|
characteristic.prototype.onWriteRequest = write
|
|
|
|
}
|
2017-08-22 03:01:08 +00:00
|
|
|
chars.push(new characteristic())
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
function createChars (onWrite, onRead) {
|
|
|
|
createChar('intval3', CHAR_ID, ['read', 'write'], onWrite, onRead)
|
2017-08-22 03:41:27 +00:00
|
|
|
createChar('wifi', WIFI_ID, ['read', 'write'], onWifiWrite, onWifiRead)
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onWifiWrite (data, offset, withoutResponse, callback) {
|
|
|
|
let result
|
|
|
|
let utf8
|
|
|
|
let obj
|
|
|
|
let ssid
|
|
|
|
let pwd
|
|
|
|
if (offset) {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.warn(`Offset scenario`)
|
2017-08-22 01:11:07 +00:00
|
|
|
result = bleno.Characteristic.RESULT_ATTR_NOT_LONG
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
utf8 = data.toString('utf8')
|
|
|
|
obj = JSON.parse(utf8)
|
|
|
|
ssid = obj.ssid
|
|
|
|
pwd = obj.pwd
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info(`connecting to AP`, { ssid : ssid })
|
2017-08-22 01:11:07 +00:00
|
|
|
return wifi.setNetwork(ssid, pwd, (err, data) => {
|
|
|
|
if (err) {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.error('Error configuring wifi', err)
|
2017-08-22 01:11:07 +00:00
|
|
|
result = bleno.Characteristic.RESULT_UNLIKELY_ERROR
|
|
|
|
return callback(result)
|
|
|
|
}
|
2017-08-22 03:01:08 +00:00
|
|
|
currentWifi = ssid
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info(`Connected to AP`, { ssid : ssid })
|
2017-08-22 01:11:07 +00:00
|
|
|
result = bleno.Characteristic.RESULT_SUCCESS
|
|
|
|
return callback(result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWifiRead (offset, callback) {
|
|
|
|
const result = bleno.Characteristic.RESULT_SUCCESS
|
2017-08-22 03:01:08 +00:00
|
|
|
const data = new Buffer(JSON.stringify(currentWifi))
|
2017-08-22 01:11:07 +00:00
|
|
|
callback(result, data.slice(offset, data.length))
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:01:08 +00:00
|
|
|
function getMac () {
|
|
|
|
const colonRe = new RegExp(':', 'g')
|
|
|
|
if (NETWORK && NETWORK.wlan0 && NETWORK.wlan0[0] && NETWORK.wlan0[0].mac) {
|
|
|
|
return NETWORK.wlan0[0].mac.replace(colonRe, '')
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function spoofMac () {
|
|
|
|
const fs = require('fs')
|
|
|
|
const FSPATH = require.resolve('uuid')
|
|
|
|
const IDFILE = os.homedir() + '/.intval3id'
|
|
|
|
let uuid
|
|
|
|
let UUIDPATH
|
|
|
|
let TMP
|
|
|
|
let MACTMP
|
|
|
|
let dashRe
|
|
|
|
delete require.cache[FSPATH]
|
|
|
|
if (fs.existsSync(IDFILE)) {
|
|
|
|
return fs.readFileSync(IDFILE, 'utf8')
|
|
|
|
}
|
|
|
|
uuid = require('uuid').v4
|
|
|
|
UUIDPATH = require.resolve('uuid')
|
|
|
|
delete require.cache[UUIDPATH]
|
|
|
|
TMP = uuid()
|
|
|
|
MACTMP = TMP.replace(dashRe, '').substring(0, 12)
|
|
|
|
dashRe = new RegExp('-', 'g')
|
|
|
|
fs.writeFileSync(IDFILE, MACTMP, 'utf8')
|
|
|
|
return MACTMP
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
function capitalize (s) {
|
|
|
|
return s[0].toUpperCase() + s.slice(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Class representing the bluetooth interface */
|
|
|
|
class BLE {
|
2017-11-22 14:58:45 +00:00
|
|
|
/**
|
|
|
|
* Establishes Bluetooth Low Energy services, accessible to process through this class
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-12-11 19:38:46 +00:00
|
|
|
constructor (bleGetState) {
|
2017-11-22 14:30:08 +00:00
|
|
|
log.info('Starting bluetooth service')
|
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
getState = bleGetState
|
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
bleno.on('stateChange', state => {
|
|
|
|
const BLE_ID = `${DEVICE_ID}_${MAC}`
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('stateChange', { state : state })
|
2017-11-22 14:30:08 +00:00
|
|
|
if (state === 'poweredOn') {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('Starting advertising', { BLE_ID : BLE_ID })
|
2017-11-22 14:30:08 +00:00
|
|
|
bleno.startAdvertising(BLENO_DEVICE_NAME, [BLE_ID])
|
|
|
|
} else {
|
|
|
|
bleno.stopAdvertising()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('advertisingStart', err => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('advertisingStart', { res : (err ? 'error ' + err : 'success') })
|
|
|
|
createChars(this._onWrite.bind(this), this._onRead.bind(this))
|
2017-11-22 14:30:08 +00:00
|
|
|
if (!err) {
|
|
|
|
bleno.setServices([
|
|
|
|
new bleno.PrimaryService({
|
|
|
|
uuid : SERVICE_ID, //hardcoded across panels
|
|
|
|
characteristics : chars
|
|
|
|
})
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('accept', clientAddress => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('accept', { clientAddress : clientAddress })
|
2017-11-22 14:30:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('disconnect', clientAddress => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('disconnect', { clientAddress : clientAddress })
|
2017-11-22 14:30:08 +00:00
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-11-22 14:58:45 +00:00
|
|
|
_onWrite (data, offset, withoutResponse, callback) {
|
2017-12-11 19:38:46 +00:00
|
|
|
let result = {}
|
2017-11-22 14:58:45 +00:00
|
|
|
let utf8
|
|
|
|
let obj
|
|
|
|
if (offset) {
|
|
|
|
log.warn(`Offset scenario`)
|
|
|
|
result = bleno.Characteristic.RESULT_ATTR_NOT_LONG
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
utf8 = data.toString('utf8')
|
2017-12-11 19:38:46 +00:00
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
obj = JSON.parse(utf8)
|
|
|
|
console.dir(obj)
|
|
|
|
result = bleno.Characteristic.RESULT_SUCCESS
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
_onRead (offset, callback) {
|
|
|
|
const result = bleno.Characteristic.RESULT_SUCCESS
|
2017-12-11 19:38:46 +00:00
|
|
|
const state = getState()
|
2017-12-11 19:47:05 +00:00
|
|
|
console.log('getState')
|
|
|
|
console.dir(state)
|
2017-12-11 19:44:27 +00:00
|
|
|
const data = new Buffer(JSON.stringify( state ))
|
2017-11-22 14:58:45 +00:00
|
|
|
callback(result, data.slice(offset, data.length))
|
|
|
|
}
|
2017-11-22 14:30:08 +00:00
|
|
|
/**
|
|
|
|
* Binds functions to events that are triggered by BLE messages
|
|
|
|
*
|
|
|
|
* @param {string} eventName Name of the event to to bind
|
|
|
|
* @param {function} callback Invoked when the event is triggered
|
|
|
|
*/
|
|
|
|
on (eventName, callback) {
|
|
|
|
this[`_on${capitalize(eventName)}`] = callback
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-12-11 19:38:46 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
module.exports = BLE
|