diff --git a/index.js b/index.js index bda4f8c..4c817e7 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/blootstrap/Readme.md b/lib/ble/Readme.md similarity index 100% rename from lib/blootstrap/Readme.md rename to lib/ble/Readme.md diff --git a/services/bluetooth/index.js b/lib/ble/index.js similarity index 64% rename from services/bluetooth/index.js rename to lib/ble/index.js index 82d42a6..f05744e 100644 --- a/services/bluetooth/index.js +++ b/lib/ble/index.js @@ -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() \ No newline at end of file +module.exports = new BLE() \ No newline at end of file diff --git a/lib/blootstrap/index.js b/lib/blootstrap/index.js deleted file mode 100644 index b4003a4..0000000 --- a/lib/blootstrap/index.js +++ /dev/null @@ -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() \ No newline at end of file diff --git a/process.json b/process.json index a98129b..d074b23 100644 --- a/process.json +++ b/process.json @@ -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" : {} } ] } \ No newline at end of file