ble-server/server.js

164 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-09-04 10:34:44 +00:00
const Koa = require('koa')
const fs = require('fs')
const path = require('path')
2017-09-07 09:18:51 +00:00
const localEvent = require('./localEvent')
const WebSocket = require('ws')
const exec = require('child_process').exec
const Printer = require('./PrinterDev')
2017-09-04 10:34:44 +00:00
// koa middleware
const KoaLogger = require('koa-morgan')
const KoaEjs = require('koa-ejs')
const KoaMount = require('koa-mount')
const KoaStatic = require('koa-static')
const KoaRouter = require('koa-router')
2017-09-11 08:04:53 +00:00
const Kcors = require('kcors')
2017-09-04 10:34:44 +00:00
const app = new Koa()
let config = {}
let setupMode = false
try {
let chk = new Promise((resolve, reject) => {
fs.access(path.resolve(process.env.PROJECT_ROOT, 'config.json'), (err) => {
2017-09-04 10:34:44 +00:00
if (err) {
setupMode = true
resolve(0)
return
}
config = require('./config.json')
resolve(1)
})
})
2017-09-05 10:36:35 +00:00
await chk //eslint-disable-line
2017-09-04 10:34:44 +00:00
} catch (err) {
setupMode = true
}
const server = app.listen(config.port || 10230, () => {
console.log(`Server start on port ${server.address().port}`)
})
2017-09-07 09:18:51 +00:00
const ws = new WebSocket.Server({ server })
2017-09-11 08:04:53 +00:00
app.use(Kcors())
2017-09-04 10:34:44 +00:00
app.use(KoaLogger('short'))
app.use(KoaStatic(path.resolve(__dirname, 'public')))
KoaEjs(app, {
root: path.resolve(__dirname, 'views'),
layout: false,
viewExt: 'ejs',
cache: false,
debug: false
})
let router = null
if (setupMode) {
console.log(`start setup mode`)
router = require('./route/install')
} else {
console.log(`start normal mode`)
2017-09-07 09:18:51 +00:00
router = require('./route/dashboard')
Printer.serial = config.printer.serial
Printer.feed = config.printer.feed
await Printer.connect()
if (config.ble.enable) {
require('./ble')
}
2017-09-04 10:34:44 +00:00
}
if (router !== null) {
// set Route
app.use(router.routes())
app.use(router.allowedMethods())
router.put('/reboot_sys', async (c, n) => {
2017-09-07 09:18:51 +00:00
setTimeout(function () {
process.exit(1)
}, 2000)
c.body = '1'
})
2017-09-07 09:18:51 +00:00
}
// listen print event
localEvent.on('print', async str => {
let chk = await Printer.printerString(str)
localEvent.emit('printResult', chk)
2017-09-07 09:18:51 +00:00
})
let systemStatus = {
ble: {
enable: false,
mac: '',
service: '',
characteristic: ''
},
printer: {
connect: false,
serial: '',
feed: 0
},
pcount: 0,
2017-09-14 07:55:10 +00:00
lastprint: '',
secret: ''
}
systemStatus.ble.enable = config.ble.enable
2017-09-14 07:55:10 +00:00
systemStatus.secret = config.api.secret
if (config.ble.enable) {
systemStatus.ble.mac = await getBTAddr()
systemStatus.ble.service = config.ble.uuid.service
systemStatus.ble.characteristic = config.ble.uuid.characteristic
}
function getPrinterStatus() {
systemStatus.printer.connect = Printer.isOpen
systemStatus.printer.serial = Printer.serial
systemStatus.printer.feed = Printer.feed
systemStatus.pcount = Printer.count
systemStatus.lastprint = Printer.lastPrint
}
getPrinterStatus()
2017-09-07 09:18:51 +00:00
ws.on('connection', async (client, req) => {
getPrinterStatus()
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
2017-09-07 09:18:51 +00:00
Printer.on('printDone', () => {
getPrinterStatus()
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
})
2017-09-07 09:18:51 +00:00
client.on('message', msg => {
let m = {}
try {
m = JSON.parse(msg)
} catch (err) {
return
}
switch (m.type) {
2017-09-07 09:18:51 +00:00
case 'status':
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
2017-09-07 09:18:51 +00:00
break
}
})
})
async function getBTAddr() {
let address = await new Promise((resolve, reject) => {
exec('bt-adapter -i | grep -i address', (err, sout, serr) => {
if (err) return resolve('')
resolve(sout)
})
})
address = address.trim()
let arr = address.split(' ')
2017-09-07 09:18:51 +00:00
if (arr.length !== 2) return ''
return arr[1].trim()
2017-09-04 10:34:44 +00:00
}