@alizadefaig/pos-server 1.2.5-dev.1 → 1.2.6
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/app/logger/Server.js +2 -2
- package/app/proxy.js +0 -1
- package/app/socket/client.js +1 -2
- package/app/socket/server.js +9 -11
- package/package.json +3 -1
- package/routes/common/socket.js +38 -1
- package/routes/desktop/api/printers.js +27 -12
- package/server.js +7 -5
- package/utils.js +43 -2
package/app/logger/Server.js
CHANGED
package/app/proxy.js
CHANGED
package/app/socket/client.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const {io} = require("socket.io-client");
|
|
2
|
-
const serverSocket = require('../socket/server')();
|
|
3
2
|
let clientSocket = null;
|
|
4
3
|
module.exports = (target, companyId, terminalId) => {
|
|
5
4
|
if (!clientSocket && companyId) {
|
|
@@ -14,7 +13,7 @@ module.exports = (target, companyId, terminalId) => {
|
|
|
14
13
|
|
|
15
14
|
if (clientSocket) {
|
|
16
15
|
clientSocket.onAny((eventName, ...args) => {
|
|
17
|
-
serverSocket.to(companyId).emit(eventName, ...args);
|
|
16
|
+
// serverSocket.to(companyId).emit(eventName, ...args);
|
|
18
17
|
})
|
|
19
18
|
}
|
|
20
19
|
|
package/app/socket/server.js
CHANGED
|
@@ -19,8 +19,7 @@ module.exports = (server = null) => {
|
|
|
19
19
|
const companyId = socket.handshake.query.company_id;
|
|
20
20
|
const terminalId = socket.handshake.query.terminal_id;
|
|
21
21
|
let userId = socket.handshake.query.user_id;
|
|
22
|
-
|
|
23
|
-
const isProxy = false;
|
|
22
|
+
const isProxy = socket.handshake.query.is_proxy_server ?? false;
|
|
24
23
|
const PROXY_TARGET = getConfig().apiUrl;
|
|
25
24
|
|
|
26
25
|
socket.join(companyId);
|
|
@@ -30,7 +29,6 @@ module.exports = (server = null) => {
|
|
|
30
29
|
socket.isProxy = isProxy;
|
|
31
30
|
|
|
32
31
|
if (!isProxy) {
|
|
33
|
-
|
|
34
32
|
axios.post(`${PROXY_TARGET}/api/socket/connection`, {
|
|
35
33
|
companyId,
|
|
36
34
|
terminalId,
|
|
@@ -67,18 +65,18 @@ module.exports = (server = null) => {
|
|
|
67
65
|
});
|
|
68
66
|
});
|
|
69
67
|
|
|
70
|
-
// socket.on('broadcast', (data) => {
|
|
71
|
-
// console.log(companyId, data.eventName, data.data);
|
|
72
|
-
// })
|
|
73
|
-
|
|
74
68
|
socket.onAny((eventName, ...data) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
axios.post(`${PROXY_TARGET}/api/socket/${eventName}`, {
|
|
70
|
+
companyId,
|
|
71
|
+
terminalId,
|
|
72
|
+
userId,
|
|
73
|
+
data
|
|
74
|
+
}).catch((err) => {
|
|
75
|
+
logger.error(err);
|
|
76
|
+
});
|
|
78
77
|
socket.to(companyId).emit(eventName, ...data);
|
|
79
78
|
});
|
|
80
79
|
} else {
|
|
81
|
-
console.log('Proxy');
|
|
82
80
|
}
|
|
83
81
|
});
|
|
84
82
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alizadefaig/pos-server",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "Server in express js with Proxy to Main server if need and will work with Electron JS",
|
|
5
5
|
"homepage": "https://gitlab.com/faigalizade/pos-server#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
"express": "^5.1.0",
|
|
20
20
|
"form-data": "^4.0.2",
|
|
21
21
|
"http-proxy-middleware": "^3.0.5",
|
|
22
|
+
"lodash": "^4.17.21",
|
|
22
23
|
"moment": "^2.30.1",
|
|
23
24
|
"multer": "^1.4.5-lts.2",
|
|
24
25
|
"sequelize": "^6.37.7",
|
|
26
|
+
"sharp": "^0.34.1",
|
|
25
27
|
"socket.io": "^4.8.1",
|
|
26
28
|
"socket.io-client": "^4.8.1",
|
|
27
29
|
"uuid": "^11.1.0"
|
package/routes/common/socket.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
|
-
const {socketSendByFilter} = require("../../utils");
|
|
2
|
+
const {socketSendByFilter, findKeyByValue} = require("../../utils");
|
|
3
3
|
const serverSocket = require("../../app/socket/server")();
|
|
4
4
|
const router = express.Router();
|
|
5
5
|
|
|
@@ -51,4 +51,41 @@ router.all('/socket', (req, res) => {
|
|
|
51
51
|
});
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
+
router.post('/socket/request', (req, res) => {
|
|
55
|
+
const eventName = req.body?.event ?? null;
|
|
56
|
+
const data = req.body?.data ?? null;
|
|
57
|
+
const requestId = req.body.request_id;
|
|
58
|
+
const terminalId = req.body?.terminal_id ?? '9ec525b4-7e0b-422b-a6e1-486290578c8c';
|
|
59
|
+
const timeOut = req.body.time_out ?? 10000;
|
|
60
|
+
|
|
61
|
+
const requestEventName = `request:${eventName}:${requestId}`;
|
|
62
|
+
const responseEventName = `response:${eventName}:${requestId}`;
|
|
63
|
+
let isSent = false;
|
|
64
|
+
const socket = findKeyByValue(serverSocket.sockets.sockets, 'terminalId', terminalId);
|
|
65
|
+
if (!socket) {
|
|
66
|
+
return res.json({
|
|
67
|
+
success: false,
|
|
68
|
+
message: 'No socket',
|
|
69
|
+
}).status(404);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
socket.emit(requestEventName, data);
|
|
73
|
+
socket.once(responseEventName, (data) => {
|
|
74
|
+
if (!isSent) {
|
|
75
|
+
isSent = true;
|
|
76
|
+
res.json(data);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
if (!isSent) {
|
|
82
|
+
isSent = true;
|
|
83
|
+
res.json({
|
|
84
|
+
success: false,
|
|
85
|
+
message: 'Timeout error'
|
|
86
|
+
}).status(408)
|
|
87
|
+
}
|
|
88
|
+
}, timeOut)
|
|
89
|
+
})
|
|
90
|
+
|
|
54
91
|
module.exports = router;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
const router = express.Router();
|
|
3
3
|
const escpos = require('escpos');
|
|
4
|
-
const {getIpAddress, ipRange, getPrinterDevice} = require("../../../utils");
|
|
4
|
+
const {getIpAddress, ipRange, getPrinterDevice, splitImageByHeight} = require("../../../utils");
|
|
5
5
|
const net = require('net');
|
|
6
6
|
escpos.USB = require('@alizadefaig/cafetap-escpos-usb');
|
|
7
7
|
escpos.Network = require('escpos-network');
|
|
@@ -41,9 +41,9 @@ const getStringDescriptor = async (device, index) => {
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
const getNetworkDevices = async () => {
|
|
44
|
+
const getNetworkDevices = async (timeOut = 1000) => {
|
|
45
45
|
let networkDevices = [];
|
|
46
|
-
const connectionTimeout =
|
|
46
|
+
const connectionTimeout = timeOut;
|
|
47
47
|
const ip = await getIpAddress();
|
|
48
48
|
if (!ip) {
|
|
49
49
|
return [];
|
|
@@ -109,18 +109,27 @@ router.get('/printers.get', async (req, res) => {
|
|
|
109
109
|
});
|
|
110
110
|
|
|
111
111
|
router.post('/printers.print', async (req, res) => {
|
|
112
|
-
const device = getPrinterDevice(req.body.printer)
|
|
112
|
+
const device = getPrinterDevice(req.body.printer);
|
|
113
|
+
const printMethod = req.body.print_method || 'raster';
|
|
114
|
+
const printMode = req.body.density || 'dhdw';
|
|
113
115
|
if (!device) {
|
|
114
116
|
return res.send('bad').status(404);
|
|
115
117
|
}
|
|
116
|
-
|
|
118
|
+
|
|
119
|
+
const contentsBuffer = await splitImageByHeight(req.body.content, 576);
|
|
120
|
+
const images = await new Promise(async (resolve, reject) => {
|
|
121
|
+
let _images = [];
|
|
122
|
+
for (const buffer of contentsBuffer) {
|
|
123
|
+
const image = await new Promise((resolve, reject) => {
|
|
124
|
+
escpos.Image.load(buffer, 'image/png', (i) => {
|
|
125
|
+
resolve(i);
|
|
126
|
+
});
|
|
127
|
+
})
|
|
128
|
+
_images.push(image);
|
|
129
|
+
}
|
|
130
|
+
resolve(_images);
|
|
131
|
+
});
|
|
117
132
|
const printer = new escpos.Printer(device, {});
|
|
118
|
-
const imageBuffer = Buffer.from(imageContent, 'base64');
|
|
119
|
-
const image = await new Promise((resolve) => {
|
|
120
|
-
escpos.Image.load(imageBuffer, 'image/png', (i) => {
|
|
121
|
-
resolve(i);
|
|
122
|
-
})
|
|
123
|
-
})
|
|
124
133
|
const timeOut = req.body.timeout || 5000;
|
|
125
134
|
|
|
126
135
|
let sent = false;
|
|
@@ -139,7 +148,13 @@ router.post('/printers.print', async (req, res) => {
|
|
|
139
148
|
return;
|
|
140
149
|
}
|
|
141
150
|
|
|
142
|
-
|
|
151
|
+
images.map(image => {
|
|
152
|
+
if (printMethod === 'raster') {
|
|
153
|
+
printer.raster(image, printMode);
|
|
154
|
+
} else {
|
|
155
|
+
printer.image(image, printMode);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
143
158
|
if (req.body.bell) {
|
|
144
159
|
printer.beep(req.body.bell_count || 2, req.body.bell_time || 3);
|
|
145
160
|
}
|
package/server.js
CHANGED
|
@@ -47,11 +47,13 @@ const runExpress = (conf = null) => {
|
|
|
47
47
|
|
|
48
48
|
// ===== Init Platform
|
|
49
49
|
app.get('/init', (req, res) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// const companyId = req.header('company-id');
|
|
51
|
+
// const terminalId = req.header('terminal-id');
|
|
52
|
+
//
|
|
53
|
+
// if (!appConfig.isServer && companyId && terminalId) {
|
|
54
|
+
// console.log('Connecting to socket');
|
|
55
|
+
// clientSocket = require('./app/socket/client')(PROXY_TARGET, companyId, terminalId);
|
|
56
|
+
// }
|
|
55
57
|
res.send('ok');
|
|
56
58
|
});
|
|
57
59
|
|
package/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const {readdirSync, existsSync} = require("node:fs");
|
|
2
2
|
const {extname, join} = require("node:path");
|
|
3
3
|
const {networkInterfaces} = require("os");
|
|
4
|
-
const
|
|
4
|
+
const sharp = require("sharp");
|
|
5
5
|
|
|
6
6
|
function loadRoutes(app, routesDir, prefix = '') {
|
|
7
7
|
if (!existsSync(routesDir)) {
|
|
@@ -102,11 +102,52 @@ const socketSendByFilter = ({companyId, eventName, data, to, toId, excepts}, ser
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
|
|
106
|
+
function findKeyByValue(map, targetKey, targetValue) {
|
|
107
|
+
for (const item of map.values()) {
|
|
108
|
+
console.log(item);
|
|
109
|
+
if (item[targetKey] === targetValue) {
|
|
110
|
+
return item;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async function splitImageByHeight(base64Data, partHeight = 1000, toWidth = 576) {
|
|
118
|
+
const base64 = base64Data.replace(/^data:image\/\w+;base64,/, '');
|
|
119
|
+
const buffer = Buffer.from(base64, 'base64');
|
|
120
|
+
|
|
121
|
+
const imageMeta = await sharp(buffer).metadata();
|
|
122
|
+
const { width, height } = imageMeta;
|
|
123
|
+
|
|
124
|
+
const numParts = Math.ceil(height / partHeight);
|
|
125
|
+
const parts = [];
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < numParts; i++) {
|
|
128
|
+
const top = i * partHeight;
|
|
129
|
+
const currentHeight = Math.min(partHeight, height - top);
|
|
130
|
+
|
|
131
|
+
const partBuffer = await sharp(buffer)
|
|
132
|
+
.extract({ left: 0, top, width, height: currentHeight })
|
|
133
|
+
.resize({width: toWidth})
|
|
134
|
+
.greyscale()
|
|
135
|
+
.png()
|
|
136
|
+
.toBuffer();
|
|
137
|
+
|
|
138
|
+
parts.push(partBuffer);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return parts;
|
|
142
|
+
}
|
|
143
|
+
|
|
105
144
|
module.exports = {
|
|
106
145
|
loadRoutes,
|
|
107
146
|
getIpAddress,
|
|
108
147
|
ipRange,
|
|
109
148
|
getPrinterDevice,
|
|
110
149
|
streamToString,
|
|
111
|
-
socketSendByFilter
|
|
150
|
+
socketSendByFilter,
|
|
151
|
+
findKeyByValue,
|
|
152
|
+
splitImageByHeight
|
|
112
153
|
}
|