2020-05-02 18:56:53 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the Octorpint status and the printer's bed/tools temperature in the status bar.
|
|
|
|
|
|
|
|
Left click opens a popup which shows the bed & tools temperatures and additionally a livestream of the webcam (if enabled).
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* octoprint.address : Octoprint address (e.q: http://192.168.1.3)
|
|
|
|
* octoprint.apitoken : Octorpint API Token (can be obtained from the Octoprint Webinterface)
|
|
|
|
* octoprint.webcam : Set to True if a webcam is connected (default: False)
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import urllib
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import queue
|
|
|
|
|
|
|
|
import tkinter as tk
|
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import simplejson
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
def get_frame(url):
|
2020-05-03 11:15:52 +02:00
|
|
|
img_bytes = b""
|
|
|
|
stream = urllib.request.urlopen(url)
|
2020-05-02 18:56:53 +02:00
|
|
|
while True:
|
|
|
|
img_bytes += stream.read(1024)
|
2020-05-03 11:15:52 +02:00
|
|
|
a = img_bytes.find(b"\xff\xd8")
|
|
|
|
b = img_bytes.find(b"\xff\xd9")
|
2020-05-02 18:56:53 +02:00
|
|
|
if a != -1 and b != -1:
|
2020-05-03 11:15:52 +02:00
|
|
|
jpg = img_bytes[a : b + 2]
|
|
|
|
img_bytes = img_bytes[b + 2 :]
|
2020-05-02 18:56:53 +02:00
|
|
|
img = Image.open(BytesIO(jpg))
|
|
|
|
return img
|
|
|
|
return None
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
class WebcamImagesWorker(threading.Thread):
|
|
|
|
def __init__(self, url, queue):
|
|
|
|
threading.Thread.__init__(self)
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
self.__url = url
|
|
|
|
self.__queue = queue
|
|
|
|
self.__running = True
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
def run(self):
|
|
|
|
while self.__running:
|
|
|
|
img = get_frame(self.__url)
|
|
|
|
self.__queue.put(img)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.__running = False
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
class Module(core.module.Module):
|
2020-05-02 19:03:17 +02:00
|
|
|
@core.decorators.every(seconds=5)
|
2020-05-02 18:56:53 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.octoprint_status))
|
|
|
|
|
|
|
|
self.__octoprint_state = "Unknown"
|
|
|
|
self.__octoprint_address = self.parameter("address", "")
|
|
|
|
self.__octoprint_api_token = self.parameter("apitoken", "")
|
|
|
|
self.__octoprint_webcam = self.parameter("webcam", False)
|
|
|
|
|
|
|
|
self.__webcam_images_worker = None
|
|
|
|
self.__webcam_image_url = self.__octoprint_address + "/webcam/?action=stream"
|
|
|
|
self.__webcam_images_queue = None
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
self.__printer_bed_temperature = "-"
|
|
|
|
self.__tool1_temperature = "-"
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
self.update_status()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__show_popup)
|
2020-05-02 18:56:53 +02:00
|
|
|
|
|
|
|
def octoprint_status(self, widget):
|
|
|
|
if self.__octoprint_state == "Offline" or self.__octoprint_state == "Unknown":
|
|
|
|
return self.__octoprint_state
|
2020-05-03 11:15:52 +02:00
|
|
|
return (
|
|
|
|
self.__octoprint_state
|
|
|
|
+ " | B: "
|
|
|
|
+ str(self.__printer_bed_temperature)
|
|
|
|
+ "°C"
|
|
|
|
+ " | T1: "
|
|
|
|
+ str(self.__tool1_temperature)
|
|
|
|
+ "°C"
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
|
|
|
|
def __get(self, endpoint):
|
|
|
|
url = self.__octoprint_address + "/api/" + endpoint
|
|
|
|
headers = {"X-Api-Key": self.__octoprint_api_token}
|
|
|
|
resp = requests.get(url, headers=headers)
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
try:
|
|
|
|
return resp.json(), resp.status_code
|
|
|
|
except simplejson.errors.JSONDecodeError:
|
|
|
|
return None, resp.status_code
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
def __get_printer_bed_temperature(self):
|
|
|
|
printer_info, status_code = self.__get("printer")
|
|
|
|
if status_code == 200:
|
2020-05-03 11:15:52 +02:00
|
|
|
return (
|
|
|
|
printer_info["temperature"]["bed"]["actual"],
|
|
|
|
printer_info["temperature"]["bed"]["target"],
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
return None, None
|
|
|
|
|
|
|
|
def __get_octoprint_state(self):
|
|
|
|
job_info, status_code = self.__get("job")
|
|
|
|
return job_info["state"] if status_code == 200 else "Unknown"
|
|
|
|
|
|
|
|
def __get_tool_temperatures(self):
|
|
|
|
tool_temperatures = []
|
|
|
|
|
|
|
|
printer_info, status_code = self.__get("printer")
|
|
|
|
if status_code == 200:
|
|
|
|
temperatures = printer_info["temperature"]
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
tool_id = 0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
tool = temperatures["tool" + str(tool_id)]
|
|
|
|
tool_temperatures.append((tool["actual"], tool["target"]))
|
|
|
|
except KeyError:
|
|
|
|
break
|
|
|
|
tool_id += 1
|
|
|
|
return tool_temperatures
|
|
|
|
|
|
|
|
def update_status(self):
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__octoprint_state = self.__get_octoprint_state()
|
2020-05-02 18:56:53 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
actual_temp, _ = self.__get_printer_bed_temperature()
|
2020-05-02 18:56:53 +02:00
|
|
|
if actual_temp is None:
|
|
|
|
actual_temp = "-"
|
|
|
|
self.__printer_bed_temperature = str(actual_temp)
|
|
|
|
|
|
|
|
tool_temps = self.__get_tool_temperatures()
|
|
|
|
if len(tool_temps) > 0:
|
|
|
|
self.__tool1_temperature = tool_temps[0][0]
|
|
|
|
else:
|
|
|
|
self.__tool1_temperature = "-"
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Couldn't get data")
|
|
|
|
|
|
|
|
def __refresh_image(self, root, webcam_image, webcam_image_container):
|
|
|
|
try:
|
|
|
|
img = self.__webcam_images_queue.get()
|
|
|
|
webcam_image = ImageTk.PhotoImage(img)
|
|
|
|
webcam_image_container.config(image=webcam_image)
|
|
|
|
except queue.Empty as e:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Couldn't refresh image")
|
|
|
|
|
|
|
|
root.after(5, self.__refresh_image, root, webcam_image, webcam_image_container)
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
def __refresh_temperatures(
|
|
|
|
self, root, printer_bed_temperature_label, tools_temperature_label
|
|
|
|
):
|
2020-05-02 18:56:53 +02:00
|
|
|
actual_bed_temp, target_bed_temp = self.__get_printer_bed_temperature()
|
|
|
|
if actual_bed_temp is None:
|
|
|
|
actual_bed_temp = "-"
|
|
|
|
if target_bed_temp is None:
|
|
|
|
target_bed_temp = "-"
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
bed_temp = "Bed: " + str(actual_bed_temp) + "/" + str(target_bed_temp) + " °C"
|
2020-05-02 18:56:53 +02:00
|
|
|
printer_bed_temperature_label.config(text=bed_temp)
|
|
|
|
|
|
|
|
tool_temperatures = self.__get_tool_temperatures()
|
|
|
|
tools_temp = "Tools: "
|
|
|
|
|
|
|
|
if len(tool_temperatures) == 0:
|
|
|
|
tools_temp += "-/- °C"
|
|
|
|
else:
|
|
|
|
for i, tool_temperature in enumerate(tool_temperatures):
|
2020-05-03 11:15:52 +02:00
|
|
|
tools_temp += (
|
|
|
|
str(tool_temperature[0]) + "/" + str(tool_temperature[1]) + "°C"
|
|
|
|
)
|
|
|
|
if i != len(tool_temperatures) - 1:
|
2020-05-02 18:56:53 +02:00
|
|
|
tools_temp += "\t"
|
|
|
|
tools_temperature_label.config(text=tools_temp)
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
|
|
root.after(
|
|
|
|
500,
|
|
|
|
self.__refresh_temperatures,
|
|
|
|
root,
|
|
|
|
printer_bed_temperature_label,
|
|
|
|
tools_temperature_label,
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
|
|
|
|
def __show_popup(self, widget):
|
|
|
|
root = tk.Tk()
|
2020-05-03 11:15:52 +02:00
|
|
|
root.attributes("-type", "dialog")
|
2020-05-02 18:56:53 +02:00
|
|
|
root.title("Octoprint")
|
2020-05-03 11:15:52 +02:00
|
|
|
frame = tk.Frame(root)
|
2020-05-02 18:56:53 +02:00
|
|
|
if self.__octoprint_webcam:
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
# load first image synchronous before popup is shown, otherwise tkinter isn't able to layout popup properly
|
2020-05-02 18:56:53 +02:00
|
|
|
img = get_frame(self.__webcam_image_url)
|
|
|
|
webcam_image = ImageTk.PhotoImage(img)
|
2020-05-03 11:15:52 +02:00
|
|
|
webcam_image_container = tk.Button(frame, image=webcam_image)
|
2020-05-02 18:56:53 +02:00
|
|
|
webcam_image_container.pack()
|
|
|
|
|
|
|
|
self.__webcam_images_queue = queue.Queue()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__webcam_images_worker = WebcamImagesWorker(
|
|
|
|
self.__webcam_image_url, self.__webcam_images_queue
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
self.__webcam_images_worker.start()
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.debug(
|
|
|
|
"Not using webcam, as webcam is disabled. Enable with --webcam."
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
frame.pack()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
temperatures_label = tk.Label(frame, text="Temperatures", font=("", 25))
|
2020-05-02 18:56:53 +02:00
|
|
|
temperatures_label.pack()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
printer_bed_temperature_label = tk.Label(
|
|
|
|
frame, text="Bed: -/- °C", font=("", 15)
|
|
|
|
)
|
|
|
|
printer_bed_temperature_label.pack()
|
|
|
|
|
|
|
|
tools_temperature_label = tk.Label(frame, text="Tools: -/- °C", font=("", 15))
|
|
|
|
tools_temperature_label.pack()
|
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
root.after(10, self.__refresh_image, root, webcam_image, webcam_image_container)
|
2020-05-03 11:15:52 +02:00
|
|
|
root.after(
|
|
|
|
500,
|
|
|
|
self.__refresh_temperatures,
|
|
|
|
root,
|
|
|
|
printer_bed_temperature_label,
|
|
|
|
tools_temperature_label,
|
|
|
|
)
|
2020-05-02 18:56:53 +02:00
|
|
|
root.bind("<Destroy>", self.__on_close_popup)
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
|
|
root.eval("tk::PlaceWindow . center")
|
2020-05-02 18:56:53 +02:00
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
def __on_close_popup(self, event):
|
|
|
|
self.__webcam_images_queue = None
|
|
|
|
self.__webcam_images_worker.stop()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.update_status()
|
|
|
|
|
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return []
|
|
|
|
|
2020-05-02 18:56:53 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|