-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_config_wifi.py
142 lines (111 loc) · 3.42 KB
/
web_config_wifi.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
import gc;
import globs;
import config;
from web_pyhtml import pyhtml_parser;
# We need a special manual dictionary of variables because uPython eval()
# doesn't support local variables (
# https://docs.micropython.org/en/latest/genrst/core_language.html#code-running-in-eval-function-doesn-t-have-access-to-local-variables
# )
vars_com = {
'title': ''
,'ver': ''
,'uid': ''
,'mac': ''
,'oper': ''
,'style': ''
,'msg': ''
,'ssid': ''
};
# Returns:
# -
def fill_vars_com(): #{
global vars_com;
print("web_config_wifi.fill_vars_com()");
import os;
# Variables
vars_com['title'] = globs.prod + " " + globs.desc;
vars_com['ver'] = '<a href="' + globs.url + '">' \
+ vars_com['title'] + " " + globs.ver + "<br>\n" \
+ "on " + os.uname().machine + " (" + os.uname().sysname + ") " + "<br>\n" \
+ "uPython " + os.uname().version + " (" + os.uname().release + ")" \
+ '</a>';
vars_com['uid'] = globs.uid;
vars_com['mac'] = globs.mac;
if (config): #{
config.config_load();
if (config.config): #{
if ('ssid' in config.config): #{
vars_com['ssid'] = config.config['ssid'];
#}
#}
#}
gc.collect();
#}
fill_vars_com()
# Returns:
# <s:buffer>, <i:next_offset>
def GET(size_buf, offset, vardict, body): #{
global vars_com;
print("web_config_wifi.GET(size_buf:%d, offset:%d, vardict, body)" % (size_buf, offset));
buf = '';
read = 0;
if (offset == 0): #{
vars_com['oper'] = 'GET';
vars_com['style'] = '';
vars_com['msg'] = '';
#}
tag, buf, read = pyhtml_parser('web_config_wifi.pyhtml', size_buf, offset, vardict, body);
if (tag): #{
print("Processing py tag:|" + buf + "|");
buf = eval(buf, vars_com);
#}
return buf, read;
#}
# Returns:
# <s:buffer>, <i:next_offset>
def POST(size_buf, offset, vardict, body): #{
global vars_com;
print("web_config_wifi.POST(size_buf:%d, offset:%d, vardict, body)" % (size_buf, offset));
buf = '';
read = 0;
if (offset == 0): #{
vars_com['oper'] = 'POST';
vars_com['style'] = '';
vars_com['msg'] = '';
#}
bodyvararr = body.split('&');
bodyvardict = dict(map(lambda s : map(str.strip, s.split('=', 1)), bodyvararr));
if (bodyvardict): #{
for key, val in bodyvardict.items(): #{
if (not key): continue;
if (key == 'ssid'): #{
if (config and config.config): #{
vars_com['ssid'] = val;
config.config['ssid'] = vars_com['ssid'];
#}
elif (key == 'passphrase'): #}{
if (config and config.config): #{
config.config['pass'] = val;
#}
#}
#}
#}
if (offset == 0): #{
try: #{
config.config_save();
# Saved
vars_com['style'] = 'confirmed';
vars_com['msg'] = 'Saved';
except: #}{
vars_com['style'] = 'warning';
vars_com['msg'] = 'Error saving config!';
#}
#}
tag, buf, read = pyhtml_parser('web_config_wifi.pyhtml', size_buf, offset, vardict, body);
if (tag): #{
print("Processing py tag:|" + buf + "|");
buf = eval(buf, vars_com);
#}
return buf, read;
#}
# vim:ts=4:tw=80:sw=4:et:ai:si