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.

150 lines
5.4 KiB

#!/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"
fullurl=f"{octoprint}/{pluginpath}?apikey={apikey}"
fullurltwo=f"{octoprint}/{printerpath}?apikey={apikey}"
# 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
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
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 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}"
def getData():
jsondata=requests.get(fullurl, verify=False).json()
jsondatatwo=requests.get(fullurltwo, verify=False).json()
state=""
if jsondatatwo["state"]["flags"]["paused"]:
state="paused"
elif jsondatatwo["state"]["flags"]["printing"]:
state="printing"
elif jsondatatwo["state"]["flags"]["error"]:
state="error"
elif jsondatatwo["state"]["flags"]["ready"]:
state="ready"
elif jsondatatwo["state"]["flags"]["cancelling"]:
state="cancelling"
else:
state="unknown"
jobdata={
"file":jsondata["currentFilename"].split('/')[-1],
"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())
try:
logging.info("Octopi print monitor starting...")
# INIT
epd = epd4in2.EPD()
logging.info("init and Clear")
epd.init()
epd.Clear()
font14 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 14)
font16 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 16)
font18 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 18)
font20 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 20)
font24 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 24)
# LOCS
# 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)
# MONITOR LOOP
while True:
epd.init()
(jobdata,lastupdate)=getData()
paper = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
draw = ImageDraw.Draw(paper)
# LOAD OUR BACKGROUND
bg = Image.open(os.path.join(imgdir, 'backgroundv2.bmp'))
paper.paste(bg, (0, 0))
# ADD OUR TEXT
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
epd.display(epd.getbuffer(paper))
epd.sleep()
if jobdata['percent'] == "100" or jobdata['bedtarget'] == jobdata['target']:
time.sleep(300)
else:
time.sleep(30)
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
epd.init()
epd4in2.epdconfig.module_exit()
exit()