webserver.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <user_config.h>
  2. #include <SmingCore/SmingCore.h>
  3. #include "../include/configuration.h"
  4. #include "webserver.h"
  5. bool serverStarted = false;
  6. HttpServer server;
  7. extern float SensorT, SensorH, SensorHI, SensorCR;
  8. extern String StrCF;
  9. extern time_t NTPLastUpdate;
  10. /*
  11. * Index page.
  12. * Show dht22 data as integer.
  13. */
  14. void
  15. onIndex (HttpRequest &request, HttpResponse &response)
  16. {
  17. TemplateFileStream *tmpl = new TemplateFileStream ("index.html");
  18. auto &vars = tmpl->variables ();
  19. vars["T"] = String (SensorT, 0);
  20. vars["RH"] = String (SensorH, 0);
  21. vars["HI"] = String (SensorHI, 0);
  22. vars["CR"] = String (SensorCR, 0);
  23. vars["CF"] = StrCF; // это первое место, где оно используется
  24. vars["ADC"] = String (system_adc_read ());
  25. vars["VDD"] = String (system_get_vdd33 ());
  26. vars["LASTNTP"] = String (SystemClock.now () - NTPLastUpdate);
  27. response.sendTemplate (tmpl);
  28. }
  29. /*
  30. * Configuration page.
  31. */
  32. void
  33. onConfiguration (HttpRequest &request, HttpResponse &response)
  34. {
  35. ClockConfig cfg = loadConfig ();
  36. if (request.getRequestMethod () == RequestMethod::POST)
  37. {
  38. // Update config
  39. if (request.getPostParameter ("SSID").length () > 0) // Network
  40. {
  41. cfg.NetworkSSID = request.getPostParameter ("SSID");
  42. cfg.NetworkPassword = request.getPostParameter ("Password");
  43. }
  44. if (request.getPostParameter ("TZ").length () > 0) // Correction
  45. {
  46. float tz = request.getPostParameter ("TZ").toFloat ();
  47. if (cfg.AddTZ != tz)
  48. {
  49. cfg.AddTZ = tz;
  50. SystemClock.setTimeZone (cfg.AddTZ);
  51. if (cfg.AddTZ < 0 || cfg.AddTZ > 23)
  52. {
  53. cfg.AddTZ = 0;
  54. }
  55. }
  56. }
  57. if (request.getPostParameter ("BLow").length () > 0) // Low brightness level.
  58. {
  59. cfg.BrightnessLow = request.getPostParameter ("BLow").toInt ();
  60. if (cfg.BrightnessLow < 0 || cfg.BrightnessLow > 15)
  61. {
  62. cfg.BrightnessLow = 0;
  63. }
  64. }
  65. if (request.getPostParameter ("BMid").length () > 0) // Middle brightness level.
  66. {
  67. cfg.BrightnessMiddle = request.getPostParameter ("BMid").toInt ();
  68. if (cfg.BrightnessMiddle < 0 || cfg.BrightnessMiddle > 15)
  69. {
  70. cfg.BrightnessMiddle = 7;
  71. }
  72. }
  73. if (request.getPostParameter ("BHigh").length () > 0) // High brightness level.
  74. {
  75. cfg.BrightnessHigh = request.getPostParameter ("BHigh").toInt ();
  76. if (cfg.BrightnessHigh < 0 || cfg.BrightnessHigh > 15)
  77. {
  78. cfg.BrightnessHigh = 15;
  79. }
  80. }
  81. if (request.getPostParameter ("LLow").length () > 0) // Low light level trh.
  82. {
  83. cfg.LightTrhLow = request.getPostParameter ("LLow").toInt ();
  84. if (cfg.LightTrhLow < 0 || cfg.LightTrhLow > 1023)
  85. {
  86. cfg.LightTrhLow = 0;
  87. }
  88. }
  89. if (request.getPostParameter ("LHigh").length () > 0) // High light level trh.
  90. {
  91. cfg.LightTrhHigh = request.getPostParameter ("LHigh").toInt ();
  92. if (cfg.LightTrhHigh < 0 || cfg.LightTrhHigh > 1023)
  93. {
  94. cfg.LightTrhHigh = 1023;
  95. }
  96. }
  97. saveConfig (cfg);
  98. response.redirect ();
  99. }
  100. TemplateFileStream *tmpl = new TemplateFileStream ("config.html");
  101. auto &vars = tmpl->variables ();
  102. vars["SSID"] = cfg.NetworkSSID;
  103. vars["TZ"] = String (cfg.AddTZ, 2);
  104. vars["LLow"] = String (cfg.LightTrhLow);
  105. vars["LHigh"] = String (cfg.LightTrhHigh);
  106. vars["BLow"] = String (cfg.BrightnessLow);
  107. vars["BMid"] = String (cfg.BrightnessMiddle);
  108. vars["BHigh"] = String (cfg.BrightnessHigh);
  109. response.sendTemplate (tmpl);
  110. }
  111. /*
  112. * роутер
  113. */
  114. void
  115. onFile (HttpRequest &request, HttpResponse &response)
  116. {
  117. String file = request.getPath ();
  118. if (file[0] == '/')
  119. file = file.substring (1);
  120. if (file[0] == '.')
  121. response.forbidden ();
  122. else
  123. {
  124. response.setCache (86400, true); // It's important to use cache for better performance.
  125. response.sendFile (file);
  126. }
  127. }
  128. /// API ///
  129. /*
  130. * API page.
  131. */
  132. void
  133. onApiDoc (HttpRequest &request, HttpResponse &response)
  134. {
  135. TemplateFileStream *tmpl = new TemplateFileStream ("api.html");
  136. auto &vars = tmpl->variables ();
  137. vars["IP"] = (
  138. WifiStation.isConnected () ?
  139. WifiStation.getIP () : WifiAccessPoint.getIP ()).toString ();
  140. response.sendTemplate (tmpl);
  141. }
  142. /*
  143. * Get json data
  144. * данные с датчиков выдаём с максимальным разрешением.
  145. */
  146. void
  147. onApiSensors (HttpRequest &request, HttpResponse &response)
  148. {
  149. JsonObjectStream* stream = new JsonObjectStream ();
  150. JsonObject& json = stream->getRoot ();
  151. json["status"] = (bool) true;
  152. JsonObject& sensors = json.createNestedObject ("sensors");
  153. sensors["temperature"] = String (SensorT, 2);
  154. sensors["humidity"] = String (SensorH, 2);
  155. sensors["heatindex"] = String (SensorHI, 2);
  156. sensors["comfortp"] = String (SensorCR, 2);
  157. sensors["comforts"] = StrCF.c_str (); // второе место
  158. sensors["adcvalue"] = String (system_adc_read ());
  159. sensors["vddvalue"] = String (system_get_vdd33 ());
  160. time_t now = SystemClock.now ();
  161. sensors["datetime"] = String (now);
  162. sensors["lastntp"] = String (now - NTPLastUpdate);
  163. response.sendJsonObject (stream);
  164. }
  165. /*
  166. * Управление выводом. кандидат на удаление.
  167. */
  168. #include "max7219.h"
  169. void
  170. onApiControl (HttpRequest &request, HttpResponse &response)
  171. {
  172. int val = request.getQueryParameter ("bright", "-1").toInt ();
  173. if (val < 1)
  174. {
  175. val = 1;
  176. }
  177. if (val > 15)
  178. {
  179. val = 15;
  180. }
  181. MAX7219_writeData (MAX7219_MODE_INTENSITY, val);
  182. JsonObjectStream* stream = new JsonObjectStream ();
  183. JsonObject& json = stream->getRoot ();
  184. json["status"] = val;
  185. response.sendJsonObject (stream);
  186. }
  187. void
  188. startWebServer ()
  189. {
  190. if (serverStarted)
  191. return;
  192. server.listen (80);
  193. server.addPath ("/", onIndex);
  194. server.addPath ("/api", onApiDoc);
  195. server.addPath ("/api/sensors", onApiSensors);
  196. server.addPath ("/api/control", onApiControl);
  197. server.addPath ("/config", onConfiguration);
  198. server.setDefaultHandler (onFile);
  199. serverStarted = true;
  200. /*
  201. if (WifiStation.isEnabled())
  202. debugf("STA: %s", WifiStation.getIP().toString().c_str());
  203. if (WifiAccessPoint.isEnabled())
  204. debugf("AP: %s", WifiAccessPoint.getIP().toString().c_str());
  205. */
  206. }