-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathstatus.py
More file actions
173 lines (131 loc) · 4.64 KB
/
status.py
File metadata and controls
173 lines (131 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
# Created by: Michael Klements & Macley(kun)
# For Raspberry Pi Desktop Case with OLED Stats Display
# Base on Adafruit CircuitPython & SSD1306 Libraries
# Installation & Setup Instructions - https://www.the-diy-life.com/add-an-oled-stats-display-to-raspberry-pi-os-bullseye/
import os, sys, time, atexit, signal
import board, digitalio
import adafruit_ssd1306
from PIL import Image, ImageDraw, ImageFont
import psutil
import socket
WIDTH, HEIGHT = 128, 64
FONT_SZ = 16
oled = adafruit_ssd1306.SSD1306_I2C(
WIDTH, HEIGHT, board.I2C(), addr=0x3C, reset=digitalio.DigitalInOut(board.D4)
)
rotation = int(os.environ.get("OLED_ROTATION", "1"))
if rotation == 2:
try:
oled.rotate(2)
except AttributeError:
oled.rotation = 2
def cleanup():
try:
oled.fill(0)
oled.show()
except Exception:
pass
def kill_handler(*_):
cleanup()
sys.exit(0)
atexit.register(cleanup)
signal.signal(signal.SIGINT, kill_handler)
signal.signal(signal.SIGTERM, kill_handler)
oled.fill(0)
oled.show()
image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("PixelOperator.ttf", FONT_SZ)
icon_font = ImageFont.truetype("lineawesome-webfont.ttf", FONT_SZ)
# Helper: get a best guess LAN IP without shelling out
def get_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "0.0.0.0"
def get_temp_c():
# Pi exposes temperature here (on typical Raspberry Pi OS)
try:
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
return float(f.read().strip()) / 1000.0
except Exception:
return 0.0
def format_uptime(seconds):
minutes = seconds // 60
hours = minutes // 60
days = hours // 24
minutes %= 60
hours %= 24
if days > 0:
return f"{days}d {hours}h"
elif hours > 0:
return f"{hours}:{minutes:02d}"
else:
return f"{minutes}m"
# Use proc uptime as psutil reported a +20m after an hour
def get_uptime_seconds():
try:
with open("/proc/uptime", "r") as f:
return int(float(f.readline().split()[0]))
except Exception:
# Fallback: keep old behavior if /proc/uptime isn't available for some reason
return int(time.time() - psutil.boot_time())
# Cache slow-ish values
ip = get_ip()
last_ip_check = 0.0
last_frame = None
# Prime psutil CPU sampling (first call can be 0.0)
psutil.cpu_percent(interval=None)
while True:
now = time.time()
# Refresh IP only every 60s
if now - last_ip_check > 60:
ip = get_ip()
last_ip_check = now
cpu = psutil.cpu_percent(interval=None)
vm = psutil.virtual_memory()
mem_used_gb = vm.used / (1024**3)
mem_total_gb = vm.total / (1024**3)
mem_pct = vm.percent
du = psutil.disk_usage("/")
disk_pct = du.percent
uptime_s = get_uptime_seconds()
uptime = format_uptime(uptime_s)
temp_c = get_temp_c()
# Build frame text. If it didn't change, skip OLED update.
frame = (
ip,
round(cpu, 0),
round(temp_c, 1),
round(mem_pct, 1),
round(mem_used_gb, 1),
round(mem_total_gb, 0),
int(disk_pct),
uptime,
)
if frame != last_frame:
draw.rectangle((0, 0, oled.width, oled.height), fill=0)
# icons
draw.text((1, 0), chr(61931), font=icon_font, fill=255) # wifi
draw.text((1, 16), chr(62171), font=icon_font, fill=255) # cpu
draw.text((111, 16), chr(62153), font=icon_font, fill=255) # temp
draw.text((1, 32), chr(62776), font=icon_font, fill=255) # memory
draw.text((1, 48), chr(63426), font=icon_font, fill=255) # disk
draw.text((111, 48), chr(62034), font=icon_font, fill=255) # time
# text
draw.text((22, 0), ip, font=font, fill=255)
draw.text((22, 16), f"{cpu:.0f}%", font=font, fill=255)
draw.text((107, 16), f"{temp_c:.1f}°C", font=font, fill=255, anchor="ra")
draw.text((22, 32), f"{mem_pct:.0f}%", font=font, fill=255)
draw.text((125, 32), f"{mem_used_gb:.1f}/{mem_total_gb:.0f}G", font=font, fill=255, anchor="ra")
draw.text((22, 48), f"{disk_pct:.0f}%", font=font, fill=255)
draw.text((107, 48), uptime, font=font, fill=255, anchor="ra")
oled.image(image)
oled.show()
last_frame = frame
time.sleep(5.0)