intval3/lib/blootstrap/index.js

39 lines
951 B
JavaScript

'use strict'
const ipc = require('node-ipc')
function capitalize (s) {
return s[0].toUpperCase() + s.slice(1)
}
/** Class representing the bluetooth interface */
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`)
})
})
}
/**
* 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()