加入初次安裝畫面

1.設定藍芽啟用與否
2.設定印表機連接埠與切紙間格
3.設定結束自動重新啟動服務
This commit is contained in:
Jay 2017-09-05 23:32:52 +08:00
parent 1941ab0c52
commit 3c06bf5d8b
7 changed files with 131 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules
yarn.lock
package-lock.json
config.json
config.default.json

View File

@ -20,8 +20,9 @@ class PrinterDevice {
}
async connect () {
let self = this
let chkSerial = await new Promise((resolve, reject) => {
fs.access(this._serial, err => {
fs.access(self._serial, err => {
if (err) return resolve(false)
return resolve(true)
})
@ -34,13 +35,14 @@ class PrinterDevice {
this._type = 'console'
}
this._printer = new escpos.Printer(this._device)
this.openSerial()
await this.openSerial()
}
async openSerial () {
let self = this
if (!this._device || this._type !== 'serial') return
this._isOpen = await new Promise((resolve, reject) => {
this._device.open(err => {
self._device.open(err => {
if (err) return resolve(false)
return resolve(true)
})
@ -88,11 +90,12 @@ class PrinterDevice {
await this.connect()
if (!this._isOpen) return false
this._printer.align('ct')
this._printer.size(1, 1)
this._printer.size(1, 2)
this._printer.text('Print Test Page')
this._printer.align('lt')
this._printer.size(1, 1)
this._printer.text('Test Page Content')
this._printer.cut(true, this._feed)
await this.close()
return true
}

3
app.js
View File

@ -1,2 +1,5 @@
// set global env
process.env.PROJECT_ROOT = __dirname
require('top-level-await')
require('./server')

View File

@ -1,15 +0,0 @@
{
"uuid":{
"main": "930da73d-1b80-4d3e-a8b6-d9b4e7e771bd",
"service":{
"id":"dd535b71-8f05-4e30-beb0-6fa7ea3dfc3e",
"func":{
"data":"00000001-8f05-4e30-beb0-6fa7ea3dfc3e"
}
}
},
"printer": {
"serial": "/dev/ttyUSB0",
"cutFeed": 8
}
}

View File

@ -11,9 +11,11 @@ router.use(async (c, n) => {
c.data = {
title: 'Printer System Setup'
}
let status = 1
try {
await n()
} catch (err) {
status = 0
if (typeof err === 'string') {
c.body = err
} else {
@ -23,7 +25,7 @@ router.use(async (c, n) => {
if (c.async) {
c.body = {
status: 0,
status,
msg: c.body
}
}
@ -40,7 +42,7 @@ router
if (err) return resolve([])
let arr = []
for (let it of list) {
if (it.startsWith('tty')) {
if (/^tty[A-z]/.test(it)) {
arr.push(path.resolve('/dev', it))
}
}
@ -63,7 +65,7 @@ router
Printer.feed = feed
let status = await Printer.printTestPage()
if (!status) c.body = '測試失敗'
else c.body = '測試成功'
})
@ -83,11 +85,11 @@ router
main: '',
service: '',
characteristic: ''
},
printer: {
serial: '',
feed: 8
}
},
printer: {
serial: '',
feed: 8
}
}
@ -95,6 +97,23 @@ router
json.ble.uuid.main = uuid.v4()
json.ble.uuid.service = uuid.v4()
json.ble.uuid.characteristic = uuid.v4()
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: 0664,
encoding: 'utf8',
flag: 'w'
}, err => {
if (err) return resolve(false)
return resolve(true)
})
})
if (!wconfig) throw '設定檔寫入失敗'
c.body = '設定檔寫入成功'
})
module.exports = router

View File

@ -58,4 +58,11 @@ if (router !== null) {
// set Route
app.use(router.routes())
app.use(router.allowedMethods())
router.put('/reboot_sys', async (c, n) => {
setTimeout(function(){
process.exit(1)
}, 2000)
c.body = '1'
})
}

View File

@ -35,4 +35,90 @@
</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: '/install/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: '/install/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>
<%- include ../includes/footer.ejs %>