diff --git a/projects/octodash/BUILD b/projects/octodash/BUILD index 4e71cdb..76aff4c 100644 --- a/projects/octodash/BUILD +++ b/projects/octodash/BUILD @@ -5,5 +5,6 @@ zapp_binary( deps = [ py_requirement("colored"), py_requirement("octorest"), + py_requirement("pyyaml"), ], ) diff --git a/projects/octodash/__main__.py b/projects/octodash/__main__.py index 0ba4694..89014c7 100644 --- a/projects/octodash/__main__.py +++ b/projects/octodash/__main__.py @@ -2,12 +2,14 @@ from configparser import ConfigParser import curses +from datetime import timedelta +from itertools import count import os -from time import sleep import signal +from time import sleep -from colored import fg, bg, attr from octorest.client import OctoRest +import yaml def draw(screen, client): @@ -17,65 +19,78 @@ def draw(screen, client): rows, cols = screen.getmaxyx() # Poll the API - job = client.job_info() - # >>> client.job_info() - # {'job': {'averagePrintTime': 7965.021392323004, - # 'estimatedPrintTime': 6132.310772608108, - # 'filament': {'tool0': {'length': 8504.781600002587, - # 'volume': 20.456397036761484}}, - # 'file': {'date': 1638666604, - # 'display': 'v2-sides.gcode', - # 'name': 'v2-sides.gcode', - # 'origin': 'local', - # 'path': 'v2-sides.gcode', - # 'size': 1074906}, - # 'lastPrintTime': 7965.021392323004, - # 'user': '_api'}, - # 'progress': {'completion': 100.0, - # 'filepos': 1074906, - # 'printTime': 7965, - # 'printTimeLeft': 0, - # 'printTimeLeftOrigin': None}, - # 'state': 'Operational'} - printer = client.printer() - # >>> client.printer() - # {'sd': {'ready': False}, - # 'state': {'error': '', - # 'flags': {'cancelling': False, - # 'closedOrError': False, - # 'error': False, - # 'finishing': False, - # 'operational': True, - # 'paused': False, - # 'pausing': False, - # 'printing': False, - # 'ready': True, - # 'resuming': False, - # 'sdReady': False}, - # 'text': 'Operational'}, - # 'temperature': {'bed': {'actual': 23.05, 'offset': 0, 'target': 0.0}, - # 'tool0': {'actual': 23.71, 'offset': 0, 'target': 0.0}}} - # Draw a screen + try: + job = client.job_info() + # >>> client.job_info() + # {'job': {'averagePrintTime': 7965.021392323004, + # 'estimatedPrintTime': 6132.310772608108, + # 'filament': {'tool0': {'length': 8504.781600002587, + # 'volume': 20.456397036761484}}, + # 'file': {'date': 1638666604, + # 'display': 'v2-sides.gcode', + # 'name': 'v2-sides.gcode', + # 'origin': 'local', + # 'path': 'v2-sides.gcode', + # 'size': 1074906}, + # 'lastPrintTime': 7965.021392323004, + # 'user': '_api'}, + # 'progress': {'completion': 100.0, + # 'filepos': 1074906, + # 'printTime': 7965, + # 'printTimeLeft': 0, + # 'printTimeLeftOrigin': None}, + # 'state': 'Operational'} - flags = printer["state"]["flags"] - ready = not flags["error"] and flags["operational"] and flags["ready"] - printing = flags["printing"] + printer = client.printer() + # >>> client.printer() + # {'sd': {'ready': False}, + # 'state': {'error': '', + # 'flags': {'cancelling': False, + # 'closedOrError': False, + # 'error': False, + # 'finishing': False, + # 'operational': True, + # 'paused': False, + # 'pausing': False, + # 'printing': False, + # 'ready': True, + # 'resuming': False, + # 'sdReady': False}, + # 'text': 'Operational'}, + # 'temperature': {'bed': {'actual': 23.05, 'offset': 0, 'target': 0.0}, + # 'tool0': {'actual': 23.71, 'offset': 0, 'target': 0.0}}} - file = job["job"]["file"] - progress = job["progress"] - completion = progress["completion"] / 100.0 - time_remaining = progress["printTimeLeft"] + # Draw a screen - progress_cols = int(cols * completion) - progress_line = ("#" * progress_cols) + ("-" * (cols - progress_cols)) + flags = printer["state"]["flags"] + ready = not flags["error"] and flags["operational"] and flags["ready"] + printing = flags["printing"] - screen.addstr(0, 0, f"Ready: {ready}") - if printing: - screen.addstr(2, 0, f"Printing: {file['name']}") - screen.addstr(3, 0, f"Remaining print time: {time_remaining}") + file = job["job"]["file"] + progress = job["progress"] + completion = progress["completion"] / 100.0 + time_remaining = timedelta(seconds=progress["printTimeLeft"]) + + progress_cols = int(cols * completion) + progress_line = ("#" * progress_cols) + ("-" * (cols - progress_cols)) + + screen.addstr(0, 0, f"Ready: {ready}") + if printing: + screen.addstr(2, 0, f"Printing: {file['name']}") + screen.addstr(3, 0, f"Remaining print time: {time_remaining}") + + screen.addstr(5, 0, progress_line) + + for i, l in zip(count(7), yaml.dump({"job": job, "printer": printer}).splitlines()): + if i >= rows: + break + else: + screen.addstr(i, 0, l) + + except Exception as e: + screen.addstr(str(e)) - screen.addstr(5, 0, progress_line) if __name__ == "__main__": config = ConfigParser() @@ -92,11 +107,15 @@ if __name__ == "__main__": signal.signal(signal.SIGTERM, kbdint) - while True: - try: - screen.clear() - draw(screen, client) - screen.refresh() - sleep(1) - except KeyboardInterrupt: - break + try: + while True: + try: + screen.clear() + draw(screen, client) + screen.refresh() + sleep(1) + except KeyboardInterrupt: + break + + finally: + curses.endwin()