2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const ipc = require('node-ipc')
|
|
|
|
|
|
|
|
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 = () => {}
|
|
|
|
ipc.connectTo('blootstrap_ble', () => {
|
|
|
|
ipc.of.blootstrap_ble.on('connect', () => {
|
|
|
|
ipc.log(`Connected to the blootstrap_ble service`)
|
|
|
|
|
|
|
|
})
|
|
|
|
ipc.of.blootstrap_ble.on('data', data => {
|
|
|
|
const str = data.toString()
|
|
|
|
ipc.log(str)
|
|
|
|
this._onData(str)
|
|
|
|
})
|
|
|
|
ipc.of.blootstrap_ble.on('disconnect', () => {
|
|
|
|
ipc.log(`Disconnected from the blootstrap_ble service`)
|
|
|
|
})
|
2017-09-25 02:28:06 +00:00
|
|
|
ipc.of.blootstrap_ble.on('error', (err) => {
|
|
|
|
if (err.code === 'EACCES') {
|
|
|
|
console.log(`Cannot access ipc`)
|
|
|
|
}
|
|
|
|
})
|
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()
|