-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
108 lines (90 loc) · 3.12 KB
/
server.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
var moment = require('moment');
const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const ThermalPrinter = require("node-thermal-printer").printer;
const Types = require("node-thermal-printer").types;
var esprima = require('esprima');
const app = express();
const port = 3005;
app.use(cors());
// Configuring body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
function printItem(item, currency, printer, priceZero, quantity) {
let qte = item.quantity || quantity
let left = qte + "X" + (item.productName || item.name);
let right = (priceZero ? 0 : (parseFloat(item.price)) * qte).toFixed(2) + " " + currency;
printer.leftRight(left, right);
}
function computeTotalDealPrice(deal) {
let total = 0;
deal.productAndSkusLines.forEach(item => {
total += parseFloat(item.price);
})
return total;
}
app.post('/printThermal', async (req, res) => {
try {
mainWindowInstance.webContents.send('dataSending');
const body = req.body;
const model = body.model;
const order = body.order;
const urlPrinter = body.printerUrl || "tcp://127.0.0.1:9100";
console.log("urlPrinter " + urlPrinter)
let printer = new ThermalPrinter({
type: model === "EPSON" ? Types.EPSON : Types.STAR,
interface: urlPrinter,
options: {
timeout: 1000
},
width: 48, // Number of characters in one line - default: 48
characterSet: 'SLOVENIA', // Character set - default: SLOVENIA
removeSpecialCharacters: false, // Removes special characters - default: false
lineCharacter: "-", // Use custom character for drawing lines - default: -
});
let isConnected = await printer.isPrinterConnected();
console.log("Printer connected:", isConnected);
console.log(body);
let data = body.data;
try {
printWithData(printer, data, body.headerScript);
printWithData(printer, data, body.bodyScript);
printWithData(printer, data, body.footerScript);
}
catch(error) {
console.log(error);
res.json({
status: 'error',
error: error.message,
})
return;
}
await printer.execute();
console.log("Print success.");
} catch (error) {
console.error("Print error:", error);
res.json({
status: 'error',
error: error.message,
})
return;
} finally {
mainWindowInstance.webContents.send('dataEnd');
}
res.json({ status: 'ok' })
});
const printWithData = (printer, data, content) => {
console.log(content)
if (content) {
esprima.parse(content);
eval(content);
}
}
var mainWindowInstance;
module.exports = {
startServer: (mainWindow) => {
mainWindowInstance = mainWindow;
app.listen(port, () => console.log(`Server running on port ${port}!`));
}
}