Skip to content

Commit 0e7e3da

Browse files
brenordvrm-hull
andauthored
Added IP info to sys_info.py (#165)
* 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. * Update CONTRIBUTING.rst * 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). * Added dependency for psutil package. * Update setup.cfg * Update sys_info.py * Update sys_info.py --------- Co-authored-by: Breno RdV <brenordv@users.noreply.github.com> Co-authored-by: Richard Hull <rm_hull@yahoo.co.uk>
1 parent 601ac32 commit 0e7e3da

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CONTRIBUTING.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Contributors
2020
* Tomislav Kopić (@Tkopic001)
2121
* Maciej Sokolowski (@matemaciek)
2222
* Sangho Kim (@rlaace423)
23+
* Breno RdV (@brenordv)

examples/sys_info.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import signal
1818
import sys
1919
import time
20+
import socket
2021
from pathlib import Path
2122
from datetime import datetime
2223

@@ -38,6 +39,32 @@
3839
# TODO: Load histogram
3940

4041

42+
class IPAddressChecker:
43+
def __init__(self, cache_duration_in_seconds=14400):
44+
"""
45+
:param cache_duration_in_seconds: The duration in seconds to cache the IP address for. Default is 4 hours.
46+
"""
47+
self._ip_address = None
48+
self._last_checked = None
49+
self._cache_duration = cache_duration_in_seconds
50+
51+
def get_ip_address(self):
52+
if self._last_checked is None or time.time() - self._last_checked > self._cache_duration:
53+
self._ip_address = self._retrieve_ip_address()
54+
self._last_checked = time.time()
55+
return self._ip_address
56+
57+
@staticmethod
58+
def _retrieve_ip_address():
59+
try:
60+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
61+
s.connect(("8.8.8.8", 80)) # Google DNS. Probably will never be down.
62+
return s.getsockname()[0]
63+
except Exception as e:
64+
print(f"Error: {e}")
65+
return ""
66+
67+
4168
def shutdown(signum, frame):
4269
device.clear()
4370
sys.exit(0)
@@ -118,6 +145,9 @@ def stats(device):
118145
try:
119146
if device.height >= (line_height * 4):
120147
draw.text((2, line_height * 3), network('wlan0'), font=font2, fill="white")
148+
149+
if device.height >= (line_height * 5):
150+
draw.text((2, line_height * 4), ip_address_checker.get_ip_address(), font=font2, fill="white")
121151
except KeyError:
122152
# no wifi enabled/available
123153
pass
@@ -132,6 +162,7 @@ def main():
132162
if __name__ == "__main__":
133163
try:
134164
device = get_device()
165+
ip_address_checker = IPAddressChecker()
135166
main()
136167
except KeyboardInterrupt:
137168
pass

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ install_requires =
3232
luma.lcd>=2.5.0
3333
luma.led_matrix>=1.5.0
3434
argcomplete
35+
psutil
3536
tests_require =
3637
pytest
3738
pytest-cov

0 commit comments

Comments
 (0)