-
Notifications
You must be signed in to change notification settings - Fork 17
/
view.html
245 lines (218 loc) · 9.62 KB
/
view.html
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
<h2>WiFi Config</h2>
<br>
<p>Connect emonPi / emonBase to local WiFi network.</p>
<P>Selected country: <span id="country"></span></p>
<button id="wifi_scan" class="btn btn-info">Scan</button>
<br><br>
<div class="wifi_scan_alert alert alert-info hide">Scanning for networks...</div>
<b>Select one or more available WiFi networks:</b>
<div id="networks"></div>
<button id="wifi_save" class="btn btn-warning">Save and connect</button><br><br>
<div class="wifi_save_alert alert alert-info hide"></div>
<div style="font-size:14px; color:#444"><b>Note:</b> If you are switching from one WiFi network to another, make sure to connect to the same WiFi network with your computer.<br>If the emonPi/base hostname is preserved on the new network, this page should pick it up, otherwise please check the new IP address on the emonPi LCD or use an app such as <a href="https://www.fing.com">Fing</a> to locate.</div>
<hr>
<button id="wifi_info" class="btn btn-info" style="float:right">Refresh</button>
<button id="wifi_restart" class="btn btn-info" style="float:right">Restart</button>
<button id="wifi_stop" class="btn btn-info" style="float:right">Stop</button>
<button id="wifi_start" class="btn btn-info" style="float:right">Start</button>
<h3>Connection Info</h3>
<table class="table">
<tr>
<td>
<b>Status: <span class="status"></span></b><br>
<b>IP Address: </b><span class="IPAddress"></span><br>
<b>Mac Address: </b><span class="MacAddress"></span><br>
<b>Sub Net Mask: </b><span class="SubNetMask"></span><br>
<b>Rx Packets: </b><span class="RxPackets"></span><br>
<b>Tx Packets: </b><span class="TxPackets"></span><br>
<b>Rx Bytes: </b><span class="RxBytes"></span><br>
<b>Tx Bytes: </b><span class="TxBytes"></span><br>
</td>
<td>
<b>Bitrate: </b><span class="Bitrate"></span><br>
<b>Frequency: </b><span class="Freq"></span><br>
<b>Link Quality: </b><span class="LinkQuality"></span><br>
<b>Signal Quality: </b><span class="SignalLevel"></span><br>
</td>
</tr>
</table>
<h3>Network Log</h3>
<pre id="update-log-bound"><div id="update-log"></div></pre>
<script>
var config = wifi_getconfig();
var networks = config.networks;
var country = config.country;
$("#country").html(country);
for (z in networks) {
networks[z].enabled = true;
}
var wifi_save_alert = false;
var wifi_save_timer = false;
draw_network_list();
wifi_scan();
wifi_info();
setInterval(wifi_info,5000);
setInterval(getlog,5000);
$("#wifi_start").click(function(){
$.ajax({ type: 'POST', url: "wifi/start.json" });
});
$("#wifi_stop").click(function(){
$.ajax({ type: 'POST', url: "wifi/stop.json" });
});
$("#wifi_restart").click(function(){
$.ajax({ type: 'POST', url: "wifi/restart.json" });
});
$("#wifi_scan").click(function(){
wifi_scan();
});
$("#wifi_info").click(function(){
wifi_info();
});
$("#wifi_save").click(function(){
$(".wifi_save_alert").html("Saving config and restarting WiFi interface, please wait 30s");
$(".wifi_save_alert").show();
wifi_save_timer = (new Date()).getTime();
wifi_save_alert = true;
var networks_to_save = {};
for (z in networks)
{
var psk = $(".psk[ssid='"+z+"']").val();
var enabled = false;
if ($(".enabled[ssid='"+z+"']")[0]!=undefined)
enabled = $(".enabled[ssid='"+z+"']")[0].checked;
networks[z]["PSK"] = psk;
networks[z].enabled = enabled;
if (enabled) networks_to_save[z] = networks[z];
}
$.ajax({type: 'POST', url: "wifi/setconfig.json", data: "networks="+encodeURIComponent(JSON.stringify(networks_to_save))+"&country="+country, dataType: 'text', async: true,
success: function(data) {}
});
});
$("#networks").on("click",".showpsk",function(){
var ssid = $(this).attr("ssid");
if ($(".psk[ssid='"+ssid+"']").attr("type")=="password") {
$(".psk[ssid='"+ssid+"']").removeAttr("type");
$(".psk[ssid='"+ssid+"']").prop("type","text");
$(this).removeClass("icon-eye-open");
$(this).addClass("icon-eye-close");
} else {
$(".psk[ssid='"+ssid+"']").removeAttr("type");
$(".psk[ssid='"+ssid+"']").prop("type","password");
$(this).removeClass("icon-eye-close");
$(this).addClass("icon-eye-open");
}
});
function wifi_info()
{
$.ajax({url: "wifi/info.json", dataType: 'json', async: true,
error: function(){
if (wifi_save_alert) {
var time_left = 30-Math.round(((new Date()).getTime() - wifi_save_timer)*0.001);
if (time_left>0) {
$(".wifi_save_alert").html("Saving config and restarting WiFi interface, please wait "+time_left+"s");
} else {
$(".wifi_save_alert").html("Saving config and restarting WiFi interface, this seems to be taking longer than usual, try refreshing the page");
}
}
},
success: function(data) {
if (data.success!=undefined && data.message=="Username or password empty") {
window.location = "/";
}
if (wifi_save_alert) {
wifi_save_alert = false;
$(".wifi_save_alert").html("WiFi reconnected. Connection info updated below:");
setTimeout(function(){
$(".wifi_save_alert").hide();
},5000);
}
if (data["status"]=="connected") {
$(".status").html("<span style='color:#00aa00'>Connected</span>");
} else {
$(".status").html("<span style='color:#aa0000'>Disconnected</span>");
}
$(".IPAddress").html(data["IPAddress"]);
$(".MacAddress").html(data["MacAddress"]);
$(".SubNetMask").html(data["SubNetMask"]);
$(".RxPackets").html(data["RxPackets"]);
$(".TxPackets").html(data["TxPackets"]);
$(".RxBytes").html(data["RxBytes"]);
$(".TxBytes").html(data["TxBytes"]);
$(".Bitrate").html(data["Bitrate"]);
$(".Freq").html(data["Freq"]);
$(".LinkQuality").html(data["LinkQuality"]);
$(".SignalLevel").html(data["SignalLevel"]);
},
timeout:1000
});
}
function wifi_scan()
{
$(".wifi_scan_alert").show();
$.ajax({url: "wifi/scan.json", dataType: 'json', async: true,
success: function(data) {
if (data.success!=undefined && data.message!=undefined) {
alert(data.message);
} else {
for (z in data) {
if (networks[z]==undefined) networks[z] = {};
for (key in data[z]) {
networks[z][key] = data[z][key];
}
}
}
draw_network_list();
$(".wifi_scan_alert").hide();
}
});
}
function wifi_getconfig()
{
var config = {};
$.ajax({url: "wifi/getconfig.json", dataType: 'json', async: false,
success: function(data) { config = data; }
});
if (config.length==0) config = {};
return config;
}
function draw_network_list()
{
var out = "";
for (z in networks)
{
if (networks[z]['SIGNAL']!=0) {
out += "<tr class='success'>";
out += "<td><input class='enabled' ssid='"+z+"' type='checkbox'/ ></td>";
out += "<td><b>SSID</b></td>";
out += "<td>"+z+" (Security: ["+networks[z]['SECURITY']+"])</td>";
out += "<td>- "+networks[z]['SIGNAL']+" dBm</td>";
out += "<tr class='success'><td></td><td><b>PSK</b></td>";
} else {
out += "<tr>";
out += "<td><input class='enabled' ssid='"+z+"' type='checkbox'/ ></td>";
out += "<td><b>SSID</b></td>";
out += "<td>"+z+"</td>";
out += "<td><i>---</i></td>";
out += "<tr><td></td><td><b>PSK</b></td>";
}
out += "<td><input class='psk' ssid='"+z+"' type='password' value='";
if (networks[z]['PSK']!=undefined) out += networks[z]['PSK'];
out += "'/ > <i ssid='"+z+"' class='showpsk icon-eye-open' style='cursor:pointer'></i></td><td></td></tr>";
out += "</tr>";
}
$("#networks").html("<table class='table'>"+out+"</table>");
for (z in networks) {
if (networks[z].enabled) {
$(".enabled[ssid='"+z+"']")[0].checked = true
}
}
}
function getlog() {
$.ajax({ url: "wifi/log", async: true, dataType: "text", success: function(result)
{
$("#update-log").html(result);
$("#update-log-bound").scrollTop = $("#update-log-bound").scrollHeight;
}
});
}
</script>