ble-server/PrinterDev.js

161 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-09-05 10:36:35 +00:00
const EventEmitter = require('events') // eslint-disable-line
2017-08-23 06:51:42 +00:00
const escpos = require('escpos')
2017-09-05 10:36:35 +00:00
// const config = require('./config.json')
2017-08-23 06:51:42 +00:00
const fs = require('fs')
const iconv = require('iconv-lite')
2017-08-23 06:51:42 +00:00
class PrinterDevice extends EventEmitter {
2017-09-05 10:36:35 +00:00
constructor () {
super()
2017-09-05 10:36:35 +00:00
this._serial = ''
this._feed = 8
2017-08-23 06:51:42 +00:00
this._isOpen = false
this._device = null
this._printer = null
this._type = null // type = serial or console
2017-09-07 09:18:51 +00:00
this._count = 0
this._lastPrint = ''
2017-08-23 06:51:42 +00:00
}
2017-09-05 10:36:35 +00:00
encodeStr (str) {
if (!str || typeof str !== 'string') return false
return iconv.encode(str, 'big5')
}
2017-09-05 10:36:35 +00:00
async connect () {
let self = this
2017-08-23 06:51:42 +00:00
let chkSerial = await new Promise((resolve, reject) => {
fs.access(self._serial, err => {
2017-08-23 06:51:42 +00:00
if (err) return resolve(false)
return resolve(true)
})
})
if (chkSerial) {
2017-09-05 10:36:35 +00:00
this._device = new escpos.Serial(this._serial)
2017-08-23 06:51:42 +00:00
this._type = 'serial'
2017-12-15 14:58:04 +00:00
this._device.on('disconnect', () => {
self.emit('close')
})
2017-08-23 06:51:42 +00:00
} else {
this._device = new escpos.Console()
this._type = 'console'
}
this._printer = new escpos.Printer(this._device)
await this.openSerial()
2017-08-23 06:51:42 +00:00
}
2017-09-05 10:36:35 +00:00
async openSerial () {
let self = this
2017-09-05 10:36:35 +00:00
if (!this._device || this._type !== 'serial') return
2017-08-23 06:51:42 +00:00
this._isOpen = await new Promise((resolve, reject) => {
self._device.open(err => {
2017-08-23 06:51:42 +00:00
if (err) return resolve(false)
return resolve(true)
})
})
}
2017-09-05 10:36:35 +00:00
async printerString (str) {
2017-08-23 15:31:25 +00:00
if (!this._isOpen || !this._printer) return false
2017-08-23 06:51:42 +00:00
this._printer.font('a')
.align('ct')
.size(1, 1)
2017-08-23 15:31:25 +00:00
let cmds = str.split(/__/).filter(t => t)
for (let i in cmds) {
let tmp = cmds[i]
if (tmp[0] === '&') {
switch (tmp[1]) {
case 'a':
let align = tmp.substring(2)
if (!align) break
this._printer.align(align)
break
case 's':
let size = tmp.substring(2)
let sarr = size.trim().split(',')
2017-09-05 10:36:35 +00:00
if (sarr.length !== 2) break
if (!isFinite(sarr[0]) || !isFinite(sarr[1])) break
sarr = sarr.map(t => Math.floor(t))
2017-08-23 15:31:25 +00:00
this._printer.size(sarr[0], sarr[1])
break
}
} else {
this._printer.print(this.encodeStr(tmp))
2017-08-23 15:31:25 +00:00
}
}
2017-08-23 15:31:25 +00:00
2017-09-05 10:36:35 +00:00
this._printer.cut(true, this._feed || 4)
2017-08-23 15:31:25 +00:00
this._lastPrint = str
this.addCount()
this.emit('printDone')
2017-08-23 15:31:25 +00:00
return true
}
2017-09-05 10:36:35 +00:00
async printTestPage () {
await this.connect()
if (!this._isOpen) return false
this._printer.align('ct')
this._printer.size(1, 2)
2017-09-05 10:36:35 +00:00
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)
2017-09-05 10:36:35 +00:00
await this.close()
return true
}
async close () {
if (!this._device) return
2017-09-05 10:36:35 +00:00
if (this._type === 'serial') {
let self = this
await new Promise((resolve, reject) => {
2017-08-23 15:31:25 +00:00
self._printer.close(() => {
2017-09-05 10:36:35 +00:00
self._printer = null
self._isOpen = false
resolve(1)
})
})
2017-08-23 06:51:42 +00:00
}
}
2017-09-07 09:18:51 +00:00
addCount () {
this._count++
}
2017-09-05 10:36:35 +00:00
get isOpen () {
return !!this._isOpen
}
set serial (str) {
this._serial = str
}
2017-09-07 09:18:51 +00:00
get serial () {
return this._serial
}
2017-09-05 10:36:35 +00:00
set feed (str) {
if (!isFinite(str)) return
this._feed = Math.floor(parseInt(str))
2017-08-23 06:51:42 +00:00
}
2017-09-07 09:18:51 +00:00
get feed () {
return this._feed
}
get count () {
return this._count
}
get lastPrint () {
return this._lastPrint || ''
}
2017-08-23 06:51:42 +00:00
}
2017-09-05 10:36:35 +00:00
module.exports = new PrinterDevice()