make health check a bit better

This commit is contained in:
James Andariese 2023-07-24 17:21:47 -05:00
parent 5e4f375f71
commit 8cd429f819
3 changed files with 28 additions and 3 deletions

28
grab.py
View File

@ -6,8 +6,11 @@ import os
import sys import sys
import time import time
import imageio
from vncdotool import api from vncdotool import api
from twisted.internet import reactor from twisted.internet import reactor
import paho.mqtt.client as paho import paho.mqtt.client as paho
# LOGGING # LOGGING
@ -68,6 +71,16 @@ NAME=DEVICE_NAME or SANITIZED_HOST
DEVICE_ID = DEVICE_ID or SANITIZED_HOST DEVICE_ID = DEVICE_ID or SANITIZED_HOST
OBJECT_ID = base64.b32encode(hashlib.sha1(NAME.encode('utf-8')).digest()).decode('utf-8') OBJECT_ID = base64.b32encode(hashlib.sha1(NAME.encode('utf-8')).digest()).decode('utf-8')
def blackwhite(imgf):
r = imageio.read(imgf)
screen = r.get_data(0).flatten()
byt = screen.tobytes()
count = len(byt)
blackcount = byt.count(b'\x00')
whitecount = byt.count(b'\xff')
return blackcount,whitecount,count
mqtt = paho.Client() mqtt = paho.Client()
mqtt.username_pw_set(MQTT_USER, MQTT_PASSWORD) mqtt.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt.connect(MQTT_HOST) mqtt.connect(MQTT_HOST)
@ -85,8 +98,11 @@ mqtt.publish(f"homeassistant/camera/{DEVICE_ID}/config", json.dumps({
}, },
}), retain=True) }), retain=True)
t = None
while True: while True:
t = time.time() if t is None:
t = time.time()
with api.connect(VNC_HOST, VNC_PASSWORD) as vnc: with api.connect(VNC_HOST, VNC_PASSWORD) as vnc:
logger.debug("refreshing for capture") logger.debug("refreshing for capture")
vnc.refreshScreen() vnc.refreshScreen()
@ -95,6 +111,13 @@ while True:
logger.debug("publishing to mqtt") logger.debug("publishing to mqtt")
with open('capture.png', 'rb') as f: with open('capture.png', 'rb') as f:
capture = f.read() capture = f.read()
(black,white,total) = blackwhite('capture.png')
if black > (total / 2): # if it's more than half black, it's a failure
logger.warning(f"screen is more than half black. considering as failure.")
continue
if white > (total / 2): # if it's more than half white, it's a failure
logger.warning(f"screen is more than half white. considering as failure.")
continue
with open('capture.time', 'wb') as f: with open('capture.time', 'wb') as f:
pass # touch :D pass # touch :D
@ -104,7 +127,8 @@ while True:
logger.debug(f"sleeping for {time_left}s") logger.debug(f"sleeping for {time_left}s")
time.sleep(time_left) time.sleep(time_left)
else: else:
logger.warning(f"interval missed ({time_left}s overdue)") logger.warning(f"interval missed ({0-time_left}s overdue)")
t = None
mqtt.loop_end() mqtt.loop_end()
reactor.stop() reactor.stop()

View File

@ -3,7 +3,7 @@
DT=$(date +%s) DT=$(date +%s)
CT=$(stat -c %Z capture.time) CT=$(stat -c %Z capture.time)
if [ $(( DT - CT )) -gt $INTERVAL ];then if [ $(( DT - CT )) -gt $(( 5 * INTERVAL )) ];then
echo bad echo bad
false false
else else

View File

@ -3,6 +3,7 @@ Automat==22.10.0
constantly==15.1.0 constantly==15.1.0
hyperlink==21.0.0 hyperlink==21.0.0
idna==3.4 idna==3.4
imageio==2.31.1
incremental==22.10.0 incremental==22.10.0
paho-mqtt==1.6.1 paho-mqtt==1.6.1
Pillow==10.0.0 Pillow==10.0.0