diff --git a/.gitignore b/.gitignore index 22fbdbe..f41585c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules yarn.lock package-lock.json +config.json +config.default.json \ No newline at end of file diff --git a/PrinterDev.js b/PrinterDev.js index 22982ea..528af3d 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -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 } diff --git a/app.js b/app.js index 196a63d..2ebcdbe 100644 --- a/app.js +++ b/app.js @@ -1,2 +1,5 @@ +// set global env +process.env.PROJECT_ROOT = __dirname + require('top-level-await') require('./server') diff --git a/config.default.json b/config.default.json deleted file mode 100644 index 58bd2f0..0000000 --- a/config.default.json +++ /dev/null @@ -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 - } -} diff --git a/route/install.js b/route/install.js index e0e058a..1adeb64 100644 --- a/route/install.js +++ b/route/install.js @@ -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 diff --git a/server.js b/server.js index 755a261..fb8a118 100644 --- a/server.js +++ b/server.js @@ -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' + }) } \ No newline at end of file diff --git a/views/install/index.ejs b/views/install/index.ejs index 2951cb8..bdda728 100644 --- a/views/install/index.ejs +++ b/views/install/index.ejs @@ -35,4 +35,90 @@ +