forked from carterw/esp8266Wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessPoints.py
executable file
·136 lines (115 loc) · 3.99 KB
/
accessPoints.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
import ujson
import os
import utime
class AccessPoints:
'''
Reads/writes a parameters file in JSON format
'''
apParams = {}
accessPointsFilename = ''
apIndex = 0
def __init__(self, filename):
self.accessPointsFilename = filename
def readApFile(self):
fileName = self.accessPointsFilename
fileList = os.listdir()
if fileName in fileList:
f = open(self.accessPointsFilename)
data = f.read()
f.close()
# print(data)
self.apParams = ujson.loads(data)
else:
self.apParams['accessPoints'] = {}
# self.setSection('accessPoints', {})
self.writeApFile()
def getSection(self, sectionName):
self.readApFile()
if sectionName in self.apParams:
section = self.apParams[sectionName]
apParams = {}
return section
else:
print('getSection', sectionName, ' not found')
return None # should raise exception
def setSection(self, sectionName, sectionData):
self.readApFile()
self.apParams[sectionName] = sectionData
self.writeApFile()
apParams = {}
def getAccessPointData(self, ssid):
accessPoints = self.getSection('accessPoints')
if ssid in accessPoints:
return accessPoints[ssid]
else:
# print(ssid, ' password not found')
return {}
def setAccessPointData(self, ssid, point):
accessPoints = self.getSection('accessPoints')
accessPoints[ssid] = point
self.setSection('accessPoints', accessPoints)
def getNextAccessPoint(self):
accessPoints = self.getSection('accessPoints')
apLength = len(accessPoints)
if self.apIndex == apLength:
self.apIndex = 0
ap = next( v for i, v in enumerate(accessPoints.values()) if i == self.apIndex )
self.apIndex += 1
return ap
def checkAccessPointIgnore(self, point):
if 'verified' in point:
if point['verified'] == 2:
return True
return False
def getAccessPointPassword(self, ssid):
accessPoints = self.getSection('accessPoints')
password = ''
if ssid in accessPoints:
point = accessPoints[ssid]
# print('password found!!!', point['password'])
return point['password']
return password
def writeApFile(self):
f = open(self.accessPointsFilename, 'w')
jsonData = ujson.dumps(self.apParams)
f.write(jsonData)
f.close()
class NetScan:
def doScan(self, wlan):
points = []
wlan.active(True)
accessPoints = wlan.scan()
for i, point in enumerate(accessPoints):
# print(point)
ssid = point[0]
ssid = str(ssid, "utf-8")
rssi = point[3]
authmode = point[4]
hidden = point[5]
if authmode == 0:
authmode = 'open'
else:
authmode = 'pw'
if hidden == 0:
hidden = 'visible'
else:
hidden = 'hidden'
pobj = {"ssid": ssid, "rssi": str(rssi), "authmode": authmode, "hidden": hidden}
points.append(pobj)
wlan.active(False)
return points
def checkLoop(self, wlan, totalMS):
'''
Look to see if we have a network connection periodically. If/when
we do, return True immediately. If we haven't seen one within
totalMS we return False
'''
checkInterval = 500
timeElapsed = 0
while timeElapsed < totalMS:
status = wlan.isconnected()
if status:
return True
utime.sleep_ms(checkInterval)
timeElapsed += checkInterval
return False