MarkoprintWiFi/MkpWIFI/mainDime.py

93 lines
2.4 KiB
Python

'''
Created on 27 set 2019
@author: Emanuele Trabattoni
'''
import time
import sys
import printer as p
import logging
import socket
import protocol
def main():
isRunning = True
lastAck = True
while isRunning:
printer = p.MarkoPrinter('./conf.json', LOGGER)
connessa = printer.connetti()
printer.sendCommand(protocol.deleteMemory())
while connessa:
try:
if lastAck == True:
lastTimestamp = int(time.time()*100)
commandString = protocol.callPrintImage(imageName='TM2.00I',
printMode='10',
fieldContent='{0}\r{0}'.format(lastTimestamp))
printer.sendCommand(commandString)
LOGGER.debug('SENT: {}'.format(subChars(commandString)))
lastAck = False
else:
r=printer.readResponse('callPrintImage')
LOGGER.debug('RECEIVED: {}'.format(subChars(r)))
if protocol.checkResponse('callPrintImage',r):
LOGGER.info("Stampa Riuscita: {}".format(lastTimestamp))
else:
LOGGER.error("Stampa Fallita: {}".format(lastTimestamp))
lastAck = True
except socket.timeout as t:
LOGGER.debug("Timeout stampa")
lastAck = False
try:
connessa = printer.keepalive()
except RuntimeError as r:
LOGGER.error("Stampante Disconnessa")
connessa = False
lastAck = True
except Exception as e:
LOGGER.error("Errore Inaspettato: {}".format(type(e)))
connessa = False
lastAck = True
pass
printer.disconnetti()
pass
def subChars(s):
subDict = {'\x02': " #STX ",
'\x03': " #ETX ",
'\x1B': " #ESC ",
'\r': " #CR ",
'\f': " #LF ",
'\t': " #TAB ",
'\x06': " #ACK ",
'\x15': " #NACK "
}
out = ""
for c in s:
if c in subDict.keys():
out += subDict[c]
else:
out += c
return out
if __name__ == '__main__':
# Setup Logger
LOGGER = logging.getLogger(__name__)
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"))
# File Logging
fh = logging.FileHandler(("./log/dimelog.log"))
fh.setLevel(logging.DEBUG)
fh.setFormatter(FORMATTER)
LOGGER.addHandler(fh)
# Console Logging
cl= logging.StreamHandler(sys.stdout)
cl.setLevel(logging.DEBUG)
cl.setFormatter(FORMATTER)
LOGGER.addHandler(cl)
LOGGER.warning("MarcaturaDime Started!")
sys.exit(main())
pass