33
44namespace thing {
55
6- Portal::Portal (const Config& config) : config_(config), callback_([](const Config&){}) {}
6+ Portal::Portal (const Config& config)
7+ : config_(config),
8+ callback_ ([](const Config&){}),
9+ debug_ ([](const char *){}) {}
10+
11+ void Portal::SetDebugHandler (std::function<void (const char * message)> handler) {
12+ debug_ = handler;
13+ }
714
815void Portal::Start () {
916 server_.on (" /" , [&] () {
@@ -50,13 +57,44 @@ void Portal::Start() {
5057 }
5158 }
5259 }
60+ function scan() {
61+ document.getElementById('scanning').style.display='inline';
62+ var xhr = new XMLHttpRequest();
63+ xhr.open("GET", "/wifi/scan", true);
64+ xhr.onreadystatechange = function () {
65+ if (xhr.readyState==4 && xhr.status==200) {
66+ load_networks(JSON.parse(xhr.responseText));
67+ document.getElementById('scanning').style.display='none';
68+ }
69+ }
70+ xhr.send();
71+ }
72+ function load_networks(networks) {
73+ select = document.getElementById('networks');
74+ select.innerHtml = '';
75+ networks.sort(function(a, b) {
76+ return b.rssi - a.rssi;
77+ });
78+ networks.forEach(function(network) {
79+ option = document.createElement('option');
80+ option.value = network.ssid;
81+ option.text = network.ssid + ' (' + network.rssi + ')';
82+ select.add(option);
83+ });
84+ select.style.display='inline';
85+ }
86+ function set_network(select) {
87+ document.getElementById('ssid').value = select[select.selectedIndex].value;
88+ }
5389 </script>
5490 </head>
5591 <body>
5692 <div>Host: <input id='host'></div>
5793 <div>Auth: <input id='auth'></div>
5894 <div>Path: <input id='path'></div>
59- <div>Wifi SSID: <input id='ssid'></div>
95+ <div>Wifi SSID: <input id='ssid'><button onclick='scan();'>scan</button></div>
96+ <div id='scanning' style='display:none'>Scanning...</div>
97+ <div><select size=10 id='networks' style='display:none' onchange='set_network(this);'></select></div>
6098 <div>Wifi Key: <input id='key'></div>
6199 <div><button onclick='send_config();'>Save</button></div>
62100 <div id='loading'>Loading....</div>
@@ -67,6 +105,7 @@ void Portal::Start() {
67105 static const PROGMEM char type[] = " text/html" ;
68106
69107 server_.send_P (200 , type, page);
108+ debug_ (" served root page." );
70109 });
71110
72111 server_.on (" /config" , [&] () {
@@ -82,9 +121,11 @@ void Portal::Start() {
82121 String buffer;
83122 root.printTo (buffer);
84123 server_.send (200 , " application/json" , buffer);
124+ debug_ (" config retrieved" );
85125 } else if (server_.method () == HTTP_POST) {
86126 if (!server_.hasArg (" config" )) {
87127 server_.send (500 , " text/plain" , " Missing config.\r\n " );
128+ debug_ (" Config updated called without param." );
88129 return ;
89130 }
90131 String config = server_.arg (" config" );
@@ -96,9 +137,28 @@ void Portal::Start() {
96137 config_.wifi_key = root[" wifi_key" ].asString ();
97138 callback_ (config_);
98139 server_.send (200 , " text/plain" , " " );
140+ debug_ (" config updated." );
141+ }
142+ });
143+
144+ server_.on (" /wifi/scan" , [&] () {
145+ int net_count = WiFi.scanNetworks ();
146+ DynamicJsonBuffer json_buffer;
147+ JsonArray& data = json_buffer.createArray ();
148+ for (int i=0 ; i < net_count; i++) {
149+ JsonObject& entry = data.createNestedObject ();
150+ entry[" ssid" ] = WiFi.SSID (i);
151+ entry[" rssi" ] = WiFi.RSSI (i);
99152 }
153+
154+ String buffer;
155+ data.printTo (buffer);
156+ server_.send (200 , " application/json" , buffer);
157+ debug_ (" served networks." );
100158 });
159+
101160 server_.begin ();
161+ debug_ (" Portal started." );
102162}
103163
104164void Portal::Loop () {
0 commit comments