httpServer.ino 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. #include "html.h"
  2. #include "html_conf.h"
  3. #include "html_confAdd.h"
  4. #include "html_confAdv.h"
  5. #include "html_confBas.h"
  6. #include "html_confDevWiFi.h"
  7. #include "html_confMqtt.h"
  8. #include "html_confWeb.h"
  9. #include "html_confTime.h"
  10. #include "html_confLog.h"
  11. #include "html_main.h"
  12. #include "html_redTemps.h"
  13. #include "html_sysinfo.h"
  14. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  15. #include "html_console_ws.h"
  16. #endif
  17. #ifdef ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
  18. #include "html_console.h"
  19. #endif
  20. void httpServerHandleMainPage()
  21. {
  22. httpServerSendHtmlHeadChunked();
  23. httpServer.sendContent_P(html_main_script);
  24. httpServer.sendContent_P(html_head_end);
  25. httpServer.sendContent_P(html_bodytag_jsinit);
  26. httpServerSendHtmlBodyPageheadChunked();
  27. httpServer.sendContent_P(html_main_body);
  28. if (httpIsAuthenticatedAdmin())
  29. {
  30. httpServer.sendContent_P(html_main_body_adminonly);
  31. #ifdef ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
  32. if (confWeb.wConsole)
  33. httpServer.sendContent_P(html_main_body_adminonly_console);
  34. #endif
  35. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  36. if (confWeb.wsConsole)
  37. httpServer.sendContent_P(html_main_body_adminonly_wsconsole);
  38. #endif
  39. }
  40. httpServer.sendContent_P(html_main_body2);
  41. httpServerSendHtmlFooterChunked();
  42. }
  43. void httpServerHandleRedTempsPage()
  44. {
  45. httpServerSendHtmlHeadChunked();
  46. httpServer.sendContent_P(html_redTemps_script);
  47. httpServer.sendContent_P(html_head_end);
  48. httpServer.sendContent_P(html_bodytag_jsinit);
  49. httpServerSendHtmlBodyPageheadChunked();
  50. httpServer.sendContent_P(html_redTemps_body);
  51. httpServerSendHtmlFooterChunked();
  52. }
  53. void httpServerHandleConfPage()
  54. {
  55. httpServerSendHtmlHeadChunked();
  56. httpServer.sendContent_P(html_head_end);
  57. httpServer.sendContent_P(html_bodytag);
  58. httpServerSendHtmlBodyPageheadChunked();
  59. httpServer.sendContent_P(html_conf_body);
  60. #ifdef ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
  61. if (confWeb.wConsole)
  62. httpServer.sendContent_P(html_conf_body_console);
  63. #endif
  64. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  65. if (confWeb.wsConsole)
  66. httpServer.sendContent_P(html_conf_body_wsconsole);
  67. #endif
  68. httpServer.sendContent_P(html_conf_body_end);
  69. httpServerSendHtmlFooterChunked();
  70. }
  71. void httpServerHandleConfWebPage()
  72. {
  73. httpServerSendHtmlHeadChunked();
  74. httpServer.sendContent_P(html_confweb_script);
  75. httpServer.sendContent_P(html_head_end);
  76. httpServer.sendContent_P(html_bodytag_jsinit);
  77. httpServerSendHtmlBodyPageheadChunked();
  78. httpServer.sendContent_P(html_confweb_body);
  79. httpServerSendHtmlFooterChunked();
  80. }
  81. void httpServerHandleConfMqttPage()
  82. {
  83. httpServerSendHtmlHeadChunked();
  84. httpServer.sendContent_P(html_confmqtt_script);
  85. httpServer.sendContent_P(html_head_end);
  86. httpServer.sendContent_P(html_bodytag_jsinit);
  87. httpServerSendHtmlBodyPageheadChunked();
  88. httpServer.sendContent_P(html_confmqtt_body);
  89. httpServerSendHtmlFooterChunked();
  90. }
  91. void httpServerHandleConfDevWiFiPage()
  92. {
  93. httpServerSendHtmlHeadChunked();
  94. httpServer.sendContent_P(html_confDevWiFi_script);
  95. httpServer.sendContent_P(html_head_end);
  96. httpServer.sendContent_P(html_bodytag_jsinit);
  97. httpServerSendHtmlBodyPageheadChunked();
  98. httpServer.sendContent_P(html_confDevWiFi_body);
  99. httpServerSendHtmlFooterChunked();
  100. }
  101. void httpServerHandleConfBasPage()
  102. {
  103. httpServerSendHtmlHeadChunked();
  104. httpServer.sendContent_P(html_confbas_script);
  105. httpServer.sendContent_P(html_head_end);
  106. httpServer.sendContent_P(html_bodytag_jsinit);
  107. httpServerSendHtmlBodyPageheadChunked();
  108. httpServer.sendContent_P(html_confbas_body);
  109. httpServerSendHtmlFooterChunked();
  110. }
  111. void httpServerHandleConfAdvPage()
  112. {
  113. httpServerSendHtmlHeadChunked();
  114. httpServer.sendContent_P(html_confadv_script);
  115. httpServer.sendContent_P(html_head_end);
  116. httpServer.sendContent_P(html_bodytag_jsinit);
  117. httpServerSendHtmlBodyPageheadChunked();
  118. httpServer.sendContent_P(html_confadv_body);
  119. httpServerSendHtmlFooterChunked();
  120. }
  121. void httpServerHandleConfAddPage()
  122. {
  123. httpServerSendHtmlHeadChunked();
  124. httpServer.sendContent_P(html_confadd_script);
  125. httpServer.sendContent_P(html_head_end);
  126. httpServer.sendContent_P(html_bodytag_jsinit);
  127. httpServerSendHtmlBodyPageheadChunked();
  128. httpServer.sendContent_P(html_confadd_body);
  129. httpServerSendHtmlFooterChunked();
  130. }
  131. void httpServerHandleConfTimePage()
  132. {
  133. httpServerSendHtmlHeadChunked();
  134. httpServer.sendContent_P(html_conftime_script);
  135. httpServer.sendContent_P(html_head_end);
  136. httpServer.sendContent_P(html_bodytag_jsinit);
  137. httpServerSendHtmlBodyPageheadChunked();
  138. httpServer.sendContent_P(html_conftime_body);
  139. httpServerSendHtmlFooterChunked();
  140. }
  141. void httpServerHandleConfLogPage()
  142. {
  143. httpServerSendHtmlHeadChunked();
  144. httpServer.sendContent_P(html_conflog_script);
  145. httpServer.sendContent_P(html_head_end);
  146. httpServer.sendContent_P(html_bodytag_jsinit);
  147. httpServerSendHtmlBodyPageheadChunked();
  148. httpServer.sendContent_P(html_conflog_body);
  149. httpServerSendHtmlFooterChunked();
  150. }
  151. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  152. void httpServerHandleWSConsolePage()
  153. {
  154. httpServerSendHtmlHeadChunked();
  155. httpServer.sendContent_P(html_head_end);
  156. httpServer.sendContent_P(html_bodytag);
  157. httpServerSendHtmlBodyPageheadChunked();
  158. httpServer.sendContent_P(html_wsconsole_body);
  159. httpServerSendHtmlFooterChunked();
  160. }
  161. #endif
  162. void httpServerHandleConsolePage()
  163. {
  164. httpServerSendHtmlHeadChunked();
  165. httpServer.sendContent_P(html_head_end);
  166. httpServer.sendContent_P(html_bodytag);
  167. httpServerSendHtmlBodyPageheadChunked();
  168. httpServer.sendContent_P(html_console_body);
  169. httpServerSendHtmlFooterChunked();
  170. }
  171. void httpServerHandleConfSavedPage()
  172. {
  173. httpServerSendHtmlHeadChunked();
  174. httpServer.sendContent_P(html_head_end);
  175. httpServer.sendContent_P(html_bodytag);
  176. httpServerSendHtmlBodyPageheadChunked();
  177. httpServer.sendContent_P(html_confsaved_body);
  178. httpServerSendHtmlFooterChunked();
  179. }
  180. //void httpServerHandleConfSavedRestartPage() {
  181. // httpServerSendHtmlHeadChunked();
  182. // httpServer.sendContent_P(html_head_end);
  183. // httpServer.sendContent_P(html_bodytag);
  184. // httpServerSendHtmlBodyPageheadChunked();
  185. // httpServer.sendContent_P(html_confsavedrestart_body);
  186. // httpServerSendHtmlFooterChunked();
  187. //}
  188. void httpServerHandleRestartPage()
  189. {
  190. httpServerSendHtmlHeadChunked();
  191. httpServer.sendContent_P(html_head_end);
  192. httpServer.sendContent_P(html_bodytag);
  193. config_was_changed = false;
  194. httpServerSendHtmlBodyPageheadChunked();
  195. httpServer.sendContent_P(html_restarting_body);
  196. httpServerSendHtmlFooterChunked();
  197. }
  198. void httpServerHandleClearconfPage()
  199. {
  200. httpServerSendHtmlHeadChunked();
  201. httpServer.sendContent_P(html_head_end);
  202. httpServer.sendContent_P(html_bodytag);
  203. httpServerSendHtmlBodyPageheadChunked();
  204. httpServer.sendContent_P(html_clearconf_body);
  205. httpServerSendHtmlFooterChunked();
  206. }
  207. void httpServerHandleSysInfoPage()
  208. {
  209. httpServerSendHtmlHeadChunked();
  210. httpServer.sendContent_P(html_sysinfo_script);
  211. httpServer.sendContent_P(html_head_end);
  212. httpServer.sendContent_P(html_bodytag_jsinit);
  213. httpServerSendHtmlBodyPageheadChunked();
  214. httpServer.sendContent_P(html_sysinfo_body);
  215. httpServerSendHtmlFooterChunked();
  216. }
  217. void httpServerSendHtmlHeadChunked()
  218. {
  219. httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
  220. httpServer.send(200, "text/html", html_head_part1);
  221. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  222. httpServer.sendContent(" - ");
  223. httpServer.sendContent(confDevWiFi.deviceName);
  224. httpServer.sendContent_P(html_head_part2);
  225. }
  226. void httpServerSendHtmlBodyPageheadChunked()
  227. {
  228. httpServer.sendContent_P(html_body_pagehead_part1);
  229. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  230. httpServer.sendContent(" - ");
  231. httpServer.sendContent(confDevWiFi.deviceName);
  232. httpServer.sendContent_P(html_body_pagehead_part2);
  233. if (config_was_changed)
  234. httpServer.sendContent_P(html_body_pagehead_confchangednote);
  235. }
  236. void httpServerSendHtmlFooterChunked()
  237. {
  238. httpServer.sendContent_P(html_footer1);
  239. httpServer.sendContent_P(html_footer_a1);
  240. httpServer.sendContent_P(PGMStr_FIRMWARE_URL);
  241. httpServer.sendContent_P(html_footer_a2);
  242. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  243. httpServer.sendContent_P(html_footer_ae);
  244. httpServer.sendContent(" ");
  245. httpServer.sendContent_P(PGMStr_FIRMWARE_VERSION);
  246. #ifdef DEBUGMODE
  247. httpServer.sendContent("&nbsp;<span style='color:red;font-weight:bold;'>DEBUG</span>");
  248. #endif
  249. httpServer.sendContent(" by ");
  250. httpServer.sendContent_P(html_footer_a1);
  251. httpServer.sendContent_P(PGMStr_FIRMWARE_COPYRIGHT_URL);
  252. httpServer.sendContent_P(html_footer_a2);
  253. httpServer.sendContent_P(PGMStr_FIRMWARE_COPYRIGHT);
  254. httpServer.sendContent_P(html_footer_ae);
  255. httpServer.sendContent_P(html_footer2);
  256. httpServer.sendContent("");
  257. httpServer.client().stop();
  258. }
  259. boolean httpIsAuthenticated()
  260. {
  261. if (WifiInApMode && strlen(confDevWiFi.WiFiAPModePassword) >= 8)
  262. return true;
  263. if (confWeb.http_user_auth)
  264. {
  265. boolean useAuth = false;
  266. boolean isAuthenticated = false;
  267. // user authentication enabled - only allow access without authentication if no admin password is set
  268. // or if user1 credentials are empty (access in "user mode")
  269. if (strlen(confWeb.http_pass) > 0 || strlen(confWeb.http_pass1) > 0 || strlen(confWeb.http_pass2) > 0)
  270. useAuth = true;
  271. if (strlen(confWeb.http_pass) == 0)
  272. {
  273. return true; // no admin password set - in this case authentication is off
  274. }
  275. if (useAuth)
  276. {
  277. // allow access if one set of valid credentials are given
  278. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0))
  279. {
  280. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  281. isAuthenticated = true;
  282. }
  283. if ((strlen(confWeb.http_user1) > 0 && strlen(confWeb.http_pass1) > 0))
  284. {
  285. if (httpServer.authenticate(confWeb.http_user1, confWeb.http_pass1))
  286. isAuthenticated = true;
  287. }
  288. if ((strlen(confWeb.http_user2) > 0 && strlen(confWeb.http_pass2) > 0))
  289. {
  290. if (httpServer.authenticate(confWeb.http_user2, confWeb.http_pass2))
  291. isAuthenticated = true;
  292. }
  293. // also allow access if admin credentials are set but user1 password is empty and user-authentication is enabled
  294. if (strlen(confWeb.http_pass1) == 0)
  295. {
  296. isAuthenticated = true;
  297. }
  298. return isAuthenticated;
  299. }
  300. // allow access if all credentials are empty
  301. else
  302. return true;
  303. }
  304. // user authentication disabled - only allow access without password if no admin password is set
  305. else
  306. {
  307. if (strlen(confWeb.http_pass) > 0)
  308. {
  309. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  310. return true;
  311. else
  312. return false;
  313. }
  314. else
  315. return true;
  316. }
  317. }
  318. boolean httpIsAuthenticatedAdmin()
  319. {
  320. if (WifiInApMode && strlen(confDevWiFi.WiFiAPModePassword) >= 8)
  321. return true;
  322. boolean auth = false;
  323. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0))
  324. auth = true;
  325. if (auth)
  326. {
  327. if (!httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  328. return false;
  329. else
  330. return true;
  331. }
  332. else
  333. return true;
  334. }
  335. void httpServerHandleNotFound()
  336. {
  337. httpServer.send(404, "text/plain", "404 NOT FOUND");
  338. }
  339. void httpSendUnauthorized()
  340. {
  341. httpServer.send(401, "text/plain", "UNAUTHORIZED");
  342. }
  343. void httpSend200OK()
  344. {
  345. httpServer.send(200, "text/plain", "OK");
  346. }
  347. boolean httpCheckToken()
  348. {
  349. if (confWeb.http_token[0] != '\0')
  350. { // dont accept empty token
  351. if (httpServer.hasArg("token"))
  352. {
  353. char buf[20];
  354. httpServer.arg("token").toCharArray(buf, 20);
  355. if (strcmp(buf, confWeb.http_token) == 0)
  356. return true;
  357. else
  358. return false;
  359. }
  360. else
  361. return false;
  362. }
  363. else
  364. return false;
  365. }
  366. void httpServerInit()
  367. {
  368. httpServer.on("/style.css", []() {
  369. httpServer.sendHeader("cache-control", "max-age=86400", false);
  370. httpServer.send_P(200, "text/css", html_stylesheet);
  371. });
  372. httpServer.on("/api", []() {
  373. if (!httpIsAuthenticated())
  374. return httpServer.requestAuthentication();
  375. else
  376. {
  377. if (confLog.logWebRequests || httpServer.args() > 0)
  378. {
  379. String addr = httpServer.client().remoteIP().toString();
  380. char buf[40];
  381. sprintf(buf, "WEB: /api (from %s)", addr.c_str());
  382. sendLog(buf, LOGLEVEL_INFO);
  383. }
  384. //sendLog("WEB: /api", LOGLEVEL_INFO);
  385. if (httpServer.hasArg("BtnPlus"))
  386. {
  387. setTempStepUp();
  388. sendLog(F("WEB: BtnPlus"), LOGLEVEL_INFO);
  389. } //if
  390. if (httpServer.hasArg("BtnMinus"))
  391. {
  392. setTempStepDown();
  393. sendLog(F("WEB: BtnMinus"), LOGLEVEL_INFO);
  394. } //if
  395. if (httpServer.hasArg("BtnPset0"))
  396. {
  397. setPresetTo(0);
  398. sendLog(F("WEB: BtnPset0"), LOGLEVEL_INFO);
  399. } //if
  400. if (httpServer.hasArg("BtnPset1"))
  401. {
  402. setPresetTo(1);
  403. sendLog(F("WEB: BtnPset1"), LOGLEVEL_INFO);
  404. } //if
  405. if (httpServer.hasArg("BtnPset2"))
  406. {
  407. setPresetTo(2);
  408. sendLog(F("WEB: BtnPset2"), LOGLEVEL_INFO);
  409. } //if
  410. if (httpServer.hasArg("BtnOn"))
  411. {
  412. setHeatingmodeTo(1);
  413. sendLog(F("WEB: BtnOn"), LOGLEVEL_INFO);
  414. } //if
  415. if (httpServer.hasArg("BtnOff"))
  416. {
  417. setHeatingmodeTo(0);
  418. sendLog(F("WEB: BtnOff"), LOGLEVEL_INFO);
  419. } //if
  420. if (httpServer.hasArg("BtnL1Plus"))
  421. {
  422. setTempLowStepUp();
  423. sendLog(F("WEB: BtnL1Plus"), LOGLEVEL_INFO);
  424. } //if
  425. if (httpServer.hasArg("BtnL1Minus"))
  426. {
  427. setTempLowStepDown();
  428. sendLog(F("WEB: BtnL1Minus"), LOGLEVEL_INFO);
  429. } //if
  430. if (httpServer.hasArg("BtnL2Plus"))
  431. {
  432. setTempLow2StepUp();
  433. sendLog(F("WEB: BtnL2Plus"), LOGLEVEL_INFO);
  434. } //if
  435. if (httpServer.hasArg("BtnL2Minus"))
  436. {
  437. setTempLow2StepDown();
  438. sendLog(F("WEB: BtnL2Minus"), LOGLEVEL_INFO);
  439. } //if
  440. if (httpServer.hasArg("setTemp"))
  441. {
  442. sendLog(F("WEB: /api?setTemp"), LOGLEVEL_INFO);
  443. char bufVal[20];
  444. httpServer.arg("setTemp").toCharArray(bufVal, 20);
  445. float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
  446. setTempTo(valueFloat);
  447. }
  448. if (httpServer.hasArg("setMode"))
  449. {
  450. sendLog(F("WEB: /api?setMode"), LOGLEVEL_INFO);
  451. char bufVal[20];
  452. httpServer.arg("setMode").toCharArray(bufVal, 20);
  453. int valueInt = atoi(bufVal);
  454. if (valueInt >= 0 && valueInt <= 1)
  455. setHeatingmodeTo(valueInt);
  456. }
  457. if (httpServer.hasArg("setPreset"))
  458. {
  459. sendLog(F("WEB: /api?setPreset"), LOGLEVEL_INFO);
  460. char bufVal[20];
  461. httpServer.arg("setPreset").toCharArray(bufVal, 20);
  462. int valueInt = atoi(bufVal);
  463. if (valueInt >= 0 && valueInt <= 2)
  464. setPresetTo(valueInt);
  465. }
  466. //char ch_currTemp[6];
  467. //char ch_currSetTemp[6];
  468. //dtostrf(currTemp, 1, 1, ch_currTemp );
  469. //dtostrf(setTemp, 1, 1, ch_currSetTemp );
  470. //build json object of program data
  471. StaticJsonDocument<700> json;
  472. json["devname"] = confDevWiFi.deviceName;
  473. json["ssid"] = WiFi.SSID();
  474. json["WiFiNum"] = persWM.getActiveWiFiNum();
  475. //json["uptime"] = uptimeStr;
  476. json["uptime"] = getUptimeStr();
  477. #ifdef ENABLE_FEATURE_NTP_TIME
  478. if (confTime.ntpEnable)
  479. {
  480. char buf[13];
  481. updateTime();
  482. if (lt.tm_year > 70)
  483. {
  484. strftime(buf, sizeof(buf), "%H:%M", &lt);
  485. json["time"] = buf;
  486. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  487. json["date"] = buf;
  488. }
  489. }
  490. #endif
  491. json["freeheap"] = ESP.getFreeHeap();
  492. mqtt_updateCurrentStateName();
  493. json["mqttstate"] = mqttCurrentStateName;
  494. json["mqtthost"] = confMqtt.mqtt_server;
  495. if (mqttReconnects < 1)
  496. json["mqttreconn"] = mqttReconnects;
  497. else
  498. json["mqttreconn"] = mqttReconnects - 1;
  499. json["setTemp"] = setTemp;
  500. json["currSetTemp"] = currSetTemp;
  501. json["temp"] = round1(currTemp);
  502. json["hum"] = int(currHum);
  503. json["heating"] = turnHeatingOn;
  504. json["mode"] = heatingMode;
  505. json["modeName"] = currentModeName;
  506. json["pset"] = preset;
  507. json["psetName"] = currentPresetName;
  508. json["psetName0"] = confAdv.psetName0;
  509. json["psetName1"] = confAdv.psetName1;
  510. json["psetName2"] = confAdv.psetName2;
  511. json["tempLow"] = setTempLow;
  512. json["tempLow2"] = setTempLow2;
  513. json["outTemp"] = round1(outTemp);
  514. json["outHum"] = outHum;
  515. char jsonchar[500];
  516. serializeJson(json, jsonchar);
  517. httpServer.send(200, "application/json", jsonchar);
  518. }
  519. }); //httpServer.on /api
  520. httpServer.on("/confDataWeb", []() {
  521. if (!httpIsAuthenticatedAdmin())
  522. httpSendUnauthorized();
  523. else
  524. {
  525. //Serial.println("httpServer.on /confdata");
  526. //sendLog("WEB: /confDataWeb", LOGLEVEL_INFO);
  527. if (confLog.logWebRequests)
  528. {
  529. String addr = httpServer.client().remoteIP().toString();
  530. char buf[40];
  531. sprintf(buf, "WEB: /confDataWeb (from %s)", addr.c_str());
  532. sendLog(buf, LOGLEVEL_INFO);
  533. }
  534. StaticJsonDocument<1000> json;
  535. json["apiToken"] = confWeb.http_token;
  536. json["httpUA"] = confWeb.http_user;
  537. //json["httpPA"] = confWeb.http_pass;
  538. if (strlen(confWeb.http_pass) > 0)
  539. json["httpPA"] = "****";
  540. else
  541. json["httpPA"] = "";
  542. if (confWeb.http_user_auth)
  543. json["httpAuth"] = 1;
  544. else
  545. json["httpAuth"] = 0;
  546. json["httpU1"] = confWeb.http_user1;
  547. //json["httpP1"] = confWeb.http_pass1;
  548. if (strlen(confWeb.http_pass1) > 0)
  549. json["httpP1"] = "****";
  550. else
  551. json["httpP1"] = "";
  552. json["httpU2"] = confWeb.http_user2;
  553. //json["httpP2"] = confWeb.http_pass2;
  554. if (strlen(confWeb.http_pass2) > 0)
  555. json["httpP2"] = "****";
  556. else
  557. json["httpP2"] = "";
  558. if (confWeb.wConsole)
  559. json["wConsole"] = 1;
  560. else
  561. json["wConsole"] = 0;
  562. if (confWeb.wsConsole)
  563. json["wsConsole"] = 1;
  564. else
  565. json["wsConsole"] = 0;
  566. yield();
  567. char jsonchar[1000];
  568. serializeJson(json, jsonchar);
  569. httpServer.send(200, "application/json", jsonchar);
  570. }
  571. }); //httpServer.on /confdweb
  572. httpServer.on("/confDataMqtt", []() {
  573. if (!httpIsAuthenticatedAdmin())
  574. httpSendUnauthorized();
  575. else
  576. {
  577. //sendLog("WEB: /confDataMqtt", LOGLEVEL_INFO);
  578. if (confLog.logWebRequests)
  579. {
  580. String addr = httpServer.client().remoteIP().toString();
  581. char buf[40];
  582. sprintf(buf, "WEB: /confDataMqtt (from %s)", addr.c_str());
  583. sendLog(buf, LOGLEVEL_INFO);
  584. }
  585. StaticJsonDocument<1000> json;
  586. if (confMqtt.mqtt_enable)
  587. json["mqttEnable"] = 1;
  588. else
  589. json["mqttEnable"] = 0;
  590. json["mqttHost"] = confMqtt.mqtt_server;
  591. json["mqttPort"] = confMqtt.mqtt_port;
  592. json["mqttUser"] = confMqtt.mqtt_user;
  593. //json["mqttPass"] = confMqtt.mqtt_pass;
  594. if (strlen(confMqtt.mqtt_pass) > 0)
  595. json["mqttPass"] = "****";
  596. else
  597. json["mqttPass"] = "";
  598. json["inTop"] = confMqtt.mqtt_topic_in;
  599. json["outTop"] = confMqtt.mqtt_topic_out;
  600. if (confMqtt.mqtt_outRetain)
  601. json["outRet"] = 1;
  602. else
  603. json["outRet"] = 0;
  604. if (confMqtt.mqtt_outRetain_sensors)
  605. json["outRetSens"] = 1;
  606. else
  607. json["outRetSens"] = 0;
  608. json["outPubInt"] = confMqtt.mqtt_outPubInterval;
  609. json["outPubIntSens"] = confMqtt.mqtt_outPubInterval_sensors;
  610. json["willTop"] = confMqtt.mqtt_willTopic;
  611. json["willQos"] = confMqtt.mqtt_willQos;
  612. if (confMqtt.mqtt_willRetain)
  613. json["willRet"] = 1;
  614. else
  615. json["willRet"] = 0;
  616. json["willMsg"] = confMqtt.mqtt_willMsg;
  617. json["connMsg"] = confMqtt.mqtt_connMsg;
  618. if (confMqtt.mqtt_enable_heartbeat)
  619. json["hbEnable"] = 1;
  620. else
  621. json["hbEnable"] = 0;
  622. json["hbReconn"] = confMqtt.mqtt_heartbeat_maxage_reconnect / 60000;
  623. json["hbReboot"] = confMqtt.mqtt_heartbeat_maxage_reboot / 60000;
  624. yield();
  625. char jsonchar[1000];
  626. serializeJson(json, jsonchar);
  627. httpServer.send(200, "application/json", jsonchar);
  628. }
  629. }); //httpServer.on /confdmqtt
  630. httpServer.on("/setConfWeb", []() {
  631. if (!httpIsAuthenticatedAdmin())
  632. httpSendUnauthorized();
  633. else
  634. {
  635. //sendLog("WEB: /setConfWeb", LOGLEVEL_INFO);
  636. String addr = httpServer.client().remoteIP().toString();
  637. char buf[40];
  638. sprintf(buf, "WEB: /setConfWeb (from %s)", addr.c_str());
  639. sendLog(buf, LOGLEVEL_INFO);
  640. bool httpPASet, httpP1Set, httpP2Set;
  641. httpPASet = false;
  642. httpP1Set = false;
  643. httpP2Set = false;
  644. if (httpServer.hasArg("httpPASet"))
  645. httpPASet = true;
  646. if (httpServer.hasArg("httpP1Set"))
  647. httpP1Set = true;
  648. if (httpServer.hasArg("httpP2Set"))
  649. httpP2Set = true;
  650. for (int i = 0; i < httpServer.args(); i++)
  651. {
  652. char bufName[20];
  653. char bufValue[101];
  654. httpServer.argName(i).toCharArray(bufName, 20);
  655. httpServer.arg(i).toCharArray(bufValue, 101);
  656. if (strlen(bufName) > 0)
  657. {
  658. Serial.print("web update ");
  659. Serial.print(bufName);
  660. Serial.print(" = ");
  661. Serial.println(bufValue);
  662. if (strcmp(bufName, "httpUA") == 0 || strcmp(bufName, "httpPA") == 0)
  663. {
  664. if (httpPASet)
  665. setConfig(bufName, bufValue);
  666. }
  667. else if (strcmp(bufName, "httpU1") == 0 || strcmp(bufName, "httpP1") == 0)
  668. {
  669. if (httpP1Set)
  670. setConfig(bufName, bufValue);
  671. }
  672. else if (strcmp(bufName, "httpU2") == 0 || strcmp(bufName, "httpP2") == 0)
  673. {
  674. if (httpP2Set)
  675. setConfig(bufName, bufValue);
  676. }
  677. else if (strcmp(bufName, "httpPASet") != 0 && strcmp(bufName, "httpP1Set") != 0 && strcmp(bufName, "httpP2Set") != 0)
  678. setConfig(bufName, bufValue);
  679. //else setConfig(bufName, bufValue);
  680. }
  681. }
  682. yield();
  683. saveConfigWeb();
  684. yield();
  685. loadConfigWeb();
  686. httpServerHandleConfSavedPage();
  687. config_was_changed = true;
  688. // yield();
  689. // ESP.restart();
  690. }
  691. }); //httpServer.on /setConfWeb
  692. httpServer.on("/setConfMqtt", []() {
  693. if (!httpIsAuthenticatedAdmin())
  694. httpSendUnauthorized();
  695. else
  696. {
  697. //Serial.println("httpServer.on /setConfMqtt");
  698. //sendLog("WEB: /setConfMqtt", LOGLEVEL_INFO);
  699. String addr = httpServer.client().remoteIP().toString();
  700. char buf[40];
  701. sprintf(buf, "WEB: /setConfMqtt (from %s)", addr.c_str());
  702. sendLog(buf, LOGLEVEL_INFO);
  703. bool mqttPassSet;
  704. mqttPassSet = false;
  705. if (httpServer.hasArg("mqttPassSet"))
  706. mqttPassSet = true;
  707. for (int i = 0; i < httpServer.args(); i++)
  708. {
  709. char bufName[20];
  710. char bufValue[101];
  711. httpServer.argName(i).toCharArray(bufName, 20);
  712. httpServer.arg(i).toCharArray(bufValue, 101);
  713. if (strlen(bufName) > 0)
  714. {
  715. Serial.print("web update ");
  716. Serial.print(bufName);
  717. Serial.print(" = ");
  718. Serial.println(bufValue);
  719. if (strcmp(bufName, "mqttUser") == 0 || strcmp(bufName, "mqttPass") == 0)
  720. {
  721. if (mqttPassSet)
  722. setConfig(bufName, bufValue);
  723. }
  724. else
  725. setConfig(bufName, bufValue);
  726. }
  727. //saveConfigMqttToFlash = true; // will be saved in next loop()
  728. //Serial.println("web triggered saveConfigMqttToFlash");
  729. }
  730. yield();
  731. saveConfigMqtt();
  732. yield();
  733. loadConfigMqtt();
  734. //httpServerHandleConfSavedRestartPage();
  735. httpServerHandleConfSavedPage();
  736. config_was_changed = true;
  737. //restart();
  738. }
  739. }); //httpServer.on /setConfMqtt
  740. httpServer.on("/confDataDevWiFi", []() {
  741. if (!httpIsAuthenticated())
  742. return httpServer.requestAuthentication();
  743. else
  744. {
  745. //sendLog("WEB: /confDataDevWiFi", LOGLEVEL_INFO);
  746. if (confLog.logWebRequests)
  747. {
  748. String addr = httpServer.client().remoteIP().toString();
  749. char buf[40];
  750. sprintf(buf, "WEB: /confDataDevWiFi (from %s)", addr.c_str());
  751. sendLog(buf, LOGLEVEL_INFO);
  752. }
  753. //for (int i = 0; i < httpServer.args(); i++) {
  754. // char bufName[20];
  755. // char bufValue[101];
  756. // httpServer.argName(i).toCharArray(bufName, 20);
  757. // httpServer.arg(i).toCharArray(bufValue, 101);
  758. //
  759. // if (strlen(bufName) > 0) {
  760. // //Serial.print("web update ");
  761. // //Serial.print(bufName);
  762. // //Serial.print(" = ");
  763. // //Serial.println(bufValue);
  764. // setConfig(bufName, bufValue);
  765. // }
  766. // saveConfigToFlash = true; // will be saved in next loop()
  767. // //Serial.println("web triggered saveConfigToFlash");
  768. //}
  769. //yield();
  770. //build json object of program data
  771. StaticJsonDocument<800> json;
  772. json["devName"] = confDevWiFi.deviceName;
  773. json["hostName"] = confDevWiFi.hostName;
  774. json["SSID1"] = confDevWiFi.WiFiSSID1;
  775. //json["WPW1"] = confDevWiFi.WiFiPW1;
  776. if (strlen(confDevWiFi.WiFiPW1) > 0)
  777. json["WPW1"] = "****";
  778. else
  779. json["WPW1"] = "";
  780. json["SSID2"] = confDevWiFi.WiFiSSID2;
  781. //json["WPW2"] = confDevWiFi.WiFiPW2;
  782. if (strlen(confDevWiFi.WiFiPW2) > 0)
  783. json["WPW2"] = "****";
  784. else
  785. json["WPW2"] = "";
  786. json["SSIDAP"] = confDevWiFi.WiFiAPModeSSID;
  787. json["WPWAP"] = confDevWiFi.WiFiAPModePassword;
  788. json["WAPtout"] = confDevWiFi.WiFiAPModeTimeout;
  789. json["WConnCheck"] = confDevWiFi.WiFiConnCheckInterval;
  790. json["Wretry"] = confDevWiFi.WiFiRetryInterval;
  791. json["Wreboot"] = confDevWiFi.WiFiRebootOnNoConnect;
  792. yield();
  793. char jsonchar[1000];
  794. serializeJson(json, jsonchar);
  795. httpServer.send(200, "application/json", jsonchar);
  796. }
  797. }); //httpServer.on /confDataDevWiFi
  798. httpServer.on("/setConfDevWiFi", []() {
  799. if (!httpIsAuthenticatedAdmin())
  800. httpSendUnauthorized();
  801. else
  802. {
  803. //sendLog("WEB: /setConfDevWiFi", LOGLEVEL_INFO);
  804. String addr = httpServer.client().remoteIP().toString();
  805. char buf[40];
  806. sprintf(buf, "WEB: /setConfDevWiFi (from %s)", addr.c_str());
  807. sendLog(buf, LOGLEVEL_INFO);
  808. bool WPW1Set, WPW2Set, WPWAPSet;
  809. WPW1Set = false;
  810. WPW2Set = false;
  811. WPWAPSet = false;
  812. if (httpServer.hasArg("WPW1Set"))
  813. WPW1Set = true;
  814. if (httpServer.hasArg("WPW2Set"))
  815. WPW2Set = true;
  816. if (httpServer.hasArg("WPWAPSet"))
  817. WPWAPSet = true;
  818. for (int i = 0; i < httpServer.args(); i++)
  819. {
  820. char bufName[20];
  821. char bufValue[101];
  822. httpServer.argName(i).toCharArray(bufName, 20);
  823. httpServer.arg(i).toCharArray(bufValue, 101);
  824. if (strlen(bufName) > 0)
  825. {
  826. Serial.print("web update ");
  827. Serial.print(bufName);
  828. Serial.print(" = ");
  829. Serial.println(bufValue);
  830. if (strcmp(bufName, "SSID1") == 0 || strcmp(bufName, "WPW1") == 0)
  831. {
  832. if (WPW1Set)
  833. setConfig(bufName, bufValue);
  834. }
  835. else if (strcmp(bufName, "SSID2") == 0 || strcmp(bufName, "WPW2") == 0)
  836. {
  837. if (WPW2Set)
  838. setConfig(bufName, bufValue);
  839. }
  840. else if (strcmp(bufName, "WPWAP") == 0)
  841. {
  842. if (WPWAPSet)
  843. setConfig(bufName, bufValue);
  844. }
  845. else
  846. setConfig(bufName, bufValue);
  847. }
  848. }
  849. yield();
  850. saveConfigDevWiFi();
  851. yield();
  852. loadConfigDevWiFi();
  853. //httpServerHandleConfSavedRestartPage();
  854. httpServerHandleConfSavedPage();
  855. config_was_changed = true;
  856. //restart();
  857. }
  858. }); //httpServer.on /setConfDev
  859. httpServer.on("/setConfBas", []() {
  860. if (!httpIsAuthenticatedAdmin())
  861. httpSendUnauthorized();
  862. else
  863. {
  864. //sendLog("WEB: /setConfBas", LOGLEVEL_INFO);
  865. String addr = httpServer.client().remoteIP().toString();
  866. char buf[40];
  867. sprintf(buf, "WEB: /setConfBas (from %s)", addr.c_str());
  868. sendLog(buf, LOGLEVEL_INFO);
  869. for (int i = 0; i < httpServer.args(); i++)
  870. {
  871. char bufName[20];
  872. char bufValue[101];
  873. httpServer.argName(i).toCharArray(bufName, 20);
  874. httpServer.arg(i).toCharArray(bufValue, 101);
  875. if (strlen(bufName) > 0)
  876. {
  877. Serial.print("web update ");
  878. Serial.print(bufName);
  879. Serial.print(" = ");
  880. Serial.println(bufValue);
  881. setConfig(bufName, bufValue);
  882. }
  883. }
  884. yield();
  885. saveConfigBas();
  886. yield();
  887. loadConfigBas();
  888. httpServerHandleConfSavedPage();
  889. config_was_changed = true;
  890. }
  891. }); //httpServer.on /setConfBas
  892. httpServer.on("/setConfAdv", []() {
  893. if (!httpIsAuthenticatedAdmin())
  894. httpSendUnauthorized();
  895. else
  896. {
  897. //sendLog("WEB: /setConfAdv", LOGLEVEL_INFO);
  898. String addr = httpServer.client().remoteIP().toString();
  899. char buf[40];
  900. sprintf(buf, "WEB: /setConfAdv (from %s)", addr.c_str());
  901. sendLog(buf, LOGLEVEL_INFO);
  902. for (int i = 0; i < httpServer.args(); i++)
  903. {
  904. char bufName[20];
  905. char bufValue[101];
  906. httpServer.argName(i).toCharArray(bufName, 20);
  907. httpServer.arg(i).toCharArray(bufValue, 101);
  908. if (strlen(bufName) > 0)
  909. {
  910. Serial.print("web update ");
  911. Serial.print(bufName);
  912. Serial.print(" = ");
  913. Serial.println(bufValue);
  914. setConfig(bufName, bufValue);
  915. }
  916. }
  917. yield();
  918. saveConfigAdv();
  919. yield();
  920. loadConfigAdv();
  921. httpServerHandleConfSavedPage();
  922. config_was_changed = true;
  923. }
  924. }); //httpServer.on /setConfAdv
  925. httpServer.on("/setConfAdd", []() {
  926. if (!httpIsAuthenticatedAdmin())
  927. httpSendUnauthorized();
  928. else
  929. {
  930. //sendLog("WEB: /setConfAdd", LOGLEVEL_INFO);
  931. String addr = httpServer.client().remoteIP().toString();
  932. char buf[40];
  933. sprintf(buf, "WEB: /setConfAdd (from %s)", addr.c_str());
  934. sendLog(buf, LOGLEVEL_INFO);
  935. for (int i = 0; i < httpServer.args(); i++)
  936. {
  937. char bufName[20];
  938. char bufValue[101];
  939. httpServer.argName(i).toCharArray(bufName, 20);
  940. httpServer.arg(i).toCharArray(bufValue, 101);
  941. if (strlen(bufName) > 0)
  942. {
  943. Serial.print("web update ");
  944. Serial.print(bufName);
  945. Serial.print(" = ");
  946. Serial.println(bufValue);
  947. setConfig(bufName, bufValue);
  948. }
  949. }
  950. yield();
  951. saveConfigAdd();
  952. yield();
  953. loadConfigAdd();
  954. httpServerHandleConfSavedPage();
  955. config_was_changed = true;
  956. }
  957. }); //httpServer.on /setConfAdd
  958. #ifdef ENABLE_FEATURE_NTP_TIME
  959. httpServer.on("/setConfTime", []() {
  960. if (!httpIsAuthenticatedAdmin())
  961. httpSendUnauthorized();
  962. else
  963. {
  964. //sendLog("WEB: /setConfTime", LOGLEVEL_INFO);
  965. String addr = httpServer.client().remoteIP().toString();
  966. char buf[40];
  967. sprintf(buf, "WEB: /setConfTime (from %s)", addr.c_str());
  968. sendLog(buf, LOGLEVEL_INFO);
  969. for (int i = 0; i < httpServer.args(); i++)
  970. {
  971. char bufName[20];
  972. char bufValue[101];
  973. httpServer.argName(i).toCharArray(bufName, 20);
  974. httpServer.arg(i).toCharArray(bufValue, 101);
  975. if (strlen(bufName) > 0)
  976. {
  977. Serial.print("web update ");
  978. Serial.print(bufName);
  979. Serial.print(" = ");
  980. Serial.println(bufValue);
  981. setConfig(bufName, bufValue);
  982. }
  983. }
  984. yield();
  985. saveConfigTime();
  986. yield();
  987. loadConfigTime();
  988. httpServerHandleConfSavedPage();
  989. config_was_changed = true;
  990. }
  991. }); //httpServer.on /setConfTime
  992. #endif
  993. httpServer.on("/setConfLog", []() {
  994. if (!httpIsAuthenticatedAdmin())
  995. httpSendUnauthorized();
  996. else
  997. {
  998. //sendLog("WEB: /setConfLog", LOGLEVEL_INFO);
  999. String addr = httpServer.client().remoteIP().toString();
  1000. char buf[40];
  1001. sprintf(buf, "WEB: /setConfLog (from %s)", addr.c_str());
  1002. sendLog(buf, LOGLEVEL_INFO);
  1003. for (int i = 0; i < httpServer.args(); i++)
  1004. {
  1005. char bufName[20];
  1006. char bufValue[101];
  1007. httpServer.argName(i).toCharArray(bufName, 20);
  1008. httpServer.arg(i).toCharArray(bufValue, 101);
  1009. if (strlen(bufName) > 0)
  1010. {
  1011. Serial.print("web update ");
  1012. Serial.print(bufName);
  1013. Serial.print(" = ");
  1014. Serial.println(bufValue);
  1015. setConfig(bufName, bufValue);
  1016. }
  1017. }
  1018. yield();
  1019. saveConfigLog();
  1020. yield();
  1021. loadConfigLog();
  1022. httpServerHandleConfSavedPage();
  1023. config_was_changed = true;
  1024. }
  1025. }); //httpServer.on /setConfLog
  1026. httpServer.on("/confDataBas", []() {
  1027. if (!httpIsAuthenticated())
  1028. return httpServer.requestAuthentication();
  1029. else
  1030. {
  1031. //sendLog("WEB: /confDataBas", LOGLEVEL_INFO);
  1032. //Serial.println("httpServer.on /confdata2");
  1033. if (confLog.logWebRequests)
  1034. {
  1035. String addr = httpServer.client().remoteIP().toString();
  1036. char buf[40];
  1037. sprintf(buf, "WEB: /confDataBas (from %s)", addr.c_str());
  1038. sendLog(buf, LOGLEVEL_INFO);
  1039. }
  1040. //for (int i = 0; i < httpServer.args(); i++) {
  1041. // char bufName[20];
  1042. // char bufValue[101];
  1043. // httpServer.argName(i).toCharArray(bufName, 20);
  1044. // httpServer.arg(i).toCharArray(bufValue, 101);
  1045. //
  1046. // if (strlen(bufName) > 0) {
  1047. // //Serial.print("web update ");
  1048. // //Serial.print(bufName);
  1049. // //Serial.print(" = ");
  1050. // //Serial.println(bufValue);
  1051. // setConfig(bufName, bufValue);
  1052. // }
  1053. // saveConfig2ToFlash = true;
  1054. // //Serial.println("web triggered saveConfig2ToFlash");
  1055. //}
  1056. //yield();
  1057. //build json object of program data
  1058. StaticJsonDocument<500> json;
  1059. if (confBas.autoSaveSetTemp)
  1060. json["autoSaveTemp"] = 1;
  1061. else
  1062. json["autoSaveTemp"] = 0;
  1063. if (confBas.autoSaveHeatingMode)
  1064. json["autoSaveMode"] = 1;
  1065. else
  1066. json["autoSaveMode"] = 0;
  1067. if (confBas.saveToMqttRetained)
  1068. json["saveToMqttRet"] = 1;
  1069. else
  1070. json["saveToMqttRet"] = 0;
  1071. json["tempMin"] = confBas.setTempMin;
  1072. json["tempMax"] = confBas.setTempMax;
  1073. json["measInt"] = confBas.measureInterval;
  1074. json["dispInt"] = confBas.displayInterval;
  1075. json["dispTout"] = confBas.displayTimeout;
  1076. if (confBas.PIR_enablesDisplay)
  1077. json["PIRenDisp"] = 1;
  1078. else
  1079. json["PIRenDisp"] = 0;
  1080. if (confBas.PIR_enablesDisplay_preset0only)
  1081. json["PIRenDispPs0"] = 1;
  1082. else
  1083. json["PIRenDispPs0"] = 0;
  1084. if (confBas.togglingTempHumAIDisplay)
  1085. json["togTHdisp"] = 1;
  1086. else
  1087. json["togTHdisp"] = 0;
  1088. yield();
  1089. char jsonchar[1000];
  1090. serializeJson(json, jsonchar);
  1091. httpServer.send(200, "application/json", jsonchar);
  1092. }
  1093. }); //httpServer.on /confdbas
  1094. httpServer.on("/confDataAdv", []() {
  1095. if (!httpIsAuthenticated())
  1096. return httpServer.requestAuthentication();
  1097. else
  1098. {
  1099. //sendLog("WEB: /confDataAdv", LOGLEVEL_INFO);
  1100. //Serial.println("httpServer.on /confdata2");
  1101. if (confLog.logWebRequests)
  1102. {
  1103. String addr = httpServer.client().remoteIP().toString();
  1104. char buf[40];
  1105. sprintf(buf, "WEB: /confDataAdv (from %s)", addr.c_str());
  1106. sendLog(buf, LOGLEVEL_INFO);
  1107. }
  1108. //for (int i = 0; i < httpServer.args(); i++) {
  1109. // char bufName[20];
  1110. // char bufValue[101];
  1111. // httpServer.argName(i).toCharArray(bufName, 20);
  1112. // httpServer.arg(i).toCharArray(bufValue, 101);
  1113. //
  1114. // if (strlen(bufName) > 0) {
  1115. // //Serial.print("web update ");
  1116. // //Serial.print(bufName);
  1117. // //Serial.print(" = ");
  1118. // //Serial.println(bufValue);
  1119. // setConfig(bufName, bufValue);
  1120. // }
  1121. // saveConfig2ToFlash = true;
  1122. // //Serial.println("web triggered saveConfig2ToFlash");
  1123. //}
  1124. //yield();
  1125. //build json object of program data
  1126. StaticJsonDocument<600> json;
  1127. json["minOffTime"] = confAdv.heatingMinOffTime;
  1128. json["tempDec"] = confAdv.setTempDecreaseVal;
  1129. json["hyst"] = confAdv.hysteresis;
  1130. json["tempCorr"] = confAdv.tempCorrVal;
  1131. json["humCorr"] = confAdv.humCorrVal;
  1132. json["offMsg"] = confAdv.offMessage;
  1133. json["modeName0"] = confAdv.modeName0;
  1134. json["modeName1"] = confAdv.modeName1;
  1135. json["psetName0"] = confAdv.psetName0;
  1136. json["psetName1"] = confAdv.psetName1;
  1137. json["psetName2"] = confAdv.psetName2;
  1138. json["iTempLab"] = confAdv.iTempLabel;
  1139. json["oTempLab"] = confAdv.oTempLabel;
  1140. yield();
  1141. char jsonchar[1000];
  1142. serializeJson(json, jsonchar);
  1143. httpServer.send(200, "application/json", jsonchar);
  1144. }
  1145. }); //httpServer.on /confdadv
  1146. httpServer.on("/confDataAdd", []() {
  1147. if (!httpIsAuthenticated())
  1148. return httpServer.requestAuthentication();
  1149. else
  1150. {
  1151. //sendLog("WEB: /confDataAdd", LOGLEVEL_INFO);
  1152. //Serial.println("httpServer.on /confdata2");
  1153. if (confLog.logWebRequests)
  1154. {
  1155. String addr = httpServer.client().remoteIP().toString();
  1156. char buf[40];
  1157. sprintf(buf, "WEB: /confDataAdd (from %s)", addr.c_str());
  1158. sendLog(buf, LOGLEVEL_INFO);
  1159. }
  1160. //for (int i = 0; i < httpServer.args(); i++) {
  1161. // char bufName[20];
  1162. // char bufValue[101];
  1163. // httpServer.argName(i).toCharArray(bufName, 20);
  1164. // httpServer.arg(i).toCharArray(bufValue, 101);
  1165. //
  1166. // if (strlen(bufName) > 0) {
  1167. // //Serial.print("web update ");
  1168. // //Serial.print(bufName);
  1169. // //Serial.print(" = ");
  1170. // //Serial.println(bufValue);
  1171. // setConfig(bufName, bufValue);
  1172. // }
  1173. // saveConfig2ToFlash = true;
  1174. // //Serial.println("web triggered saveConfig2ToFlash");
  1175. //}
  1176. //yield();
  1177. //build json object of program data
  1178. StaticJsonDocument<1000> json;
  1179. json["outTempTop"] = confAdd.outTemp_topic_in;
  1180. json["outHumTop"] = confAdd.outHum_topic_in;
  1181. json["PIRTop"] = confAdd.mqtt_topic_pir;
  1182. json["PIROnPld"] = confAdd.mqtt_payload_pir_on;
  1183. json["PIROffPld"] = confAdd.mqtt_payload_pir_off;
  1184. yield();
  1185. char jsonchar[1000];
  1186. serializeJson(json, jsonchar);
  1187. httpServer.send(200, "application/json", jsonchar);
  1188. }
  1189. }); //httpServer.on /confdadd
  1190. httpServer.on("/confDataTime", []() {
  1191. if (!httpIsAuthenticated())
  1192. return httpServer.requestAuthentication();
  1193. else
  1194. {
  1195. //sendLog("WEB: /confDataTime", LOGLEVEL_INFO);
  1196. //Serial.println("httpServer.on /confdata2");
  1197. if (confLog.logWebRequests)
  1198. {
  1199. String addr = httpServer.client().remoteIP().toString();
  1200. char buf[40];
  1201. sprintf(buf, "WEB: /confDataTime (from %s)", addr.c_str());
  1202. sendLog(buf, LOGLEVEL_INFO);
  1203. }
  1204. //for (int i = 0; i < httpServer.args(); i++) {
  1205. // char bufName[20];
  1206. // char bufValue[101];
  1207. // httpServer.argName(i).toCharArray(bufName, 20);
  1208. // httpServer.arg(i).toCharArray(bufValue, 101);
  1209. //
  1210. // if (strlen(bufName) > 0) {
  1211. // //Serial.print("web update ");
  1212. // //Serial.print(bufName);
  1213. // //Serial.print(" = ");
  1214. // //Serial.println(bufValue);
  1215. // setConfig(bufName, bufValue);
  1216. // }
  1217. // saveConfig2ToFlash = true;
  1218. // //Serial.println("web triggered saveConfig2ToFlash");
  1219. //}
  1220. //yield();
  1221. //build json object of program data
  1222. StaticJsonDocument<1000> json;
  1223. if (confTime.ntpEnable)
  1224. json["NTPEnable"] = 1;
  1225. else
  1226. json["NTPEnable"] = 0;
  1227. json["NTPServer1"] = confTime.ntpServer1;
  1228. json["NTPServer2"] = confTime.ntpServer2;
  1229. json["TZStr"] = confTime.timeZoneStr;
  1230. json["NTPSyncInt"] = confTime.ntpSyncInterval / 60;
  1231. yield();
  1232. char jsonchar[1000];
  1233. serializeJson(json, jsonchar);
  1234. httpServer.send(200, "application/json", jsonchar);
  1235. }
  1236. }); //httpServer.on /confDataTime
  1237. httpServer.on("/confDataLog", []() {
  1238. if (!httpIsAuthenticated())
  1239. return httpServer.requestAuthentication();
  1240. else
  1241. {
  1242. //sendLog("WEB: /confDataLog", LOGLEVEL_INFO);
  1243. if (confLog.logWebRequests)
  1244. {
  1245. String addr = httpServer.client().remoteIP().toString();
  1246. char buf[40];
  1247. sprintf(buf, "WEB: /confDataLog (from %s)", addr.c_str());
  1248. sendLog(buf, LOGLEVEL_INFO);
  1249. }
  1250. //for (int i = 0; i < httpServer.args(); i++) {
  1251. // char bufName[20];
  1252. // char bufValue[101];
  1253. // httpServer.argName(i).toCharArray(bufName, 20);
  1254. // httpServer.arg(i).toCharArray(bufValue, 101);
  1255. //
  1256. // if (strlen(bufName) > 0) {
  1257. // //Serial.print("web update ");
  1258. // //Serial.print(bufName);
  1259. // //Serial.print(" = ");
  1260. // //Serial.println(bufValue);
  1261. // setConfig(bufName, bufValue);
  1262. // }
  1263. // saveConfig2ToFlash = true;
  1264. // //Serial.println("web triggered saveConfig2ToFlash");
  1265. //}
  1266. //yield();
  1267. //build json object of program data
  1268. StaticJsonDocument<1000> json;
  1269. //if(confTime.ntpEnable) json["NTPEnable"] = 1;
  1270. //else json["NTPEnable"] = 0;
  1271. json["logLevSer"] = confLog.logLevelSerial;
  1272. json["logLevWeb"] = confLog.logLevelWeb;
  1273. json["logLevMqtt"] = confLog.logLevelMqtt;
  1274. if (confLog.logWebRequests)
  1275. json["logWebRequests"] = 1;
  1276. else
  1277. json["logWebRequests"] = 0;
  1278. yield();
  1279. char jsonchar[1000];
  1280. serializeJson(json, jsonchar);
  1281. httpServer.send(200, "application/json", jsonchar);
  1282. }
  1283. }); //httpServer.on /confDataLog
  1284. //get heap status, analog input value and all GPIO statuses in one json call
  1285. httpServer.on("/sysinfod", HTTP_GET, []() {
  1286. if (!httpIsAuthenticated())
  1287. return httpServer.requestAuthentication();
  1288. else
  1289. {
  1290. if (confLog.logWebRequests)
  1291. {
  1292. String addr = httpServer.client().remoteIP().toString();
  1293. char buf[40];
  1294. sprintf(buf, "WEB: /sysinfod (from %s)", addr.c_str());
  1295. sendLog(buf, LOGLEVEL_INFO);
  1296. }
  1297. char buf[40];
  1298. byte mac[6];
  1299. WiFi.macAddress(mac);
  1300. StaticJsonDocument<1000> json;
  1301. json["DevName"] = confDevWiFi.deviceName;
  1302. json["HostName"] = confDevWiFi.hostName;
  1303. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", (uint8_t)mac[0], (uint8_t)mac[1], (uint8_t)mac[2], (uint8_t)mac[3], (uint8_t)mac[4], (uint8_t)mac[5]);
  1304. json["MAC"] = buf;
  1305. //sprintf(buf, "%s", WiFi.localIP().toString().c_str());
  1306. //json["IP"] = buf;
  1307. //sprintf(buf, "%s", WiFi.gatewayIP().toString().c_str());
  1308. //json["Gateway"] = buf;
  1309. json["IP"] = WiFi.localIP().toString();
  1310. json["GW"] = WiFi.gatewayIP().toString();
  1311. json["DNS"] = WiFi.dnsIP().toString();
  1312. json["SSID"] = WiFi.SSID();
  1313. json["WiFiConf"] = persWM.getActiveWiFiNum();
  1314. json["UpTime"] = getUptimeStr();
  1315. json["HeapFree"] = ESP.getFreeHeap();
  1316. json["HeapFragment"] = ESP.getHeapFragmentation();
  1317. json["HeapMaxBlock"] = ESP.getMaxFreeBlockSize();
  1318. json["ResetReason"] = ESP.getResetReason();
  1319. json["CoreVersion"] = ESP.getCoreVersion();
  1320. json["SDKVersion"] = ESP.getSdkVersion();
  1321. json["CPUfreq"] = ESP.getCpuFreqMHz();
  1322. json["SketchSize"] = ESP.getSketchSize();
  1323. json["FlashSize"] = ESP.getFlashChipRealSize();
  1324. sprintf(buf, "%s", PGMStr_FIRMWARE_NAME);
  1325. json["FWName"] = buf;
  1326. sprintf(buf, "%s", PGMStr_FIRMWARE_VERSION);
  1327. json["FWVer"] = buf;
  1328. sprintf(buf, "%s", PGMStr_FIRMWARE_COPYRIGHT);
  1329. json["FWCr"] = buf;
  1330. sprintf(buf, "%s", compile_date);
  1331. json["FWBuilt"] = buf;
  1332. #ifdef DEBUGMODE
  1333. json["FWDebug"] = "yes";
  1334. #else
  1335. json["FWDebug"] = "no";
  1336. #endif
  1337. #ifdef ENABLE_FEATURE_NTP_TIME
  1338. if (confTime.ntpEnable)
  1339. {
  1340. char buf[13];
  1341. updateTime();
  1342. if(lt.tm_year > 70) {
  1343. strftime(buf, sizeof(buf), "%H:%M", &lt);
  1344. json["Time"] = buf;
  1345. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  1346. json["Date"] = buf;
  1347. }
  1348. }
  1349. #endif
  1350. yield();
  1351. char jsonchar[1000];
  1352. serializeJson(json, jsonchar);
  1353. httpServer.send(200, "application/json", jsonchar);
  1354. }
  1355. }); //httpServer.on /sysinfod
  1356. httpServer.on("/sysinfo", HTTP_GET, []() {
  1357. if (!httpIsAuthenticated())
  1358. return httpServer.requestAuthentication();
  1359. else
  1360. {
  1361. if (confLog.logWebRequests)
  1362. {
  1363. String addr = httpServer.client().remoteIP().toString();
  1364. char buf[40];
  1365. sprintf(buf, "WEB: /sysinfo (from %s)", addr.c_str());
  1366. sendLog(buf, LOGLEVEL_INFO);
  1367. }
  1368. httpServerHandleSysInfoPage();
  1369. }
  1370. });
  1371. /*httpServer.on("/", []() {
  1372. if ( WifiInApMode ) {
  1373. httpServer.sendHeader("Location", "/wifi.htm", true);
  1374. httpServer.send(302);
  1375. }
  1376. else {
  1377. httpServer.sendHeader("Location", "/main", true);
  1378. httpServer.send(302);
  1379. }
  1380. });*/
  1381. httpServer.on("/", []() {
  1382. String addr = httpServer.client().remoteIP().toString();
  1383. if (confLog.logWebRequests || httpServer.args() > 0)
  1384. {
  1385. char buf[40];
  1386. sprintf(buf, "WEB: / (from %s)", addr.c_str());
  1387. sendLog(buf, LOGLEVEL_INFO);
  1388. }
  1389. if (httpServer.hasArg("restart"))
  1390. {
  1391. if (httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()))
  1392. {
  1393. sendLog("WEB: /?restart", LOGLEVEL_INFO);
  1394. httpServerHandleRestartPage();
  1395. restart();
  1396. }
  1397. else
  1398. httpSendUnauthorized();
  1399. }
  1400. else if (httpServer.hasArg("mqttreconnect"))
  1401. {
  1402. if (httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()))
  1403. {
  1404. //Serial.println("web triggered mqttreconnect");
  1405. sendLog("WEB: /?mqttreconnect", LOGLEVEL_INFO);
  1406. mqttReconnect();
  1407. httpServer.sendHeader("Location", "/", true);
  1408. httpServer.send(303);
  1409. }
  1410. else
  1411. httpSendUnauthorized();
  1412. }
  1413. else if (httpServer.hasArg("clearconf"))
  1414. {
  1415. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1416. {
  1417. sendLog("WEB: /?clearconf", LOGLEVEL_INFO);
  1418. httpServerHandleClearconfPage();
  1419. yield();
  1420. delay(5);
  1421. deleteConfig();
  1422. }
  1423. else
  1424. httpSendUnauthorized();
  1425. }
  1426. else if (httpServer.hasArg("clearwifi"))
  1427. {
  1428. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1429. {
  1430. sendLog("WEB: /?clearwifi", LOGLEVEL_INFO);
  1431. httpServerHandleClearconfPage();
  1432. yield();
  1433. delay(5);
  1434. confClearWiFiCredentials();
  1435. }
  1436. else
  1437. httpSendUnauthorized();
  1438. }
  1439. else if (httpServer.hasArg("clearcreds"))
  1440. {
  1441. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1442. {
  1443. sendLog("WEB: /?clearcreds", LOGLEVEL_INFO);
  1444. httpServerHandleClearconfPage();
  1445. yield();
  1446. delay(5);
  1447. confClearCredentials();
  1448. }
  1449. else
  1450. httpSendUnauthorized();
  1451. }
  1452. else if (!httpIsAuthenticated())
  1453. return httpServer.requestAuthentication();
  1454. else
  1455. {
  1456. if (confLog.logWebRequests)
  1457. sendLog("WEB: /", LOGLEVEL_INFO);
  1458. httpServerHandleMainPage();
  1459. }
  1460. });
  1461. httpServer.on("/conf", []() {
  1462. if (!httpIsAuthenticatedAdmin())
  1463. return httpServer.requestAuthentication();
  1464. else
  1465. {
  1466. //sendLog("WEB: /conf", LOGLEVEL_INFO);
  1467. if (confLog.logWebRequests)
  1468. {
  1469. String addr = httpServer.client().remoteIP().toString();
  1470. char buf[40];
  1471. sprintf(buf, "WEB: /conf (from %s)", addr.c_str());
  1472. sendLog(buf, LOGLEVEL_INFO);
  1473. }
  1474. httpServerHandleConfPage();
  1475. }
  1476. });
  1477. httpServer.on("/confweb", []() {
  1478. if (!httpIsAuthenticatedAdmin())
  1479. return httpServer.requestAuthentication();
  1480. else
  1481. {
  1482. //sendLog("WEB: /confweb", LOGLEVEL_INFO);
  1483. if (confLog.logWebRequests)
  1484. {
  1485. String addr = httpServer.client().remoteIP().toString();
  1486. char buf[40];
  1487. sprintf(buf, "WEB: /confweb (from %s)", addr.c_str());
  1488. sendLog(buf, LOGLEVEL_INFO);
  1489. }
  1490. httpServerHandleConfWebPage();
  1491. }
  1492. });
  1493. httpServer.on("/confmqtt", []() {
  1494. if (!httpIsAuthenticatedAdmin())
  1495. return httpServer.requestAuthentication();
  1496. else
  1497. {
  1498. //sendLog("WEB: /confmqtt", LOGLEVEL_INFO);
  1499. if (confLog.logWebRequests)
  1500. {
  1501. String addr = httpServer.client().remoteIP().toString();
  1502. char buf[40];
  1503. sprintf(buf, "WEB: /confmqtt (from %s)", addr.c_str());
  1504. sendLog(buf, LOGLEVEL_INFO);
  1505. }
  1506. httpServerHandleConfMqttPage();
  1507. }
  1508. });
  1509. httpServer.on("/confdevwifi", []() {
  1510. if (!httpIsAuthenticatedAdmin())
  1511. return httpServer.requestAuthentication();
  1512. else
  1513. {
  1514. //sendLog("WEB: /confdevwifi", LOGLEVEL_INFO);
  1515. if (confLog.logWebRequests)
  1516. {
  1517. String addr = httpServer.client().remoteIP().toString();
  1518. char buf[40];
  1519. sprintf(buf, "WEB: /confdevwifi (from %s)", addr.c_str());
  1520. sendLog(buf, LOGLEVEL_INFO);
  1521. }
  1522. httpServerHandleConfDevWiFiPage();
  1523. }
  1524. });
  1525. httpServer.on("/confbas", []() {
  1526. if (!httpIsAuthenticatedAdmin())
  1527. return httpServer.requestAuthentication();
  1528. else
  1529. {
  1530. //sendLog("WEB: /confbas", LOGLEVEL_INFO);
  1531. if (confLog.logWebRequests)
  1532. {
  1533. String addr = httpServer.client().remoteIP().toString();
  1534. char buf[40];
  1535. sprintf(buf, "WEB: /confbas (from %s)", addr.c_str());
  1536. sendLog(buf, LOGLEVEL_INFO);
  1537. }
  1538. httpServerHandleConfBasPage();
  1539. }
  1540. });
  1541. httpServer.on("/confadv", []() {
  1542. if (!httpIsAuthenticatedAdmin())
  1543. return httpServer.requestAuthentication();
  1544. else
  1545. {
  1546. //sendLog("WEB: /confadv", LOGLEVEL_INFO);
  1547. if (confLog.logWebRequests)
  1548. {
  1549. String addr = httpServer.client().remoteIP().toString();
  1550. char buf[40];
  1551. sprintf(buf, "WEB: /confadv (from %s)", addr.c_str());
  1552. sendLog(buf, LOGLEVEL_INFO);
  1553. }
  1554. httpServerHandleConfAdvPage();
  1555. }
  1556. });
  1557. httpServer.on("/confadd", []() {
  1558. if (!httpIsAuthenticatedAdmin())
  1559. return httpServer.requestAuthentication();
  1560. else
  1561. {
  1562. //sendLog("WEB: /confadd", LOGLEVEL_INFO);
  1563. if (confLog.logWebRequests)
  1564. {
  1565. String addr = httpServer.client().remoteIP().toString();
  1566. char buf[40];
  1567. sprintf(buf, "WEB: /confadd (from %s)", addr.c_str());
  1568. sendLog(buf, LOGLEVEL_INFO);
  1569. }
  1570. httpServerHandleConfAddPage();
  1571. }
  1572. });
  1573. httpServer.on("/conftime", []() {
  1574. if (!httpIsAuthenticatedAdmin())
  1575. return httpServer.requestAuthentication();
  1576. else
  1577. {
  1578. //sendLog("WEB: /conftime", LOGLEVEL_INFO);
  1579. if (confLog.logWebRequests)
  1580. {
  1581. String addr = httpServer.client().remoteIP().toString();
  1582. char buf[40];
  1583. sprintf(buf, "WEB: /conftime (from %s)", addr.c_str());
  1584. sendLog(buf, LOGLEVEL_INFO);
  1585. }
  1586. httpServerHandleConfTimePage();
  1587. }
  1588. });
  1589. httpServer.on("/conflog", []() {
  1590. if (!httpIsAuthenticatedAdmin())
  1591. return httpServer.requestAuthentication();
  1592. else
  1593. {
  1594. //sendLog("WEB: /conflog", LOGLEVEL_INFO);
  1595. if (confLog.logWebRequests)
  1596. {
  1597. String addr = httpServer.client().remoteIP().toString();
  1598. char buf[40];
  1599. sprintf(buf, "WEB: /conflog (from %s)", addr.c_str());
  1600. sendLog(buf, LOGLEVEL_INFO);
  1601. }
  1602. httpServerHandleConfLogPage();
  1603. }
  1604. });
  1605. httpServer.on("/redTemps", []() {
  1606. if (!httpIsAuthenticatedAdmin())
  1607. return httpServer.requestAuthentication();
  1608. else
  1609. {
  1610. //sendLog("WEB: /redTemps", LOGLEVEL_INFO);
  1611. if (confLog.logWebRequests)
  1612. {
  1613. String addr = httpServer.client().remoteIP().toString();
  1614. char buf[40];
  1615. sprintf(buf, "WEB: /redTemps (from %s)", addr.c_str());
  1616. sendLog(buf, LOGLEVEL_INFO);
  1617. }
  1618. httpServerHandleRedTempsPage();
  1619. }
  1620. });
  1621. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  1622. if (confWeb.wsConsole)
  1623. {
  1624. httpServer.on("/wsconsole", []() {
  1625. if (!httpIsAuthenticatedAdmin())
  1626. return httpServer.requestAuthentication();
  1627. else
  1628. {
  1629. //sendLog("WEB: /wsconsole", LOGLEVEL_INFO);
  1630. if (confLog.logWebRequests)
  1631. {
  1632. String addr = httpServer.client().remoteIP().toString();
  1633. char buf[40];
  1634. sprintf(buf, "WEB: /wsconsole (from %s)", addr.c_str());
  1635. sendLog(buf, LOGLEVEL_INFO);
  1636. }
  1637. wsValidSessionId = millis();
  1638. char sidbuf[20];
  1639. sprintf(sidbuf, "sessionId=%lu|", wsValidSessionId);
  1640. httpServer.sendHeader("Set-Cookie", sidbuf);
  1641. httpServerHandleWSConsolePage();
  1642. }
  1643. });
  1644. httpServer.on("/wsapp.js", []() {
  1645. httpServer.sendHeader("cache-control", "max-age=86400", false);
  1646. httpServer.send_P(200, "text/javascript", js_wsapp);
  1647. });
  1648. }
  1649. #endif
  1650. httpServer.onNotFound([]() {
  1651. String addr = httpServer.client().remoteIP().toString();
  1652. if (confLog.logWebRequests)
  1653. {
  1654. char buf[40];
  1655. sprintf(buf, "WEB: 404 (from %s)", addr.c_str());
  1656. sendLog(buf, LOGLEVEL_INFO);
  1657. }
  1658. httpServerHandleNotFound();
  1659. }); //httpServer.onNotFound
  1660. #ifdef ENABLE_FEATURE_WEB_CONSOLE_BUFFERED
  1661. if (confWeb.wConsole)
  1662. {
  1663. httpServer.on("/console", []() {
  1664. if (!httpIsAuthenticatedAdmin())
  1665. return httpServer.requestAuthentication();
  1666. else
  1667. {
  1668. //sendLog("WEB: /console", LOGLEVEL_INFO);
  1669. if (confLog.logWebRequests)
  1670. {
  1671. String addr = httpServer.client().remoteIP().toString();
  1672. char buf[40];
  1673. sprintf(buf, "WEB: /console (from %s)", addr.c_str());
  1674. sendLog(buf, LOGLEVEL_INFO);
  1675. }
  1676. httpServerHandleConsolePage();
  1677. }
  1678. });
  1679. httpServer.on("/webcsapi", []() {
  1680. if (!httpIsAuthenticatedAdmin())
  1681. return httpServer.requestAuthentication();
  1682. else
  1683. {
  1684. if (httpServer.hasArg("cmd"))
  1685. {
  1686. char buf[101];
  1687. httpServer.arg("cmd").toCharArray(buf, 100);
  1688. strlcpy(cmdPayload, buf, sizeof(cmdPayload));
  1689. cmdInQueue = true;
  1690. evalCmd();
  1691. }
  1692. if (confLog.logWebRequests)
  1693. {
  1694. String addr = httpServer.client().remoteIP().toString();
  1695. char buf[40];
  1696. sprintf(buf, "WEB: /conflog (from %s)", addr.c_str());
  1697. sendLog(buf, LOGLEVEL_INFO);
  1698. }
  1699. httpServer.send(200, "text/plain", webLogBuffer);
  1700. }
  1701. });
  1702. httpServer.on("/csapp.js", []() {
  1703. httpServer.sendHeader("cache-control", "max-age=86400", false);
  1704. httpServer.send_P(200, "text/javascript", js_csapp);
  1705. });
  1706. }
  1707. #endif
  1708. // HTTP Updater at /update
  1709. #ifdef ENABLE_FEATURE_HTTP_UPDATER
  1710. httpUpdater.setup(&httpServer, "/update", confWeb.http_user, confWeb.http_pass);
  1711. #endif
  1712. httpServer.begin();
  1713. }