From 8831aaa641ffc39ee68fa0bca2ee2c7738720f18 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Mon, 30 Sep 2019 17:28:11 +0200 Subject: [PATCH] Affidabile per un test in produzione --- MkpWIFI/mainDime.py | 16 +++++++++++----- MkpWIFI/printer.py | 24 +++++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/MkpWIFI/mainDime.py b/MkpWIFI/mainDime.py index d5d8d1e..66e7cbe 100644 --- a/MkpWIFI/mainDime.py +++ b/MkpWIFI/mainDime.py @@ -38,12 +38,18 @@ def main(): except socket.timeout as t: LOGGER.debug("Timeout stampa") lastAck = False - printer.keepalive() - pass + try: + connessa = printer.keepalive() + except RuntimeError as r: + LOGGER.error("Stampante Disconnessa") + connessa = False + lastAck = True except Exception as e: - LOGGER.error(str(e)) + LOGGER.error("Errore Inaspettato: {}".format(type(e))) + connessa = False + lastAck = True pass - pass + printer.disconnetti() pass def subChars(s): @@ -67,7 +73,7 @@ def subChars(s): if __name__ == '__main__': # Setup Logger LOGGER = logging.getLogger(__name__) - LOGGER.setLevel(logging.DEBUG) + LOGGER.setLevel(logging.INFO) LOGGER.propagate = False FORMATTER = logging.Formatter(("%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s"), ("%m-%d %H:%M:%S")) diff --git a/MkpWIFI/printer.py b/MkpWIFI/printer.py index 78eebfb..fc93d3d 100644 --- a/MkpWIFI/printer.py +++ b/MkpWIFI/printer.py @@ -1,8 +1,7 @@ import socket import json import time -import protocol -import threading + import commands as cmd class MarkoPrinter: @@ -34,7 +33,7 @@ class MarkoPrinter: self.connected = True self.printSock.settimeout(0.5) self.checkStatus() - self.LOGGER.debug("Connessione Riuscita") + self.LOGGER.info("Connessione Riuscita") except socket.error as e: self.printSock = None self.connected = False @@ -125,20 +124,22 @@ class MarkoPrinter: if self.connected: try: self.printSock.settimeout(1.0) - self.printSock.send(bytearray(c,'ascii')) + sent=self.printSock.send(bytearray(c,'ascii')) self.printSock.settimeout(0.5) self.sending = False + if sent < len(c): + raise RuntimeError("Stampante Disconnessa") return True except socket.error as e: self.LOGGER.error('Socket Error: {}'.format(e)) - return False self.sending = False + return False pass def readResponse(self, c): self.reading = True buf = bytearray() - respTerm = cmd.DEFprefix['end'] if cmd.MKPcommands[c]['defPrefix'] else cmd.MKPcommands[c]['end'] + respTerm = bytearray(cmd.DEFprefix['end'] if cmd.MKPcommands[c]['defPrefix'] else cmd.MKPcommands[c]['end'], 'ascii') if self.connected: try: # lettura bloccante del socket in attesa di conferma @@ -146,8 +147,6 @@ class MarkoPrinter: c = self.printSock.recv(1) while c != respTerm: buf += c - if str(c, 'ascii') == respTerm: - break c = self.printSock.recv(1) # disattivo lettura bloccante self.printSock.settimeout(0.5) @@ -163,17 +162,20 @@ class MarkoPrinter: def keepalive(self): statusUpdated = False + keepCmd = b"\x1B*\r" self.LOGGER.debug("Keepalive") if not self.sending and not self.reading: if self.connected: try: - self.printSock.send(b"\x1B*\r") + sent = self.printSock.send(keepCmd) + if sent < len(keepCmd): + raise RuntimeError("Stampante Disconnessa") self.LOGGER.debug(str(self.printSock.recv(5))) if self.inkStatCounter <= self.settings["inkstat"]: self.inkStatCounter += 1 else: statusUpdated = self.checkStatus() self.inkStatCounter = 0 - except: - self.disconnetti() + except socket.timeout as t: + raise RuntimeError("Stampante Disconnessa: {}".format(t)) return self.connected, statusUpdated