2017-09-07 09:18:51 +00:00
|
|
|
/* 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')
|
2017-09-11 08:04:53 +00:00
|
|
|
const config = require('../config.json')
|
2017-09-07 09:18:51 +00:00
|
|
|
/* 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) => {
|
2017-09-11 08:04:53 +00:00
|
|
|
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
|
|
|
|
|
2017-09-07 09:18:51 +00:00
|
|
|
await c.render('dashboard/index', c.data)
|
|
|
|
})
|
|
|
|
|
2017-09-10 04:08:45 +00:00
|
|
|
router.post('/api/print', KoaBody(), async (c, n) => {
|
|
|
|
c.async = true
|
|
|
|
let arr = c.request.body
|
|
|
|
if (!arr.data) throw 'print data empty'
|
2017-09-14 07:55:10 +00:00
|
|
|
if (config.api.secret && !arr.key) throw 'access key enpty'
|
|
|
|
if (config.api.secret !== arr.key.trim()) throw 'access key error'
|
2017-09-10 04:08:45 +00:00
|
|
|
|
|
|
|
let status = await Printer.printerString(arr.data)
|
|
|
|
|
|
|
|
if (!status) throw 'printer fail'
|
|
|
|
|
|
|
|
c.body = 'print success'
|
|
|
|
})
|
|
|
|
|
2017-09-11 08:04:53 +00:00
|
|
|
.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
|
|
|
|
|
2017-09-14 07:55:10 +00:00
|
|
|
json.api.secret = arr.secret || ''
|
|
|
|
|
2017-09-11 08:04:53 +00:00
|
|
|
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 = '設定檔寫入成功'
|
|
|
|
})
|
|
|
|
|
2017-09-07 09:18:51 +00:00
|
|
|
module.exports = router
|