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 = [
py_requirement("colored"),
py_requirement("octorest"),
py_requirement("pyyaml"),
],
)

View file

@ -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,6 +19,8 @@ def draw(screen, client):
rows, cols = screen.getmaxyx()
# Poll the API
try:
job = client.job_info()
# >>> client.job_info()
# {'job': {'averagePrintTime': 7965.021392323004,
@ -37,6 +41,7 @@ def draw(screen, client):
# 'printTimeLeft': 0,
# 'printTimeLeftOrigin': None},
# 'state': 'Operational'}
printer = client.printer()
# >>> client.printer()
# {'sd': {'ready': False},
@ -65,7 +70,7 @@ def draw(screen, client):
file = job["job"]["file"]
progress = job["progress"]
completion = progress["completion"] / 100.0
time_remaining = progress["printTimeLeft"]
time_remaining = timedelta(seconds=progress["printTimeLeft"])
progress_cols = int(cols * completion)
progress_line = ("#" * progress_cols) + ("-" * (cols - progress_cols))
@ -77,6 +82,16 @@ def draw(screen, client):
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))
if __name__ == "__main__":
config = ConfigParser()
config.read(os.path.expanduser("~/.config/octoprint-cli.ini"))
@ -92,6 +107,7 @@ if __name__ == "__main__":
signal.signal(signal.SIGTERM, kbdint)
try:
while True:
try:
screen.clear()
@ -100,3 +116,6 @@ if __name__ == "__main__":
sleep(1)
except KeyboardInterrupt:
break
finally:
curses.endwin()