aggiunti comandi e migliorato logging

le funzioni che chiamano i comandi sono volutamente tutte separate,
perche' non si sa mai
master
Emanuele Trabattoni 2020-03-20 13:57:52 +01:00
parent c168a8df07
commit 59c203f3a2
5 changed files with 70 additions and 28 deletions

View File

@ -1,4 +1,5 @@
{
"command_queue": "queueObject",
"ip1": {
"queue_id": "queueObject",
"thr": "threadObject",

View File

@ -22,14 +22,13 @@ app = Flask(__name__)
@app.route('/tscprinter/printer/<ip>/<command>', methods=['POST'])
def printer_in(ip, command):
parameters = request.get_json()
print(f"Comando Stampante: {ip} {command} {str(parameters)}")
logger.debug(f"Comando Stampante: {ip} {command} {str(parameters)}")
# questo prossimo mi sa che non funzia perché la funzione
# inizia a esistere quando si esegue tsc_manager.main ma poi non
# viene esportata nello scope del modulo tsc_manager
queue_id = tsc_manager.get_queue_id(ip)
print(queue_id)
if queue_id is not None and queue_id in queues_in:
queues_in[queue_id].put({'name': command, 'parameters': parameters})
if queue_id is not None:
queue_id.put({'name': command, 'parameters': parameters})
return "Comando Stampante Inviato", 200
else:
return "Stampante non trovata", 404
@ -37,7 +36,7 @@ def printer_in(ip, command):
@app.route('/tscprinter/gest/<command>', methods=['POST'])
def gest_in(command):
parameters = request.get_json()
print(f"Comando Gestore: {str(parameters)}")
logger.debug(f"Comando Gestore: {str(parameters)}")
# istes di sopra
queue_command = tsc_manager.get_queue_command()
if queue_command is not None:

View File

@ -22,7 +22,7 @@ def notifier(queue_out, logger):
requests.post(django_addr+msg['url'], json=msg['msg'])
riuscita = True
except Exception as e:
print("django irraggiungibile. aspetto 2 sec...")
logger.debug("django irraggiungibile. aspetto 2 sec...")
time.sleep(2)
print(e)
@ -37,6 +37,7 @@ def send_response(queue_out, ip, command, data):
'url' : URL_RESPONSE,
'msg': resp
}
print(f"Mando Risposta: {msg}")
queue_out.put(msg)
def send_hello(queue_out):

View File

@ -5,9 +5,15 @@ Created on 19 mar 2020
'''
TSC_COMMANDS = {'DOWNLOAD': "DOWNLOAD {location}{name}{size}{content}",
'REMOVE': "KILL {location}{name}",
'MOVE': "MOVE",
'LIST': "~!F",
'INFO': "~!T",
'USE': "~!@",
'PAUSE': "!P",
'RESUME': "!O",
'CLEAR': "!.",
'RUN': "RUN {name}",
'STATUS': '!?'}
'STATUS': "!?"}
TSC_STATUS = {0x00:"Normal",
0x01:"Head opened",

View File

@ -6,7 +6,9 @@ Created on 19 mar 2020
import queue, time, socket, logging
from driver.tsc_commands import TSC_COMMANDS, TSC_STATUS, LINE_SEP
from driver.notifier import URL_ERR, URL_EVENT, URL_RESPONSE
from driver.notifier import URL_ERR, URL_EVENT, URL_RESPONSE, send_response
import traceback
from win32comext.mapi.mapiutil import prTable
SOCK_TIMEOUT = 2
@ -15,24 +17,13 @@ def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: s
prt = None
def read_until(term):
buf = ''
buf = bytearray()
c=prt.recv(1)
while c is not term:
while c != term:
buf = buf+c
c = prt.recv(1)
return buf
def list_files():
nonlocal prt
prt.sendall(TSC_COMMANDS['LIST']+LINE_SEP)
time.sleep(0.1)
retval = read_until(0x1A).split(0x0D)
q_out.put({'url':URL_RESPONSE,
'msg': retval})
pass
def send_file(name=None, content=None, ftype=None):
nonlocal prt
if ftype == 'batch':
@ -40,7 +31,7 @@ def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: s
elif ftype == 'data':
data = content.join(LINE_SEP)+LINE_SEP
to_send = TSC_COMMANDS['DOWNLOAD'].format(name=f'"{name}"',size=','+len(data),content=data)
prt.sendall(to_send)
prt.send_all(bytearray(to_send, encoding='utf-8'))
pass
def delete_file(name=None):
@ -49,27 +40,59 @@ def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: s
to_send = TSC_COMMANDS['REMOVE'].format(location='F,', name=f'"{name}"')
else:
to_send = TSC_COMMANDS['REMOVE'].format(name=f'"{name}"')
prt.sendall(to_send+LINE_SEP)
prt.sendall(bytearray(to_send+LINE_SEP, encoding='utf-8'))
pass
def move_file(name=None):
#per default muove i file dalla RAM alla FLASH
nonlocal prt
to_send = TSC_COMMANDS['MOVE'].format(name=f'"{name}"')
prt.sendall(to_send+LINE_SEP)
prt.sendall(bytearray(to_send+LINE_SEP, encoding='utf-8'))
pass
def list_files():
nonlocal prt
prt.sendall(bytearray(TSC_COMMANDS['LIST']+LINE_SEP, encoding="ascii"))
time.sleep(0.1)
retval = read_until(b'\x1A').decode('utf-8').split('\r')
logger.info(f"Risposta: {retval}")
send_response(q_out, ip, "LIST",{"lista_file": retval})
pass
def printer_info():
nonlocal prt
pass
def printer_usage():
nonlocal prt
pass
def printer_pause():
nonlocal prt
prt.sendall(bytearray(b'\x1B'+TSC_COMMANDS['PAUSE']))
pass
def printer_resume():
nonlocal prt
prt.sendall(bytearray(b'\x1B'+TSC_COMMANDS['RESUME']))
pass
def clear_memory():
nonlocal prt
prt.sendall(bytearray(b'\x1B'+TSC_COMMANDS['CLEAR']))
pass
def run_batch(name=None):
nonlocal prt
to_send = TSC_COMMANDS['RUN'].format(name=f'"{name}"')
prt.sendall(to_send+LINE_SEP)
prt.sendall(bytearray(to_send+LINE_SEP, encoding='utf-8'))
pass
def get_status():
nonlocal prt
prt.sendall(0x1B+TSC_COMMANDS['STATUS'])
prt.sendall(bytearray(b'\x1B'+TSC_COMMANDS['STATUS']))
time.sleep(0.1)
return TSC_STATUS[prt.recv(1)]
return TSC_STATUS[int(prt.recv(1))]
while True: # connetti mantieni vivo il socket
@ -90,11 +113,22 @@ def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: s
move_file(**par)
pass
elif cmd == 'LIST':
get_status()
list_files()
pass
elif cmd == 'INFO':
pass
elif cmd == 'USE':
pass
elif cmd == 'PAUSE':
pass
elif cmd == 'RESUME':
pass
elif cmd == 'CLEAR':
pass
elif cmd == 'RUN':
pass
elif cmd == 'STATUS':
get_status()
pass
else:
logger.error("Comando stampante non riconosciuto")
@ -107,6 +141,7 @@ def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: s
prt.close()
del prt
except Exception as e:
traceback.print_exc()
logger.error("Errore generale inizializzando la stampante: {}".format(e))
pass