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) diff --git a/examples/sys_info.py b/examples/sys_info.py index 4c0ba3f..60ecf33 100755 --- a/examples/sys_info.py +++ b/examples/sys_info.py @@ -17,6 +17,7 @@ import signal import sys import time +import socket from pathlib import Path from datetime import datetime @@ -38,6 +39,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 "" + + def shutdown(signum, frame): device.clear() sys.exit(0) @@ -118,6 +145,9 @@ def stats(device): try: if device.height >= (line_height * 4): draw.text((2, line_height * 3), network('wlan0'), font=font2, fill="white") + + if device.height >= (line_height * 5): + draw.text((2, line_height * 4), ip_address_checker.get_ip_address(), font=font2, fill="white") except KeyError: # no wifi enabled/available pass @@ -132,6 +162,7 @@ def main(): if __name__ == "__main__": try: device = get_device() + ip_address_checker = IPAddressChecker() main() except KeyboardInterrupt: pass diff --git a/setup.cfg b/setup.cfg index a7b5b4a..8c089f1 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 tests_require = pytest pytest-cov