-
Notifications
You must be signed in to change notification settings - Fork 0
/
printerStatusServer.js
117 lines (101 loc) · 4.18 KB
/
printerStatusServer.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
109
110
111
112
113
114
115
116
117
const http = require("http");
const os = require("os");
const util = require("util");
const hostname = "127.0.0.1";
const port = 3030;
const server = http.createServer(async (request, response) => {
console.log("Server got a request - gonna handle it :)");
switch (request.url) {
case "/": {
response.statusCode = 200;
response.setHeader("Content-Type", "text/plain");
response.end(
"Welcome to the printer status server!\nThe Server is up and running :)!\n"
);
break;
}
case "/status": {
var printerStatus = await checkPrinterStatus();
// When the printer is not available, 418 is returned - 200 otherwise.
if (printerStatus === "not available") {
response.statusCode = 418;
response.setHeader("Content-Type", "text/plain");
response.end(printerStatus);
} else {
response.statusCode = 200;
response.setHeader("Content-Type", "text/plain");
response.end(printerStatus);
}
break;
}
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
async function checkPrinterStatus() {
var runningWindows = os.type() === "Windows_NT";
var configuredPrintersCheckList = ["Posiflex DJM32184 Printer"];
var testPrinterListCommandOutput =
"Location Name PrinterState PrinterStatus ShareName SystemName Posiflex DJM32184 Printer 0 3 DJM32184 OneNote (Desktop) 0 3 DJM32184 Microsoft XPS Document Writer 0 3 DJM32184 Microsoft Print to PDF 0 3 DJM32184 Fax 0 3 DJM32184";
var printerStatus = "not available";
var availablePrintersListOutput = "noDevicesFound";
if (runningWindows) {
console.log("Running on: Windows OS.");
const exec = util.promisify(require("child_process").exec);
// Windows command to list all available printers.
const { error, stdout, stderror } = await exec(
"wmic printer list brief"
);
availablePrintersListOutput = stdout;
console.log("stdout: " + stdout);
if (error) {
printerStatus = "Error trying to get printer status.";
return printerStatus;
}
printerStatus = checkAvailabilityOfCheckListPrinters(
availablePrintersListOutput,
configuredPrintersCheckList,
printerStatus
);
return printerStatus;
} else {
availablePrintersListOutput = testPrinterListCommandOutput;
printerStatus = checkAvailabilityOfCheckListPrinters(
availablePrintersListOutput,
configuredPrintersCheckList,
printerStatus
);
return printerStatus;
}
}
function checkAvailabilityOfCheckListPrinters(
availablePrintersListOutput,
configuredPrintersCheckList,
printerStatus
) {
console.log("Going to check for configured printers availability.");
console.log(
"Available printers list output: " + availablePrintersListOutput
);
if (availablePrintersListOutput !== "noDevicesFound") {
console.log("Available printers: " + availablePrintersListOutput);
configuredPrintersCheckList.forEach((configuredPrinterToCheck) => {
console.log(
"check if printer is available: " + configuredPrinterToCheck
);
console.log(
"Is configured printer to check included: " +
availablePrintersListOutput.includes(
configuredPrinterToCheck
)
);
if (
availablePrintersListOutput.includes(configuredPrinterToCheck)
) {
printerStatus = "Configured printer was found :)";
}
});
}
return printerStatus;
}