escpos-mc 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +113 -60
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -12,8 +12,6 @@ const Promiseify = require('./promisify');
12
12
  const statuses = require('./statuses');
13
13
  const {PrinterStatus,OfflineCauseStatus,ErrorCauseStatus,RollPaperSensorStatus, ExternalSensorStatus} = statuses;
14
14
 
15
- var dontCheckStatus = false;
16
- var dontCheckTimeout;
17
15
 
18
16
  /**
19
17
  * [function ESC/POS Printer]
@@ -32,6 +30,8 @@ function Printer(adapter, options) {
32
30
  this.encoding = options && options.encoding || 'GB18030';
33
31
  this.width = options && options.width || 48;
34
32
  this._model = null;
33
+ this._dontCheckStatus = false;
34
+ this._dontCheckTimeout = null;
35
35
  };
36
36
 
37
37
  Printer.create = function (device) {
@@ -110,14 +110,21 @@ Printer.prototype.marginRight = function (size) {
110
110
  * @return {[Printer]} printer [the escpos printer instance]
111
111
  */
112
112
  Printer.prototype.print = function (content) {
113
- clearTimeout(dontCheckTimeout);
114
- dontCheckStatus = true;
113
+ // dok štampa, nemoj status polling (osim direct poziva)
114
+ if (this._dontCheckTimeout) clearTimeout(this._dontCheckTimeout);
115
+ this._dontCheckStatus = true;
116
+
117
+ // DEBUG (ostavi ako želiš)
115
118
  console.log("PRINT_DEBUG", "PRINT_TO_BUFFER", content);
119
+
116
120
  this.buffer.write(content);
117
- dontCheckTimeout = setTimeout(() => {
118
- dontCheckStatus = false;
119
- clearTimeout(dontCheckTimeout);
120
- },50000);
121
+
122
+ // fallback: ako se iz nekog razloga close/flush ne desi, otpusti lock posle max vremena
123
+ this._dontCheckTimeout = setTimeout(() => {
124
+ this._dontCheckStatus = false;
125
+ if (this._dontCheckTimeout) clearTimeout(this._dontCheckTimeout);
126
+ this._dontCheckTimeout = null;
127
+ }, 50000);
121
128
 
122
129
  return this;
123
130
  };
@@ -815,8 +822,19 @@ Printer.prototype.beep = function (n, t) {
815
822
  callback && callback(null);
816
823
  return this;
817
824
  }
818
- this.adapter.write(buf, callback);
825
+ this.adapter.write(buf, (err) => {
826
+ // ako smo poslali buffer, možemo ranije da otpustimo status lock
827
+ // (ali ne odmah — ostavi malo prostora da printer obradi)
828
+ if (this._dontCheckTimeout) clearTimeout(this._dontCheckTimeout);
829
+ this._dontCheckTimeout = setTimeout(() => {
830
+ this._dontCheckStatus = false;
831
+ this._dontCheckTimeout = null;
832
+ }, 1500);
833
+
834
+ callback && callback(err || null);
835
+ });
819
836
  return this;
837
+
820
838
  }
821
839
 
822
840
  // normalize anything else to Buffer
@@ -826,7 +844,17 @@ Printer.prototype.beep = function (n, t) {
826
844
  callback && callback(null);
827
845
  return this;
828
846
  }
829
- this.adapter.write(b, callback);
847
+ this.adapter.write(b, (err) => {
848
+ // ako smo poslali buffer, možemo ranije da otpustimo status lock
849
+ // (ali ne odmah — ostavi malo prostora da printer obradi)
850
+ if (this._dontCheckTimeout) clearTimeout(this._dontCheckTimeout);
851
+ this._dontCheckTimeout = setTimeout(() => {
852
+ this._dontCheckStatus = false;
853
+ this._dontCheckTimeout = null;
854
+ }, 1500);
855
+
856
+ callback && callback(err || null);
857
+ });
830
858
  } catch (e) {
831
859
  callback && callback(e);
832
860
  }
@@ -916,7 +944,7 @@ Printer.prototype.raw = function raw(data) {
916
944
  * @param {Function} callback
917
945
  * @return {Printer}
918
946
  */
919
- Printer.prototype.getStatus = function(statusClassName, callback) {
947
+ Printer.prototype.getStatus = function (statusClassName, callback) {
920
948
  // upiši komande u buffer
921
949
  statuses[statusClassName].commands().forEach((c) => {
922
950
  this.buffer.write(c);
@@ -924,7 +952,7 @@ Printer.prototype.raw = function raw(data) {
924
952
 
925
953
  // pošalji komande
926
954
  this.flush(() => {
927
- const timeoutMs = 300;
955
+ const timeoutMs = 400;
928
956
  let done = false;
929
957
 
930
958
  const t = setTimeout(() => {
@@ -933,7 +961,6 @@ Printer.prototype.raw = function raw(data) {
933
961
  callback(null);
934
962
  }, timeoutMs);
935
963
 
936
- // compat: read može biti cb(data) ili cb(err,data)
937
964
  this.adapter.read((a, b) => {
938
965
  let err, data;
939
966
  if (b === undefined) { err = null; data = a; }
@@ -943,7 +970,7 @@ Printer.prototype.raw = function raw(data) {
943
970
  done = true;
944
971
  clearTimeout(t);
945
972
 
946
- if (err || !data || data.length < 1) return callback(null);
973
+ if (err || !data || !Buffer.isBuffer(data) || data.length < 1) return callback(null);
947
974
 
948
975
  const byte = data.readUInt8(0);
949
976
  const status = new statuses[statusClassName](byte);
@@ -959,10 +986,10 @@ Printer.prototype.raw = function raw(data) {
959
986
  * @param {Function} callback
960
987
  * @return {Printer}
961
988
  */
962
- Printer.prototype.getStatuses = function(callback) {
989
+ Printer.prototype.getStatuses = function (callback) {
963
990
  let buffer = [];
964
991
 
965
- const timeoutMs = 400;
992
+ const timeoutMs = 600;
966
993
  let done = false;
967
994
  const t = setTimeout(() => {
968
995
  if (done) return;
@@ -970,14 +997,14 @@ Printer.prototype.getStatuses = function(callback) {
970
997
  callback(null);
971
998
  }, timeoutMs);
972
999
 
973
- this.adapter.read((a, b) => {
1000
+ const onRead = (a, b) => {
974
1001
  let err, data;
975
1002
  if (b === undefined) { err = null; data = a; }
976
1003
  else { err = a; data = b; }
977
1004
 
978
1005
  if (done) return;
979
1006
 
980
- if (err || !data) {
1007
+ if (err || !data || !Buffer.isBuffer(data)) {
981
1008
  done = true;
982
1009
  clearTimeout(t);
983
1010
  return callback(null);
@@ -987,26 +1014,33 @@ Printer.prototype.getStatuses = function(callback) {
987
1014
  buffer.push(data.readInt8(i));
988
1015
  }
989
1016
 
990
- if (buffer.length < 4) return;
1017
+ // očekujemo bar 4 bajta (PrinterStatus/RollPaper/OfflineCause/ErrorCause)
1018
+ if (buffer.length < 4) {
1019
+ // nastavi da čitaš dok ne skupiš dovoljno
1020
+ return this.adapter.read(onRead);
1021
+ }
991
1022
 
992
1023
  done = true;
993
1024
  clearTimeout(t);
994
1025
 
995
- let out = [];
996
- for (let i = 0; i < buffer.length; i++) { // < buffer.length
997
- let byte = buffer[i];
1026
+ const out = [];
1027
+ for (let i = 0; i < buffer.length; i++) {
1028
+ const byte = buffer[i];
998
1029
  switch (i) {
999
1030
  case 0: out.push(new PrinterStatus(byte)); break;
1000
1031
  case 1: out.push(new RollPaperSensorStatus(byte)); break;
1001
1032
  case 2: out.push(new OfflineCauseStatus(byte)); break;
1002
1033
  case 3: out.push(new ErrorCauseStatus(byte)); break;
1003
1034
  case 4: out.push(new ExternalSensorStatus(byte)); break;
1035
+ default: break;
1004
1036
  }
1005
1037
  }
1006
1038
 
1007
1039
  buffer = [];
1008
1040
  callback(out);
1009
- });
1041
+ };
1042
+
1043
+ this.adapter.read(onRead);
1010
1044
 
1011
1045
  PrinterStatus.commands().forEach((c) => this.adapter.write(c));
1012
1046
  RollPaperSensorStatus.commands().forEach((c) => this.adapter.write(c));
@@ -1022,65 +1056,84 @@ Printer.prototype.getStatuses = function(callback) {
1022
1056
  * @return {dontCheckStatus}
1023
1057
  */
1024
1058
  Printer.prototype.getCheckStatus = function () {
1025
- return dontCheckStatus;
1026
- }
1059
+ return !!this._dontCheckStatus;
1060
+ };
1027
1061
 
1028
1062
  /**
1029
1063
  * get custom statuses from the printer
1030
1064
  * @param {Function} callback
1031
1065
  * @return {Printer}
1032
1066
  */
1033
- Printer.prototype.getCustomStatuses = function(callback) {
1034
- let buffer = [];
1067
+ Printer.prototype.getCustomStatuses = function (callback) {
1068
+ const self = this;
1035
1069
 
1036
- const timeoutMs = 400;
1037
- let done = false;
1038
- const t = setTimeout(() => {
1039
- if (done) return;
1040
- done = true;
1041
- callback(null);
1042
- }, timeoutMs);
1070
+ let buffer = [];
1071
+ let done = false;
1043
1072
 
1044
- this.adapter.read((a, b) => {
1045
- let err, data;
1046
- if (b === undefined) { err = null; data = a; }
1047
- else { err = a; data = b; }
1073
+ // U praksi VM ume da kasni — ali ti u roll-paper-status već radiš Promise.race timeout,
1074
+ // pa ovde možemo malo više (da bi imalo šanse da dođe)
1075
+ const timeoutMs = 3000;
1048
1076
 
1049
- if (done) return;
1077
+ const t = setTimeout(() => {
1078
+ if (done) return;
1079
+ done = true;
1080
+ callback(null);
1081
+ }, timeoutMs);
1050
1082
 
1051
- if (err || !data) {
1052
- done = true;
1053
- clearTimeout(t);
1054
- return callback(null);
1055
- }
1083
+ function finish(out) {
1084
+ if (done) return;
1085
+ done = true;
1086
+ clearTimeout(t);
1087
+ callback(out);
1088
+ }
1056
1089
 
1057
- for (let i = 0; i < data.byteLength; i++) {
1058
- buffer.push(data.readInt8(i));
1059
- }
1090
+ function onRead(a, b) {
1091
+ let err, data;
1092
+ if (b === undefined) { err = null; data = a; }
1093
+ else { err = a; data = b; }
1060
1094
 
1061
- if (buffer.length < 2) return;
1095
+ if (done) return;
1062
1096
 
1063
- done = true;
1064
- clearTimeout(t);
1097
+ if (err || !data || !Buffer.isBuffer(data) || data.length < 1) {
1098
+ return finish(null);
1099
+ }
1065
1100
 
1066
- let out = [];
1067
- for (let i = 0; i < buffer.length; i++) { // ✅ < buffer.length
1068
- let byte = buffer[i];
1101
+ for (let i = 0; i < data.byteLength; i++) {
1102
+ buffer.push(data.readInt8(i));
1103
+ }
1104
+
1105
+ // očekujemo 2 bajta: RollPaperSensor + ExternalSensor (tvoj custom)
1106
+ if (buffer.length >= 2) {
1107
+ const out = [];
1108
+ for (let i = 0; i < buffer.length; i++) {
1109
+ const byte = buffer[i];
1069
1110
  switch (i) {
1070
1111
  case 0: out.push(new RollPaperSensorStatus(byte)); break;
1071
1112
  case 1: out.push(new ExternalSensorStatus(byte)); break;
1113
+ default: break;
1072
1114
  }
1073
1115
  }
1116
+ return finish(out);
1117
+ }
1074
1118
 
1075
- buffer = [];
1076
- callback(out);
1077
- });
1119
+ // nastavi da čitaš dok ne dobiješ oba bajta
1120
+ self.adapter.read(onRead);
1121
+ }
1078
1122
 
1079
- RollPaperSensorStatus.commands().forEach((c) => this.adapter.write(c));
1080
- ExternalSensorStatus.commands().forEach((c) => this.adapter.write(c));
1123
+ // start listening first, then send commands
1124
+ self.adapter.read(onRead);
1081
1125
 
1082
- return this;
1083
- };
1126
+ // send roll paper command first
1127
+ RollPaperSensorStatus.commands().forEach((c) => self.adapter.write(c));
1128
+
1129
+ // mali delay pa external
1130
+ setTimeout(() => {
1131
+ if (done) return;
1132
+ ExternalSensorStatus.commands().forEach((c) => self.adapter.write(c));
1133
+ }, 80);
1134
+
1135
+ return this;
1136
+ };
1084
1137
 
1085
1138
  /**
1086
1139
  * Printer Supports
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "escpos-mc",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "My temp ESC/POS Printer driver for nodejs",
5
5
  "main": "index.js",
6
6
  "scripts": {