Advertisement
Guest User

Untitled

a guest
Jun 6th, 2019
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class EpsonPrinter {
  2.   name = null
  3.   ipAddress = null
  4.   port = null
  5.   deviceId = null
  6.   crypto = false
  7.   buffer = false
  8.   eposdev = null
  9.   printer = null
  10.   intervalID = null
  11.   restry = 0
  12.  
  13.   constructor (props) {
  14.     const {
  15.       name = 'Epson printer',
  16.       ipAddress,
  17.       port = 8008,
  18.       deviceId = 'local_printer',
  19.       crypto = false,
  20.       buffer = false
  21.     } = props
  22.     this.name = name
  23.     this.ipAddress = ipAddress
  24.     this.port = port
  25.     this.deviceId = deviceId
  26.     this.crypto = crypto
  27.     this.buffer = buffer
  28.  
  29.     this.eposdev = new window.epson.ePOSDevice()
  30.     this.eposdev.onreconnecting = this.onReconnecting
  31.     this.eposdev.onreconnect = this.onReconnect
  32.     this.eposdev.ondisconnect = this.onDisconnect
  33.  
  34.     this.connect()
  35.   }
  36.  
  37.   onReconnecting = () => {
  38.     this.consoleLog('reconnecting')
  39.   }
  40.   onReconnect = () => {
  41.     this.consoleLog('reconnect')
  42.   }
  43.  
  44.   onDisconnect = () => {
  45.     this.consoleLog('disconnect')
  46.  
  47.     if (this.intervalID === null ){
  48.       this.intervalID = setInterval(() => this.reconnect(), 5000)
  49.     }
  50.   }
  51.  
  52.   connect = () => {
  53.     this.consoleLog('connect')
  54.  
  55.     this.eposdev.ondisconnect = null
  56.     this.eposdev.disconnect()
  57.  
  58.     this.eposdev.connect(this.ipAddress, this.port, this.connectCallback)
  59.   }
  60.  
  61.   reconnect = () => {
  62.     this.consoleLog('(Re)connect')
  63.  
  64.     this.eposdev.connect(this.ipAddress, this.port, this.connectCallback)
  65.   }
  66.  
  67.   connectCallback = (data) => {
  68.     clearInterval(this.intervalID)
  69.     this.intervalID = null
  70.     this.eposdev.ondisconnect = this.onDisconnect
  71.  
  72.     if (data === 'OK' || data === 'SSL_CONNECT_OK') {
  73.       this.createDevice()
  74.     } else {
  75.       setTimeout(() => this.reconnect(), 5000)
  76.     }
  77.   }
  78.  
  79.   createDevice = () => {
  80.     console.log('create device, try: ' + this.restry)
  81.  
  82.     const options = {
  83.       crypto: this.crypto,
  84.       buffer: this.buffer
  85.     }
  86.  
  87.     this.eposdev.createDevice(this.deviceId, this.eposdev.DEVICE_TYPE_PRINTER, options, this.createDeviceCallback)
  88.   }
  89.  
  90.   createDeviceCallback = (deviceObj, code) => {
  91.     this.restry++
  92.  
  93.     if (code === 'OK') {
  94.       this.printer = deviceObj
  95.       this.printer.onreceive = this.onReceive
  96.     } else if (code === 'DEVICE_IN_USE') {
  97.       if (this.restry < 5) {
  98.         setTimeout(() => this.createDevice(), 3000)
  99.       }
  100.     }
  101.   }
  102.  
  103.   onReceive = (response) => {
  104.     this.consoleLog('on receive: ', response)
  105.     let message = `Print ${this.name} ${response.success ? 'success' : 'failute'}\n`
  106.     message += `Code: ${response.code}\n`
  107.     message += `Status: \n`
  108.  
  109.     if (response.status === this.printer.ASB_NO_RESPONSE) { message += ' No printer response\n' }
  110.     if (response.status === this.printer.ASB_PRINT_SUCCESS) { message += ' Print complete\n' }
  111.     if (response.status === this.printer.ASB_DRAWER_KICK) { message += ' Status of the drawer kick number 3 connector pin = "H"\n' }
  112.     if (response.status === this.printer.ASB_OFF_LINE)  {   message += ' Offline status\n' }
  113.     if (response.status === this.printer.ASB_COVER_OPEN)    {   message += ' Cover is open\n' }
  114.     if (response.status === this.printer.ASB_PAPER_FEED) {  message += ' Paper feed switch is feeding paper\n' }
  115.     if (response.status === this.printer.ASB_WAIT_ON_LINE) {    message += '  Waiting for online recovery\n' }
  116.     if (response.status === this.printer.ASB_PANEL_SWITCH) {    message += ' Panel switch is ON\n' }
  117.     if (response.status === this.printer.ASB_MECHANICAL_ERR) {  message += ' Mechanical error generated\n' }
  118.     if (response.status === this.printer.ASB_AUTOCUTTER_ERR) {  message += ' Auto cutter error generated\n' }
  119.     if (response.status === this.printer.ASB_UNRECOVER_ERR) {   message += ' Unrecoverable error generated\n' }
  120.     if (response.status === this.printer.ASB_AUTORECOVER_ERR) { message += ' Auto recovery error generated\n' }
  121.     if (response.status === this.printer.ASB_RECEIPT_NEAR_END) {    message += ' No paper in the roll paper near end detector\n' }
  122.     if (response.status === this.printer.ASB_RECEIPT_END) { message += ' No paper in the roll paper end detector\n' }
  123.     if (response.status === this.printer.ASB_SPOOLER_IS_STOPPED) {  message += ' Stop the spooler\n' }
  124.  
  125.     if (!response.success) {
  126.       alert(message)
  127.       // TODO: error message?
  128.     } else {
  129.       // TODO: success -> remove from queue
  130.     }
  131.   }
  132.  
  133.   printReceipt = () => {
  134.     this.consoleLog(`Print receipt, `, this)
  135.     try {
  136.       if (!this.printer) {
  137.         throw `No printer created for ${this.name}`
  138.       }
  139.  
  140.       this.printer.addPulse(this.printer.DRAWER_1, this.printer.PULSE_100)
  141.  
  142.       this.printer.addText(`Printed from: ${this.name}\n`)
  143.  
  144.       this.printer.send()
  145.     } catch (err) {
  146.       let message = `Print ${this.name} failure\n`
  147.       message += `Error: ${err}`
  148.  
  149.       alert(message)
  150.     }
  151.   }
  152.  
  153.   consoleLog = (...rest) => {
  154.     console.log(`${this.name}: `, ...rest)
  155.   }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement