| 142 | } |
| 143 | |
| 144 | void onAjaxConnect(HttpRequest& request, HttpResponse& response) |
| 145 | { |
| 146 | JsonObjectStream* stream = new JsonObjectStream(); |
| 147 | JsonObject json = stream->getRoot(); |
| 148 | |
| 149 | String curNet = request.getPostParameter("network"); |
| 150 | String curPass = request.getPostParameter("password"); |
| 151 | |
| 152 | bool updating = curNet.length() > 0 && (WifiStation.getSSID() != curNet || WifiStation.getPassword() != curPass); |
| 153 | bool connectingNow = WifiStation.getConnectionStatus() == eSCS_Connecting || network.length() > 0; |
| 154 | |
| 155 | if(updating && connectingNow) { |
| 156 | debugf("wrong action: %s %s, (updating: %d, connectingNow: %d)", network.c_str(), password.c_str(), updating, |
| 157 | connectingNow); |
| 158 | json["status"] = (bool)false; |
| 159 | json["connected"] = (bool)false; |
| 160 | } else { |
| 161 | json["status"] = (bool)true; |
| 162 | if(updating) { |
| 163 | network = curNet; |
| 164 | password = curPass; |
| 165 | debugf("CONNECT TO: %s %s", network.c_str(), password.c_str()); |
| 166 | json["connected"] = false; |
| 167 | connectionTimer.initializeMs<1200>(makeConnection).startOnce(); |
| 168 | } else { |
| 169 | json["connected"] = WifiStation.isConnected(); |
| 170 | debugf("Network already selected. Current status: %s", WifiStation.getConnectionStatusName().c_str()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if(!updating && !connectingNow && WifiStation.isConnectionFailed()) |
| 175 | json["error"] = WifiStation.getConnectionStatusName(); |
| 176 | |
| 177 | response.setAllowCrossDomainOrigin("*"); |
| 178 | response.sendDataStream(stream, MIME_JSON); |
| 179 | } |
| 180 | |
| 181 | void startWebServer() |
| 182 | { |
nothing calls this directly
no test coverage detected