Add bleno to main process by adding a module that gets incorporated into the same process running express.
This commit is contained in:
parent
b9ceb506c7
commit
0272ca21f6
6
index.js
6
index.js
|
@ -4,7 +4,7 @@ const restify = require('restify')
|
||||||
const log = require('./lib/log')('main')
|
const log = require('./lib/log')('main')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
|
||||||
//const ble = require('./lib/blootstrap')
|
const ble = require('./lib/ble')
|
||||||
const intval = require('./lib/intval')
|
const intval = require('./lib/intval')
|
||||||
|
|
||||||
const PACKAGE = require('./package.json')
|
const PACKAGE = require('./package.json')
|
||||||
|
@ -38,6 +38,10 @@ function createServer () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bleBindings () {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function rDir (req, res, next) {
|
function rDir (req, res, next) {
|
||||||
let dir = true
|
let dir = true
|
||||||
let set = false
|
let set = false
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const ipc = require('node-ipc')
|
|
||||||
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 wifi = require('../../lib/wifi')
|
const wifi = require('../../lib/wifi')
|
||||||
|
|
||||||
const BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'my_project'
|
const BLENO_DEVICE_NAME = process.env.BLENO_DEVICE_NAME || 'intval3'
|
||||||
const DEVICE_ID = process.env.DEVICE_ID || 'my_project_id'
|
const DEVICE_ID = process.env.DEVICE_ID || 'intval3'
|
||||||
const SERVICE_ID = process.env.SERVICE_ID || 'blootstrap'
|
const SERVICE_ID = process.env.SERVICE_ID || 'blootstrap'
|
||||||
const CHAR_ID = process.env.CHAR_ID || 'blootstrapchar'
|
const CHAR_ID = process.env.CHAR_ID || 'blootstrapchar'
|
||||||
const WIFI_ID = process.env.WIFI_ID || 'blootstrapwifi'
|
const WIFI_ID = process.env.WIFI_ID || 'blootstrapwifi'
|
||||||
|
@ -19,10 +19,6 @@ let currentWifi = 'disconnected'
|
||||||
|
|
||||||
const chars = []
|
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 createChar(name, uuid, prop, write, read) {
|
||||||
function characteristic () {
|
function characteristic () {
|
||||||
|
@ -112,9 +108,17 @@ function spoofMac () {
|
||||||
return MACTMP
|
return MACTMP
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Starting bluetooth service')
|
|
||||||
|
|
||||||
bleno.on('stateChange', state => {
|
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}`
|
const BLE_ID = `${DEVICE_ID}_${MAC}`
|
||||||
console.log(`on -> stateChange: ${state}`)
|
console.log(`on -> stateChange: ${state}`)
|
||||||
if (state === 'poweredOn') {
|
if (state === 'poweredOn') {
|
||||||
|
@ -123,9 +127,9 @@ bleno.on('stateChange', state => {
|
||||||
} else {
|
} else {
|
||||||
bleno.stopAdvertising()
|
bleno.stopAdvertising()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
bleno.on('advertisingStart', err => {
|
bleno.on('advertisingStart', err => {
|
||||||
console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success'))
|
console.log('on -> advertisingStart: ' + (err ? 'error ' + err : 'success'))
|
||||||
createChars()
|
createChars()
|
||||||
if (!err) {
|
if (!err) {
|
||||||
|
@ -136,26 +140,25 @@ bleno.on('advertisingStart', err => {
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
bleno.on('accept', clientAddress => {
|
bleno.on('accept', clientAddress => {
|
||||||
console.log(`${clientAddress} accepted`)
|
console.log(`${clientAddress} accepted`)
|
||||||
})
|
})
|
||||||
|
|
||||||
bleno.on('disconnect', clientAddress => {
|
bleno.on('disconnect', clientAddress => {
|
||||||
console.log(`${clientAddress} disconnected`)
|
console.log(`${clientAddress} disconnected`)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ipc.serve(() => {
|
module.exports = new BLE()
|
||||||
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()
|
|
|
@ -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()
|
|
12
process.json
12
process.json
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"apps" : [
|
"apps" : [
|
||||||
{
|
{
|
||||||
"name" : "ble",
|
"name" : "intval3",
|
||||||
"script" : "./services/bluetooth/index.js",
|
"script" : "./index.js",
|
||||||
"watch" : false,
|
"watch" : true,
|
||||||
"env" : {
|
"env" : {
|
||||||
"BLENO_DEVICE_NAME" : "intval3",
|
"BLENO_DEVICE_NAME" : "intval3",
|
||||||
"DEVICE_ID" : "intval3",
|
"DEVICE_ID" : "intval3",
|
||||||
|
@ -12,12 +12,6 @@
|
||||||
"WIFI_ID" : "3fe7d9cf-7bd2-4ff0-97c5-ebe87288c2cc",
|
"WIFI_ID" : "3fe7d9cf-7bd2-4ff0-97c5-ebe87288c2cc",
|
||||||
"PORT" : 6699
|
"PORT" : 6699
|
||||||
}
|
}
|
||||||
},
|
|
||||||
{
|
|
||||||
"name" : "intval3",
|
|
||||||
"script" : "./index.js",
|
|
||||||
"watch" : false,
|
|
||||||
"env" : {}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
Reference in New Issue