From ee4fecc8938b13f7bbb6d3fe7bfdfc35d47f25c9 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Thu, 19 Mar 2020 15:23:20 +0100 Subject: [PATCH] aggiungendo la logica.. --- driver/cose.json | 11 +++++++++ driver/tsc_commands.py | 5 ++++ driver/tsc_manager.py | 38 ++++++++++++++++++++++-------- driver/tsc_printer.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 driver/cose.json create mode 100644 driver/tsc_commands.py create mode 100644 driver/tsc_printer.py diff --git a/driver/cose.json b/driver/cose.json new file mode 100644 index 0000000..6299184 --- /dev/null +++ b/driver/cose.json @@ -0,0 +1,11 @@ +{ + "ip1": { + "queue_id": "queueObject", + "thr": "threadObject", + "port": "int" + }, + "ip2": { + "queue_id": "queueObject", + "thr": "threadObject", + "port": "int" +} diff --git a/driver/tsc_commands.py b/driver/tsc_commands.py new file mode 100644 index 0000000..6a30106 --- /dev/null +++ b/driver/tsc_commands.py @@ -0,0 +1,5 @@ +''' +Created on 19 mar 2020 + +@author: Emanuele Trabattoni +''' diff --git a/driver/tsc_manager.py b/driver/tsc_manager.py index fad79d8..91490e7 100644 --- a/driver/tsc_manager.py +++ b/driver/tsc_manager.py @@ -6,22 +6,22 @@ Created on 19 mar 2020 @contact: Scritto per Briq srl come collaboratore esterno, contiene dei frammenti di codice di proprieta' di Briq srl ''' -import threading, socket, sys, queue, logging +import threading, sys, queue, logging +from driver.tsc_printer import tsc_printer Q_TIMEOUT = 2 URL_ERR = 'tscprinter/errore/' URL_EVENT = 'tscprinter/evento/' -def stampante(q_in, q_out, ip, ): - pass - - #lancia i thread, uno per stampante def main(q_in :queue.Queue, q_out :queue.Queue, q_comm: queue.Queue, logger: logging.getLogger): - queue_ip_map={} + printer_map={} def get_queue_id(ip): - return True + if ip in printer_map.keys(): + return printer_map[str(ip)]['queue_id'] + else: + return None def get_queue_command(): return q_comm @@ -29,16 +29,34 @@ def main(q_in :queue.Queue, q_out :queue.Queue, q_comm: queue.Queue, logger: log while True: try: comando = q_comm.get(block=True, timeout=Q_TIMEOUT) - if comando['name'] == "CONNETTI": + cmd = comando['name'] + par = comando['parameters'] + + if cmd == "CONNETTI": + printer_map[par['ip']] = {} + # genera nuove code in ingresso per la nuova stampante + q_in_prt = queue.Queue() + q_cmd_prt = queue.Queue() + printer_map[par['ip']]['queue_id'] = q_in_prt # aggiungi la nuova coda al dict delle code + printer_map[par['ip']]['port'] = par['port'] + # lancia un thread stampante con la nuova coda ingresso + t_prt = threading.Thread(name = f"Stampante {par['ip']}:{par['port']}", + target = tsc_printer, + args = [q_in_prt, q_out, q_cmd_prt, par['ip'], par['port'], logger], + daemon = True) + printer_map[par['ip']]['thr'] = t_prt # aggiungi il nuovo thread al dict dei thread + t_prt.start() pass + elif comando['name'] == "DISCONNETTI": pass + else: q_out.put({'url':URL_ERR, 'msg':{'error':'comando inesistente'} }) logger.error("Comando non Trovato") - except queue.Empty: - logger.error("Coda comandi vuota") + except (InterruptedError, KeyboardInterrupt): + logger.critical("sto quittando") pass diff --git a/driver/tsc_printer.py b/driver/tsc_printer.py new file mode 100644 index 0000000..f253a4d --- /dev/null +++ b/driver/tsc_printer.py @@ -0,0 +1,52 @@ +''' +Created on 19 mar 2020 + +@author: Emanuele Trabattoni +''' + +import queue, sys, time, socket, logging + +SOCK_TIMEOUT = 2 + +def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: str, port: int, logger: logging.getLogger): + + def riconnetti_socket(): + pass + + def invia_file(): + pass + + def elimina_file(): + pass + + def muovi_file(): + pass + + while True: # connetti mantieni vivo il socket + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as prt: + prt.create_connection(address=(ip, port), timeout=SOCK_TIMEOUT) + while True: + itm = q_in.get(block=True) + cmd = itm['name'] + par = itm['parameters'] + if cmd == 'DOWNLOAD': + pass + elif cmd == 'REMOVE': + pass + elif cmd == 'MOVE': + pass + elif cmd == 'LIST': + pass + elif cmd == 'RUN': + pass + else: + logger.error("Comando stampante non riconosciuto") + except socket.timeout as st: + logger.error(f"Stampante {__name__} irraggiungibile:{st}") + prt.close() + del prt + except Exception as e: + logger.error("Errore generale inizializzando la stampante: {}".format(e)) + + pass \ No newline at end of file