add setting page

This commit is contained in:
Jay 2017-09-11 16:04:53 +08:00
parent debf1f03d8
commit 948541c1aa
4 changed files with 211 additions and 1 deletions

View File

@ -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

View File

@ -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, {

View File

@ -9,13 +9,17 @@
<div class="item">
<div class="header">系統功能</div>
<div class="menu">
<a href="#" class="item">Func1</a>
<a href="/setting" class="item <%= route == 'setting' ? 'active' : '' %>">修改設定</a>
</div>
</div>
</div>
</div>
<div class="thirteen wide column">
<% if(route == 'index'){ %>
<%- include statusview.ejs %>
<% } else { %>
<%- include setting.ejs %>
<% } %>
</div>
</div>
</div>

121
views/dashboard/setting.ejs Normal file
View File

@ -0,0 +1,121 @@
<div class="ui container" style="padding-top: 20px;">
<h1 class="ui header">系統安裝設定</h1>
<form id="setup" class="ui form">
<h3 class="ui dividing header">印表機設定</h3>
<div class="field">
<label for="tty">印表機連接埠</label>
<select name="tty" id="tty">
<option value="">選擇裝置</option>
<% for(let i of ttys) { %>
<option value="<%= i %>" <%= ttys[i] == defConfig.printer.serial ? 'selected' : '' %>><%= i %></option>
<% } %>
</select>
</div>
<div class="field">
<label for="feed">切紙前空行</label>
<input type="text" name="feed" value="<%= defConfig.printer.feed %>" id="feed">
</div>
<div class="field">
<button class="ui button mini" type="button" id="test-printer">測試印表機</button>
</div>
<h3 class="ui dividing header">藍芽列印設定</h3>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" class="hidden" id="en-ble" <%= defConfig.ble.enable ? 'checked' : '' %>>
<label for="en-ble">啟用藍芽</label>
</div>
</div>
<div style="margin-top: 40px; text-align: right;">
<button class="ui button blue" type="submit">送出設定</button>
</div>
</form>
</div>
<div class="ui dimmer" id="loader">
<div class="ui loader text">Rebooting...</div>
</div>
<script>
function getValue(obj){
if(!obj || !('value' in obj)) return ''
return obj.value
}
$('#test-printer').click(function(){
let json = {
device: '',
feed: 0
}
let dev = getValue(document.querySelector('select#tty'))
let feed = getValue(document.querySelector('input#feed'))
if(!dev) return alert('請選擇印表機連接埠')
if (!isFinite(feed) || feed < 0) return alert('切紙前空行請輸入整數')
json.device = dev
json.feed = Math.floor(parseInt(feed))
$.ajax({
url: '/api/printer_test',
type: 'post',
data: json,
success: function(e){
alert(e.msg || '資料傳送失敗')
}
})
})
$('form#setup').submit(function(e) {
e.preventDefault()
let json = {
tty: '',
feed: 0,
ble: ''
}
let dev = getValue(document.querySelector('select#tty'))
let feed = getValue(document.querySelector('input#feed'))
let elBle = document.querySelector('input#en-ble')
let ble = ''
if(elBle != null && 'checked' in elBle) ble = elBle.checked ? 'yes' : 'no'
if(!dev) return alert('請選擇印表機連接埠')
if (!isFinite(feed) || feed < 0) return alert('切紙前空行請輸入整數')
json.tty = dev
json.feed = Math.floor(parseInt(feed))
json.ble = ble
$.ajax({
url: '/api/write_config',
type: 'post',
data: json,
success: function(e){
if(e.status != 1) {
alert(e.msg || '寫入失敗')
return
}
$.ajax({
url: '/reboot_sys',
type: 'put',
success: function(){
setTimeout(function(){
location.replace('/')
}, 30000)
$('#loader').addClass('active')
},
error: function(){
alert('系統重新啟動失敗')
}
})
}
})
return false
})
</script>