161 lines
3.5 KiB
JavaScript
161 lines
3.5 KiB
JavaScript
const EventEmitter = require('events') // eslint-disable-line
|
|
const escpos = require('escpos')
|
|
// const config = require('./config.json')
|
|
const fs = require('fs')
|
|
const iconv = require('iconv-lite')
|
|
|
|
class PrinterDevice extends EventEmitter {
|
|
constructor () {
|
|
super()
|
|
this._serial = ''
|
|
this._feed = 8
|
|
this._isOpen = false
|
|
this._device = null
|
|
this._printer = null
|
|
this._type = null // type = serial or console
|
|
this._count = 0
|
|
this._lastPrint = ''
|
|
}
|
|
|
|
encodeStr (str) {
|
|
if (!str || typeof str !== 'string') return false
|
|
return iconv.encode(str, 'big5')
|
|
}
|
|
|
|
async connect () {
|
|
let self = this
|
|
let chkSerial = await new Promise((resolve, reject) => {
|
|
fs.access(self._serial, err => {
|
|
if (err) return resolve(false)
|
|
return resolve(true)
|
|
})
|
|
})
|
|
if (chkSerial) {
|
|
this._device = new escpos.Serial(this._serial)
|
|
this._type = 'serial'
|
|
this._device.on('disconnect', () => {
|
|
self.emit('close')
|
|
})
|
|
} else {
|
|
this._device = new escpos.Console()
|
|
this._type = 'console'
|
|
}
|
|
this._printer = new escpos.Printer(this._device)
|
|
await this.openSerial()
|
|
}
|
|
|
|
async openSerial () {
|
|
let self = this
|
|
if (!this._device || this._type !== 'serial') return
|
|
this._isOpen = await new Promise((resolve, reject) => {
|
|
self._device.open(err => {
|
|
if (err) return resolve(false)
|
|
return resolve(true)
|
|
})
|
|
})
|
|
}
|
|
|
|
async printerString (str) {
|
|
if (!this._isOpen || !this._printer) return false
|
|
|
|
this._printer.font('a')
|
|
.align('ct')
|
|
.size(1, 1)
|
|
|
|
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(',')
|
|
if (sarr.length !== 2) break
|
|
if (!isFinite(sarr[0]) || !isFinite(sarr[1])) break
|
|
sarr = sarr.map(t => Math.floor(t))
|
|
this._printer.size(sarr[0], sarr[1])
|
|
break
|
|
}
|
|
} else {
|
|
this._printer.print(this.encodeStr(tmp))
|
|
}
|
|
}
|
|
|
|
this._printer.cut(true, this._feed || 4)
|
|
|
|
this._lastPrint = str
|
|
this.addCount()
|
|
this.emit('printDone')
|
|
return true
|
|
}
|
|
|
|
async printTestPage () {
|
|
await this.connect()
|
|
if (!this._isOpen) return false
|
|
this._printer.align('ct')
|
|
this._printer.size(1, 2)
|
|
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)
|
|
await this.close()
|
|
return true
|
|
}
|
|
|
|
async close () {
|
|
if (!this._device) return
|
|
if (this._type === 'serial') {
|
|
let self = this
|
|
await new Promise((resolve, reject) => {
|
|
self._printer.close(() => {
|
|
self._printer = null
|
|
self._isOpen = false
|
|
resolve(1)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
addCount () {
|
|
this._count++
|
|
}
|
|
|
|
get isOpen () {
|
|
return !!this._isOpen
|
|
}
|
|
|
|
set serial (str) {
|
|
this._serial = str
|
|
}
|
|
|
|
get serial () {
|
|
return this._serial
|
|
}
|
|
|
|
set feed (str) {
|
|
if (!isFinite(str)) return
|
|
this._feed = Math.floor(parseInt(str))
|
|
}
|
|
|
|
get feed () {
|
|
return this._feed
|
|
}
|
|
|
|
get count () {
|
|
return this._count
|
|
}
|
|
|
|
get lastPrint () {
|
|
return this._lastPrint || ''
|
|
}
|
|
}
|
|
|
|
module.exports = new PrinterDevice()
|