118 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # from os import environ
 | |
| import argparse
 | |
| from flask import Flask, request, make_response, send_from_directory, jsonify
 | |
| from config import PrinterConfig
 | |
| 
 | |
| argument_parser = argparse.ArgumentParser()
 | |
| argument_parser.add_argument("--address", type=str, required=False, default="0.0.0.0")
 | |
| argument_parser.add_argument("--port", type=int, required=False, default=8005)
 | |
| 
 | |
| args = argument_parser.parse_args()
 | |
| 
 | |
| # todo read parameters from env
 | |
| 
 | |
| printer_config = PrinterConfig()
 | |
| app = Flask(__name__)
 | |
| 
 | |
| 
 | |
| @app.route("/")
 | |
| def index_route():
 | |
|     return send_from_directory("static", "index.html")
 | |
| 
 | |
| 
 | |
| @app.route("/config", methods=["GET", "POST"])
 | |
| def config_route():
 | |
|     json_request = request.args.get("json", False)
 | |
|     print(f"{json_request=}")
 | |
|     if not json_request and json_request != "":
 | |
|         json_request = False
 | |
|     elif json_request == "":
 | |
|         json_request = True
 | |
| 
 | |
|     if request.method == "GET" and not json_request:
 | |
|         # return config html page
 | |
|         return send_from_directory("static", "config.html")
 | |
| 
 | |
|     elif request.method == "GET" and json_request:
 | |
|         # render config as JSON
 | |
|         print(get_config())
 | |
|         return jsonify(get_config())
 | |
| 
 | |
|     elif request.method == "POST" and request.is_json:
 | |
|         # accept JSON form with new parameters
 | |
|         errors = {"critical": False, "errors": {}}
 | |
|         try:
 | |
|             request_data = request.json
 | |
|             print(request_data)
 | |
|         except Exception:
 | |
|             errors["errors"]["json"] = "Could not decode message as json"
 | |
|             errors["critical"] = True
 | |
| 
 | |
|         request_params = {}
 | |
|         if "address" in request_data:
 | |
|             request_params["address"] = request_data["address"]
 | |
|         if "port" in request_data:
 | |
|             port = request_data["port"]
 | |
|             # todo remove redundant checks
 | |
|             if isinstance(port, int) and 0 < port <= 65535:
 | |
|                 request_params["port"] = port
 | |
|             else:
 | |
|                 errors["errors"][
 | |
|                     "parseint"
 | |
|                 ] = "Could not parse port number as an integer"
 | |
|                 errors["critical"] = True
 | |
| 
 | |
|         response = jsonify(errors)
 | |
|         if errors["critical"]:
 | |
|             response.status_code = 422
 | |
|         else:
 | |
|             response.status_code = 200
 | |
|             set_config(**request_params)
 | |
|         return response
 | |
| 
 | |
| 
 | |
| def get_config():
 | |
|     return {
 | |
|         "address": printer_config.address,
 | |
|         "port": printer_config.port,
 | |
|         "profile": printer_config.profile,
 | |
|     }
 | |
| 
 | |
| 
 | |
| def set_config(address: str = None, port: int = None):
 | |
|     printer_config.address = address
 | |
|     printer_config.port = port
 | |
|     # printer_config.profile = profile  # fixed default in config.py
 | |
|     printer_config.save()
 | |
| 
 | |
| 
 | |
| def can_print():
 | |
|     return printer_config.complete
 | |
| 
 | |
| 
 | |
| @app.route("/status", methods=["GET"])
 | |
| def printer_status_route():
 | |
|     return jsonify(
 | |
|         {
 | |
|             "configured": can_print(),
 | |
|             "connected": printer_config.printer.test_connection(),
 | |
|         }
 | |
|     )
 | |
| 
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     try:
 | |
|         printer_config.load()
 | |
|     except FileNotFoundError:
 | |
|         printer_config.address = "localhost"
 | |
|         printer_config.port = 9100
 | |
|         # printer_config.profile = ""
 | |
| 
 | |
|     try:
 | |
|         app.run(host=args.address, port=args.port, threaded=False)
 | |
|     except KeyboardInterrupt:
 | |
|         pass
 | |
| 
 | |
|     printer_config.save()
 | |
|     exit(0)
 |