修改服務啟動動作與QRCode產生按鈕

1.啟動時連接印表機
2.判斷是否要開啟藍芽
3.畫面加入QRCode產生按鈕,方便APP設定
This commit is contained in:
Jay 2017-09-07 23:40:51 +08:00
parent 593c017a08
commit a7f7653efa
10 changed files with 88 additions and 21 deletions

View File

@ -85,6 +85,9 @@ class PrinterDevice {
this._printer.cut(true, this._feed || 4) this._printer.cut(true, this._feed || 4)
this._lastPrint = str
this.addCount()
return true return true
} }

7
app.js
View File

@ -2,4 +2,9 @@
process.env.PROJECT_ROOT = __dirname process.env.PROJECT_ROOT = __dirname
require('top-level-await') require('top-level-await')
require('./server')
try {
require('./server')
} catch (err) {
throw err
}

View File

@ -1,6 +1,6 @@
const bleno = require('bleno') const bleno = require('bleno')
const config = require('./config.json') const config = require('../config.json')
const localEvent = require('./localEvent') const localEvent = require('../localEvent')
var tmp = [] var tmp = []
var idx = 0 var idx = 0
@ -14,7 +14,7 @@ const notifyDesciptor = new bleno.Descriptor({
class DataCharacteristic extends bleno.Characteristic { class DataCharacteristic extends bleno.Characteristic {
constructor () { constructor () {
super({ super({
uuid: config.uuid.service.func.data, uuid: config.ble.uuid.characteristic,
properties: ['write', 'writeWithoutResponse', 'notify'], properties: ['write', 'writeWithoutResponse', 'notify'],
descriptors: [ descriptors: [
notifyDesciptor notifyDesciptor

View File

@ -1,8 +1,7 @@
const config = require('../config.json') const config = require('../config.json')
const bleno = require('bleno') const bleno = require('bleno')
const adapterName = 'BLE_Printer' const adapterName = 'BLE_Printer'
const serverUUID = config.uuid.main const serverUUID = config.ble.uuid.main
const localEvent = require('./localEvent') //eslint-disable-line
const MainService = require('./main-service') const MainService = require('./main-service')
bleno.on('stateChange', state => { bleno.on('stateChange', state => {

View File

@ -1,6 +1,6 @@
const bleno = require('bleno') const bleno = require('bleno')
const config = require('./config.json') const config = require('../config.json')
const localEvent = require('./localEvent') const localEvent = require('../localEvent')
const GdataCharacteristic = require('./gdata-characteristic') const GdataCharacteristic = require('./gdata-characteristic')
const dataCharacteristic = new GdataCharacteristic() const dataCharacteristic = new GdataCharacteristic()
@ -8,7 +8,7 @@ const dataCharacteristic = new GdataCharacteristic()
class MainService extends bleno.PrimaryService { class MainService extends bleno.PrimaryService {
constructor () { constructor () {
super({ super({
uuid: config.uuid.service.id, uuid: config.ble.uuid.service,
characteristics: [ characteristics: [
dataCharacteristic dataCharacteristic
] ]

View File

@ -6,4 +6,20 @@ html, body{
.t-right { .t-right {
text-align: right; text-align: right;
}
#qr img {
margin: 0 auto;
}
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1; /*For IE 6&7 only*/
} }

1
public/js/qrcode.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ let setupMode = false
try { try {
let chk = new Promise((resolve, reject) => { let chk = new Promise((resolve, reject) => {
fs.access(path.resolve('config.json'), (err) => { fs.access(path.resolve(process.env.PROJECT_ROOT, 'config.json'), (err) => {
if (err) { if (err) {
setupMode = true setupMode = true
resolve(0) resolve(0)
@ -59,6 +59,14 @@ if (setupMode) {
} else { } else {
console.log(`start normal mode`) console.log(`start normal mode`)
router = require('./route/dashboard') router = require('./route/dashboard')
Printer.serial = config.printer.serial
Printer.feed = config.printer.feed
await Printer.connect()
if (config.ble.enable) {
require('./ble')
}
} }
if (router !== null) { if (router !== null) {
@ -76,8 +84,7 @@ if (router !== null) {
// listen print event // listen print event
localEvent.on('print', str => { localEvent.on('print', str => {
Printer.addCount() Printer.printerString(str)
Printer.lastPrint = str
}) })
ws.on('connection', async (client, req) => { ws.on('connection', async (client, req) => {
@ -120,7 +127,7 @@ ws.on('connection', async (client, req) => {
return return
} }
switch(m.type){ switch (m.type) {
case 'status': case 'status':
client.send(JSON.stringify({ type: 'status', data: json })) client.send(JSON.stringify({ type: 'status', data: json }))
break break
@ -137,7 +144,7 @@ async function getBTAddr() {
}) })
address = address.trim() address = address.trim()
let arr = address.split(':') let arr = address.split(' ')
if (arr.length !== 2) return '' if (arr.length !== 2) return ''
return arr[1].trim() return arr[1].trim()
} }

View File

@ -1,5 +1,10 @@
<div class="ui container"> <div class="ui container">
<div class="ui grid"> <div class="ui grid">
<div class="column sixteen wide t-right">
<div class="ui segment">
<button class="ui button mini" type="button" id="gen-qr">GenerateQR</button>
</div>
</div>
<div class="column eight wide"> <div class="column eight wide">
<div class="segment ui"> <div class="segment ui">
<h4 class="ui header dividing">藍芽狀態</h4> <h4 class="ui header dividing">藍芽狀態</h4>
@ -30,16 +35,16 @@
<div class="ui divided list"> <div class="ui divided list">
<div class="item"> <div class="item">
<div class="ui horizontal label basic">連線狀態</div> <div class="ui horizontal label basic">連線狀態</div>
<div class="description t-right" id="prt-con">Disconnect</div> <div class="description t-right" id="prt-con">Disconnected</div>
</div> </div>
<div class="item"> <div class="item">
<div class="ui horizontal label basic">連接埠</div> <div class="ui horizontal label basic">連接埠</div>
<div class="description t-right" id="prt-port">/dev/ttyUSB0</div> <div class="description t-right" id="prt-port">/dev/ttyUSB0</div>
</div> </div>
<div class="item"> <div class="item">
<div class="ui horizontal label basic">切紙前空行</div> <div class="ui horizontal label basic">切紙前空行</div>
<div class="description t-right" id="prt-feed">8</div> <div class="description t-right" id="prt-feed">8</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
@ -65,7 +70,19 @@
</div> </div>
</div> </div>
<div class="ui page dimmer" id="qr-dimmer">
<div class="content">
<div class="center" id="qr"></div>
</div>
</div>
<script> <script>
var setting = {
mac: '',
service: '',
characteristic: ''
}
var ws = new WebSocket('ws://' + location.host) var ws = new WebSocket('ws://' + location.host)
ws.onmessage = msg => { ws.onmessage = msg => {
// console.log(msg) // console.log(msg)
@ -76,16 +93,20 @@
return return
} }
switch(data.type) { switch (data.type) {
case 'status': case 'status':
console.log(data) console.log(data)
let d = data.data let d = data.data
$('#en-ble').text(d.ble.enable ? 'On' : 'Off') $('#en-ble').text(d.ble.enable ? 'On' : 'Off')
$('#ble-mac').text(d.ble.mac) $('#ble-mac').text(d.ble.mac)
$('#ble-service').text(d.ble.service) $('#ble-service').text(d.ble.service)
$('#ble-character').text(d.ble.characteristic) $('#ble-character').text(d.ble.characteristic)
$('#prt-con').text(d.printer.connect ? 'Connect' : 'Disconnect') setting.mac = d.ble.mac
setting.service = d.ble.service
setting.characteristic = d.ble.characteristic
$('#prt-con').text(d.printer.connect ? 'Connected' : 'Disconnected')
$('#prt-port').text(d.printer.serial) $('#prt-port').text(d.printer.serial)
$('#prt-feed').text(d.printer.feed) $('#prt-feed').text(d.printer.feed)
@ -95,4 +116,18 @@
break break
} }
} }
$('#gen-qr').click(function () {
let qrdiv = document.querySelector('div#qr')
qrdiv.innerHTML = ''
let qr = new QRCode(qrdiv, {
text: JSON.stringify(setting, null, 2),
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.L
})
// qr.makeCode(JSON.stringify(setting, null, 2))
$('#qr-dimmer').dimmer('show')
})
</script> </script>

View File

@ -9,5 +9,6 @@
<link rel="stylesheet" href="/css/main.css"> <link rel="stylesheet" href="/css/main.css">
<script src="/js/jquery-3.2.1.min.js"></script> <script src="/js/jquery-3.2.1.min.js"></script>
<script src="/js/semantic.min.js"></script> <script src="/js/semantic.min.js"></script>
<script src="/js/qrcode.min.js"></script>
</head> </head>
<body> <body>