webserver.cpp 5.8 KB

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