2021-12-30 15:18:03 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-12-27 14:31:26 -05:00
|
|
|
from datetime import datetime, timedelta
|
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
|
|
|
|
|
2021-12-27 23:56:52 -05:00
|
|
|
if current_job["state"] == "Printing":
|
|
|
|
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():
|
2021-12-28 00:01:05 -05:00
|
|
|
calendar = CalendarService()
|
2021-12-27 23:56:52 -05:00
|
|
|
|
|
|
|
yaml = YAML(typ="safe")
|
|
|
|
with open("printers.yaml") as f:
|
|
|
|
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()
|