Affidabile per un test in produzione

master
Emanuele Trabattoni 2019-09-30 17:28:11 +02:00
parent d87eb7a22b
commit 8831aaa641
2 changed files with 24 additions and 16 deletions

View File

@ -38,12 +38,18 @@ def main():
except socket.timeout as t: except socket.timeout as t:
LOGGER.debug("Timeout stampa") LOGGER.debug("Timeout stampa")
lastAck = False lastAck = False
printer.keepalive() try:
pass connessa = printer.keepalive()
except RuntimeError as r:
LOGGER.error("Stampante Disconnessa")
connessa = False
lastAck = True
except Exception as e: except Exception as e:
LOGGER.error(str(e)) LOGGER.error("Errore Inaspettato: {}".format(type(e)))
connessa = False
lastAck = True
pass pass
pass printer.disconnetti()
pass pass
def subChars(s): def subChars(s):
@ -67,7 +73,7 @@ def subChars(s):
if __name__ == '__main__': if __name__ == '__main__':
# Setup Logger # Setup Logger
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG) LOGGER.setLevel(logging.INFO)
LOGGER.propagate = False LOGGER.propagate = False
FORMATTER = logging.Formatter(("%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s"), FORMATTER = logging.Formatter(("%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s"),
("%m-%d %H:%M:%S")) ("%m-%d %H:%M:%S"))

View File

@ -1,8 +1,7 @@
import socket import socket
import json import json
import time import time
import protocol
import threading
import commands as cmd import commands as cmd
class MarkoPrinter: class MarkoPrinter:
@ -34,7 +33,7 @@ class MarkoPrinter:
self.connected = True self.connected = True
self.printSock.settimeout(0.5) self.printSock.settimeout(0.5)
self.checkStatus() self.checkStatus()
self.LOGGER.debug("Connessione Riuscita") self.LOGGER.info("Connessione Riuscita")
except socket.error as e: except socket.error as e:
self.printSock = None self.printSock = None
self.connected = False self.connected = False
@ -125,20 +124,22 @@ class MarkoPrinter:
if self.connected: if self.connected:
try: try:
self.printSock.settimeout(1.0) self.printSock.settimeout(1.0)
self.printSock.send(bytearray(c,'ascii')) sent=self.printSock.send(bytearray(c,'ascii'))
self.printSock.settimeout(0.5) self.printSock.settimeout(0.5)
self.sending = False self.sending = False
if sent < len(c):
raise RuntimeError("Stampante Disconnessa")
return True return True
except socket.error as e: except socket.error as e:
self.LOGGER.error('Socket Error: {}'.format(e)) self.LOGGER.error('Socket Error: {}'.format(e))
return False
self.sending = False self.sending = False
return False
pass pass
def readResponse(self, c): def readResponse(self, c):
self.reading = True self.reading = True
buf = bytearray() 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: if self.connected:
try: try:
# lettura bloccante del socket in attesa di conferma # lettura bloccante del socket in attesa di conferma
@ -146,8 +147,6 @@ class MarkoPrinter:
c = self.printSock.recv(1) c = self.printSock.recv(1)
while c != respTerm: while c != respTerm:
buf += c buf += c
if str(c, 'ascii') == respTerm:
break
c = self.printSock.recv(1) c = self.printSock.recv(1)
# disattivo lettura bloccante # disattivo lettura bloccante
self.printSock.settimeout(0.5) self.printSock.settimeout(0.5)
@ -163,17 +162,20 @@ class MarkoPrinter:
def keepalive(self): def keepalive(self):
statusUpdated = False statusUpdated = False
keepCmd = b"\x1B*\r"
self.LOGGER.debug("Keepalive") self.LOGGER.debug("Keepalive")
if not self.sending and not self.reading: if not self.sending and not self.reading:
if self.connected: if self.connected:
try: 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))) self.LOGGER.debug(str(self.printSock.recv(5)))
if self.inkStatCounter <= self.settings["inkstat"]: if self.inkStatCounter <= self.settings["inkstat"]:
self.inkStatCounter += 1 self.inkStatCounter += 1
else: else:
statusUpdated = self.checkStatus() statusUpdated = self.checkStatus()
self.inkStatCounter = 0 self.inkStatCounter = 0
except: except socket.timeout as t:
self.disconnetti() raise RuntimeError("Stampante Disconnessa: {}".format(t))
return self.connected, statusUpdated return self.connected, statusUpdated