Fixed logging & added hup support

main
parent 70c1ae8e3c
commit 3b313062e7

@ -18,7 +18,7 @@ fullurltwo=f"{octoprint}/{printerpath}?apikey={apikey}"
import sys import sys
import os import os
import requests,datetime import requests,datetime,signal
imgdir = "/home/pi/epaper/img"#os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'img')#dontask 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') libdir = "/home/pi/epaper/lib"#os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir): if os.path.exists(libdir):
@ -41,14 +41,35 @@ handler=logging.handlers.RotatingFileHandler(filename='octopimonitor.log', encod
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) 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): def timestr(time):
return f"{datestr(time)} {time.hour:02}:{time.minute:02}" return f"{datestr(time)} {time.hour:02}:{time.minute:02}"
def datestr(time): def datestr(time):
return f"{time.year}-{time.month:02}-{time.day:02}" return f"{time.year}-{time.month:02}-{time.day:02}"
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)
def getData(): def getData():
info(f"requesting data from {octoprint}...")
jsondata=requests.get(fullurl, verify=False).json() jsondata=requests.get(fullurl, verify=False).json()
jsondatatwo=requests.get(fullurltwo, verify=False).json() jsondatatwo=requests.get(fullurltwo, verify=False).json()
info(f"Data received. Porgcessing.")
state="" state=""
if jsondatatwo["state"]["flags"]["paused"]: if jsondatatwo["state"]["flags"]["paused"]:
state="paused" state="paused"
@ -75,23 +96,24 @@ def getData():
"temp":jsondatatwo["temperature"]["tool0"]["actual"], "temp":jsondatatwo["temperature"]["tool0"]["actual"],
"target":jsondatatwo["temperature"]["tool0"]["target"], "target":jsondatatwo["temperature"]["tool0"]["target"],
"state":state "state":state
} }
return (jobdata,datetime.datetime.now()) return (jobdata,datetime.datetime.now())
try: def drawData(jobdata,lastupdate):
logging.info("Octopi print monitor starting...") info(f"Loading fonts and images")
# INIT
epd = epd4in2.EPD()
logging.info("init and Clear")
epd.init()
epd.Clear()
font14 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 14) font14 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 14)
font16 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 16) font16 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 16)
font18 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 18) font18 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 18)
font20 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 20) font20 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 20)
font24 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 24) font24 = ImageFont.truetype(os.path.join(imgdir, 'Font.ttc'), 24)
# LOCS # 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 # filename (60,6) file
# percent (125,45) percent # percent (125,45) percent
# eta (265,45) eta # eta (265,45) eta
@ -100,50 +122,58 @@ try:
# lastlayer (125,162) lastlayertime # lastlayer (125,162) lastlayertime
# lastupdate (117,250) # lastupdate (117,250)
# lastrefresh (117,278) # 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 # MONITOR LOOP
while True: while True:
epd.init()
(jobdata,lastupdate)=getData() (jobdata,lastupdate)=getData()
paper = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame drawData(jobdata,lastupdate)
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']: if jobdata['percent'] == "100" or jobdata['bedtarget'] == jobdata['target']:
info("Jobs idle - sleeping for 5 minutes")
time.sleep(300) time.sleep(300)
else: else:
info("Print in progress - sleeping for 30 seconds")
time.sleep(30) time.sleep(30)
except IOError as e: except IOError as e:
logging.info(e) error(e)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("ctrl + c:") critical("ctrl + c:")
epd.init() epd.init()
epd4in2.epdconfig.module_exit() epd4in2.epdconfig.module_exit()
exit() exit()

@ -18,6 +18,7 @@ User=pi
Group=pi Group=pi
WorkingDirectory=/home/pi/epaper/ WorkingDirectory=/home/pi/epaper/
ExecStart=/usr/bin/python3 /home/pi/epaper/octomonitor.py ExecStart=/usr/bin/python3 /home/pi/epaper/octomonitor.py
ExecReload=/usr/bin/kill -s HUP $MAINPID
KillSignal=SIGINT KillSignal=SIGINT
Restart=on-failure Restart=on-failure

Loading…
Cancel
Save