From 553f3db8dfacd8d86c10359f1763d3f4e1b98d26 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Wed, 11 Mar 2020 01:38:12 -0700 Subject: [PATCH 1/2] print client returns promise --- client/printing/print_client.js | 13 +++++++++---- package-lock.json | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/client/printing/print_client.js b/client/printing/print_client.js index ac01aa1..a6361b4 100644 --- a/client/printing/print_client.js +++ b/client/printing/print_client.js @@ -17,10 +17,15 @@ function sendPrintRequest(raw, copies, sides, pageRanges, destination) { for (let key in printOptions) { request.getOptionsMap().set(key, printOptions[key]); } - client.printPage(request, function (err, response) { - if (err) console.log(err); - console.log('Message: ', response.getMessage()); - }); + return new Promise(function (resolve, reject) { + client.printPage(request, function (err, response) { + if (err) reject({ message: 'Failed to print', error: true }) + resolve({ + message: response && response.getMessage(), + error: false + }) + }); + }) } module.exports = { sendPrintRequest } diff --git a/package-lock.json b/package-lock.json index f148762..f5d326c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -710,4 +710,4 @@ } } } -} +} \ No newline at end of file From 68be0d6ebe8d31398da8276d8923ee30a8e24e17 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Wed, 11 Mar 2020 21:13:19 -0700 Subject: [PATCH 2/2] Made printer return promise --- client/printing/print_client.js | 6 ++++-- server/printing/printing_server.py | 15 +++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client/printing/print_client.js b/client/printing/print_client.js index a6361b4..03285ba 100644 --- a/client/printing/print_client.js +++ b/client/printing/print_client.js @@ -19,12 +19,14 @@ function sendPrintRequest(raw, copies, sides, pageRanges, destination) { } return new Promise(function (resolve, reject) { client.printPage(request, function (err, response) { - if (err) reject({ message: 'Failed to print', error: true }) + if (err || response.getMessage() == 'error') { + reject({ message: 'Failed to print', error: true }) + } resolve({ message: response && response.getMessage(), error: false }) - }); + }) }) } diff --git a/server/printing/printing_server.py b/server/printing/printing_server.py index 6272e79..a6c305d 100644 --- a/server/printing/printing_server.py +++ b/server/printing/printing_server.py @@ -8,15 +8,14 @@ class PrintServicer(print_pb2_grpc.PrinterServicer): - left_pages = 0 - right_pages = 0 + pages = 0 def DeterminePrinterForJob(self, copies): - if (self.left_pages > self.right_pages): - self.right_pages += copies + if (self.pages > 0): + self.pages += copies return "HP-LaserJet-p2015dn-right" else: - self.left_pages += copies + self.pages -= copies return "HP-LaserJet-p2015dn-left" def SendRequestToPrinter(self, encoded_file, copies=1, options={}): @@ -32,12 +31,12 @@ def SendRequestToPrinter(self, encoded_file, copies=1, options={}): command += "-o " + str(current_option) + " " else: command += "-o " + str(current_option) + "="\ - + str(options[current_option]) + " " + + str(options[current_option]) + " " command += "-d " + self.DeterminePrinterForJob(copies) + " " command += "tmp.pdf" - os.system(command) + status = os.popen(command) os.remove("tmp.pdf") - return "printed" + return 'printed' if status else 'error' def PrintPage(self, request, context): response = print_pb2.PrintResponse()