From 3b313062e7b2f9e4e1a832b602bd7a8a650a8295 Mon Sep 17 00:00:00 2001 From: WhatTheMike Date: Mon, 20 Mar 2023 12:54:32 -0400 Subject: [PATCH] Fixed logging & added hup support --- octomonitor.py | 116 ++++++++++++++++++++++++++++---------------- octomonitor.service | 1 + 2 files changed, 74 insertions(+), 43 deletions(-) diff --git a/octomonitor.py b/octomonitor.py index ddf7feb..88e9eb7 100644 --- a/octomonitor.py +++ b/octomonitor.py @@ -18,7 +18,7 @@ fullurltwo=f"{octoprint}/{printerpath}?apikey={apikey}" import sys 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 libdir = "/home/pi/epaper/lib"#os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib') if os.path.exists(libdir): @@ -41,14 +41,35 @@ handler=logging.handlers.RotatingFileHandler(filename='octopimonitor.log', encod 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}" +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(): + info(f"requesting data from {octoprint}...") jsondata=requests.get(fullurl, verify=False).json() jsondatatwo=requests.get(fullurltwo, verify=False).json() + info(f"Data received. Porgcessing.") state="" if jsondatatwo["state"]["flags"]["paused"]: state="paused" @@ -75,23 +96,24 @@ def getData(): "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() - +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) 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 + # 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 @@ -100,50 +122,58 @@ try: # 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: - 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() + 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) except IOError as e: - logging.info(e) + error(e) except KeyboardInterrupt: - logging.info("ctrl + c:") + critical("ctrl + c:") epd.init() epd4in2.epdconfig.module_exit() exit() diff --git a/octomonitor.service b/octomonitor.service index 1a27d4f..bbcc9d1 100644 --- a/octomonitor.service +++ b/octomonitor.service @@ -18,6 +18,7 @@ User=pi Group=pi WorkingDirectory=/home/pi/epaper/ ExecStart=/usr/bin/python3 /home/pi/epaper/octomonitor.py +ExecReload=/usr/bin/kill -s HUP $MAINPID KillSignal=SIGINT Restart=on-failure