-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetmonitor.py
More file actions
337 lines (298 loc) · 11 KB
/
netmonitor.py
File metadata and controls
337 lines (298 loc) · 11 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from batterystate import BatteryState
from display import Display
from wifitools import WLAN, WLANList, WIFIScanner
from bletools import BLEScanner, BLEItem
from displayhelper import BaseItem
import time
import gc
import bluetooth
class LANItem( BaseItem ):
'''
A LAN/Wifi item that we have found, uses the BaseItem class, so that we have
the needed functionality for the graphics display code.
Note: Override the needed functions to ensure that the proper functionality
is presented when called.
'''
def __init__( self, wlan, function ):
'''
Parameters:
wlan - A wifi wlan object
function - The function to hand off when someone calls "getFunction"
'''
self.wlan = wlan
msg = str(wlan)
super().__init__( msg, function )
def getColor(self):
'''
This member function is called when we want to determine the color to
print something in. In the case of the WLAN, we want to convert the
RSSI to a color value, based on the standard of what is a good power
level and what is not.
Returns:
A color that represents how good the signal is.
'''
if self.wlan.rssi > -67:
return Display.GREEN # Excellent
elif self.wlan.rssi > -70:
return Display.YELLOW # Good
elif self.wlan.rssi > -80:
return Display.ORANGE # Limited
elif self.wlan.rssi > -90:
return Display.RED # Poor
else:
return Display.RED # Yeah, not worth it.
def getName( self ):
'''
This member function returns the name of the WLAN object, note tht if
this is HIDDEN WLAN, we return the bssid instead of the SSID
Returns:
The functional name of the WLAN
'''
if self.wlan.ssid == "":
return f"{self.wlan.bssid}"
return f"{self.wlan.ssid}"
def __str__(self):
'''
Override the Dunder function to return a string based off of the wlan
'''
return str(self.wlan)
def dummy( item ):
print( f"Called {item}" )
def scaleValue( value, start,stop):
'''
Given the value passed in, convert it so that it is between the start
and stop values passed as well.
Parameters:
value - The value we wish to scale
start - The start of the range
stop - The end of the range
Returns:
The scaled value.
'''
result = abs(value) + start
if result > stop:
result = stop
return result
def displayGraphLine( xstart, ystart, height):
'''
Display a bar graph line. The assumption is that the color of the line
is already set. In order to draw the line we create a rectangle 2 pixels
wide.
Two more rectangles are drawn, one *above* the area we just drew clearing
out anything that may be left over, and then another one is drawn using the
full range in GREY to poing out where we are currently observing.
Parameters:
xstart - x position of the start of the graph
ystart - y position of the start of the graph
height - how high does the rectangle go relative to the ystart position
'''
display.rectangle( xstart, ystart, 2, height-ystart )
display.set_pen( Display.BLACK )
display.rectangle( xstart, 102, 2, ystart-102)
display.set_pen( Display.GREY )
display.rectangle( xstart+2, 102, 2, height-102)
def watchSingleNetwork( item ):
'''
Monitor a single network. Get the network by SSID and report on the
power/channel and Security. Generate a graph of the relative signale
strength
Parameters:
item - the network wlan item we are monitoring
Returns:
True - if we want to coninue viewing other networks
False - Terminate the montioring and bounce back to the root function
'''
panel = display.createPanel( f"Monitoring {item.getName()}" )
count = 0
graphHeight = display.height - 2 #panel.height
graphWidth = panel.width
xpos = 2 #panel.startx
panel.displayPanel(False)
display.set_pen( Display.PANEL )
display.line( 0, 100, graphWidth, 100 )
tabs = [0,100]
while True:
if display.getCancelButton():
return False
if display.getSelButton():
return True
if count % 20 == 0:
display.LED( 0, 0, 128 )
lan = wscanner.scanForSpecificWLAN( item.wlan.ssid, 1 )
if lan is None:
lan = item.wlan
else:
item.wlan = lan
display.LED( 0, 0, 0 )
panel.textAtClear( 2 )
panel.textAt( f"Channel:\t{lan.channel}", 1, Display.GREY, tabs=tabs )
panel.textAt( f"RSSI:\t{lan.rssi}", 2, item.getColor(),tabs=tabs)
panel.textAt( f"Security:\t{lan.getSecurity()}", 3, Display.GREY,tabs=tabs)
value = scaleValue( lan.rssi, 100, graphHeight )
display.set_pen( item.getColor() )
displayGraphLine( xpos, value, graphHeight)
xpos += 2
if xpos > graphWidth:
xpos = panel.startx
count = 0
display.update()
count += 1
time.sleep( 0.1 )
return True
def systemStatus(item):
'''
Display the status of the system, i.e. current battery level, memory available etc.
Parameters:
item - an item function from the object that was selected by the listbox,
not used.
'''
panel = display.createPanel( 'System Status' )
count = 0
tabs = [0, 180]
panel.displayPanel(False)
bs = BatteryState()
while not display.getCancelButton():
if count % 20 == 0:
panel.displayPanel(False)
count = 0
gc.collect()
panel.textAt( f"Memory Used:\t{gc.mem_alloc()}", 1, Display.GREY, tabs=tabs )
panel.textAt( f"Memory Free:\t{gc.mem_free()}", 2, Display.GREY, tabs=tabs )
if bs.isCharging():
panel.textAt( f"Battery:\tCharging...", 3, Display.GREY, tabs=tabs )
else:
panel.textAt( f"Voltage:\t{bs.getVoltage():.2f}", 3, Display.GREY,tabs=tabs )
panel.textAt( f"Percentage:\t{bs.getPercentage():.2f}%", 4, Display.GREY,tabs=tabs )
display.update()
count += 1
time.sleep( 0.1 )
return False
def watchSingleBLE( item ):
'''
Monitor a single Bluetooth entry. The Select button will switch between
information collected mode, an a bar graph of the current RSSI of the device,
the graph helps in locating where the bluetooth device is by following a
stronger field strength.
Parameters:
item - the item we are monitoring. This item actually gets updated in
the background so we can see any new messages that come in.
Returns:
True - Continue processing the called from menu
False - Terminate the called from menu
'''
panel = display.createPanel( f"Monitoring {item.getName()}" )
panel.displayPanel(False)
graphHeight = display.height - 2 #panel.height
graphWidth = panel.width
count = 0
displayMsgs = True
xpos = 2
while True:
if display.getCancelButton():
return True
if display.getSelButton():
panel.clearPanel()
count = 0
if displayMsgs:
xpos = 2
display.set_pen( Display.PANEL )
display.line( 0, 100, graphWidth, 100 )
displayMsgs = False
else:
displayMsgs = True
#
if count % 20 == 0:
panel.textAt( f"Address Type: {item.getAddrType()}", 1, Display.GREY )
panel.textAtClear( 2 )
panel.textAt( f"RSSI: {item.rssi}",2, item.getColor() )
if displayMsgs:
line = 3
for data in item.data:
panel.textAt( f"{data[0]}: {data[1]}",line,Display.GREY,wrap=False)
line += 1
else:
value = scaleValue( item.rssi, 100, graphHeight )
display.set_pen( item.getColor() )
displayGraphLine( xpos, value, graphHeight)
xpos += 2
if xpos > graphWidth:
xpos = 2
display.update()
count += 1
time.sleep( 0.1 )
def bluetoothAsyncFunction():
gc.collect()
return scanner.get_scan_results()
def bluetoothDisplay( item ):
'''
Display all of the bluetooth items we find. The call to scanner.get_scan_results
simply returns any and all items we have located, the scanning is occuring in
the background.
Parameters:
item - The item from the main menu
'''
listBox = display.createListBox( 'Blue Tooth' )
running = True
scanner.start_scan(0,active=True)
items = []
tabs = [0,190, 210, 270,0]
while running:
items = scanner.get_scan_results()
if len(items):
listBox.setList( items, tabs )
item = listBox.draw(asyncFunc=bluetoothAsyncFunction)
if item is None:
running = False
else:
function = item.getFunction()
running = function(item)
else:
time.sleep( 0.1 )
scanner.stop_scan()
return False
def networkDisplay(item):
'''
Display the currently available wireless networks available, and display
information about them.
Parameters:
A context item from where every we were called from. In this
case we do not care about the context item, as we are using it
to simply redirect us here.
Returns: False
'''
panel = display.createListBox( "Networking" )
panel.displayPanel()
wlanList = WLANList()
running = True
tabs = [0,240,275,0]
while running:
panel.changeTitle( "Scanning...", True )
display.LED( 0, 0, 128 )
wscanner.scanForWLANS( wlanList, 1 )
wlanList.sortItems()
items = []
for lan in wlanList:
items.append( LANItem( lan, watchSingleNetwork ) )
display.LED( 0, 0, 0 )
panel.changeTitle( "Networking" )
panel.setList( items, tabs )
item = panel.draw()
if not item is None:
function = item.getFunction()
running = function( item )
return False
scanner = BLEScanner(bluetooth.BLE(),watchSingleBLE)
wscanner = WIFIScanner()
display = Display()
mainItems = []
mainItems.append( BaseItem( f"Status", systemStatus ) )
mainItems.append( BaseItem( f"BlueTooth", bluetoothDisplay ) )
mainItems.append( BaseItem( f"Networks", networkDisplay ) )
mainPanel = display.createListBox( "NetMonitor v1.0" )
mainPanel.setList( mainItems )
gc.enable()
while True:
item = mainPanel.draw()
if not item is None:
function = item.getFunction()
function( item )