webserver.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <SmingCore.h>
  2. #include "configuration.h"
  3. bool serverStarted = false;
  4. HttpServer server;
  5. extern float SensorT, SensorH, SensorHI, SensorCR;
  6. extern String StrCF;
  7. extern time_t NTPLastUpdate;
  8. void onIndex(HttpRequest& request, HttpResponse& response)
  9. {
  10. TemplateFileStream* tmpl = new TemplateFileStream("index.html");
  11. auto& vars = tmpl->variables();
  12. vars["T"] = String(SensorT, 1);
  13. vars["RH"] = String(SensorH, 1);
  14. vars["HI"] = String(SensorHI, 0);
  15. vars["CR"] = String(SensorCR, 0);
  16. vars["CF"] = StrCF; // это первое место, где оно используется
  17. vars["VDD"] = String(system_get_vdd33 ());
  18. vars["LASTNTP"] = String (SystemClock.now () - NTPLastUpdate);
  19. response.sendNamedStream(tmpl);
  20. }
  21. void onConfiguration(HttpRequest& request, HttpResponse& response)
  22. {
  23. ClockConfig cfg = loadConfig();
  24. if(request.method == HTTP_POST) {
  25. debugf("Update config");
  26. // Update config
  27. // Network
  28. if(request.getPostParameter("SSID").length() > 0)
  29. {
  30. cfg.NetworkSSID = request.getPostParameter("SSID");
  31. cfg.NetworkPassword = request.getPostParameter("Password");
  32. }
  33. // Correction
  34. if(request.getPostParameter("TZ").length() > 0) {
  35. float tz = request.getPostParameter("TZ").toFloat();
  36. if (cfg.AddTZ != tz) {
  37. cfg.AddTZ = tz;
  38. if (cfg.AddTZ < 0 || cfg.AddTZ > 23) {
  39. cfg.AddTZ = 0;
  40. }
  41. SystemClock.setTimeZone(cfg.AddTZ);
  42. }
  43. }
  44. // Low brightness level.
  45. if (request.getPostParameter ("BLow").length () > 0) {
  46. cfg.BrightnessLow = request.getPostParameter ("BLow").toInt ();
  47. if (cfg.BrightnessLow < LedBrightMin || cfg.BrightnessLow > LedBrightMax) {
  48. cfg.BrightnessLow = LedBrightMin;
  49. }
  50. }
  51. // Middle brightness level.
  52. if (request.getPostParameter ("BMid").length () > 0) {
  53. cfg.BrightnessMiddle = request.getPostParameter ("BMid").toInt ();
  54. if (cfg.BrightnessMiddle < LedBrightMin || cfg.BrightnessMiddle > LedBrightMax) {
  55. cfg.BrightnessMiddle = LedBrightMiddl;
  56. }
  57. }
  58. // High brightness level.
  59. if (request.getPostParameter ("BHigh").length () > 0) {
  60. cfg.BrightnessHigh = request.getPostParameter ("BHigh").toInt ();
  61. if (cfg.BrightnessHigh < LedBrightMin || cfg.BrightnessHigh > LedBrightMax) {
  62. cfg.BrightnessHigh = LedBrightMax;
  63. }
  64. }
  65. saveConfig(cfg);
  66. response.headers[HTTP_HEADER_LOCATION] = "/";
  67. }
  68. debugf("Send template");
  69. TemplateFileStream* tmpl = new TemplateFileStream("config.html");
  70. auto& vars = tmpl->variables();
  71. vars["SSID"] = cfg.NetworkSSID;
  72. vars["TZ"] = String(cfg.AddTZ, 2);
  73. vars["BLow"] = String(cfg.BrightnessLow);
  74. vars["BMid"] = String(cfg.BrightnessMiddle);
  75. vars["BHigh"] = String(cfg.BrightnessHigh);
  76. response.sendNamedStream(tmpl);
  77. }
  78. /**
  79. * @brief Router
  80. */
  81. void onFile(HttpRequest& request, HttpResponse& response)
  82. {
  83. String file = request.uri.getRelativePath();
  84. if(file[0] == '.')
  85. response.code = HTTP_STATUS_FORBIDDEN;
  86. else {
  87. response.setCache(86400, true); // It's important to use cache for better performance.
  88. response.sendFile(file);
  89. }
  90. }
  91. /// API ///
  92. void onApiDoc(HttpRequest& request, HttpResponse& response)
  93. {
  94. TemplateFileStream* tmpl = new TemplateFileStream("api.html");
  95. auto& vars = tmpl->variables();
  96. vars["IP"] = (WifiStation.isConnected() ? WifiStation.getIP() : WifiAccessPoint.getIP()).toString();
  97. response.sendNamedStream(tmpl);
  98. }
  99. /**
  100. * @brief Get json data
  101. * данные с датчиков выдаём с максимальным разрешением.
  102. */
  103. void onApiSensors(HttpRequest& request, HttpResponse& response)
  104. {
  105. JsonObjectStream* stream = new JsonObjectStream();
  106. JsonObject json = stream->getRoot();
  107. json["status"] = (bool)true;
  108. JsonObject sensors = json.createNestedObject("sensors");
  109. sensors["temperature"] = String(SensorT, 2);
  110. sensors["humidity"] = String(SensorH, 2);
  111. sensors["heatindex"] = String(SensorHI, 2);
  112. sensors["comfortp"] = String(SensorCR, 2);
  113. sensors["comforts"] = StrCF.c_str (); //??? второе место
  114. sensors["vddvalue"] = String(system_get_vdd33 ());
  115. time_t now = SystemClock.now();
  116. sensors["datetime"] = String(now);
  117. sensors["lastntp"] = String(now - NTPLastUpdate);
  118. response.sendDataStream(stream, MIME_JSON);
  119. }
  120. #include "tm1650.h"
  121. void onApiControl (HttpRequest &request, HttpResponse &response)
  122. {
  123. int val = request.getQueryParameter ("bright", "-1").toInt ();
  124. if (val < LedBrightMin) { val = LedBrightMin; }
  125. if (val > LedBrightMax) { val = LedBrightMax; }
  126. TM1650_Bright(val);
  127. JsonObjectStream* stream = new JsonObjectStream();
  128. JsonObject json = stream->getRoot();
  129. json["status"] = val;
  130. response.sendDataStream(stream, MIME_JSON);
  131. }
  132. void startWebServer()
  133. {
  134. if(serverStarted)
  135. return;
  136. server.listen(80);
  137. server.paths.set("/", onIndex);
  138. server.paths.set("/api", onApiDoc);
  139. server.paths.set("/api/sensors", onApiSensors);
  140. server.paths.set("/config", onConfiguration);
  141. server.paths.setDefault(onFile);
  142. serverStarted = true;
  143. if(WifiStation.isEnabled())
  144. debugf("STA: %s", WifiStation.getIP().toString().c_str());
  145. if(WifiAccessPoint.isEnabled())
  146. debugf("AP: %s", WifiAccessPoint.getIP().toString().c_str());
  147. }