You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.1 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at app://mozilla.org/MPL/2.0/.
*/
var printer = require("node-thermal-printer");
var express = require('express');
var app = express();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.json());
function initPrinter() {
printer.init({
type: devtype,
interface: device
});
}
function printLine(line) {
if (line.format.indexOf('bold') > -1) {
printer.bold(true);
} else {
printer.bold(false);
}
if (line.format.indexOf('hr') > -1) {
printer.drawLine();
} else if (line.format.indexOf('blank') > -1) {
printer.newLine();
} else {
console.log(line.text);
printer.println(line.text);
}
}
app.use('/', express.static('control'));
app.get('/status', function (request, response) {
response.send({
status: "OK",
width: linewidth
});
});
app.post('/print', function (request, response) {
initPrinter();
var receipt = request.body;
console.log(JSON.stringify(request.body));
var header = receipt.header.length > 0;
var footer = receipt.footer.length > 0;
receipt.header.forEach(function (line) {
printLine(line);
});
if (header) {
printLine({text: '', format: ['hr']});
}
receipt.lines.forEach(function (line) {
printLine(line);
});
if (footer) {
printLine({text: '', format: ['hr']});
}
receipt.footer.forEach(function (line) {
printLine(line);
});
printer.cut();
printer.execute();
response.send({
status: "OK"
});
});
app.post('/opendrawer', function (request, response) {
initPrinter();
printer.openCashDrawer();
printer.execute();
response.send({
status: "OK"
});
});
app.listen(64269);