-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
89 lines (71 loc) · 2.3 KB
/
console.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
from wifi_manager import WifiManager as wm
import json
from uio import open
def _read_config():
with open(wm.config_file, 'r') as f:
return json.load(f)
def status():
w = wm.wlan()
if w is None:
print('No WLAN')
return
if w.active():
print('WLAN active')
else:
print('WLAN inactive')
return
print('Available WLAN:')
nets = []
for n in w.scan():
nets.append((n[3], n[0].decode()))
nets.sort(reverse=True)
for n in nets:
print(' ', '"', n[1], '", ', n[0], sep='')
config = _read_config()
print()
print('Listed:')
for n in config['known_networks']:
print(' ', '"', n['ssid'], '", "', n['password'], '"', sep='')
print()
apc = config['access_point']['config']
print('essid=', apc['essid'], 'pw=', apc['password'], 'channel=', apc['channel'])
def _delete_wifi_config(s):
new = []
for n in wm.preferred_networks:
if s == n['ssid']:
print('Deleting "', s, '", "', n['password'], '"', sep='')
else:
new.append(n)
return new
def _write_config(nets=None, ap=None):
if nets is None and ap is None:
return
with open(wm.config_file, 'r') as f:
config = json.load(f)
if nets is not None:
config['known_networks'] = nets
if ap is not None:
config['access_point'] = ap
with open(wm.config_file, 'w') as f:
json.dump(config, f)
def delete_wifi(s):
_write_config(nets=_delete_wifi_config(s))
def add_wifi(s, pw):
new = _delete_wifi_config(s)
new.append({'ssid': s, 'password': pw, 'enables_webrepl': False})
_write_config(nets=new)
def new_ap(new_essid=None, new_pw=None, new_ch=None):
with open(wm.config_file, 'r') as f:
ap = json.load(f)['access_point']
apc = ap['config']
if new_essid is not None:
print('Essid', apc['essid'], 'to', new_essid)
apc['essid'] = new_essid
if new_pw is not None:
print('Password', apc['password'], 'to', new_pw)
apc['password'] = new_pw
if new_ch is not None:
print('Channel', apc['channel'], 'to', new_ch)
apc['channel'] = new_ch
ap['config'] = apc
_write_config(ap=ap)