加入WebAPI

1.PrinterDev add EventEmitter
2.add Printer WebAPI
This commit is contained in:
Jay 2017-09-10 12:08:45 +08:00
parent a7f7653efa
commit debf1f03d8
3 changed files with 56 additions and 33 deletions

View File

@ -4,8 +4,9 @@ const escpos = require('escpos')
const fs = require('fs') const fs = require('fs')
const iconv = require('iconv-lite') const iconv = require('iconv-lite')
class PrinterDevice { class PrinterDevice extends EventEmitter {
constructor () { constructor () {
super()
this._serial = '' this._serial = ''
this._feed = 8 this._feed = 8
this._isOpen = false this._isOpen = false
@ -87,7 +88,7 @@ class PrinterDevice {
this._lastPrint = str this._lastPrint = str
this.addCount() this.addCount()
this.emit('printDone')
return true return true
} }

View File

@ -37,4 +37,16 @@ router.get('/', async (c, n) => {
await c.render('dashboard/index', c.data) await c.render('dashboard/index', c.data)
}) })
router.post('/api/print', KoaBody(), async (c, n) => {
c.async = true
let arr = c.request.body
if (!arr.data) throw 'print data empty'
let status = await Printer.printerString(arr.data)
if (!status) throw 'printer fail'
c.body = 'print success'
})
module.exports = router module.exports = router

View File

@ -83,41 +83,51 @@ if (router !== null) {
} }
// listen print event // listen print event
localEvent.on('print', str => { localEvent.on('print', async str => {
Printer.printerString(str) let chk = await Printer.printerString(str)
localEvent.emit('printResult', chk)
}) })
let systemStatus = {
ble: {
enable: false,
mac: '',
service: '',
characteristic: ''
},
printer: {
connect: false,
serial: '',
feed: 0
},
pcount: 0,
lastprint: ''
}
systemStatus.ble.enable = config.ble.enable
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()
ws.on('connection', async (client, req) => { ws.on('connection', async (client, req) => {
let json = {
ble: {
enable: false,
mac: '',
service: '',
characteristic: ''
},
printer: {
connect: false,
serial: '',
feed: 0
},
pcount: 0,
lastprint: ''
}
// 第一次連上線推送系統狀態 getPrinterStatus()
json.ble.enable = config.ble.enable client.send(JSON.stringify({ type: 'status', data: systemStatus }))
if (config.ble.enable) {
json.ble.mac = await getBTAddr()
json.ble.service = config.ble.uuid.service
json.ble.characteristic = config.ble.uuid.characteristic
}
json.printer.connect = Printer.isOpen
json.printer.serial = Printer.serial
json.printer.feed = Printer.feed
json.pcount = Printer.count
json.lastprint = Printer.lastPrint
client.send(JSON.stringify({ type: 'status', data: json })) Printer.on('printDone', () => {
getPrinterStatus()
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
})
client.on('message', msg => { client.on('message', msg => {
let m = {} let m = {}
@ -129,7 +139,7 @@ ws.on('connection', async (client, req) => {
switch (m.type) { switch (m.type) {
case 'status': case 'status':
client.send(JSON.stringify({ type: 'status', data: json })) client.send(JSON.stringify({ type: 'status', data: systemStatus }))
break break
} }
}) })