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:
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"))

View File

@ -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