This commit is contained in:
Jay 2017-09-04 18:34:44 +08:00
parent ca119388d7
commit 2c89f32c8d
9 changed files with 115 additions and 59 deletions

61
app.js
View File

@ -1,59 +1,2 @@
const bleno = require('bleno')
const config = require('./config.json')
const adapterName = 'BLE_Printer'
const serverUUID = config.uuid.main
const localEvent = require('./localEvent')
const fs = require('fs')
const MainService = require('./main-service')
const printer = require('./PrinterDev')
async function initSystem(){
try{
await printer.connect()
}catch(err){
throw err
}
}
initSystem()
bleno.on('stateChange', state => {
console.log(`bt device state ${state}`)
if (state == 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID], (error) => {
})
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function (error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
if (!error) {
bleno.setServices([new MainService()], error => {
console.log('set service', error)
})
}
})
localEvent.on('print', async (str) => {
let status = false
if (printer.isOpen) {
try {
status = await printer.printerString(str)
} catch (err) {
status = false
}
}
localEvent.emit('printResult', status)
})
process.on('SIGINT', () => {
printer.close()
process.exit(0)
})
require('top-level-await')
require('./server')

28
ble/index.js Normal file
View File

@ -0,0 +1,28 @@
const bleno = require('bleno')
const adapterName = 'BLE_Printer'
const serverUUID = config.uuid.main
const localEvent = require('./localEvent')
const fs = require('fs')
const MainService = require('./main-service')
bleno.on('stateChange', state => {
console.log(`bt device state ${state}`)
if (state == 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID], (error) => {
})
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function (error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
if (!error) {
bleno.setServices([new MainService()], error => {
console.log('set service', error)
})
}
})

View File

@ -7,6 +7,18 @@
"bleno": "^0.4.2",
"escpos": "^2.4.3",
"iconv-lite": "^0.4.18",
"kcors": "^2.2.1",
"koa": "^2.3.0",
"koa-body": "^2.3.0",
"koa-ejs": "^4.1.0",
"koa-morgan": "^1.0.1",
"koa-mount": "^3.0.0",
"koa-router": "^7.2.1",
"koa-static": "^4.0.1",
"top-level-await": "^1.1.0",
"uuid": "^3.1.0"
},
"devDependencies": {
"standard": "^10.0.3"
}
}

12
route/install.js Normal file
View File

@ -0,0 +1,12 @@
const KoaRouter = require('koa-router')
const router = new KoaRouter()
router
.get('/', async (c, n) => {
c.redirect('/install')
})
.all('/install', async (c, n) => {
c.body = 'install'
})
module.exports = router

61
server.js Normal file
View File

@ -0,0 +1,61 @@
const Koa = require('koa')
const fs = require('fs')
const path = require('path')
// 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')
const app = new Koa()
let config = {}
let setupMode = false
try {
let chk = new Promise((resolve, reject) => {
fs.access(path.resolve('config.json'), (err) => {
if (err) {
setupMode = true
resolve(0)
return
}
config = require('./config.json')
resolve(1)
})
})
await chk
} catch (err) {
setupMode = true
}
const server = app.listen(config.port || 10230, () => {
console.log(`Server start on port ${server.address().port}`)
})
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`)
}
if (router !== null) {
// set Route
app.use(router.routes())
app.use(router.allowedMethods())
}