diff --git a/comm/main.py b/comm/main.py index 6a30106..d240481 100644 --- a/comm/main.py +++ b/comm/main.py @@ -2,4 +2,71 @@ Created on 19 mar 2020 @author: Emanuele Trabattoni +@copyright: Briq srl +@contact: Scritto per Briq srl come collaboratore esterno, contiene dei frammenti di codice di proprieta' di Briq srl ''' + +from flask import Flask, escape, request +import threading +import multiprocessing +import sys +import logging +from waitress import serve +import os + +app = Flask(__name__) + +#questo manda le rest di risposta a django +@app.route('/markoprinter//', methods=['POST']) +def hello(mac, command): + parameters = request.get_json() + print(mac, command, parameters) + queue_id = printer_manager.get_queue_id(mac) + print(mac) + print(printer_manager.mappa_mac) + if queue_id is not None and queue_id in queues_in: + print("trovata") + queues_in[queue_id].put({'name': command, 'parameters': parameters}) + return "Inviato", 200 + else: + print("non trovata") + return "Stampante non trovata", 404 + + +def build_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) + return logger + + +if __name__ == '__main__': + logger = build_logger() + queues_in = {} + queue_out = multiprocessing.Queue() + django_addr = ("http://{}:{}/" + .format(os.getenv('DJANGO_HOST', '127.0.0.1'), + os.getenv('DJANGO_PORT'))) + t_notify_django = threading.Thread(target=notify_django.notifier, + args=[django_addr, queue_out, logger], + daemon=True) + t_notify_django.start() + t_print = threading.Thread(target=printer_manager.main, + args=[queues_in, queue_out, logger], + daemon=True) + t_print.start() + listen = "{}:{}".format(os.getenv("MARKOPRINTER_DRIVER_HOST"), + os.getenv("MARKOPRINTER_DRIVER_PORT")) + serve(app, listen=listen) \ No newline at end of file