From 97ee7dc6cd500a390ef8ec1a2b2b5805f4332bd9 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 7 Sep 2017 17:18:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=8B=80=E6=85=8B=E7=95=AB?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrinterDev.js | 22 ++++++++ package.json | 5 +- public/css/main.css | 4 ++ route/dashboard.js | 40 ++++++++++++++ route/install.js | 4 +- server.js | 80 ++++++++++++++++++++++++++- views/dashboard/index.ejs | 24 +++++++++ views/dashboard/statusview.ejs | 98 ++++++++++++++++++++++++++++++++++ 8 files changed, 272 insertions(+), 5 deletions(-) create mode 100644 route/dashboard.js create mode 100644 views/dashboard/index.ejs create mode 100644 views/dashboard/statusview.ejs diff --git a/PrinterDev.js b/PrinterDev.js index 528af3d..3983b8b 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -12,6 +12,8 @@ class PrinterDevice { this._device = null this._printer = null this._type = null // type = serial or console + this._count = 0 + this._lastPrint = '' } encodeStr (str) { @@ -114,6 +116,10 @@ class PrinterDevice { } } + addCount () { + this._count++ + } + get isOpen () { return !!this._isOpen } @@ -122,10 +128,26 @@ class PrinterDevice { this._serial = str } + get serial () { + return this._serial + } + set feed (str) { if (!isFinite(str)) return this._feed = Math.floor(parseInt(str)) } + + get feed () { + return this._feed + } + + get count () { + return this._count + } + + get lastPrint () { + return this._lastPrint || '' + } } module.exports = new PrinterDevice() diff --git a/package.json b/package.json index 136a163..1370cc8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "main": "app.js", "license": "MIT", "scripts": { - "check":"standard --fix --verbose" + "check": "standard --fix --verbose" }, "dependencies": { "bleno": "^0.4.2", @@ -19,7 +19,8 @@ "koa-router": "^7.2.1", "koa-static": "^4.0.1", "top-level-await": "^1.1.0", - "uuid": "^3.1.0" + "uuid": "^3.1.0", + "ws": "^3.1.0" }, "devDependencies": { "standard": "^10.0.3" diff --git a/public/css/main.css b/public/css/main.css index f4c6343..a21001d 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -2,4 +2,8 @@ html, body{ height: 100%; width: 100%; background-color: #eee; +} + +.t-right { + text-align: right; } \ No newline at end of file diff --git a/route/dashboard.js b/route/dashboard.js new file mode 100644 index 0000000..6b680f0 --- /dev/null +++ b/route/dashboard.js @@ -0,0 +1,40 @@ +/* eslint-disable no-throw-literal */ +/* eslint-disable */ +const KoaRouter = require('koa-router') +const router = new KoaRouter() +const fs = require('fs') +const path = require('path') +const Printer = require('../PrinterDev') +const KoaBody = require('koa-body') +const uuid = require('uuid') +/* eslint-enable */ + +router.use(async (c, n) => { + c.data = { + title: 'Printer System Dashboard' + } + let status = 1 + try { + await n() + } catch (err) { + status = 0 + if (typeof err === 'string') { + c.body = err + } else { + c.body = err.toString() + } + } + + if (c.async) { + c.body = { + status, + msg: c.body + } + } +}) + +router.get('/', async (c, n) => { + await c.render('dashboard/index', c.data) +}) + +module.exports = router diff --git a/route/install.js b/route/install.js index 1adeb64..47ce3a7 100644 --- a/route/install.js +++ b/route/install.js @@ -65,7 +65,7 @@ router Printer.feed = feed let status = await Printer.printTestPage() - + if (!status) c.body = '測試失敗' else c.body = '測試成功' }) @@ -103,7 +103,7 @@ router let wconfig = await new Promise((resolve, reject) => { fs.writeFile(path.resolve(process.env.PROJECT_ROOT, 'config.json'), JSON.stringify(json, null, 2), { - mode: 0664, + mode: 0o664, encoding: 'utf8', flag: 'w' }, err => { diff --git a/server.js b/server.js index fb8a118..231172f 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,10 @@ const Koa = require('koa') const fs = require('fs') const path = require('path') +const localEvent = require('./localEvent') +const WebSocket = require('ws') +const exec = require('child_process').exec +const Printer = require('./PrinterDev') // koa middleware const KoaLogger = require('koa-morgan') @@ -35,6 +39,8 @@ const server = app.listen(config.port || 10230, () => { console.log(`Server start on port ${server.address().port}`) }) +const ws = new WebSocket.Server({ server }) + app.use(KoaLogger('short')) app.use(KoaStatic(path.resolve(__dirname, 'public'))) KoaEjs(app, { @@ -52,6 +58,7 @@ if (setupMode) { router = require('./route/install') } else { console.log(`start normal mode`) + router = require('./route/dashboard') } if (router !== null) { @@ -60,9 +67,80 @@ if (router !== null) { app.use(router.allowedMethods()) router.put('/reboot_sys', async (c, n) => { - setTimeout(function(){ + setTimeout(function () { process.exit(1) }, 2000) c.body = '1' }) +} + +// listen print event +localEvent.on('print', str => { + +}) + +ws.on('connection', async (client, req) => { + console.log(`client conneected`) + + 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 + + client.send(JSON.stringify({ type: 'status', data: json })) + + client.on('message', msg => { + let m = {} + try { + m = JSON.parse(msg) + } catch (err) { + return + } + + switch(m.type){ + case 'status': + client.send(JSON.stringify({ type: 'status', data: json })) + break + } + + console.log(`get msg >> ${msg}`) + }) +}) + +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(':') + if (arr.length !== 2) return '' + return arr[1].trim() } \ No newline at end of file diff --git a/views/dashboard/index.ejs b/views/dashboard/index.ejs new file mode 100644 index 0000000..723ed5c --- /dev/null +++ b/views/dashboard/index.ejs @@ -0,0 +1,24 @@ +<%- include ../includes/header.ejs %> + +
+
+
+
+ +
+
+ <%- include statusview.ejs %> +
+
+
+
+ +<%- include ../includes/footer.ejs %> \ No newline at end of file diff --git a/views/dashboard/statusview.ejs b/views/dashboard/statusview.ejs new file mode 100644 index 0000000..0fff695 --- /dev/null +++ b/views/dashboard/statusview.ejs @@ -0,0 +1,98 @@ +
+
+
+
+

藍芽狀態

+
+
+
啟用狀態
+
On
+
+
+
Mac Address
+
00:11:22:33:44:55:66
+
+
+
ServiceUUID
+
37B2975E-1A4B-4975-9211-9D47142B8183
+
+
+
CharacteristicUUID
+
37B2975E-1A4B-4975-9211-9D47142B8183
+
+
+
+
+ +
+
+

印表機狀態

+
+
+
連線狀態
+
Disconnect
+
+
+
連接埠
+
/dev/ttyUSB0
+
+
+
切紙前空行
+
8
+
+
+
+
+ +
+
+

列印計數器

+
+
+
列印次數
+
0
+
+
+
+
+ +
+
+

最後列印內容

+
+
+
+
+
+ + \ No newline at end of file