Add bleno to main process by adding a module that gets incorporated into the same process running express.

This commit is contained in:
mmcw-dev 2017-11-22 09:30:08 -05:00
parent b9ceb506c7
commit 0272ca21f6
5 changed files with 61 additions and 108 deletions

View File

@ -4,7 +4,7 @@ const restify = require('restify')
const log = require('./lib/log')('main')
const fs = require('fs')
//const ble = require('./lib/blootstrap')
const ble = require('./lib/ble')
const intval = require('./lib/intval')
const PACKAGE = require('./package.json')
@ -38,6 +38,10 @@ function createServer () {
})
}
function bleBindings () {
}
function rDir (req, res, next) {
let dir = true
let set = false

View File

@ -1,14 +1,14 @@
'use strict'
const ipc = require('node-ipc')
const os = require('os')
const bleno = require('bleno')
const util = require('util')
const log = require('../log')('ble')
const wifi = require('../../lib/wifi')
const BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'my_project'
const DEVICE_ID = process.env.DEVICE_ID || 'my_project_id'
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'
@ -19,10 +19,6 @@ let currentWifi = 'disconnected'
const chars = []
ipc.config.id = 'blootstrap_ble'
ipc.config.retry = 1500
ipc.config.rawBuffer = true
ipc.config.encoding = 'hex'
function createChar(name, uuid, prop, write, read) {
function characteristic () {
@ -112,50 +108,57 @@ function spoofMac () {
return MACTMP
}
console.log('Starting bluetooth service')
bleno.on('stateChange', state => {
const BLE_ID = `${DEVICE_ID}_${MAC}`
console.log(`on -> stateChange: ${state}`)
if (state === 'poweredOn') {
console.log(`Started advertising BLE serveses as ${BLE_ID}`)
bleno.startAdvertising(BLENO_DEVICE_NAME, [BLE_ID])
} else {
bleno.stopAdvertising()
function capitalize (s) {
return s[0].toUpperCase() + s.slice(1)
}
/** Class representing the bluetooth interface */
class BLE {
constructor () {
log.info('Starting bluetooth service')
bleno.on('stateChange', state => {
const BLE_ID = `${DEVICE_ID}_${MAC}`
console.log(`on -> stateChange: ${state}`)
if (state === 'poweredOn') {
console.log(`Started advertising BLE serveses as ${BLE_ID}`)
bleno.startAdvertising(BLENO_DEVICE_NAME, [BLE_ID])
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', err => {
console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success'))
createChars()
if (!err) {
bleno.setServices([
new bleno.PrimaryService({
uuid : SERVICE_ID, //hardcoded across panels
characteristics : chars
})
])
}
})
bleno.on('accept', clientAddress => {
console.log(`${clientAddress} accepted`)
})
bleno.on('disconnect', clientAddress => {
console.log(`${clientAddress} disconnected`)
})
}
})
bleno.on('advertisingStart', err => {
console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success'))
createChars()
if (!err) {
bleno.setServices([
new bleno.PrimaryService({
uuid : SERVICE_ID, //hardcoded across panels
characteristics : chars
})
])
/**
* 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
}
})
}
bleno.on('accept', clientAddress => {
console.log(`${clientAddress} accepted`)
})
bleno.on('disconnect', clientAddress => {
console.log(`${clientAddress} disconnected`)
})
ipc.serve(() => {
ipc.server.on('connect', socket => {
ipc.log('Client connected to socket')
})
ipc.server.on('disconnect', () => {
ipc.log('Client disconnected from socket')
})
ipc.server.on('data', (data, socket) => {
ipc.server.emit(socket, JSON.stringify({}))
})
})
ipc.server.start()
module.exports = new BLE()

View File

@ -1,48 +0,0 @@
'use strict'
const ipc = require('node-ipc')
const log = require('../log')('ble')
function capitalize (s) {
return s[0].toUpperCase() + s.slice(1)
}
/** Class representing the bluetooth interface */
class Blootstrap {
constructor () {
this._onData = () => {}
ipc.config.maxRetries = 0
ipc.connectTo('blootstrap_ble', () => {
ipc.of.blootstrap_ble.on('connect', () => {
log.info('connect', `Connected to the blootstrap_ble service`)
})
ipc.of.blootstrap_ble.on('data', data => {
const str = data.toString()
log.info('data', str)
this._onData(str)
})
ipc.of.blootstrap_ble.on('disconnect', () => {
log.info('disconnect', `Disconnected from the blootstrap_ble service`)
})
ipc.of.blootstrap_ble.on('error', (err) => {
if (err.code === 'EACCES') {
log.warn('ble', `Cannot access ipc`)
} else {
log.error('ble', { error : err })
}
})
})
}
/**
* 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
}
}
module.exports = new Blootstrap()

View File

@ -1,9 +1,9 @@
{
"apps" : [
{
"name" : "ble",
"script" : "./services/bluetooth/index.js",
"watch" : false,
"name" : "intval3",
"script" : "./index.js",
"watch" : true,
"env" : {
"BLENO_DEVICE_NAME" : "intval3",
"DEVICE_ID" : "intval3",
@ -12,12 +12,6 @@
"WIFI_ID" : "3fe7d9cf-7bd2-4ff0-97c5-ebe87288c2cc",
"PORT" : 6699
}
},
{
"name" : "intval3",
"script" : "./index.js",
"watch" : false,
"env" : {}
}
]
}