-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printer.js
140 lines (113 loc) · 4.52 KB
/
Printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {cashDrawerState, DGFE_MPD_State, operativeState, printerState, receiptDocumentState} from "./constants";
import {fiscalPrint} from "./js/fiscalprint.js";
import {XMLBuilder} from "fast-xml-parser";
class Printer {
url = ""
printer = null
constructor(ipAddress, protocol = "http", debug = false) {
this.url = `${protocol}://${ipAddress}/cgi-bin/fpmate.cgi`;
this.printer = new fiscalPrint();
}
sendToPrinter(xmlData, onError = null, onReceive = null, resetPrinter = true, debug = false) {
if (!this._isValidXml(xmlData)) {
throw new Error("Dati XML non validi.");
}
this.printer.onerror = function (result) {
if (debug) console.error("[PRINTER] Errore durante la stampa: " + result);
if (onError) onError(result);
}
this.printer.onreceive = function (result, tag_names_array, add_info, res_add) {
if (debug) console.log("[PRINTER] ", result, tag_names_array, add_info, res_add);
if (onReceive) onReceive(result, tag_names_array, add_info, res_add);
}
this.printer.send(this.url, xmlData, 10000);
}
resetPrinter(onError = null, onReceive = null, debug = false) {
const xmlData = this._getResetPrinterXml();
const onResetError = (result) => {
if (debug) console.log("[PRINTER][CALLBACK] Errore durante il reset della stampante: " + result);
const error = this._formatError(result);
if (onError) onError(error);
}
const onResetReceive = (result, tag_names_array, add_info, res_add) => {
if (debug) console.log("[PRINTER][CALLBACK] ", result, tag_names_array, add_info, res_add);
if (onReceive) onReceive(result, tag_names_array, add_info, res_add);
}
this.sendToPrinter(xmlData, onResetError, onResetReceive, false);
}
getPrinterStatus(onError = null, onReceive = null, debug = false) {
const xmlData = this._getPrinterStatusXml();
const onStatusError = (result) => {
if (debug) console.log("[PRINTER][CALLBACK] Errore durante la stampa: " + result);
const error = this._formatError(result);
if (onError) onError(error);
}
const onStatusReceive = (result, tag_names_array, add_info, res_add) => {
if (debug) console.log("[PRINTER][CALLBACK] ", result, tag_names_array, add_info, res_add);
const status = this._formatStatus(result, add_info);
if (onReceive) onReceive(status);
}
this.sendToPrinter(xmlData, onStatusError, onStatusReceive);
}
_isValidXml(xmlData) {
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlData, "application/xml");
return xmlDoc.getElementsByTagName("parsererror").length === 0;
} catch (e) {
return false;
}
}
_getPrinterStatusXml() {
const statusObj = {
printerCommand: {
queryPrinterStatus: ""
}
}
const options = {
suppressEmptyNode: true
}
const builder = new XMLBuilder(options)
return builder.build(statusObj);
}
_getResetPrinterXml() {
const resetObj = {
printerCommand: {
resetPrinter: ""
}
}
const options = {
suppressEmptyNode: true
}
const builder = new XMLBuilder(options)
return builder.build(resetObj);
}
_formatError(error) {
return {
code: error?.code,
success: error?.success,
message: error?.message
}
}
_formatStatus(status, add_info) {
const success = status?.success
if (success) {
return {
code: status?.code,
success: status?.success,
message: status?.message,
printerState: printerState[add_info.fpStatus?.substring(0, 1)],
DGFE_MPD_State: DGFE_MPD_State[add_info.fpStatus?.substring(1, 2)],
cashDrawerState: cashDrawerState[add_info.fpStatus?.substring(2, 3)],
receiptDocumentState: receiptDocumentState[add_info.fpStatus?.substring(3, 4)],
operativeState: operativeState[add_info.fpStatus?.substring(4, 5)]
}
}
return {
code: status?.code,
success: status?.success,
message: status?.message
}
}
}
export default Printer