You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.2 KiB

2 years ago
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# secrets.py:
# octoprint="https://your.octoprint.ip.address.or.domain.name"
# apikey="your-octoprint-api-key"
from secrets import *
pluginpath="plugin/DisplayLayerProgress/values"
printerpath="api/printer"
2 years ago
fullurl=f"{octoprint}/{pluginpath}?apikey={apikey}"
fullurltwo=f"{octoprint}/{printerpath}?apikey={apikey}"
2 years ago
# CODED FOR WAVESHARE 4.2" 400x300 B&W ePaper Display
# Requires the DisplayLayerProgress plugin
# https://github.com/OllisGit/OctoPrint-DisplayLayerProgress
import sys
import os
import requests,datetime,signal
2 years ago
imgdir = "/home/pi/epaper/img"#os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'img')#dontask
libdir = "/home/pi/epaper/lib"#os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
sys.path.append(libdir)
import logging,logging.handlers
2 years ago
from waveshare_epd import epd4in2
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
# https://stackoverflow.com/a/32282390
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
logging.basicConfig(level=logging.INFO)
logger=logging.getLogger('octopimonitor')
formatter=logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', '%Y-%m-%d %H:%M:%S', style='{')
handler=logging.handlers.RotatingFileHandler(filename='octopimonitor.log', encoding='utf-8', maxBytes=32*1024*1024, backupCount=5)
handler.setFormatter(formatter)
logger.addHandler(handler)
def debug(a):
logger.debug(a)
def info(a):
logger.info(a)
def warn(a):
logger.warning(a)
def error(a):
logger.error(a)
def critical(a):
logger.critical(a)
def timestr(time):
return f"{datestr(time)} {time.hour:02}:{time.minute:02}"
def datestr(time):
return f"{time.year}-{time.month:02}-{time.day:02}"
2 years ago
def trapsignal(signalnum, frame):
# https://stackabuse.com/handling-unix-signals-in-python/
info(f"Caught signal {signalnum}")
if signalnum == 1:
info(f"Caught hup - reloading (signal {signalnum})")
(jobdata,lastupdate)=getData()
drawData(jobdata,lastupdate)
2 years ago
def getData():
info(f"requesting data from {octoprint}...")
2 years ago
jsondata=requests.get(fullurl, verify=False).json()
jsondatatwo=requests.get(fullurltwo, verify=False).json()
info(f"Data received. Porgcessing.")
state=""
2 years ago
if jsondatatwo["state"]["flags"]["paused"]:
state="paused"
2 years ago
elif jsondatatwo["state"]["flags"]["printing"]:
state="printing"
2 years ago
elif jsondatatwo["state"]["flags"]["error"]:
state="error"
2 years ago
elif jsondatatwo["state"]["flags"]["ready"]:
state="ready"
elif jsondatatwo["state"]["flags"]["cancelling"]:
state="cancelling"
else:
state="unknown"
2 years ago
jobdata={
"file":jsondata["currentFilename"].split('/')[-1],
2 years ago
"layer":jsondata["layer"]["current"],
"layers":jsondata["layer"]["total"],
"lastlayertime":jsondata["layer"]["lastLayerDuration"],
"percent":jsondata["print"]["progress"],
"eta":jsondata["print"]["timeLeft"],
"remaining":jsondata["print"]["timeLeftInSeconds"],
"bedtemp":jsondatatwo["temperature"]["bed"]["actual"],
"bedtarget":jsondatatwo["temperature"]["bed"]["target"],
"temp":jsondatatwo["temperature"]["tool0"]["actual"],
"target":jsondatatwo["temperature"]["tool0"]["target"],
"state":state
}
return (jobdata,datetime.datetime.now())
2 years ago
def drawData(jobdata,lastupdate):
info(f"Loading fonts and images")
font14 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 14)
font16 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 16)
2 years ago
font18 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 18)
font20 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 20)
2 years ago
font24 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 24)
# LOAD OUR BACKGROUND
bg = Image.open(os.path.join(imgdir, 'backgroundv2.bmp'))
info(f"Initializing ePaper display")
epd.init()
paper = Image.new('1', (epd.width, epd.height), 255) # Blank canvas
draw = ImageDraw.Draw(paper)
paper.paste(bg, (0, 0))
# ADD OUR TEXT
# filename (60,6) file
# percent (125,45) percent
# eta (265,45) eta
# layer (125,85) layer
# total (125,125) layers
# lastlayer (125,162) lastlayertime
# lastupdate (117,250)
# lastrefresh (117,278)
info(f"Preparing job data on canvas...")
if len(jobdata['file']) < 20:
draw.text((60, 6), jobdata['file'], font = font18, fill = 0)
else:
draw.text((60, 1), jobdata['file'][0:35], font = font18, fill = 0)
draw.text((60, 17), jobdata['file'][35:70], font = font18, fill = 0)
draw.text((125,42), f"{jobdata['percent']}%", font=font24, fill=0)
draw.text((268,42),jobdata['eta'],font=font24,fill=0)
draw.text((125,82),jobdata['layer'],font=font24,fill=0)
draw.text((125,122),jobdata['layers'],font=font24,fill=0)
draw.text((125,162),jobdata['lastlayertime'],font=font20,fill=0)
draw.text((117,250),timestr(lastupdate),font=font14,fill=0)
draw.text((117,278),timestr(datetime.datetime.now()),font=font14,fill=0)
# (240,82) under ETA
# (25,228) over last update
if jobdata['percent'] == "100" or jobdata['bedtarget'] == jobdata['target']:
draw.text((280,82),"Job complete.",font=font14,fill=0)
draw.text((250,96)," Refreshing every 5m.",font=font14,fill=0)
else:
draw.text((288,82),jobdata["state"],font=font18,fill=0)
draw.text((25,228),f"{jobdata['bedtemp']}°c/{jobdata['bedtarget']}°c {jobdata['temp']}°c/{jobdata['target']}°c",font=font14,fill=0)
# DISPLAY OUR MASTERPIECE
info("Drawing canvas on ePaper display")
epd.display(epd.getbuffer(paper))
epd.sleep()
try:
signal.signal(signal.SIGHUP, trapsignal)
info("Octopi print monitor starting...")
# INIT
epd = epd4in2.EPD()
info("init and Clear")
epd.init()
epd.Clear()
# MONITOR LOOP
while True:
(jobdata,lastupdate)=getData()
drawData(jobdata,lastupdate)
if jobdata['percent'] == "100" or jobdata['bedtarget'] == jobdata['target']:
info("Jobs idle - sleeping for 5 minutes")
time.sleep(300)
else:
info("Print in progress - sleeping for 30 seconds")
time.sleep(30)
2 years ago
except IOError as e:
error(e)
2 years ago
except KeyboardInterrupt:
critical("ctrl + c:")
epd.init()
2 years ago
epd4in2.epdconfig.module_exit()
exit()