Last active
May 27, 2024 17:33
-
-
Save deckar01/7099fc92865c6173d8c9e04bd32015d5 to your computer and use it in GitHub Desktop.
Send push notifications when a Prusa printer needs attention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import requests | |
import time | |
from tqdm import tqdm | |
class PrintMonitor: | |
# !!! ADD CONFIGURATION !!! # | |
# See https://help.prusa3d.com/guide/wi-fi-and-prusalink-setup-xl-firmware-up-to-4-7-5_419630 | |
printer = '10.0.0.1' | |
printer_user = 'maker' | |
printer_password = 'XXXXXXXXXXXXXXX' | |
# See https://pushover.net/api | |
pushover_token = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' | |
pushover_user = 'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu' | |
priority = {'priority': 2, 'retry': 30, 'expire': 600} | |
# !!! ENJOY ;) !!! # | |
EVENTS = ['PAUSED', 'FINISHED'] | |
progress = 0 | |
timer = None | |
bar = {'total': 100, 'bar_format': '{l_bar}{bar}|'} | |
last_notification = None | |
def update(self): | |
printer = f'http://{self.printer}/api/v1/status' | |
printer_auth = requests.auth.HTTPDigestAuth(self.printer_user, self.printer_password) | |
response = requests.get(printer, auth=printer_auth) | |
if not response.ok: | |
print(response, response.text) | |
return | |
status = response.json() | |
self.state = status['printer']['state'] | |
if self.state in self.EVENTS and self.last_notification != self.progress: | |
self.last_notification = self.progress | |
self.notify(self.state) | |
if job := status.get('job', None): | |
self.progress = job['progress'] | |
change_in = job.get('filament_change_in', None) | |
done_in = job.get('time_remaining', None) | |
if change_in: | |
self.state += f' (change in {datetime.timedelta(seconds=change_in)})' | |
elif done_in: | |
self.state += f' (done in {datetime.timedelta(seconds=done_in)})' | |
def notify(self, message): | |
phone = 'https://api.pushover.net/1/messages.json' | |
auth = {'user': self.pushover_user, 'token': self.pushover_token} | |
data = {'message': message} | |
response = requests.post(phone, json={**auth, **data, **self.priority}) | |
print(message, response, response.json()) | |
def start(self): | |
while True: | |
last_tick = self.progress | |
self.update() | |
if self.progress < last_tick or self.timer is None: | |
self.timer = tqdm(desc=self.state, initial=self.progress, **self.bar) | |
last_tick = self.progress | |
self.timer.set_description(self.state) | |
if self.progress > last_tick: | |
self.timer.update(self.progress - last_tick) | |
# TODO: Use job.filament_change_in to sleep longer? | |
time.sleep(10) | |
if __name__ == '__main__': | |
PrintMonitor().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment