-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingwidget.py
executable file
·60 lines (48 loc) · 1.71 KB
/
pingwidget.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
#!/usr/bin/env python3
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from widget import Widget
from PIL import Image, ImageDraw, ImageFont
import time
from pythonping import ping
class PingWidget(Widget):
def __init__(self,x=0,y=0,color=None,size=15,width=64,height=64,target=None,every=60):
if color is None:
color = (0,255,0)
super(PingWidget,self).__init__(x,y,color,size,width,height)
self.lastping = False
self.target = target
self.every = every
self.lasttime = 0
def update(self):
if self.target != None and time.time() > self.lasttime + self.every:
try:
goodPing = ping(self.target,count=1,verbose=False).success()
except:
goodPing = False
if (goodPing != self.lastping):
if (goodPing):
color = self.color
else:
color = (255,0,0)
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
draw.point((self.x,self.y),fill=color)
self.changed = True
else:
self.changed = False
self.lastping = goodPing
self.lasttime = time.time()
else:
self.changed = False
if __name__ == "__main__":
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
options.drop_privileges = False
matrix = RGBMatrix(options = options)
myping = PingWidget(every=10, target="192.168.1.254")
while True:
myping.update();
if (myping.changed):
matrix.SetImage(myping.image.convert("RGB"))
time.sleep(0.5)