diff --git a/build_deb.sh b/build_deb.sh index 6ba60ec..4b36fad 100755 --- a/build_deb.sh +++ b/build_deb.sh @@ -34,7 +34,7 @@ fpm -s python --python-bin python3 --python-pip pip3 --python-package-name-prefi -d "python3-pigpio" \ -d "python3-numpy" \ -d "ffmpeg" \ - -v 0.4.0-0 -t deb setup.py + -v 0.3.9-0 -t deb setup.py # --deb-changelog changelog \ # --deb-upstream-changelog changelog \ diff --git a/compLib/Api.py b/compLib/Api.py index f36e79a..6776af0 100644 --- a/compLib/Api.py +++ b/compLib/Api.py @@ -8,13 +8,6 @@ import requests from compLib.LogstashLogging import Logging API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/" - -api_override = os.getenv("API_FORCE", "") - -if api_override != "": - print(f"API_URL was set to {API_URL} but was overwritten with {api_override}") - API_URL = api_override - API_URL_GET_DELIVERY = API_URL + "getDelivery" API_URL_GET_MATERIAL = API_URL + "getMaterial" API_URL_GET_GARBAGE = API_URL + "getGarbage" diff --git a/compLib/VisionDaemon.py b/compLib/IPService.py similarity index 85% rename from compLib/VisionDaemon.py rename to compLib/IPService.py index 404c8d0..1a97fff 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,17 @@ 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 +88,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/LogstashLogging.py b/compLib/LogstashLogging.py index b36196a..c766f74 100644 --- a/compLib/LogstashLogging.py +++ b/compLib/LogstashLogging.py @@ -6,7 +6,7 @@ import requests from logstash_async.transport import HttpTransport from logstash_async.handler import AsynchronousLogstashHandler -EXTENSIVE_LOGGING = os.getenv("EXTENSIVE_LOGGING", "False") +EXTENSIVE_LOGGING = os.getenv("EXTENSIVE_LOGGING", "True") if EXTENSIVE_LOGGING == "True": EXTENSIVE_LOGGING = True diff --git a/compLib/__init__.py b/compLib/__init__.py index 0110242..ff019af 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -1,9 +1,10 @@ -__version__ = "0.4.0-0" +__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 diff --git a/setup.py b/setup.py index 78a9f6e..1258275 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ else: setuptools.setup( name="complib", - version="0.4.0-0", + version="0.3.9-0", author="F-WuTs", author_email="--", description="",