-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPInfo.py
164 lines (130 loc) · 4.84 KB
/
APInfo.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
#!/usr/bin/python
import os
import sys
import re
import time
from commands import *
from papirus import Papirus
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from time import sleep
import RPi.GPIO as GPIO
user = os.getuid()
if user != 0:
print "Please run script as root"
sys.exit()
# Command line usage
# papirus-buttons
WHITE = 1
BLACK = 0
L_SIZE = 25
M_SIZE = 20
S_SIZE = 15
SW1 = 16
SW2 = 26
SW3 = 20
SW4 = 21
def GetInterfacesStats():
ret = {}
f = open("/proc/net/dev", "r");
data = f.read()
f.close()
r = re.compile("[:\s]+")
lines = re.split("[\r\n]+", data)
for line in lines[2:]:
columns = r.split(line)
if len(columns) < 18:
continue
info = {}
info["rx_bytes"] = columns[2]
info["tx_bytes"] = columns[10]
iface = columns[1]
ret[iface] = info
return ret
def BytesPerSecond(interface):
bandwidth = {'tx_bytes_t1': 0, 'rx_bytes_t1': 0, 'tx_bytes_t2': 0, 'rx_bytes_t2': 0};
interfaceStats_t1 = GetInterfacesStats()
bandwidth['tx_bytes_t1'] = interfaceStats_t1[interface]['tx_bytes']
bandwidth['rx_bytes_t1'] = interfaceStats_t1[interface]['rx_bytes']
time.sleep(1)
interfaceStats_t2 = GetInterfacesStats()
bandwidth['tx_bytes_t2'] = interfaceStats_t2[interface]['tx_bytes']
bandwidth['rx_bytes_t2'] = interfaceStats_t2[interface]['rx_bytes']
return {'delta_tx' : int(bandwidth['tx_bytes_t2']) - int(bandwidth['tx_bytes_t1']), 'delta_rx' : int(bandwidth['rx_bytes_t2']) - int(bandwidth['rx_bytes_t1'])}
def APName():
hostapConf = open("/etc/hostapd/hostapd.conf","r")
lines = hostapConf.readlines()
for line in lines:
words = line.split('=')
if len(words) > 1:
for word in words:
if word == 'ssid':
apName = words[1]
return apName
def IPAddress():
interfacesInfo = getoutput("ip addr show br0")
interfacesInfoSplit = interfacesInfo.split()
for index in range(0,len(interfacesInfoSplit)):
if interfacesInfoSplit[index] == 'inet':
return (interfacesInfoSplit[index+1].split('/'))[0]
def InterfaceInfo():
interfacesConf = open("/etc/network/interfaces","r")
br0Conf = " "
addToBr0Conf = False
lines = interfacesConf.readlines()
for index in range(0,len(lines)):
words = lines[index].split()
for word in words:
if word == "Bridge":
addToBr0Conf = True
elif word == "dns-nameservers":
addToBr0Conf = False
if addToBr0Conf:
br0Conf += lines[index+1].lstrip()
return br0Conf
def write_text(papirus, text, size):
# initially set all white background
image = Image.new('1', papirus.size, WHITE)
# prepare for drawing
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', size)
# Calculate the max number of char to fit on line
line_size = (papirus.width / (size*0.65))
current_line = 0
for l in re.split(r'[\r\n]+', text):
current_line += 1
draw.text( (0, ((size*current_line)-size)) , l, font=font, fill=BLACK)
papirus.display(image)
papirus.partial_update()
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(SW1, GPIO.IN)
GPIO.setup(SW2, GPIO.IN)
GPIO.setup(SW3, GPIO.IN)
GPIO.setup(SW4, GPIO.IN)
interface = 'wlan0'
scaleSpeed = 262144
papirus = Papirus()
write_text(papirus,"--4-----3-----2-----1--------\r\nPress 1 for basic info\r\nPress 2 for stats\r\nPress 3 for detailed info", S_SIZE)
while True:
if GPIO.input(SW1) == False:
write_text(papirus,"AP= " + APName() + "\r\n" + "IP= " + IPAddress(), L_SIZE)
if GPIO.input(SW2) == False:
while GPIO.input(SW1) and GPIO.input(SW3) and GPIO.input(SW4):
netstats = BytesPerSecond(interface)
write_text(papirus,"Long press 1,3,4 exit\r\nBandwidth:"
+ "\r\ntx: " + str(netstats['delta_tx']/1024)
+ " Kb/s\r\n"
+ "=" + ">"*(netstats['delta_tx']/scaleSpeed)
+ "\r\nrx: " + str(netstats['delta_rx']/1024)
+ " Kb/s\r\n"
+ "=" + "<"*(netstats['delta_rx']/scaleSpeed), M_SIZE)
time.sleep(5)
if GPIO.input(SW3) == False:
write_text(papirus,InterfaceInfo(), S_SIZE)
if GPIO.input(SW4) == False:
write_text(papirus,"Four", M_SIZE)
sleep(0.1)
if __name__ == '__main__':
main()