@@ -37,75 +37,98 @@ void WebPortal::setup(Entry& mqttEntry, ConfigEntry& config, NTPClient& ntpClien
37
37
38
38
void WebPortal::handleRoot () {
39
39
if (!auth ()) return ;
40
- String page = " " ;
41
- page += FPSTR (HTML_MAIN1);
42
- // Sensors
40
+ webServer->send (200 , " text/html" , " " );
41
+
42
+ webServer->sendContent_P (HTML_MAIN1);
43
+
43
44
mqtt->each ([&](Entry* entry) {
44
- if (!entry->isInternal () || webServer->arg (" show" ).equals (" all" )) {
45
- String value = entry->getValue ();
46
- if (value != NULL ) {
47
- page += FPSTR (HTML_SENSOR);
48
- if (getName (entry).endsWith (" password" )) {
49
- page.replace (" {value}" , " ***" );
50
- } else {
51
- value.replace (" {value}" , entry->getValue ());
52
- page.replace (" {value}" , value);
53
- }
54
- page.replace (" {last_updated}" , time (entry->getLastUpdate ()));
55
- }
56
- if (entry->isOut ()) {
57
- page += FPSTR (HTML_INPUT);
58
- }
59
- page.replace (" {name}" , getName (entry));
60
- page.replace (" {path}" , getRestPath (entry));
61
- }
45
+ if (!entry->isInternal () || webServer->arg (" show" ).equals (" all" )) {
46
+ sendSensor (entry);
47
+ }
62
48
});
63
49
64
- // Config
65
- page += FPSTR (HTML_MAIN2);
66
- page += FPSTR (HTML_CONFIG_HEADER);
67
- page.replace (" {title}" , " General" );
50
+ webServer->sendContent_P (HTML_MAIN2);
51
+
52
+ webServer->sendContent_P (HTML_CONFIG_HEADER);
68
53
config->each ([&](Entry* entry) {
69
- if (entry == config) return ;
70
- page += FPSTR (HTML_CONFIG_ENTRY);
71
- String name = getName (config, entry).substring (1 );
72
- page.replace (" {key}" , name);
73
- if (name.endsWith (" password" )) {
74
- page.replace (" {type}" , " password" );
75
- page.replace (" {value}" , " " );
76
- } else {
77
- page.replace (" {type}" , " text" );
78
- page.replace (" {value}" , entry->getValue ());
79
- }
54
+ if (entry != config) {
55
+ sendConfig (entry);
56
+ }
80
57
});
81
58
82
- // About
83
- page += FPSTR (HTML_MAIN3);
59
+ webServer-> sendContent_P (HTML_MAIN3);
60
+
84
61
mqtt->each ([&](Entry* entry) {
85
- if (entry->isOut () || entry->isIn ()) {
86
- page += FPSTR (HTML_API_DOC);
87
- String path = entry->getTopic ();
88
- if (entry->isOut ()) path += " <span class=\" badge\" >Set</span>" ;
89
- if (entry->isIn ()) path += " <span class=\" badge\" >Get</span>" ;
90
- page.replace (" {path}" , path);
91
- }
62
+ if (entry->isOut () || entry->isIn ()) {
63
+ sendMqttApi (entry);
64
+ }
92
65
});
93
66
94
- page += FPSTR (HTML_MAIN4);
67
+ webServer->sendContent_P (HTML_MAIN4);
68
+
95
69
mqtt->each ([&](Entry* entry) {
96
- if (entry->isOut () || entry->isIn ()) {
97
- page += FPSTR (HTML_API_DOC);
98
- String path = getRestPath (entry);
99
- if (entry->isOut ()) path += " <span class=\" badge\" >POST</span>" ;
100
- if (entry->isIn ()) path += " <span class=\" badge\" >GET</span>" ;
101
- page.replace (" {path}" , path);
102
- }
70
+ if (entry->isOut () || entry->isIn ()) {
71
+ sendRestApi (entry);
72
+ }
103
73
});
104
74
105
- page + = FPSTR (HTML_MAIN5);
75
+ String page = FPSTR (HTML_MAIN5);
106
76
page.replace (" {device_id}" , mqtt->get (" $system" )[" deviceId" ].getValue ());
107
77
page.replace (" {topic}" , mqtt->getTopic ());
108
- webServer->send (200 , " text/html" , page);
78
+ webServer->sendContent (page);
79
+ }
80
+
81
+ void WebPortal::sendSensor (Entry* entry) {
82
+ String page = " " ;
83
+ String value = entry->getValue ();
84
+ if (value != NULL ) {
85
+ page += FPSTR (HTML_SENSOR);
86
+ if (getName (entry).endsWith (" password" )) {
87
+ page.replace (" {value}" , " ***" );
88
+ } else {
89
+ value.replace (" {value}" , entry->getValue ());
90
+ page.replace (" {value}" , value);
91
+ }
92
+ page.replace (" {last_updated}" , time (entry->getLastUpdate ()));
93
+ }
94
+ if (entry->isOut ()) {
95
+ page += FPSTR (HTML_INPUT);
96
+ }
97
+ page.replace (" {name}" , getName (entry));
98
+ page.replace (" {path}" , getRestPath (entry));
99
+ webServer->sendContent (page);
100
+ }
101
+
102
+ void WebPortal::sendConfig (Entry* entry) {
103
+ String page = FPSTR (HTML_CONFIG_ENTRY);
104
+ String name = getName (config, entry).substring (1 );
105
+ page.replace (" {key}" , name);
106
+ if (name.endsWith (" password" )) {
107
+ page.replace (" {type}" , " password" );
108
+ page.replace (" {value}" , " " );
109
+ } else {
110
+ page.replace (" {type}" , " text" );
111
+ page.replace (" {value}" , entry->getValue ());
112
+ }
113
+ webServer->sendContent (page);
114
+ }
115
+
116
+ void WebPortal::sendMqttApi (Entry* entry) {
117
+ String page = FPSTR (HTML_API_DOC);
118
+ String path = entry->getTopic ();
119
+ if (entry->isOut ()) path += " <span class=\" badge\" >Set</span>" ;
120
+ if (entry->isIn ()) path += " <span class=\" badge\" >Get</span>" ;
121
+ page.replace (" {path}" , path);
122
+ webServer->sendContent (page);
123
+ }
124
+
125
+ void WebPortal::sendRestApi (Entry* entry) {
126
+ String page = FPSTR (HTML_API_DOC);
127
+ String path = getRestPath (entry);
128
+ if (entry->isOut ()) path += " <span class=\" badge\" >POST</span>" ;
129
+ if (entry->isIn ()) path += " <span class=\" badge\" >GET</span>" ;
130
+ page.replace (" {path}" , path);
131
+ webServer->sendContent (page);
109
132
}
110
133
111
134
void WebPortal::handleRest () {
@@ -170,7 +193,7 @@ String WebPortal::time(long time) {
170
193
bool WebPortal::auth () {
171
194
char pass[32 ];
172
195
config->getCString (" password" , " " , pass);
173
- if (stelen (pass) > 0 && !webServer->authenticate (" admin" , pass)) {
196
+ if (strlen (pass) > 0 && !webServer->authenticate (" admin" , pass)) {
174
197
webServer->requestAuthentication ();
175
198
return false ;
176
199
}
0 commit comments