From 948541c1aaa407d231222b65306c80d740060a2a Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 11 Sep 2017 16:04:53 +0800 Subject: [PATCH] add setting page --- route/dashboard.js | 83 +++++++++++++++++++++++++ server.js | 2 + views/dashboard/index.ejs | 6 +- views/dashboard/setting.ejs | 121 ++++++++++++++++++++++++++++++++++++ 4 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 views/dashboard/setting.ejs diff --git a/route/dashboard.js b/route/dashboard.js index 3b70c6b..e11165c 100644 --- a/route/dashboard.js +++ b/route/dashboard.js @@ -7,6 +7,7 @@ const path = require('path') const Printer = require('../PrinterDev') const KoaBody = require('koa-body') const uuid = require('uuid') +const config = require('../config.json') /* eslint-enable */ router.use(async (c, n) => { @@ -34,6 +35,29 @@ router.use(async (c, n) => { }) router.get('/', async (c, n) => { + c.data.route = 'index' + await c.render('dashboard/index', c.data) +}) + +router.get('/setting', async (c, n) => { + c.data.route = 'setting' + + let ttys = await new Promise((resolve, reject) => { + fs.readdir(path.resolve('/dev'), (err, list) => { + if (err) return resolve([]) + let arr = [] + for (let it of list) { + if (/^tty[A-z]/.test(it)) { + arr.push(path.resolve('/dev', it)) + } + } + resolve(arr) + }) + }) + c.data.ttys = ttys + + c.data.defConfig = config + await c.render('dashboard/index', c.data) }) @@ -49,4 +73,63 @@ router.post('/api/print', KoaBody(), async (c, n) => { c.body = 'print success' }) + .post('/api/printer_test', KoaBody(), async (c, n) => { + c.async = true + let arr = c.request.body + if (!arr.device) throw '請輸入印表機連接埠' + let device = arr.device + let feed = arr.feed || 8 + if (!isFinite(feed) || feed < 0) throw '切紙前換行必須是整數' + feed = typeof feed !== 'number' ? Math.floor(parseInt(feed)) : feed + + let oriDev = Printer.serial + let oriFeed = Printer.feed + + if (Printer.isOpen) { + await Printer.close() + } + + Printer.serial = device + Printer.feed = feed + + let status = await Printer.printTestPage() + + if (!status) c.body = '測試失敗' + else c.body = '測試成功' + + Printer.serial = oriDev + Printer.feed = oriFeed + await Printer.connect() + }) + .post('/api/write_config', KoaBody(), async (c, n) => { + c.async = true + let arr = c.request.body + if (!arr.tty) throw '請輸入印表機連接埠' + if (!arr.ble || (arr.ble !== 'yes' && arr.ble !== 'no')) throw '請輸入藍牙啟用設定' + let feed = arr.feed || 8 + if (!isFinite(feed) || feed < 0) throw '切紙前換行必須是整數' + feed = typeof feed !== 'number' ? Math.floor(parseInt(feed)) : feed + + let json = Object.assign({}, config) + + json.ble.enable = arr.ble === 'yes' + + json.printer.serial = arr.tty + json.printer.feed = feed + + let wconfig = await new Promise((resolve, reject) => { + fs.writeFile(path.resolve(process.env.PROJECT_ROOT, 'config.json'), JSON.stringify(json, null, 2), { + mode: 0o664, + encoding: 'utf8', + flag: 'w' + }, err => { + if (err) return resolve(false) + return resolve(true) + }) + }) + + if (!wconfig) throw '設定檔寫入失敗' + c.body = '設定檔寫入成功' + }) + module.exports = router diff --git a/server.js b/server.js index 6caabc8..22a5782 100644 --- a/server.js +++ b/server.js @@ -12,6 +12,7 @@ const KoaEjs = require('koa-ejs') const KoaMount = require('koa-mount') const KoaStatic = require('koa-static') const KoaRouter = require('koa-router') +const Kcors = require('kcors') const app = new Koa() @@ -41,6 +42,7 @@ const server = app.listen(config.port || 10230, () => { const ws = new WebSocket.Server({ server }) +app.use(Kcors()) app.use(KoaLogger('short')) app.use(KoaStatic(path.resolve(__dirname, 'public'))) KoaEjs(app, { diff --git a/views/dashboard/index.ejs b/views/dashboard/index.ejs index 723ed5c..4fdb537 100644 --- a/views/dashboard/index.ejs +++ b/views/dashboard/index.ejs @@ -9,13 +9,17 @@
系統功能
+ <% if(route == 'index'){ %> <%- include statusview.ejs %> + <% } else { %> + <%- include setting.ejs %> + <% } %>
diff --git a/views/dashboard/setting.ejs b/views/dashboard/setting.ejs new file mode 100644 index 0000000..eb7fcce --- /dev/null +++ b/views/dashboard/setting.ejs @@ -0,0 +1,121 @@ + +
+

系統安裝設定

+
+

印表機設定

+
+ + +
+
+ + +
+
+ +
+ +

藍芽列印設定

+
+
+ > + +
+
+ +
+ +
+
+
+ +
+
Rebooting...
+
+ + \ No newline at end of file