Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Contributors
* Tomislav Kopić (@Tkopic001)
* Maciej Sokolowski (@matemaciek)
* Sangho Kim (@rlaace423)
* Breno RdV (@brenordv)
29 changes: 29 additions & 0 deletions examples/sys_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import sys
import time
import socket
from pathlib import Path
from datetime import datetime

Expand All @@ -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 ""


def bytes2human(n):
"""
>>> bytes2human(10000)
Expand Down Expand Up @@ -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
Expand All @@ -109,6 +137,7 @@ def main():
if __name__ == "__main__":
try:
device = get_device()
ip_address_checker = IPAddressChecker()
main()
except KeyboardInterrupt:
pass
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading