From e7f0f95d664b220e8074fb90ec82f98f4bc85e1a Mon Sep 17 00:00:00 2001 From: Joel Klimont Date: Sat, 12 Feb 2022 13:16:26 +0100 Subject: [PATCH 1/3] updated version number to 0.4.0-0 added API_FORCE for overriding API_URL extensive logging is now disabled by default --- build_deb.sh | 2 +- compLib/Api.py | 7 +++++++ compLib/LogstashLogging.py | 2 +- compLib/__init__.py | 2 +- setup.py | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/build_deb.sh b/build_deb.sh index 4b36fad..6ba60ec 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.3.9-0 -t deb setup.py + -v 0.4.0-0 -t deb setup.py # --deb-changelog changelog \ # --deb-upstream-changelog changelog \ diff --git a/compLib/Api.py b/compLib/Api.py index 6776af0..f36e79a 100644 --- a/compLib/Api.py +++ b/compLib/Api.py @@ -8,6 +8,13 @@ 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/LogstashLogging.py b/compLib/LogstashLogging.py index c766f74..b36196a 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", "True") +EXTENSIVE_LOGGING = os.getenv("EXTENSIVE_LOGGING", "False") if EXTENSIVE_LOGGING == "True": EXTENSIVE_LOGGING = True diff --git a/compLib/__init__.py b/compLib/__init__.py index 10cd837..0110242 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.3.9-0" +__version__ = "0.4.0-0" import importlib import compLib.LogstashLogging diff --git a/setup.py b/setup.py index 1258275..78a9f6e 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ else: setuptools.setup( name="complib", - version="0.3.9-0", + version="0.4.0-0", author="F-WuTs", author_email="--", description="", -- 2.47.2 From 01cb854f50f8efb443225d25f86298e92eb9759a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 13 Feb 2022 17:30:10 +0000 Subject: [PATCH 2/3] Add Locking --- compLib/{VisionDaemon.py => IPService.py} | 20 +++++++++++------- compLib/Lock.py | 25 +++++++++++++++++++++++ compLib/__init__.py | 9 +++++++- complib.service | 2 +- postinstall.sh | 2 +- 5 files changed, 48 insertions(+), 10 deletions(-) rename compLib/{VisionDaemon.py => IPService.py} (86%) create mode 100644 compLib/Lock.py 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 -- 2.47.2 From 0e1b767f2637ccf2a6949caf68a53ea3aa8ae5a7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 13 Feb 2022 17:50:06 +0000 Subject: [PATCH 3/3] Fix while true in IPService Lock --- compLib/IPService.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compLib/IPService.py b/compLib/IPService.py index dcf81b2..1a97fff 100644 --- a/compLib/IPService.py +++ b/compLib/IPService.py @@ -69,7 +69,8 @@ def write_ip_to_screen(): Display.write(4, f"IP: {ip}") Display.write(1, datetime.datetime.now().strftime("%b %d %H:%M:%S")) Lock.unlock() - time.sleep(10) + + time.sleep(10) except Exception as e: print(f"Exception in write ip thread: {e}") time.sleep(5) -- 2.47.2