diff --git a/printer/mock_printer.py b/printer/mock_printer.py new file mode 100644 index 00000000..68443ee5 --- /dev/null +++ b/printer/mock_printer.py @@ -0,0 +1,52 @@ +import time +import logging + +class MockPrinter(): + _instance = None + _current_print_id_num = 0 + _current_time_left = 10 + _jobs = {} + _queue = [] + + @classmethod + def instance(cls): + if cls._instance is None: + cls._instance = cls.__new__(cls) + return cls._instance + + def get_job_status(self, id: str) -> str: + if id in self._jobs: + return self._jobs[id] + else: + return "PRINTED" + + def remove_job(self, id: str) -> None: + if id in self._jobs: + self._jobs.pop(id) + + def lp(self) -> str: + print_id = "HP_LaserJet_p2015dn_Right-" + str(self._current_print_id_num) + self._current_print_id_num += 1 + self._jobs[print_id] = "PENDING" + self._queue.append(print_id) + return print_id + + def update(self) -> None: + while True: + time.sleep(1) + + if self._queue.__len__() == 0: + continue + + if (self._current_time_left > 0): + self._current_time_left -= 1 + continue + + self._jobs[self._queue[0]] = "PRINTED" + self._queue.pop(0) + self._current_time_left = 10 + + def log(self) -> None: + logging.info("-----------------") + logging.info(f"jobs: {self._jobs}") + logging.info(f"queue: {self._queue}") diff --git a/printer/server.py b/printer/server.py index 17838312..431b37e6 100644 --- a/printer/server.py +++ b/printer/server.py @@ -8,16 +8,18 @@ import uuid import collector -from fastapi import FastAPI, File, Form, HTTPException, UploadFile +from fastapi import FastAPI, File, Form, HTTPException, UploadFile, Body from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import PlainTextResponse import prometheus_client import uvicorn from metrics import MetricsHandler +from mock_printer import MockPrinter metrics_handler = MetricsHandler.instance() +mock_printer = MockPrinter.instance() app = FastAPI() app.add_middleware( @@ -117,7 +119,7 @@ def send_file_to_printer( logging.warning( f"server is in development mode, command would've been `{command}`" ) - return None + return mock_printer.lp() print_job = subprocess.Popen( command, @@ -173,9 +175,9 @@ async def read_item( """ incoming request to print looks like { - "file": file data - "copies": integer or whatever, we insert this into the lp command, - "sides": string value from user input on clark frontend; we insert this into the lp command, + "file": file data + "copies": integer or whatever, we insert this into the lp command, + "sides": string value from user input on clark frontend; we insert this into the lp command, } """ try: @@ -202,6 +204,17 @@ async def read_item( detail="printing failed, check logs", ) +@app.get("/status/") +async def status(id: str = ''): + logging.info(id) + if args.development: + mock_printer.log() + status = mock_printer.get_job_status(id) + + if (status == "PRINTED"): + mock_printer.remove_job(id) + + return {"status": status} # we have a separate __name__ check here due to how FastAPI starts # a server. the file is first ran (where __name__ == "__main__") @@ -212,6 +225,13 @@ async def read_item( # the thread interacts with an instance different than the one the # server uses if __name__ == "server": + if args.development: + mock_printer_upd_thread = threading.Thread( + target=mock_printer.update, + daemon=True + ) + mock_printer_upd_thread.start() + if not args.development: # set the last time we opened an ssh tunnel to now because # when the script runs for the first time, we did so in what.sh