123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include <user_config.h>
- #include <SmingCore/SmingCore.h>
- #include "../include/configuration.h"
- #include "webserver.h"
- bool serverStarted = false;
- HttpServer server;
- extern String StrT, StrRH, StrHI, StrCR, StrCF; // Sensors string values
- void onIndex(HttpRequest &request, HttpResponse &response)
- {
- TemplateFileStream *tmpl = new TemplateFileStream("index.html");
- auto &vars = tmpl->variables();
- vars["T"] = StrT;
- vars["RH"] = StrRH;
- vars["HI"] = StrHI;
- vars["CR"] = StrCR;
- vars["CF"] = StrCF;
- vars["ADC"] = String(system_adc_read());
- vars["VDD"] = String(system_get_vdd33());
- vars["TIME"] = SystemClock.getSystemTimeString();
- response.sendTemplate(tmpl);
- }
- void onConfiguration(HttpRequest &request, HttpResponse &response)
- {
- MeteoConfig cfg = loadConfig();
- if (request.getRequestMethod() == RequestMethod::POST)
- {
- // debugf("Update config");
- // Update config
- if (request.getPostParameter("SSID").length() > 0) // Network
- {
- cfg.NetworkSSID = request.getPostParameter("SSID");
- cfg.NetworkPassword = request.getPostParameter("Password");
- }
- if (request.getPostParameter("TZ").length() > 0) // Correction
- {
- cfg.AddT = request.getPostParameter("T").toFloat();
- cfg.AddRH = request.getPostParameter("RH").toFloat();
- cfg.AddTZ = request.getPostParameter("TZ").toFloat();
- }
- if (request.getPostParameter("Trigger").length() > 0) // Trigger
- {
- cfg.Trigger = (TriggerType)request.getPostParameter("Trigger").toInt();
- cfg.RangeMin = request.getPostParameter("RMin").toFloat();
- cfg.RangeMax = request.getPostParameter("RMax").toFloat();
- }
- saveConfig(cfg);
- SystemClock.setTimeZone(cfg.AddTZ);
- response.redirect();
- }
- // debugf("Send template");
- TemplateFileStream *tmpl = new TemplateFileStream("config.html");
- auto &vars = tmpl->variables();
- vars["SSID"] = cfg.NetworkSSID;
- vars["T"] = String(cfg.AddT, 2);
- vars["RH"] = String(cfg.AddRH, 2);
- vars["TZ"] = String(cfg.AddTZ, 2);
- vars["Trigger"] = String((int)cfg.Trigger);
- vars["RMin"] = String(cfg.RangeMin, 2);
- vars["RMax"] = String(cfg.RangeMax, 2);
- response.sendTemplate(tmpl);
- }
- void onFile(HttpRequest &request, HttpResponse &response)
- {
- String file = request.getPath();
- if (file[0] == '/')
- file = file.substring(1);
- if (file[0] == '.')
- response.forbidden();
- else
- {
- response.setCache(86400, true); // It's important to use cache for better performance.
- response.sendFile(file);
- }
- }
- /// API ///
- void onApiDoc(HttpRequest &request, HttpResponse &response)
- {
- TemplateFileStream *tmpl = new TemplateFileStream("api.html");
- auto &vars = tmpl->variables();
- vars["IP"] = (WifiStation.isConnected() ? WifiStation.getIP() : WifiAccessPoint.getIP()).toString();
- response.sendTemplate(tmpl);
- }
- void onApiSensors(HttpRequest &request, HttpResponse &response)
- {
- JsonObjectStream* stream = new JsonObjectStream();
- JsonObject& json = stream->getRoot();
- json["status"] = (bool)true;
- JsonObject& sensors = json.createNestedObject("sensors");
- sensors["temperature"] = StrT.c_str();
- sensors["humidity"] = StrRH.c_str();
- sensors["heatindex"] = StrHI.c_str();
- sensors["comfortp"] = StrCR.c_str();
- sensors["comforts"] = StrCF.c_str();
- sensors["adcvalue"] = String(system_adc_read());
- sensors["vddvalue"] = String(system_get_vdd33());
- sensors["datetime"] = SystemClock.getSystemTimeString();
- response.sendJsonObject(stream);
- }
- void onApiOutput(HttpRequest &request, HttpResponse &response)
- {
- int val = request.getQueryParameter("control", "-1").toInt();
- if (val == 0 || val == 1)
- digitalWrite(CONTROL_PIN, val == 1);
- else
- val = -1;
- JsonObjectStream* stream = new JsonObjectStream();
- JsonObject& json = stream->getRoot();
- json["status"] = val != -1;
- if (val == -1) json["error"] = "Wrong control parameter value, please use: ?control=0|1";
- response.sendJsonObject(stream);
- }
- void startWebServer()
- {
- if (serverStarted) return;
- server.listen(80);
- server.addPath("/", onIndex);
- server.addPath("/api", onApiDoc);
- server.addPath("/api/sensors", onApiSensors);
- server.addPath("/api/output", onApiOutput);
- server.addPath("/config", onConfiguration);
- server.setDefaultHandler(onFile);
- serverStarted = true;
- /*
- if (WifiStation.isEnabled())
- debugf("STA: %s", WifiStation.getIP().toString().c_str());
- if (WifiAccessPoint.isEnabled())
- debugf("AP: %s", WifiAccessPoint.getIP().toString().c_str());
- */
- }
|