diff --git a/grab.py b/grab.py index bb54768..f678aaf 100644 --- a/grab.py +++ b/grab.py @@ -6,8 +6,11 @@ import os import sys import time +import imageio + from vncdotool import api from twisted.internet import reactor + import paho.mqtt.client as paho # LOGGING @@ -68,6 +71,16 @@ NAME=DEVICE_NAME or SANITIZED_HOST DEVICE_ID = DEVICE_ID or SANITIZED_HOST 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.username_pw_set(MQTT_USER, MQTT_PASSWORD) mqtt.connect(MQTT_HOST) @@ -85,8 +98,11 @@ mqtt.publish(f"homeassistant/camera/{DEVICE_ID}/config", json.dumps({ }, }), retain=True) +t = None + while True: - t = time.time() + if t is None: + t = time.time() with api.connect(VNC_HOST, VNC_PASSWORD) as vnc: logger.debug("refreshing for capture") vnc.refreshScreen() @@ -95,6 +111,13 @@ while True: logger.debug("publishing to mqtt") with open('capture.png', 'rb') as f: 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: pass # touch :D @@ -104,7 +127,8 @@ while True: logger.debug(f"sleeping for {time_left}s") time.sleep(time_left) 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() reactor.stop() diff --git a/health.sh b/health.sh index c2aadfb..8e43a77 100755 --- a/health.sh +++ b/health.sh @@ -3,7 +3,7 @@ DT=$(date +%s) CT=$(stat -c %Z capture.time) -if [ $(( DT - CT )) -gt $INTERVAL ];then +if [ $(( DT - CT )) -gt $(( 5 * INTERVAL )) ];then echo bad false else diff --git a/requirements.txt b/requirements.txt index 17d7728..158cb68 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ Automat==22.10.0 constantly==15.1.0 hyperlink==21.0.0 idna==3.4 +imageio==2.31.1 incremental==22.10.0 paho-mqtt==1.6.1 Pillow==10.0.0