From 2c89f32c8d15a697317ea0d76c123e82c54ed3b4 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 4 Sep 2017 18:34:44 +0800 Subject: [PATCH] temp --- app.js | 61 +------------------ .../gdata-characteristic.js | 0 ble/index.js | 28 +++++++++ main-service.js => ble/main-service.js | 0 .../time-characteristic.js | 0 config.json => config.default.json | 0 package.json | 12 ++++ route/install.js | 12 ++++ server.js | 61 +++++++++++++++++++ 9 files changed, 115 insertions(+), 59 deletions(-) rename gdata-characteristic.js => ble/gdata-characteristic.js (100%) create mode 100644 ble/index.js rename main-service.js => ble/main-service.js (100%) rename time-characteristic.js => ble/time-characteristic.js (100%) rename config.json => config.default.json (100%) create mode 100644 route/install.js create mode 100644 server.js diff --git a/app.js b/app.js index b2f6dc4..ebcddb4 100644 --- a/app.js +++ b/app.js @@ -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) -}) \ No newline at end of file +require('top-level-await') +require('./server') \ No newline at end of file diff --git a/gdata-characteristic.js b/ble/gdata-characteristic.js similarity index 100% rename from gdata-characteristic.js rename to ble/gdata-characteristic.js diff --git a/ble/index.js b/ble/index.js new file mode 100644 index 0000000..6f0605b --- /dev/null +++ b/ble/index.js @@ -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) + }) + } +}) diff --git a/main-service.js b/ble/main-service.js similarity index 100% rename from main-service.js rename to ble/main-service.js diff --git a/time-characteristic.js b/ble/time-characteristic.js similarity index 100% rename from time-characteristic.js rename to ble/time-characteristic.js diff --git a/config.json b/config.default.json similarity index 100% rename from config.json rename to config.default.json diff --git a/package.json b/package.json index 3ff193f..eb837e4 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/route/install.js b/route/install.js new file mode 100644 index 0000000..77d3993 --- /dev/null +++ b/route/install.js @@ -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 \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..8503a59 --- /dev/null +++ b/server.js @@ -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()) +} \ No newline at end of file