}
}
+void reportMessage(const __FlashStringHelper* msg) {
+ char buf[256];
+ strcpy_P(buf, (PGM_P)msg);
+ events.send(buf,"message",millis());
+}
+
void sendInitial(AsyncEventSourceClient *client) {
String mac = WiFi.macAddress();
char buf[256];
sprintf(buf,"{\"_mac\":\"%s\",\"_weather\":\"%s\"}", mac.c_str(), weatherData);
- client->send(buf,"keepalive",millis());
+ client->send(buf,"update",millis());
mac = String();
}
void sendWeather() {
char buf[256];
sprintf(buf,"{\"_weather\":\"%s\"}",weatherData);
- events.send(buf,"keepalive",millis());
+ events.send(buf,"update",millis());
}
void sendKeepalive() {
} else {
sprintf(buf,"{\"_uptime\":\"%d м %d с\", \"_date\":\"%02d.%2d.%04d\", \"_time\":\"%02d:%02d\",\"_heap\":\"%d б\", \"_rssi\":\"%d\", \"_last_sync\":\"%s\", \"_changed\":%s}", mins, uptime, dd, mm, yy, hh, mi, heap, rssi, sync, changed?"true":"false");
}
- events.send(buf,"keepalive",millis());
+ events.send(buf,"update",millis());
}
void apply(const char* name) {
isWebStarted = true;
- Serial.println("Setting authentication...");
strncpy(auth_user,cfg.getCharValue(F("auth_user")),31);
strncpy(auth_pwd,cfg.getCharValue(F("auth_pwd")),31);
server.on("/action", HTTP_GET, [](AsyncWebServerRequest* request) {
if (auth_user && auth_pwd && auth_user[0] && auth_pwd[0] && !request->authenticate(auth_user, auth_pwd)) {
- Serial.print("Failed auth as user: "); Serial.print(auth_user); Serial.print(':'); Serial.print(auth_pwd); Serial.println('!');
return request-> requestAuthentication();
}
if(request->hasParam("name")) {
millisScheduled = millis();
actionScheduled = "save";
}
+ } else if (strcmp(action,"time") == 0) {
+ if(request->hasParam("timestamp")) {
+ unsigned long timestamp = atoi(request->getParam("timestamp")->value().c_str());
+ if (timestamp) {
+ timeval tv = { timestamp, 0 };
+ settimeofday(&tv, nullptr);
+ if (isRTCEnabled) {
+ Serial.println(F("Время установлено вручную"));
+ RTC.adjust(DateTime(timestamp));
+ }
+ }
+ request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Устанавливаю время\"}");
+ } else {
+ request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}");
+ }
+ } else {
+ request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Unsupported action\"}");
}
+ } else {
+ request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}");
}
});
}
});
- server.serveStatic("ui", LittleFS, "/ui.json");
+ server.serveStatic("ui", LittleFS, "/ui.json").setAuthentication(auth_user,auth_pwd);
- server.serveStatic("/", LittleFS, "/web/").setDefaultFile("index.html");
+ server.serveStatic("/", LittleFS, "/web/").setDefaultFile("index.html").setAuthentication(auth_user,auth_pwd);
server.onNotFound([](AsyncWebServerRequest *request){
request->send(404,"text/plain","Not found");
sendInitial(client);
});
+ events.setAuthentication(auth_user,auth_pwd);
+
server.addHandler(&events);
server.begin();
static bool connectInProgress = false;
static unsigned long connectMillis = 0;
if (actionScheduled && millis()>millisScheduled+300) {
- Serial.print(F("Scheduled action ")); Serial.println(actionScheduled);
+ Serial.print(F("Запланированная операция ")); Serial.println(actionScheduled);
//
if (strcmp(actionScheduled,"restart") == 0) {
server.end();
reboot();
} else if (strcmp(actionScheduled,"auth") == 0) {
- Serial.println("New authentication credentials");
+ Serial.println("Логин/пароль изменены");
strncpy(auth_user,cfg.getCharValue(F("auth_user")),31);
strncpy(auth_pwd,cfg.getCharValue(F("auth_pwd")),31);
pendingAuth = false;
} else if (strcmp(actionScheduled,"wifi") == 0) {
- Serial.println("New wifi credentials");
+ Serial.println("Применяю настройки сети");
strcpy(storedSSID,WiFi.SSID().c_str());
strcpy(storedPSK,WiFi.psk().c_str());
WiFi.mode(WIFI_STA);
if (!pendingWiFi && !pendingAuth && cfg.getTimestamp() && cfg.getTimestamp() < now - CFG_AUTOSAVE) {
saveConfig();
+ reportMessage(F("Настройки сохранены"));
Serial.println(F("Настройки сохранены"));
}
}