Roll over API exceptions

This commit is contained in:
Reid 'arrdem' McKenzie 2021-12-27 00:49:17 -07:00
parent 355ec6974f
commit b5bb534664
2 changed files with 83 additions and 63 deletions

View file

@ -5,5 +5,6 @@ zapp_binary(
deps = [ deps = [
py_requirement("colored"), py_requirement("colored"),
py_requirement("octorest"), py_requirement("octorest"),
py_requirement("pyyaml"),
], ],
) )

View file

@ -2,12 +2,14 @@
from configparser import ConfigParser from configparser import ConfigParser
import curses import curses
from datetime import timedelta
from itertools import count
import os import os
from time import sleep
import signal import signal
from time import sleep
from colored import fg, bg, attr
from octorest.client import OctoRest from octorest.client import OctoRest
import yaml
def draw(screen, client): def draw(screen, client):
@ -17,65 +19,78 @@ def draw(screen, client):
rows, cols = screen.getmaxyx() rows, cols = screen.getmaxyx()
# Poll the API # 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"] printer = client.printer()
ready = not flags["error"] and flags["operational"] and flags["ready"] # >>> client.printer()
printing = flags["printing"] # {'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"] # Draw a screen
progress = job["progress"]
completion = progress["completion"] / 100.0
time_remaining = progress["printTimeLeft"]
progress_cols = int(cols * completion) flags = printer["state"]["flags"]
progress_line = ("#" * progress_cols) + ("-" * (cols - progress_cols)) ready = not flags["error"] and flags["operational"] and flags["ready"]
printing = flags["printing"]
screen.addstr(0, 0, f"Ready: {ready}") file = job["job"]["file"]
if printing: progress = job["progress"]
screen.addstr(2, 0, f"Printing: {file['name']}") completion = progress["completion"] / 100.0
screen.addstr(3, 0, f"Remaining print time: {time_remaining}") 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__": if __name__ == "__main__":
config = ConfigParser() config = ConfigParser()
@ -92,11 +107,15 @@ if __name__ == "__main__":
signal.signal(signal.SIGTERM, kbdint) signal.signal(signal.SIGTERM, kbdint)
while True: try:
try: while True:
screen.clear() try:
draw(screen, client) screen.clear()
screen.refresh() draw(screen, client)
sleep(1) screen.refresh()
except KeyboardInterrupt: sleep(1)
break except KeyboardInterrupt:
break
finally:
curses.endwin()