2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const ipc = require('node-ipc')
|
2017-09-26 03:25:54 +00:00
|
|
|
const log = require('../log')('ble')
|
2017-08-22 01:11:07 +00:00
|
|
|
|
|
|
|
function capitalize (s) {
|
|
|
|
return s[0].toUpperCase() + s.slice(1)
|
|
|
|
}
|
|
|
|
|
2017-09-17 21:54:07 +00:00
|
|
|
/** Class representing the bluetooth interface */
|
2017-08-22 01:11:07 +00:00
|
|
|
class Blootstrap {
|
|
|
|
constructor () {
|
|
|
|
this._onData = () => {}
|
2017-10-21 01:45:17 +00:00
|
|
|
try {
|
|
|
|
ipc.connectTo('blootstrap_ble', () => {
|
|
|
|
ipc.of.blootstrap_ble.on('connect', () => {
|
|
|
|
log.info('connect', `Connected to the blootstrap_ble service`)
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-10-21 01:45:17 +00:00
|
|
|
})
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
})
|
2017-10-21 01:45:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
log.error('connectTo', { error : e })
|
|
|
|
}
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-09-17 21:54:07 +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
|
|
|
|
*/
|
2017-08-22 01:11:07 +00:00
|
|
|
on (eventName, callback) {
|
|
|
|
this[`_on${capitalize(eventName)}`] = callback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new Blootstrap()
|