Compare commits

..

3 commits

2 changed files with 36 additions and 5 deletions

View file

@ -157,6 +157,10 @@ CREATE TABLE IF NOT EXISTS email_spool (
, FOREIGN KEY(user_id) REFERENCES users(id) , FOREIGN KEY(user_id) REFERENCES users(id)
); );
-- name: migration-0002-create-occupied-state
-- Create a state representing that the printer needs to be unloaded after a print
INSERT OR IGNORE INTO printer_statuses (id, name) VALUES (5, 'occupied');
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
-- Users -- Users
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------

View file

@ -8,6 +8,7 @@ Mostly related to monitoring and managing Printer state.
""" """
from contextlib import closing from contextlib import closing
from functools import cache
import logging import logging
from pathlib import Path from pathlib import Path
from pprint import pformat from pprint import pformat
@ -49,6 +50,11 @@ class OctoRest(_OR):
raise e raise e
@cache
def get_client(url, key):
return OctoRest(url=url, apikey=key)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -63,9 +69,27 @@ def poll_printers(app: App, db: Db) -> None:
log.info(f"Printer {printer.id} {printer.status} -> {status}") log.info(f"Printer {printer.id} {printer.status} -> {status}")
db.update_printer_status(pid=printer.id, status=status) db.update_printer_status(pid=printer.id, status=status)
def _bed_clear():
if not (
snapshots := client._post(
"/api/plugin/bedready", json={"command": "list_snapshots"}
)
):
return True # Assume the bed is ready
status = client._post(
"/api/plugin/bedready",
json={
"command": "check_bed",
"similarity": 0.97,
"reference": snapshots[0],
},
)
return status.get("bed_clear", True)
printer_job = {} printer_job = {}
try: try:
client = OctoRest(url=printer.url, apikey=printer.api_key) client = get_client(printer.url, printer.api_key)
printer_job: dict = client.job_info() printer_job: dict = client.job_info()
try: try:
printer_state: dict = client.printer().get("state").get("flags", {}) printer_state: dict = client.printer().get("state").get("flags", {})
@ -92,7 +116,10 @@ def poll_printers(app: App, db: Db) -> None:
_set_status("connecting") _set_status("connecting")
elif printer_state.get("ready"): elif printer_state.get("ready"):
if _bed_clear():
_set_status("idle") _set_status("idle")
else:
_set_status("occupied")
else: else:
raise Exception( raise Exception(
@ -136,7 +163,7 @@ def push_jobs(app: App, db: Db) -> None:
db.delete_job(job.user_id, job.id) db.delete_job(job.user_id, job.id)
try: try:
client = OctoRest(url=printer.url, apikey=printer.api_key) client = get_client(printer.url, printer.api_key)
printer_job = client.job_info() printer_job = client.job_info()
try: try:
printer_state = client.printer().get("state").get("flags", {}) printer_state = client.printer().get("state").get("flags", {})
@ -182,7 +209,7 @@ def revoke_jobs(app: App, db: Db) -> None:
try: try:
log.info(f"Cancelling running job {job.id}") log.info(f"Cancelling running job {job.id}")
client = OctoRest(url=printer.url, apikey=printer.api_key) client = get_client(printer.url, printer.api_key)
try: try:
client.cancel() client.cancel()
except HTTPError as e: except HTTPError as e:
@ -211,7 +238,7 @@ def pull_jobs(app: App, db: Db) -> None:
for job in db.list_running_jobs(): for job in db.list_running_jobs():
printer = db.fetch_printer(pid=job.printer_id) printer = db.fetch_printer(pid=job.printer_id)
try: try:
client = OctoRest(url=printer.url, apikey=printer.api_key) client = get_client(printer.url, printer.api_key)
job_state = client.job_info() job_state = client.job_info()
try: try:
printer_state = client.printer().get("state").get("flags", {}) printer_state = client.printer().get("state").get("flags", {})