diff --git a/compLib/VisionDaemon.py b/compLib/IPService.py similarity index 86% rename from compLib/VisionDaemon.py rename to compLib/IPService.py index 404c8d0..dcf81b2 100644 --- a/compLib/VisionDaemon.py +++ b/compLib/IPService.py @@ -5,6 +5,7 @@ import threading import time import systemd.daemon +from compLib.Lock import Lock try: from compLib.LogstashLogging import Logging @@ -29,6 +30,7 @@ RUN_IP_CHECK = False try: from compLib.Display import Display from compLib.Spi import Spi + from compLib import __version__ RUN_IP_CHECK = True except Exception as e: print(f"Could not import display or spi for ip output {str(e)}") @@ -58,13 +60,16 @@ def get_ip(): def write_ip_to_screen(): while os.getenv("IP_OUTPUT", "true") == "true": try: - ip = str(get_ip()) - print(f"writing {ip} to display") - Display.write(2, f"FW. V{Spi.get_version()}") - Display.write(3, "IP:") - Display.write(4, ip) - Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S")) - time.sleep(5) + if not Lock.is_locked(): + Lock.lock() + ip = str(get_ip()) + print(f"writing {ip} to display") + Display.write(2, f"LIB: V{__version__}") + Display.write(3, f"FW: V{Spi.get_version()}") + Display.write(4, f"IP: {ip}") + Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S")) + Lock.unlock() + time.sleep(10) except Exception as e: print(f"Exception in write ip thread: {e}") time.sleep(5) @@ -82,6 +87,7 @@ if __name__ == '__main__': if RUN_IP_CHECK and IP_OUTPUT: print("starting ip output") Logging.get_logger().info("starting ip output") + Lock.unlock() try: Spi.disable_health_check() ip_output = threading.Thread(target=write_ip_to_screen) diff --git a/compLib/Lock.py b/compLib/Lock.py new file mode 100644 index 0000000..2184b6b --- /dev/null +++ b/compLib/Lock.py @@ -0,0 +1,25 @@ +from filelock import FileLock, Timeout +import atexit + +FILELOCK_PATH = "/root/complib.lock" + +global_lock = FileLock(FILELOCK_PATH, timeout=1) +class Lock(object): + @staticmethod + def lock(): + global_lock.acquire() + + @staticmethod + def unlock(): + global_lock.release() + + @staticmethod + def is_locked(): + try: + global_lock.acquire() + global_lock.release() + return False + except Timeout: + return True + +atexit.register(Lock.unlock) \ No newline at end of file diff --git a/compLib/__init__.py b/compLib/__init__.py index 10cd837..ff019af 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -1,9 +1,10 @@ __version__ = "0.3.9-0" +import sys import importlib import compLib.LogstashLogging import compLib.MetricsLogging - +from compLib.Lock import Lock apt_found = importlib.util.find_spec("apt") is not None spi_found = importlib.util.find_spec("spidev") is not None @@ -25,6 +26,12 @@ if apt_found: else: print("apt is not installed! This is for local development only!") +if Lock.is_locked(): + print("Another program using compLib is still running! Delete /root/complib.lock if this is an error. Exiting...") + sys.exit() +else: + Lock.lock() + if spi_found: from compLib.Spi import Spi from compLib.Reset import Reset diff --git a/complib.service b/complib.service index 43e3f49..758aaf7 100644 --- a/complib.service +++ b/complib.service @@ -1,7 +1,7 @@ [Unit] Description=Monitoring service [Service] -ExecStart=/usr/bin/python3 /usr/local/lib/python3.7/dist-packages/compLib/VisionDaemon.py +ExecStart=/usr/bin/python3 /usr/local/lib/python3.7/dist-packages/compLib/IPService.py Environment="debug=False" Restart=always RestartSec=5 diff --git a/postinstall.sh b/postinstall.sh index 357fe4b..fabfa94 100644 --- a/postinstall.sh +++ b/postinstall.sh @@ -23,7 +23,7 @@ install_package() { #install_package "spidev" #install_package "influxdb_client" -pip3 install smbus requests flask python-logstash-async RPi.GPIO spidev influxdb_client +pip3 install smbus requests flask python-logstash-async RPi.GPIO spidev influxdb_client filelock echo "Setting up opencv4" pkg-config --modversion opencv4