ble-server/app.js

59 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-08-09 10:15:07 +00:00
const bleno = require('bleno')
2017-08-10 12:18:43 +00:00
const config = require('./config.json')
2017-08-09 10:15:07 +00:00
const adapterName = 'BLE_Printer'
2017-08-10 12:18:43 +00:00
const serverUUID = config.uuid.main
2017-08-23 06:51:42 +00:00
const localEvent = require('./localEvent')
const fs = require('fs')
2017-08-09 10:15:07 +00:00
const MainService = require('./main-service')
2017-08-23 06:51:42 +00:00
const printer = require('./PrinterDev')
2017-08-23 15:31:25 +00:00
async function initSystem(){
try{
await printer.connect()
}catch(err){
throw err
}
}
initSystem()
2017-08-09 10:15:07 +00:00
bleno.on('stateChange', state => {
console.log(`bt device state ${state}`)
if (state == 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID], (error) => {
})
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function (error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
if (!error) {
bleno.setServices([new MainService()], error => {
console.log('set service', error)
})
}
2017-08-10 12:18:43 +00:00
})
2017-08-23 06:51:42 +00:00
localEvent.on('print', async (str) => {
let status = false
if (printer.isOpen) {
try {
status = await printer.printerString(str)
} catch (err) {
status = false
}
}
localEvent.emit('printResult', status)
2017-08-23 13:23:55 +00:00
})
process.on('SIGINT', () => {
printer.close()
2017-08-23 15:31:25 +00:00
process.exit(0)
2017-08-23 06:51:42 +00:00
})