加入WebAPI
1.PrinterDev add EventEmitter 2.add Printer WebAPI
This commit is contained in:
parent
a7f7653efa
commit
debf1f03d8
@ -4,8 +4,9 @@ const escpos = require('escpos')
|
||||
const fs = require('fs')
|
||||
const iconv = require('iconv-lite')
|
||||
|
||||
class PrinterDevice {
|
||||
class PrinterDevice extends EventEmitter {
|
||||
constructor () {
|
||||
super()
|
||||
this._serial = ''
|
||||
this._feed = 8
|
||||
this._isOpen = false
|
||||
@ -87,7 +88,7 @@ class PrinterDevice {
|
||||
|
||||
this._lastPrint = str
|
||||
this.addCount()
|
||||
|
||||
this.emit('printDone')
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -37,4 +37,16 @@ router.get('/', async (c, n) => {
|
||||
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
|
||||
|
72
server.js
72
server.js
@ -83,41 +83,51 @@ if (router !== null) {
|
||||
}
|
||||
|
||||
// listen print event
|
||||
localEvent.on('print', str => {
|
||||
Printer.printerString(str)
|
||||
localEvent.on('print', async 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) => {
|
||||
let json = {
|
||||
ble: {
|
||||
enable: false,
|
||||
mac: '',
|
||||
service: '',
|
||||
characteristic: ''
|
||||
},
|
||||
printer: {
|
||||
connect: false,
|
||||
serial: '',
|
||||
feed: 0
|
||||
},
|
||||
pcount: 0,
|
||||
lastprint: ''
|
||||
}
|
||||
|
||||
// 第一次連上線推送系統狀態
|
||||
json.ble.enable = config.ble.enable
|
||||
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
|
||||
getPrinterStatus()
|
||||
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
||||
|
||||
client.send(JSON.stringify({ type: 'status', data: json }))
|
||||
Printer.on('printDone', () => {
|
||||
getPrinterStatus()
|
||||
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
||||
})
|
||||
|
||||
client.on('message', msg => {
|
||||
let m = {}
|
||||
@ -129,7 +139,7 @@ ws.on('connection', async (client, req) => {
|
||||
|
||||
switch (m.type) {
|
||||
case 'status':
|
||||
client.send(JSON.stringify({ type: 'status', data: json }))
|
||||
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
||||
break
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user