Modifica comandi, aggiunta nuovo main per marcatura dime

master
Emanuele Trabattoni 2019-09-27 15:44:43 +02:00
parent 29624c8798
commit 35ee4ed722
3 changed files with 167 additions and 149 deletions

View File

@ -45,38 +45,38 @@ MKPcommands = {
'begin': '\x02', #STX 'begin': '\x02', #STX
'end': '\x03', #ETX 'end': '\x03', #ETX
'command': 'TZ', 'command': 'TZ',
'success': '\x06\x02[0-9]OK\x03', 'success': '\x06\x02([0-9])OK\x03',
'error': '[0-9]E[0-9]', 'error': '([0-9])E([0-9])',
'args': ['imageName', 'printMode'] 'args': ['imageName', 'printMode']
}, },
'sendImage': { 'sendImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EW----', 'command': 'EW----;',
'success': None, 'success': None,
'error': None, 'error': None,
'args': ['imageName'] 'args': ['imageName']
}, },
'sendImageEnd': { 'sendImageEnd': {
'defPrefix': True, 'defPrefix': True,
'command': 'EX---', 'command': 'EX----;',
'success': 'OK\r', 'success': 'OK\r',
'error': None, 'error': None,
}, },
'receiveImage': { 'receiveImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EL', 'command': 'EL----;',
'success': None, 'success': True,
'error': None 'error': None
}, },
'receiveDir': { 'receiveDir': {
'defPrefix': True, 'defPrefix': True,
'command': 'ED----', 'command': 'ED----;',
'success': None, 'success': True,
'error': None 'error': None
}, },
'deleteImage': { 'deleteImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EQ----', 'command': 'EQ----;',
'success': 'OK\r', 'success': 'OK\r',
'error': 'Err\r', 'error': 'Err\r',
'args': ['imageName'] 'args': ['imageName']
@ -91,26 +91,26 @@ MKPcommands = {
'receiveDate': { 'receiveDate': {
'defPrefix': True, 'defPrefix': True,
'command': '*GSTD', 'command': '*GSTD',
'success': '[0-9]{13}', 'success': True,
'error': None, 'error': None,
'args': ['',''] 'args': ['dateTime']
}, },
'status': { 'status': {
'defPrefix': True, 'defPrefix': True,
'command': 'SV', 'command': 'SV',
'success': None, 'success': True,
'error': None 'error': None
}, },
'status1': { 'status1': {
'defPrefix': True, 'defPrefix': True,
'command': 'S1', 'command': 'S1',
'success': None, 'success': True,
'error': None 'error': None
}, },
'status2': { 'status2': {
'defPrefix': True, 'defPrefix': True,
'command': 'S2', 'command': 'S2',
'success': None, 'success': True,
'error': None 'error': None
}, },
} }

View File

@ -0,0 +1,19 @@
'''
Created on 27 set 2019
@author: Emanuele Trabattoni
'''
import time
import sys
import printer
import commands
def main():
printer = printer.MarkoPrinter('./conf.json')
printer.connetti()
pass
if __name__ == '__main__':
sys.exit(main())
pass

View File

@ -1,132 +1,131 @@
import socket import socket
import json import json
import time import time
class MarkoPrinter:
class MarkoPrinter: def __init__(self, configFilePath):
def __init__(self, configFilePath): self.inkStatCounter = 0
self.inkStatCounter = 0 self.printSock = None
self.printSock = None self.headStatus = dict()
self.headStatus = dict() self.connected = False
self.connected = False self.sending = False
self.sending = False with open(configFilePath, "r") as setFile:
with open(configFilePath, "r") as setFile: self.settings = json.load(setFile)
self.settings = json.load(setFile) self.data = self.settings["printcmd"]
self.data = self.settings["printcmd"] self.debug = True
self.debug = True
def log(self, *logtxt):
def log(self, *logtxt): if self.debug:
if self.debug: print(*logtxt)
print(*logtxt)
def connetti(self):
def connetti(self): if not self.connected:
if not self.connected: if self.printSock is None:
if self.printSock is None: try:
try: self.printSock = socket.socket(
self.printSock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
socket.AF_INET, socket.SOCK_STREAM) self.printSock.connect(
self.printSock.connect( (self.settings["ip"], self.settings["port"]))
(self.settings["ip"], self.settings["port"])) self.connected = True
self.connected = True self.printSock.settimeout(0.5)
self.printSock.settimeout(0.5) self.checkStatus()
self.checkStatus() except socket.error as e:
except socket.error as e: self.printSock = None
self.printSock = None self.connected = False
self.connected = False self.log("Connessione Fallita: {}".format(e))
self.log("Connessione Fallita: {}".format(e)) else:
else: self.printSock.close()
self.printSock.close() self.printSock = None
self.printSock = None self.connetti()
self.connetti() return self.connected
return self.connected
def disconnetti(self):
def disconnetti(self): if self.connected:
if self.connected: self.printSock.close()
self.printSock.close() self.printSock = None
self.printSock = None self.connected = False
self.connected = False return self.connected
return self.connected
def checkStatus(self):
def checkStatus(self): statusUpdated = False
statusUpdated = False if self.connected:
if self.connected: try:
try: self.printSock.send(b"\x1BS1\r")
self.printSock.send(b"\x1BS1\r") time.sleep(1)
time.sleep(1) c = self.printSock.recv(1)
c = self.printSock.recv(1) inData = b""
inData = b"" while c != b"\r":
while c != b"\r": inData += c
inData += c if len(inData) > 200:
if len(inData) > 200: self.log("Status Message Length Error")
self.log("Status Message Length Error") raise IOError
raise IOError c = self.printSock.recv(1)
c = self.printSock.recv(1) inData = str(inData, "ascii")
inData = str(inData, "ascii") inData = inData.split(":")
inData = inData.split(":") for index, key in enumerate(self.data["headstatus"]):
for index, key in enumerate(self.data["headstatus"]): if key != "txt":
if key != "txt": self.headStatus[key] = int(inData[index])
self.headStatus[key] = int(inData[index]) else:
else: self.headStatus[key] = str(inData[index])
self.headStatus[key] = str(inData[index]) self.log("Status: {}".format(self.headStatus))
self.log("Status: {}".format(self.headStatus)) statusUpdated = True
statusUpdated = True except:
except: self.log("Status retreive error!")
self.log("Status retreive error!") return statusUpdated
return statusUpdated
def upload(self, descr, datastr):
def upload(self, descr, datastr): self.sending = True
self.sending = True response = ""
response = "" if self.connected:
if self.connected: cmdstr = "\x02TZ" + self.data["filename"] + \
cmdstr = "\x02TZ" + self.data["filename"] + \ ";" + self.data["printmode"] + "\r"
";" + self.data["printmode"] + "\r" self.log(bytearray(descr, "ascii"))
self.log(bytearray(descr, "ascii")) descr = descr.replace("\n", "\t")
descr = descr.replace("\n", "\t") outstr = cmdstr + datastr + "\03\r"
outstr = cmdstr + datastr + "\03\r" self.printSock.settimeout(1.0)
self.printSock.settimeout(1.0) try:
try: self.printSock.send(bytearray(outstr, "ascii"))
self.printSock.send(bytearray(outstr, "ascii")) retval = self.printSock.recv(1)
retval = self.printSock.recv(1) self.log("ACK: ", retval)
self.log("ACK: ", retval) if retval == b'\x06':
if retval == b'\x06': retval = self.printSock.recv(6)
retval = self.printSock.recv(6) if retval == b'\x021OK\x03':
if retval == b'\x021OK\x03': self.log("RESP: ", retval)
self.log("RESP: ", retval) response = "Invio Riuscito!"
response = "Invio Riuscito!" self.log("Printer OK")
self.log("Printer OK") else:
else: response = "Invio Fallito!"
response = "Invio Fallito!" self.log("Printer Error")
self.log("Printer Error") else:
else: self.log("Send Error")
self.log("Send Error") self.printSock.settimeout(0.5)
self.printSock.settimeout(0.5) statusUpdated = self.checkStatus()
statusUpdated = self.checkStatus() if self.headStatus["ink"] == 2:
if self.headStatus["ink"] == 2: response = "CARTUCCIA ESAURITA\nSostituire!"
response = "CARTUCCIA ESAURITA\nSostituire!" elif self.headStatus["ink"] == 1:
elif self.headStatus["ink"] == 1: response = "Cartuccia in Esaurimento"
response = "Cartuccia in Esaurimento" except socket.error as e:
except socket.error as e: self.log("Socket error: ", e)
self.log("Socket error: ", e) self.printSock.settimeout(0.5)
self.printSock.settimeout(0.5) response = "Invio Fallito!"
response = "Invio Fallito!" self.printSock.sendall(b"\r"*5)
self.printSock.sendall(b"\r"*5) self.printSock.recv(5)
self.printSock.recv(5) self.sending = False
self.sending = False return response, statusUpdated
return response, statusUpdated
def keepalive(self):
def keepalive(self): statusUpdated = False
statusUpdated = False if not self.sending:
if not self.sending: self.log("Keepalive")
self.log("Keepalive") if self.connected:
if self.connected: try:
try: self.printSock.send(b"\x1B*\r")
self.printSock.send(b"\x1B*\r") self.log(str(self.printSock.recv(5)))
self.log(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: self.disconnetti()
self.disconnetti() return self.connected, statusUpdated
return self.connected, statusUpdated