From ad31512b7e47d79528b9f27a6f94f4886593355d Mon Sep 17 00:00:00 2001 From: Breno Ribeiro do Valle <28983571+brenordv@users.noreply.github.com> Date: Sun, 3 Dec 2023 02:06:40 -0500 Subject: [PATCH 1/4] Added IP info to sys_info.py Added IP info to the sys_info.py example. Also included a caching (default: 4 hours) since IP address is not something that changes frequently. --- examples/sys_info.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/examples/sys_info.py b/examples/sys_info.py index e42f7cd..2bbf0ba 100755 --- a/examples/sys_info.py +++ b/examples/sys_info.py @@ -16,6 +16,7 @@ import os import sys import time +import socket from pathlib import Path from datetime import datetime @@ -37,6 +38,32 @@ # TODO: Load histogram +class IPAddressChecker: + def __init__(self, cache_duration_in_seconds=14400): + """ + :param cache_duration_in_seconds: The duration in seconds to cache the IP address for. Default is 4 hours. + """ + self._ip_address = None + self._last_checked = None + self._cache_duration = cache_duration_in_seconds + + def get_ip_address(self): + if self._last_checked is None or time.time() - self._last_checked > self._cache_duration: + self._ip_address = self._retrieve_ip_address() + self._last_checked = time.time() + return self._ip_address + + @staticmethod + def _retrieve_ip_address(): + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) # Google DNS. Probably will never be down. + return s.getsockname()[0] + except Exception as e: + print(f"Error: {e}") + return None + + def bytes2human(n): """ >>> bytes2human(10000) @@ -95,6 +122,7 @@ def stats(device): draw.text((0, 26), disk_usage('/'), font=font2, fill="white") try: draw.text((0, 38), network('wlan0'), font=font2, fill="white") + draw.text((0, 50), ip_address_checker.get_ip_address(), font=font2, fill="white") except KeyError: # no wifi enabled/available pass @@ -109,6 +137,7 @@ def main(): if __name__ == "__main__": try: device = get_device() + ip_address_checker = IPAddressChecker() main() except KeyboardInterrupt: pass From a1fb02eca148c11ca24056740fd82b901e162a87 Mon Sep 17 00:00:00 2001 From: Breno Ribeiro do Valle <28983571+brenordv@users.noreply.github.com> Date: Sun, 3 Dec 2023 02:09:50 -0500 Subject: [PATCH 2/4] Update CONTRIBUTING.rst --- CONTRIBUTING.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index ca71a77..ec39bff 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -20,3 +20,4 @@ Contributors * Tomislav Kopić (@Tkopic001) * Maciej Sokolowski (@matemaciek) * Sangho Kim (@rlaace423) +* Breno RdV (@brenordv) From 90f4266981d941f4e7a0ceeef4ae4348efd955e3 Mon Sep 17 00:00:00 2001 From: Breno Ribeiro do Valle <28983571+brenordv@users.noreply.github.com> Date: Sun, 3 Dec 2023 02:14:27 -0500 Subject: [PATCH 3/4] Update sys_info.py Changed the strategy for failure cases. Now returning an empty string and (hopefully) ensuring nothing else will go wrong (like failing to display None). --- examples/sys_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sys_info.py b/examples/sys_info.py index 2bbf0ba..07500a4 100755 --- a/examples/sys_info.py +++ b/examples/sys_info.py @@ -61,7 +61,7 @@ def _retrieve_ip_address(): return s.getsockname()[0] except Exception as e: print(f"Error: {e}") - return None + return "" def bytes2human(n): From 06bb4918f4ffca98669b89fe05000e9b70a68123 Mon Sep 17 00:00:00 2001 From: Breno RdV Date: Wed, 10 Sep 2025 19:09:18 -0400 Subject: [PATCH 4/4] Added dependency for psutil package. --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index a7b5b4a..3ccd3f0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,6 +32,7 @@ install_requires = luma.lcd>=2.5.0 luma.led_matrix>=1.5.0 argcomplete + psutil = "==7.0.0" tests_require = pytest pytest-cov