forked from willdurand/pihole-oled
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnightly.py
292 lines (242 loc) · 8.37 KB
/
nightly.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python3
import os
import platform
import time
import humanize
import psutil
import requests
#imports for Fritz.Box
from fritzconnection import FritzConnection
from fritzconnection.lib.fritzstatus import FritzStatus
#imports for Display
from luma.core.interface.serial import i2c
from luma.core.sprite_system import framerate_regulator
from luma.oled.device import ssd1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageSequence
from datetime import datetime
# Network interface to retrieve the IP address / use wlan0 for pizero / eth0 for pi
interface = os.getenv('PIHOLE_OLED_INTERFACE', 'wlan0')
# Mount point for disk usage info
mount_point = os.getenv('PIHOLE_OLED_MOUNT_POINT', '/')
# initialisation for Fritz.Box API / IP and Password may need to be changed.
fritzconnection = FritzConnection(address='192.168.178.1', password='password')
fc = FritzStatus(address='192.168.178.1', password='password')
try:
serial = i2c(port=1, address=0x3C)
disp = ssd1306(serial)
serial2 = i2c(port=0, address=0x3C)
disp2 = ssd1306(serial2)
is_noop = False
except FileNotFoundError:
# The error is probably due to this script being run on a system that does
# not have an OLED connected. In this case, we create fake objects to
# render the result in the console.
from image_noop import NoopDisplay, NoopImage, InMemoryImageDraw
disp = NoopDisplay()
is_noop = True
width = disp.width
height = disp.height
giftimer = 2
disp.clear()
if is_noop:
image = NoopImage()
draw = InMemoryImageDraw(image)
font = None
else:
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default() #truetype('./SF_Pixelate.ttf', 10)
sleep = 1 # seconds
hostname = platform.node()
try:
elapsed_seconds = 0
while True:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
if elapsed_seconds == 17:
elapsed_seconds = 0
if elapsed_seconds >= 5 and elapsed_seconds <= 10:
addr = psutil.net_if_addrs()[interface][0]
draw.text(
(0, 0),
"Pi-hole %s" % addr.address.rjust(15),
font=font,
fill=255
)
uptime = datetime.now() - datetime.fromtimestamp(
psutil.boot_time()
)
draw.text(
(0, 12),
"Up: %s" % humanize.naturaltime(uptime),
font=font,
fill=255
)
draw.text(
(0, 22),
" %.1f %.1f %.1f" % os.getloadavg(),
font=font,
fill=255
)
cpu = int(psutil.cpu_percent(percpu=False))
draw.text((0, 34), "CPU", font=font, fill=255)
draw.rectangle(
(26, 34, 126, 34 + 6),
outline=255,
fill=0
)
draw.rectangle(
(26, 34, 26 + cpu, 34 + 6),
outline=255,
fill=255
)
mem = int(psutil.virtual_memory().percent)
draw.text((0, 44), "RAM", font=font, fill=255)
draw.rectangle(
(26, 44, 126, 44 + 6),
outline=255,
fill=0
)
draw.rectangle(
(26, 44, 26 + cpu, 44 + 6),
outline=255,
fill=255
)
disk = int(psutil.disk_usage(mount_point).percent)
draw.text((0, 54), "Disk", font=font, fill=255)
draw.rectangle(
(26, 54, 126, 54 + 6),
outline=255,
fill=0
)
draw.rectangle(
(26, 54, 26 + disk, 54 + 6),
outline=255,
fill=255
)
disp.display(image)
elif elapsed_seconds >= 10 and elapsed_seconds <= 15:
fbuptime = fc.str_uptime
fbspeed = fc.str_max_bit_rate
draw.text(
(0, 0),
"Fritz.Box informations: ",
font=font,
fill=255
)
draw.line((0, 10, width, 10), fill=255)
draw.text(
(0, 14),
"Uptime: ",
font=font,
fill=255
)
draw.text(
(64, 14),
fbuptime,
font=font,
fill=255
)
draw.text(
(0,26),
"Upload-Speed: ",
font=font,
fill=255
)
draw.text(
(50,36),
fbspeed[0],
font=font,
fill=255
)
draw.text(
(0,46),
"Download-Speed: ",
font=font,
fill=255
)
draw.text(
(50,56),
fbspeed[1],
font=font,
fill=255
)
disp2.display(image)
elif elapsed_seconds >= 15 and elapsed_seconds <= 17:
regulator = framerate_regulator(fps=10)
left_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'res', 'Fallin-L.gif'))
left = Image.open(left_path)
size = [min(*disp.size)] * 2
posn = ((disp.width - size[0]), disp.height - size[1])
timecheck = time.time()
while time.time() <= timecheck + giftimer:
for frame in ImageSequence.Iterator(left):
with regulator:
background = Image.new("RGB", disp.size, "white")
background.paste(frame.resize(size, resample=Image.LANCZOS), posn)
disp.display(background.convert("1"))
if time.time() >= timecheck + giftimer:
break
regulator2 = framerate_regulator(fps=10)
right_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'res', 'Fallin-R.gif'))
right = Image.open(right_path)
size = [disp.size]
posn = ((disp.width - size[0]), disp.height - size[1])
timecheck = time.time()
while time.time() <= timecheck + giftimer:
for frame in ImageSequence.Iterator(right):
with regulator2:
background = Image.new("RGB", disp.size, "white")
background.paste(frame.resize(size, resample=Image.LANCZOS), posn)
disp2.display(background.convert("1"))
if time.time() >= timecheck + giftimer:
break
else:
try:
req = requests.get('http://pi.hole/admin/api.php')
data = req.json()
draw.text(
(0, 0),
"Pi-hole (%s)" % data["status"],
font=font,
fill=255
)
draw.line((0, 12, width, 12), fill=255)
draw.text(
(0, 22),
"Blocked: %d (%d%%)" % (
data["ads_blocked_today"],
data["ads_percentage_today"]
),
font=font,
fill=255
)
draw.text(
(0, 32),
"Queries: %d" % data["dns_queries_today"],
font=font,
fill=255
)
draw.line((0, 50, width, 50), fill=255)
draw.text(
(0, 54),
"Blocklist: %d" % data["domains_being_blocked"],
font=font,
fill=255
)
except: ## noqa
draw.text(
(0, 0),
"ERROR!",
font=font,
fill=255
)
disp.display(image)
time.sleep(sleep)
elapsed_seconds += 1
except (KeyboardInterrupt, SystemExit):
print("Exiting...")