node-red-contrib-tcp-escpos 0.0.1 → 0.1.0

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.
package/README.md CHANGED
@@ -1 +1,9 @@
1
1
  # node-red-contrib-tcp-escpos [![npm](https://img.shields.io/npm/v/node-red-contrib-tcp-escpos.svg)](https://www.npmjs.com/package/node-red-contrib-tcp-escpos) [<img height="20px" src="https://nodered.org/about/resources/media/node-red-hexagon.png">](https://flows.nodered.org/node/node-red-contrib-tcp-escpos)
2
+
3
+ ![Epson printer example](./docs/exampleHardware.jpg)
4
+
5
+ ### Example usage ([JSON export](./examples/ledSemaphore.json))
6
+
7
+ ![Example nodes composition](./docs/exampleNode.png)
8
+
9
+ ![Example node configuration](./docs/exampleConfiguration.png)
package/nodes/node.html CHANGED
@@ -9,7 +9,7 @@
9
9
  defaults: {
10
10
  name: { value: '' },
11
11
  host: { value: '' },
12
- type: { value: 'text' },
12
+ payloadType: { value: 'text' },
13
13
  payload: { value: "I'm Mr. Printer.\nLook at me!" },
14
14
  },
15
15
  label: function () {
@@ -27,8 +27,10 @@
27
27
  <input type="text" id="node-input-host" placeholder="192.168.1.101:9100" />
28
28
  </div>
29
29
  <div class="form-row">
30
- <label for="node-input-type"><i class="fa fa-play-circle"></i> Type</label>
31
- <select id="node-input-type">
30
+ <label for="node-input-payloadType"
31
+ ><i class="fa fa-play-circle"></i> Type</label
32
+ >
33
+ <select id="node-input-payloadType">
32
34
  <option value="text">Text</option>
33
35
  <option value="image">Image</option>
34
36
  <option value="buffer">Buffer</option>
package/nodes/node.js CHANGED
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
+ const iconv_lite_1 = __importDefault(require("iconv-lite"));
14
15
  const node_net_1 = __importDefault(require("node:net"));
15
16
  const node_url_1 = require("node:url");
16
17
  const connectTcp = (options) => new Promise((resolve, reject) => {
@@ -18,7 +19,12 @@ const connectTcp = (options) => new Promise((resolve, reject) => {
18
19
  socket.once('connect', () => resolve(socket));
19
20
  socket.once('error', (error) => reject(error));
20
21
  });
21
- const allowedTypes = ['text', 'image', 'buffer'];
22
+ const encodeText = (text) => {
23
+ // Remove all characters that are not letters, numbers, punctuation or whitespace printers cannot handle emojis
24
+ const filtered = text.replaceAll(/[^\p{L}\p{N}\p{P}\p{Z}\n\r×]+/gu, '');
25
+ return iconv_lite_1.default.encode(filtered, 'CP852');
26
+ };
27
+ const allowedPayloadTypes = ['text', 'image', 'buffer'];
22
28
  module.exports = function (RED) {
23
29
  function TcpEscposNode(configuration) {
24
30
  RED.nodes.createNode(this, configuration);
@@ -29,22 +35,29 @@ module.exports = function (RED) {
29
35
  const error = yield (() => __awaiter(this, void 0, void 0, function* () {
30
36
  try {
31
37
  const payload = (() => {
32
- const type = allowedTypes.find((type) => type === message.type) ||
33
- configuration.type;
38
+ const payloadType = allowedPayloadTypes.find((type) => type === message.type) ||
39
+ configuration.payloadType;
34
40
  const payload = message.payload || configuration.payload;
35
- if (type === 'text') {
36
- throw new Error('Text type not implemented yet');
41
+ if (payloadType === 'text') {
42
+ const commands = [];
43
+ commands.push(Buffer.from([0x1b, 0x40])); // Initialize printer
44
+ commands.push(Buffer.from([0x1b, 0x74, 18])); // Language options: 18 is CP852 code table
45
+ commands.push(Buffer.from([0x1b, 0x61, 1])); // Center alignment
46
+ commands.push(encodeText(String(payload)));
47
+ commands.push(Buffer.from([0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a])); // New lines
48
+ commands.push(Buffer.from([0x1b, 0x69])); // Cut
49
+ return Buffer.concat(commands);
37
50
  }
38
- if (type === 'image') {
51
+ if (payloadType === 'image') {
39
52
  throw new Error('Image type not implemented yet');
40
53
  }
41
- if (type === 'buffer') {
54
+ if (payloadType === 'buffer') {
42
55
  if (Array.isArray(payload)) {
43
56
  return Buffer.from(payload);
44
57
  }
45
58
  return Buffer.from(payload, 'base64');
46
59
  }
47
- return type;
60
+ return payloadType;
48
61
  })();
49
62
  this.status({ fill: 'yellow', shape: 'dot', text: 'connecting…' });
50
63
  const hostname = message.host || configuration.host;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-tcp-escpos",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "Escpos over TCP/IP node for Node-RED",
5
5
  "devDependencies": {
6
6
  "@types/node": "^18.14.0",
@@ -37,5 +37,8 @@
37
37
  "nodes": {
38
38
  "node": "nodes/node.js"
39
39
  }
40
+ },
41
+ "dependencies": {
42
+ "iconv-lite": "^0.7.0"
40
43
  }
41
44
  }