From debf1f03d8c8336fdb255267fca8dfa71d75ed13 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 10 Sep 2017 12:08:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5WebAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.PrinterDev add EventEmitter 2.add Printer WebAPI --- PrinterDev.js | 5 ++-- route/dashboard.js | 12 ++++++++ server.js | 72 ++++++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index 4956efc..cf7b5bb 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -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 } diff --git a/route/dashboard.js b/route/dashboard.js index 6b680f0..3b70c6b 100644 --- a/route/dashboard.js +++ b/route/dashboard.js @@ -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 diff --git a/server.js b/server.js index c8ca3f0..6caabc8 100644 --- a/server.js +++ b/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 } })