From 2c2848732e7de25aa08ff46b283bcf52049b96ea Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 27 Dec 2021 23:56:52 -0500 Subject: [PATCH] Add querying of octoprint servers for current job status --- .gitignore | 1 + Pipfile | 2 ++ Pipfile.lock | 67 +++++++++++++++++++++++++++++++++++++++++----- google_calendar.py | 2 +- main.py | 42 +++++++++++++++++++++++------ 5 files changed, 99 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 41dc91c..b155648 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /credentials.json /token.json +/printers.yaml diff --git a/Pipfile b/Pipfile index 082a946..2254c2d 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,8 @@ name = "pypi" google-api-python-client = "*" google-auth-httplib2 = "*" google-auth-oauthlib = "*" +octorest = "*" +"ruamel.yaml" = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index ac36ad7..5b6a3b5 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "2497f937c64121e013bf6c3d6f709f64a2c55889085b2656d11cbb2874e11e6f" + "sha256": "3a27d99fc413c54746d9a6a2bfd5e50a193a698cc4ee690b63f99ce76963a7ed" }, "pipfile-spec": 6, "requires": { @@ -111,6 +111,14 @@ "markers": "python_version >= '3.6'", "version": "==3.1.1" }, + "octorest": { + "hashes": [ + "sha256:6ce49bddfcd36ba0073556ef940f786edc69394e8779bc960d31969c60661a3b", + "sha256:7e70512cb0aaf3f576c4736e97e372e1497c0a97bfec64c0466826c6c27588d6" + ], + "index": "pypi", + "version": "==0.4" + }, "protobuf": { "hashes": [ "sha256:038daf4fa38a7e818dd61f51f22588d61755160a98db087a046f80d66b855942", @@ -182,7 +190,7 @@ "sha256:04ff808a5b90911829c55c4e26f75fa5ca8a2f5f36aa3a51f68e27033341d3e4", "sha256:d9bdec0013ef1eb5a84ab39a3b3868911598afa494f5faa038647101504e2b81" ], - "markers": "python_version > '3.0'", + "markers": "python_version >= '3.1'", "version": "==3.0.6" }, "requests": { @@ -209,20 +217,59 @@ "markers": "python_version >= '3.6'", "version": "==4.8" }, + "ruamel.yaml": { + "hashes": [ + "sha256:92ac00b312c9a83ff3253a8f7b86dfe6f9996b4082b103af84b8df99175945bc", + "sha256:b9ce9a925d0f0c35a1dbba56b40f253c53cd526b0fa81cf7b1d24996f28fb1d7" + ], + "index": "pypi", + "version": "==0.17.19" + }, + "ruamel.yaml.clib": { + "hashes": [ + "sha256:0847201b767447fc33b9c235780d3aa90357d20dd6108b92be544427bea197dd", + "sha256:1070ba9dd7f9370d0513d649420c3b362ac2d687fe78c6e888f5b12bf8bc7bee", + "sha256:1866cf2c284a03b9524a5cc00daca56d80057c5ce3cdc86a52020f4c720856f0", + "sha256:221eca6f35076c6ae472a531afa1c223b9c29377e62936f61bc8e6e8bdc5f9e7", + "sha256:31ea73e564a7b5fbbe8188ab8b334393e06d997914a4e184975348f204790277", + "sha256:3fb9575a5acd13031c57a62cc7823e5d2ff8bc3835ba4d94b921b4e6ee664104", + "sha256:4ff604ce439abb20794f05613c374759ce10e3595d1867764dd1ae675b85acbd", + "sha256:6e7be2c5bcb297f5b82fee9c665eb2eb7001d1050deaba8471842979293a80b0", + "sha256:72a2b8b2ff0a627496aad76f37a652bcef400fd861721744201ef1b45199ab78", + "sha256:77df077d32921ad46f34816a9a16e6356d8100374579bc35e15bab5d4e9377de", + "sha256:78988ed190206672da0f5d50c61afef8f67daa718d614377dcd5e3ed85ab4a99", + "sha256:7b2927e92feb51d830f531de4ccb11b320255ee95e791022555971c466af4527", + "sha256:7f7ecb53ae6848f959db6ae93bdff1740e651809780822270eab111500842a84", + "sha256:825d5fccef6da42f3c8eccd4281af399f21c02b32d98e113dbc631ea6a6ecbc7", + "sha256:846fc8336443106fe23f9b6d6b8c14a53d38cef9a375149d61f99d78782ea468", + "sha256:89221ec6d6026f8ae859c09b9718799fea22c0e8da8b766b0b2c9a9ba2db326b", + "sha256:9efef4aab5353387b07f6b22ace0867032b900d8e91674b5d8ea9150db5cae94", + "sha256:a32f8d81ea0c6173ab1b3da956869114cae53ba1e9f72374032e33ba3118c233", + "sha256:a49e0161897901d1ac9c4a79984b8410f450565bbad64dbfcbf76152743a0cdb", + "sha256:ada3f400d9923a190ea8b59c8f60680c4ef8a4b0dfae134d2f2ff68429adfab5", + "sha256:bf75d28fa071645c529b5474a550a44686821decebdd00e21127ef1fd566eabe", + "sha256:cfdb9389d888c5b74af297e51ce357b800dd844898af9d4a547ffc143fa56751", + "sha256:d67f273097c368265a7b81e152e07fb90ed395df6e552b9fa858c6d2c9f42502", + "sha256:dc6a613d6c74eef5a14a214d433d06291526145431c3b964f5e16529b1842bed", + "sha256:de9c6b8a1ba52919ae919f3ae96abb72b994dd0350226e28f3686cb4f142165c" + ], + "markers": "python_version < '3.11' and platform_python_implementation == 'CPython'", + "version": "==0.2.6" + }, "setuptools": { "hashes": [ - "sha256:8244876a37456ccbcbe1247b4cba1b015f4e42dfebfadd6a5488bfc12060f21b", - "sha256:dae4d7933ca671d51fa44c10b4ef8165319d9be3aa41807dd4c73f1c1ba81dc3" + "sha256:a4377723c53721515f72a3dfc1bfacdcd61edfa19a4cccf82e72e4f50d9cecbd", + "sha256:ad0ea3d172404abb14d8f7bd7f54f2ccd4ed9dd00c9da0b1398862e69eb22c03" ], "markers": "python_version >= '3.7'", - "version": "==60.0.4" + "version": "==60.1.0" }, "six": { "hashes": [ "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.16.0" }, "uritemplate": { @@ -240,6 +287,14 @@ ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'", "version": "==1.26.7" + }, + "websocket-client": { + "hashes": [ + "sha256:1315816c0acc508997eb3ae03b9d3ff619c9d12d544c9a9b553704b1cc4f6af5", + "sha256:2eed4cc58e4d65613ed6114af2f380f7910ff416fc8c46947f6e76b6815f56c0" + ], + "markers": "python_version >= '3.6'", + "version": "==1.2.3" } }, "develop": {} diff --git a/google_calendar.py b/google_calendar.py index aa2cde8..e7ab23f 100644 --- a/google_calendar.py +++ b/google_calendar.py @@ -66,7 +66,7 @@ def insert_or_update_event( except HttpError as error: # Create event if it doesn't exist - if error.status_code == 409: + if error.status_code == 404: service.events().insert(calendarId=CALENDAR_ID, body=event).execute() else: diff --git a/main.py b/main.py index 452c9ec..6b2c27b 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,45 @@ from datetime import datetime, timedelta +from octorest import OctoRest +from ruamel.yaml import YAML + import google_calendar +def get_printer_job(calendar, printer): + octoprint_client = OctoRest(url=printer["url"], apikey=printer["apikey"]) + current_job = octoprint_client.job_info() + 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) + event_name = f'{current_job["job"]["user"]} | Digital Fabrication and Electronics Lab - {printer["name"]}' + + google_calendar.insert_or_update_event( + calendar, + f'OctoPrint Reservations: {printer["name"]}', + event_name, + start, + estimated_end, + ) + + else: + print("No active job") + + def main(): creds = google_calendar.authenticate() - service = google_calendar.get_service(creds) + calendar = google_calendar.get_service(creds) - google_calendar.insert_or_update_event( - service, - "octoprint_test_event", - "Test!", - datetime.utcnow(), - datetime.utcnow() + timedelta(hours=1), - ) + yaml = YAML(typ="safe") + with open("printers.yaml") as f: + printers = yaml.load(f) + + for printer in printers: + get_printer_job(calendar, printer) if __name__ == "__main__":