2021-12-30 15:18:03 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-03-29 15:11:01 -04:00
|
|
|
import argparse
|
2021-12-27 14:31:26 -05:00
|
|
|
from datetime import datetime, timedelta
|
2022-03-29 15:11:01 -04:00
|
|
|
import pathlib
|
2021-12-27 14:21:48 -05:00
|
|
|
|
2021-12-27 23:56:52 -05:00
|
|
|
from octorest import OctoRest
|
|
|
|
from ruamel.yaml import YAML
|
|
|
|
|
2022-03-29 14:48:03 -04:00
|
|
|
from .google_calendar import CalendarService
|
2021-12-27 14:21:48 -05:00
|
|
|
|
|
|
|
|
2021-12-27 23:56:52 -05:00
|
|
|
def get_printer_job(calendar, printer):
|
2022-03-29 14:16:21 -04:00
|
|
|
try:
|
|
|
|
octoprint_client = OctoRest(url=printer["url"], apikey=printer["apikey"])
|
|
|
|
current_job = octoprint_client.job_info()
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Failed to get current job for {printer['name']}: {e}")
|
|
|
|
return
|
|
|
|
|
2022-04-02 18:58:21 -04:00
|
|
|
if (
|
|
|
|
current_job["state"] == "Printing"
|
|
|
|
and current_job["progress"]["printTimeLeft"] is not None
|
|
|
|
):
|
2021-12-27 23:56:52 -05:00
|
|
|
now = datetime.utcnow()
|
|
|
|
start = now - timedelta(seconds=current_job["progress"]["printTime"])
|
|
|
|
estimated_end = now + timedelta(
|
|
|
|
seconds=current_job["progress"]["printTimeLeft"]
|
|
|
|
)
|
|
|
|
print(current_job["job"]["file"]["name"], start, estimated_end)
|
2021-12-28 00:04:08 -05:00
|
|
|
event_name = (
|
|
|
|
f'{current_job["job"]["user"]} | {printer["shop"]} - {printer["name"]}'
|
|
|
|
)
|
2021-12-27 23:56:52 -05:00
|
|
|
|
2021-12-28 00:01:05 -05:00
|
|
|
calendar.insert_or_update_event(
|
2021-12-28 00:04:08 -05:00
|
|
|
printer["calendar_id"],
|
2021-12-27 23:56:52 -05:00
|
|
|
f'OctoPrint Reservations: {printer["name"]}',
|
|
|
|
event_name,
|
|
|
|
start,
|
|
|
|
estimated_end,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-27 14:21:48 -05:00
|
|
|
def main():
|
2022-03-29 15:11:01 -04:00
|
|
|
parser = argparse.ArgumentParser(description="Process some integers.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--config",
|
|
|
|
type=pathlib.Path,
|
|
|
|
default="printers.yaml",
|
|
|
|
help="A yaml file defining printers to query (default: %(default)s)",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-12-28 00:01:05 -05:00
|
|
|
calendar = CalendarService()
|
2021-12-27 23:56:52 -05:00
|
|
|
|
|
|
|
yaml = YAML(typ="safe")
|
2022-03-29 15:11:01 -04:00
|
|
|
with open(args.config) as f:
|
2021-12-27 23:56:52 -05:00
|
|
|
printers = yaml.load(f)
|
|
|
|
|
|
|
|
for printer in printers:
|
|
|
|
get_printer_job(calendar, printer)
|
2021-12-27 14:21:48 -05:00
|
|
|
|
|
|
|
|
2021-12-27 14:31:26 -05:00
|
|
|
if __name__ == "__main__":
|
2021-12-27 14:21:48 -05:00
|
|
|
main()
|