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.

31 lines
1.0 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. void NTPStatus::ntpStatus(AsyncWebServerRequest* request) {
  9. AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE);
  10. JsonObject root = response->getRoot();
  11. // request time now first, this can sometimes force a sync
  12. time_t timeNow = now();
  13. timeStatus_t status = timeStatus();
  14. time_t lastSync = NTP.getLastNTPSync();
  15. root["status"] = (int)status;
  16. root["last_sync"] = lastSync;
  17. root["server"] = NTP.getNtpServerName();
  18. root["interval"] = NTP.getInterval();
  19. root["uptime"] = NTP.getUptime();
  20. // only add now to response if we have successfully synced
  21. if (status != timeNotSet) {
  22. root["now"] = timeNow;
  23. }
  24. response->setLength();
  25. request->send(response);
  26. }