2017-09-05 10:36:35 +00:00
|
|
|
/* eslint-disable no-throw-literal */
|
2017-09-04 10:34:44 +00:00
|
|
|
const KoaRouter = require('koa-router')
|
|
|
|
const router = new KoaRouter()
|
2017-09-05 10:36:35 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const Printer = require('../PrinterDev')
|
|
|
|
const KoaBody = require('koa-body')
|
|
|
|
const uuid = require('uuid')
|
|
|
|
|
|
|
|
router.use(async (c, n) => {
|
|
|
|
c.data = {
|
|
|
|
title: 'Printer System Setup'
|
|
|
|
}
|
2017-09-05 15:32:52 +00:00
|
|
|
let status = 1
|
2017-09-05 10:36:35 +00:00
|
|
|
try {
|
|
|
|
await n()
|
|
|
|
} catch (err) {
|
2017-09-05 15:32:52 +00:00
|
|
|
status = 0
|
2017-09-05 10:36:35 +00:00
|
|
|
if (typeof err === 'string') {
|
|
|
|
c.body = err
|
|
|
|
} else {
|
|
|
|
c.body = err.toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.async) {
|
|
|
|
c.body = {
|
2017-09-05 15:32:52 +00:00
|
|
|
status,
|
2017-09-05 10:36:35 +00:00
|
|
|
msg: c.body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-09-04 10:34:44 +00:00
|
|
|
|
|
|
|
router
|
|
|
|
.get('/', async (c, n) => {
|
|
|
|
c.redirect('/install')
|
|
|
|
})
|
2017-09-05 10:36:35 +00:00
|
|
|
.get('/install', async (c, n) => {
|
|
|
|
// get ttys
|
|
|
|
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) {
|
2017-09-05 15:32:52 +00:00
|
|
|
if (/^tty[A-z]/.test(it)) {
|
2017-09-05 10:36:35 +00:00
|
|
|
arr.push(path.resolve('/dev', it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve(arr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
c.data.ttys = ttys
|
|
|
|
await c.render('install/index', c.data)
|
|
|
|
})
|
|
|
|
.post('/install/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
|
|
|
|
|
|
|
|
Printer.serial = device
|
|
|
|
Printer.feed = feed
|
|
|
|
|
|
|
|
let status = await Printer.printTestPage()
|
2017-09-07 09:18:51 +00:00
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
if (!status) c.body = '測試失敗'
|
|
|
|
else c.body = '測試成功'
|
|
|
|
})
|
|
|
|
.post('/install/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 = {
|
|
|
|
ble: {
|
|
|
|
enable: false,
|
|
|
|
uuid: {
|
|
|
|
main: '',
|
|
|
|
service: '',
|
|
|
|
characteristic: ''
|
|
|
|
}
|
2017-09-05 15:32:52 +00:00
|
|
|
},
|
|
|
|
printer: {
|
|
|
|
serial: '',
|
|
|
|
feed: 8
|
2017-09-13 00:06:57 +00:00
|
|
|
},
|
|
|
|
api: {
|
|
|
|
secret: ''
|
2017-09-05 10:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json.ble.enable = arr.ble === 'yes'
|
|
|
|
json.ble.uuid.main = uuid.v4()
|
|
|
|
json.ble.uuid.service = uuid.v4()
|
|
|
|
json.ble.uuid.characteristic = uuid.v4()
|
2017-09-05 15:32:52 +00:00
|
|
|
|
|
|
|
json.printer.serial = arr.tty
|
|
|
|
json.printer.feed = feed
|
|
|
|
|
2017-09-13 00:06:57 +00:00
|
|
|
json.api.secret = arr.secret || ''
|
|
|
|
|
2017-09-05 15:32:52 +00:00
|
|
|
let wconfig = await new Promise((resolve, reject) => {
|
|
|
|
fs.writeFile(path.resolve(process.env.PROJECT_ROOT, 'config.json'), JSON.stringify(json, null, 2), {
|
2017-09-07 09:18:51 +00:00
|
|
|
mode: 0o664,
|
2017-09-05 15:32:52 +00:00
|
|
|
encoding: 'utf8',
|
|
|
|
flag: 'w'
|
|
|
|
}, err => {
|
|
|
|
if (err) return resolve(false)
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!wconfig) throw '設定檔寫入失敗'
|
|
|
|
c.body = '設定檔寫入成功'
|
2017-09-04 10:34:44 +00:00
|
|
|
})
|
|
|
|
|
2017-09-05 10:36:35 +00:00
|
|
|
module.exports = router
|