2017-08-23 06:51:42 +00:00
|
|
|
const EventEmitter = require('events')
|
|
|
|
const escpos = require('escpos')
|
|
|
|
const config = require('./config.json')
|
|
|
|
const fs = require('fs')
|
2017-08-24 02:51:22 +00:00
|
|
|
const iconv = require('iconv-lite')
|
2017-08-23 06:51:42 +00:00
|
|
|
|
|
|
|
class PrinterDevice {
|
|
|
|
constructor() {
|
|
|
|
this._isOpen = false
|
|
|
|
this._device = null
|
|
|
|
this._printer = null
|
|
|
|
this._type = null // type = serial or console
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:51:22 +00:00
|
|
|
encodeStr(str){
|
|
|
|
if(!str || typeof str != 'string') return false
|
|
|
|
return iconv.encode(str, 'big5')
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:51:42 +00:00
|
|
|
async connect() {
|
|
|
|
let chkSerial = await new Promise((resolve, reject) => {
|
|
|
|
fs.access(config.printer.serial, err => {
|
|
|
|
if (err) return resolve(false)
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if (chkSerial) {
|
|
|
|
this._device = new escpos.Serial(config.printer.serial)
|
|
|
|
this._type = 'serial'
|
|
|
|
} else {
|
|
|
|
this._device = new escpos.Console()
|
|
|
|
this._type = 'console'
|
|
|
|
}
|
|
|
|
this._printer = new escpos.Printer(this._device)
|
|
|
|
this.openSerial()
|
|
|
|
}
|
|
|
|
|
|
|
|
async openSerial() {
|
|
|
|
if (!this._device || this._type != 'serial') return
|
|
|
|
this._isOpen = await new Promise((resolve, reject) => {
|
|
|
|
this._device.open(err => {
|
|
|
|
if (err) return resolve(false)
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-23 15:31:25 +00:00
|
|
|
async printerString(str) {
|
|
|
|
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(',')
|
|
|
|
if (sarr != 2) break
|
2017-08-24 02:51:22 +00:00
|
|
|
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 {
|
2017-08-24 02:51:22 +00:00
|
|
|
this._printer.print(this.encodeStr(tmp))
|
2017-08-23 15:31:25 +00:00
|
|
|
}
|
2017-08-23 13:20:22 +00:00
|
|
|
}
|
2017-08-23 15:31:25 +00:00
|
|
|
|
|
|
|
this._printer.cut(true, config.printer.cutFeed || 4)
|
|
|
|
|
|
|
|
return true
|
2017-08-23 13:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
if (!this._device) return
|
|
|
|
if (this._type == 'serial') {
|
|
|
|
let self = this
|
|
|
|
await new Promise((resolve, reject) => {
|
2017-08-23 15:31:25 +00:00
|
|
|
self._printer.close(() => {
|
2017-08-23 13:20:22 +00:00
|
|
|
resolve(1)
|
|
|
|
})
|
|
|
|
})
|
2017-08-23 06:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isOpen() {
|
|
|
|
return this._isOpen ? true : false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new PrinterDevice()
|