2017-09-04 10:34:44 +00:00
|
|
|
const Koa = require('koa')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2017-09-07 09:18:51 +00:00
|
|
|
const localEvent = require('./localEvent')
|
|
|
|
const WebSocket = require('ws')
|
|
|
|
const exec = require('child_process').exec
|
|
|
|
const Printer = require('./PrinterDev')
|
2017-09-04 10:34:44 +00:00
|
|
|
|
|
|
|
// koa middleware
|
|
|
|
const KoaLogger = require('koa-morgan')
|
|
|
|
const KoaEjs = require('koa-ejs')
|
|
|
|
const KoaMount = require('koa-mount')
|
|
|
|
const KoaStatic = require('koa-static')
|
|
|
|
const KoaRouter = require('koa-router')
|
2017-09-11 08:04:53 +00:00
|
|
|
const Kcors = require('kcors')
|
2017-09-04 10:34:44 +00:00
|
|
|
|
|
|
|
const app = new Koa()
|
|
|
|
|
|
|
|
let config = {}
|
|
|
|
let setupMode = false
|
|
|
|
|
|
|
|
try {
|
|
|
|
let chk = new Promise((resolve, reject) => {
|
2017-09-07 15:40:51 +00:00
|
|
|
fs.access(path.resolve(process.env.PROJECT_ROOT, 'config.json'), (err) => {
|
2017-09-04 10:34:44 +00:00
|
|
|
if (err) {
|
|
|
|
setupMode = true
|
|
|
|
resolve(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config = require('./config.json')
|
|
|
|
resolve(1)
|
|
|
|
})
|
|
|
|
})
|
2017-09-05 10:36:35 +00:00
|
|
|
await chk //eslint-disable-line
|
2017-09-04 10:34:44 +00:00
|
|
|
} catch (err) {
|
|
|
|
setupMode = true
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = app.listen(config.port || 10230, () => {
|
|
|
|
console.log(`Server start on port ${server.address().port}`)
|
|
|
|
})
|
|
|
|
|
2017-09-07 09:18:51 +00:00
|
|
|
const ws = new WebSocket.Server({ server })
|
|
|
|
|
2017-09-11 08:04:53 +00:00
|
|
|
app.use(Kcors())
|
2017-09-04 10:34:44 +00:00
|
|
|
app.use(KoaLogger('short'))
|
|
|
|
app.use(KoaStatic(path.resolve(__dirname, 'public')))
|
|
|
|
KoaEjs(app, {
|
|
|
|
root: path.resolve(__dirname, 'views'),
|
|
|
|
layout: false,
|
|
|
|
viewExt: 'ejs',
|
|
|
|
cache: false,
|
|
|
|
debug: false
|
|
|
|
})
|
|
|
|
|
|
|
|
let router = null
|
|
|
|
|
|
|
|
if (setupMode) {
|
|
|
|
console.log(`start setup mode`)
|
|
|
|
router = require('./route/install')
|
|
|
|
} else {
|
|
|
|
console.log(`start normal mode`)
|
2017-09-07 09:18:51 +00:00
|
|
|
router = require('./route/dashboard')
|
2017-09-07 15:40:51 +00:00
|
|
|
|
|
|
|
Printer.serial = config.printer.serial
|
|
|
|
Printer.feed = config.printer.feed
|
|
|
|
await Printer.connect()
|
|
|
|
|
2017-12-15 14:58:04 +00:00
|
|
|
Printer.on('close', ()=>{
|
|
|
|
console.log('device disconnect')
|
|
|
|
})
|
|
|
|
|
2017-09-04 10:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (router !== null) {
|
|
|
|
// set Route
|
|
|
|
app.use(router.routes())
|
|
|
|
app.use(router.allowedMethods())
|
2017-09-05 15:32:52 +00:00
|
|
|
|
|
|
|
router.put('/reboot_sys', async (c, n) => {
|
2017-09-07 09:18:51 +00:00
|
|
|
setTimeout(function () {
|
2017-09-05 15:32:52 +00:00
|
|
|
process.exit(1)
|
|
|
|
}, 2000)
|
|
|
|
c.body = '1'
|
|
|
|
})
|
2017-09-07 09:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listen print event
|
2017-09-10 04:08:45 +00:00
|
|
|
localEvent.on('print', async str => {
|
|
|
|
let chk = await Printer.printerString(str)
|
|
|
|
localEvent.emit('printResult', chk)
|
2017-09-07 09:18:51 +00:00
|
|
|
})
|
|
|
|
|
2017-09-10 04:08:45 +00:00
|
|
|
let systemStatus = {
|
|
|
|
printer: {
|
|
|
|
connect: false,
|
|
|
|
serial: '',
|
|
|
|
feed: 0
|
|
|
|
},
|
|
|
|
pcount: 0,
|
2017-09-14 07:55:10 +00:00
|
|
|
lastprint: '',
|
|
|
|
secret: ''
|
2017-09-10 04:08:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:55:10 +00:00
|
|
|
systemStatus.secret = config.api.secret
|
2017-12-15 14:58:04 +00:00
|
|
|
|
2017-09-10 04:08:45 +00:00
|
|
|
function getPrinterStatus() {
|
|
|
|
systemStatus.printer.connect = Printer.isOpen
|
|
|
|
systemStatus.printer.serial = Printer.serial
|
|
|
|
systemStatus.printer.feed = Printer.feed
|
|
|
|
systemStatus.pcount = Printer.count
|
|
|
|
systemStatus.lastprint = Printer.lastPrint
|
|
|
|
}
|
|
|
|
getPrinterStatus()
|
|
|
|
|
2017-09-07 09:18:51 +00:00
|
|
|
ws.on('connection', async (client, req) => {
|
|
|
|
|
2017-09-10 04:08:45 +00:00
|
|
|
getPrinterStatus()
|
|
|
|
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
2017-09-07 09:18:51 +00:00
|
|
|
|
2017-09-10 04:08:45 +00:00
|
|
|
Printer.on('printDone', () => {
|
|
|
|
getPrinterStatus()
|
|
|
|
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
|
|
|
})
|
2017-09-07 09:18:51 +00:00
|
|
|
|
|
|
|
client.on('message', msg => {
|
|
|
|
let m = {}
|
|
|
|
try {
|
|
|
|
m = JSON.parse(msg)
|
|
|
|
} catch (err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:40:51 +00:00
|
|
|
switch (m.type) {
|
2017-09-07 09:18:51 +00:00
|
|
|
case 'status':
|
2017-09-10 04:08:45 +00:00
|
|
|
client.send(JSON.stringify({ type: 'status', data: systemStatus }))
|
2017-09-07 09:18:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|