Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

  1. #include <NTPStatus.h>
  2. NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
  3. server->on(NTP_STATUS_SERVICE_PATH,
  4. HTTP_GET,
  5. securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1),
  6. AuthenticationPredicates::IS_AUTHENTICATED));
  7. }
  8. String toISOString(tm* time, bool incOffset) {
  9. char time_string[25];
  10. strftime(time_string, 25, incOffset ? "%FT%T%z" : "%FT%TZ", time);
  11. return String(time_string);
  12. }
  13. void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {
  14. AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE);
  15. JsonObject root = response->getRoot();
  16. // grab the current instant in unix seconds
  17. time_t now = time(nullptr);
  18. // only provide enabled/disabled status for now
  19. root["status"] = sntp_enabled() ? 1 : 0;
  20. // the current time in UTC
  21. root["time_utc"] = toISOString(gmtime(&now), false);
  22. // local time as ISO String with TZ
  23. root["time_local"] = toISOString(localtime(&now), true);
  24. // the sntp server name
  25. root["server"] = sntp_getservername(0);
  26. // device uptime in seconds
  27. root["uptime"] = millis() / 1000;
  28. response->setLength();
  29. request->send(response);
  30. }