forked from Roos-AID/ModbusRTU-ESP8266-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingStuff.ino
281 lines (246 loc) · 11 KB
/
settingStuff.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
***************************************************************************
** Program : settingStuff.ino
** Version 1.8.1
**
**
** Copyright (c) 2021 Rob Roos
** based on Framework ESP8266 from Willem Aandewiel and modifications
** from Robert van Breemen
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
//=======================================================================
void writeSettings(bool show)
{
//let's use JSON to write the setting file
DebugTf("Writing to [%s] ..\r\n", SETTINGS_FILE);
File file = LittleFS.open(SETTINGS_FILE, "w"); // open for reading and writing
if (!file)
{
DebugTf("open(%s, 'w') FAILED!!! --> Bailout\r\n", SETTINGS_FILE);
return;
}
yield();
DebugT(F("Start writing setting data "));
//const size_t capacity = JSON_OBJECT_SIZE(6); // save more setting, grow # of objects accordingly
DynamicJsonDocument doc(1024);
JsonObject root = doc.to<JsonObject>();
root["hostname"] = settingHostname;
root["MQTTenable"] = settingMQTTenable;
root["MQTTbroker"] = settingMQTTbroker;
root["MQTTbrokerPort"] = settingMQTTbrokerPort;
root["MQTTuser"] = settingMQTTuser;
root["MQTTpasswd"] = settingMQTTpasswd;
root["MQTTtoptopic"] = settingMQTTtopTopic;
root["MQTThaprefix"] = settingMQTThaprefix;
root["MQTTuniqueid"] = settingMQTTuniqueid;
root["NTPenable"] = settingNTPenable;
root["NTPtimezone"] = settingNTPtimezone;
root["NTPhostname"] = settingNTPhostname;
root["LEDblink"] = settingLEDblink;
root["modbusconfigfile"] = settingModbusCfgfile;
root["modbusslaveadres"] = settingModbusSlaveAdr;
root["modbusbaudrate"] = settingModbusBaudrate;
root["modbusreadinterval"] = settingModbusReadInterval;
root["modbussinglephase"] = settingModbusSinglephase;
root["timebasedswitch"] = settingTimebasedSwitch;
root["relayallwayson"] = settingRelayAllwaysOnSwitch;
serializeJsonPretty(root, TelnetStream);
serializeJsonPretty(root, file);
Debugln(F("... done!"));
if (show) serializeJsonPretty(root, TelnetStream); //Debug stream ;-)
file.close();
} // writeSettings()
//=======================================================================
void readSettings(bool show)
{
// Open file for reading
File file = LittleFS.open(SETTINGS_FILE, "r");
DebugTf(" %s ..\r\n", SETTINGS_FILE);
if (!LittleFS.exists(SETTINGS_FILE))
{ //create settings file if it does not exist yet.
DebugTln(F(" .. file not found! --> created file!"));
writeSettings(show);
readSettings(false); //now it should work...
return;
}
// Deserialize the JSON document
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, file);
if (error)
{
DebugTln(F("Failed to read file, use existing defaults."));
DebugTf("Settings Deserialisation error: %s \r\n", error.c_str());
return;
}
// Copy values from the JsonDocument to the Config
settingHostname = doc["hostname"].as<String>();
if (settingHostname.length()==0) settingHostname = _HOSTNAME;
settingMQTTenable = doc["MQTTenable"]|settingMQTTenable;
settingMQTTbroker = doc["MQTTbroker"].as<String>();
settingMQTTbrokerPort = doc["MQTTbrokerPort"]; //default port
settingMQTTuser = doc["MQTTuser"].as<String>();
settingMQTTpasswd = doc["MQTTpasswd"].as<String>();
settingMQTTtopTopic = doc["MQTTtoptopic"].as<String>();
if (settingMQTTtopTopic == "null")
{
settingMQTTtopTopic = _HOSTNAME;
settingMQTTtopTopic.toLowerCase();
}
settingMQTThaprefix = doc["MQTThaprefix"].as<String>();
if (settingMQTThaprefix == "null")
settingMQTThaprefix = HOME_ASSISTANT_DISCOVERY_PREFIX;
settingMQTTuniqueid = doc["MQTTuniqueid"].as<String>();
if (settingMQTTuniqueid == "null")
settingMQTTuniqueid = getUniqueId();
settingNTPenable = doc["NTPenable"];
settingNTPtimezone = doc["NTPtimezone"].as<String>();
if (settingNTPtimezone=="null") settingNTPtimezone = NTP_DEFAULT_TIMEZONE;
settingNTPhostname = doc["NTPhostname"].as<String>();
if (settingNTPhostname=="null") settingNTPhostname = NTP_HOST_DEFAULT;
settingLEDblink = doc["LEDblink"]|settingLEDblink;
settingModbusCfgfile = doc["modbusconfigfile"].as<String>();
if (settingModbusCfgfile == "null") settingModbusCfgfile = "Modbusmap.cfg" ;
settingModbusSlaveAdr = doc["modbusslaveadres"];
settingModbusBaudrate = doc["modbusbaudrate"];
settingModbusReadInterval = doc["modbusreadinterval"];
if (settingModbusReadInterval== 0) settingModbusReadInterval = 30 ;
settingModbusSinglephase = doc["modbussinglephase"]|settingModbusSinglephase;
settingTimebasedSwitch = doc["timebasedswitch"] | settingTimebasedSwitch;
settingRelayAllwaysOnSwitch = doc["relayallwayson"] | settingRelayAllwaysOnSwitch;
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
DebugTln(F(" .. done\r"));
if (show) {
Debugln(F("\r\n==== read Settings ===================================================\r"));
Debugf("Hostname : %s\r\n", CSTR(settingHostname));
Debugf("MQTT enabled : %s\r\n", CBOOLEAN(settingMQTTenable));
Debugf("MQTT broker : %s\r\n", CSTR(settingMQTTbroker));
Debugf("MQTT port : %d\r\n", settingMQTTbrokerPort);
Debugf("MQTT username : %s\r\n", CSTR(settingMQTTuser));
Debugf("MQTT password : %s\r\n", CSTR(settingMQTTpasswd));
Debugf("MQTT toptopic : %s\r\n", CSTR(settingMQTTtopTopic));
Debugf("HA prefix : %s\r\n", CSTR(settingMQTThaprefix));
Debugf("MQTT uniqueid : %s\r\n", CSTR(settingMQTTuniqueid));
Debugf("NTP enabled : %s\r\n", CBOOLEAN(settingNTPenable));
Debugf("NTP timezone : %s\r\n", CSTR(settingNTPtimezone));
Debugf("NTP hostname : %s\r\n", CSTR(settingNTPhostname));
Debugf("Led Blink : %s\r\n", CBOOLEAN(settingLEDblink));
Debugf("Modbus configfile : %s\r\n", CSTR(settingModbusCfgfile));
Debugf("Modbus slaveadr : %d\r\n", settingModbusSlaveAdr);
Debugf("Modbus baudrate : %d\r\n", settingModbusBaudrate);
Debugf("Modbus read interval : %d\r\n", settingModbusReadInterval);
Debugf("Modbus singlephase : %s\r\n", CBOOLEAN(settingModbusSinglephase));
Debugf("Timebased switch : %s\r\n", CBOOLEAN(settingTimebasedSwitch));
Debugf("Relay Allways On switch : %s\r\n", CBOOLEAN(settingRelayAllwaysOnSwitch));
}
Debugln(F("-\r"));
} // readSettings()
//=======================================================================
void updateSetting(const char *field, const char *newValue)
{ //do not just trust the caller to do the right thing, server side validation is here!
DebugTf("-> field[%s], newValue[%s]\r\n", field, newValue);
if (stricmp(field, "hostname")==0)
{ //make sure we have a valid hostname here...
settingHostname = String(newValue);
if (settingHostname.length()==0) settingHostname=_HOSTNAME;
int pos = settingMQTTtopTopic.indexOf("."); //strip away anything beyond the dot
if (pos){
settingMQTTtopTopic = settingMQTTtopTopic.substring(0, pos-1);
}
//Update some settings right now
startMDNS(CSTR(settingHostname));
startLLMNR(CSTR(settingHostname));
Debugln();
DebugTf("Need reboot before new %s.local will be available!\r\n\n", CSTR(settingHostname));
}
if (stricmp(field, "MQTTenable")==0) settingMQTTenable = EVALBOOLEAN(newValue);
if (stricmp(field, "MQTTbroker")==0) settingMQTTbroker = String(newValue);
if (stricmp(field, "MQTTbrokerPort")==0) settingMQTTbrokerPort = atoi(newValue);
if (stricmp(field, "MQTTuser")==0) settingMQTTuser = String(newValue);
if (stricmp(field, "MQTTpasswd")==0) settingMQTTpasswd = String(newValue);
if (stricmp(field, "MQTTtoptopic") == 0)
{
settingMQTTtopTopic = String(newValue);
if (settingMQTTtopTopic.length() == 0)
{
settingMQTTtopTopic = _HOSTNAME;
settingMQTTtopTopic.toLowerCase();
}
}
if (stricmp(field, "MQTThaprefix") == 0)
{
settingMQTThaprefix = String(newValue);
if (settingMQTThaprefix.length() == 0)
settingMQTThaprefix = HOME_ASSISTANT_DISCOVERY_PREFIX;
}
if (stricmp(field, "MQTTuniqueid") == 0)
{
settingMQTTuniqueid = String(newValue);
if (settingMQTTuniqueid.length() == 0)
settingMQTTuniqueid = getUniqueId();
}
if (stricmp(field, "NTPenable") == 0)
settingNTPenable = EVALBOOLEAN(newValue);
if (stricmp(field, "NTPhostname")==0) {
settingNTPhostname = String(newValue);
startNTP();
}
if (stricmp(field, "NTPtimezone")==0) {
settingNTPtimezone = String(newValue);
startNTP(); // update timezone if changed
}
if (stricmp(field, "LEDblink")==0) settingLEDblink = EVALBOOLEAN(newValue);
if (stricmp(field, "modbusconfigfile")==0)
{
settingModbusCfgfile = String(newValue);
if (settingModbusCfgfile.length() == 0) settingModbusCfgfile = "Modbusmap.cfg";
}
if (stricmp(field, "modbusslaveadres")==0) settingModbusSlaveAdr = atoi(newValue);
if (stricmp(field, "modbusbaudrate")==0) settingModbusBaudrate = atoi(newValue);
if (stricmp(field, "modbusreadinterval") == 0)
{
settingModbusReadInterval = atoi(newValue);
CHANGE_INTERVAL_SEC(timerreadmodbus, settingModbusReadInterval, CATCH_UP_MISSED_TICKS);
}
if (stricmp(field, "modbussinglephase")==0) settingModbusSinglephase = EVALBOOLEAN(newValue);
if (stricmp(field, "timebasedswitch")==0) settingTimebasedSwitch = EVALBOOLEAN(newValue);
if (stricmp(field, "relayallwayson") == 0) {
settingRelayAllwaysOnSwitch = EVALBOOLEAN(newValue);
checkactivateRelay(true) ;
}
// without NTP no timebased switching
if (!settingNTPenable) {
settingTimebasedSwitch = false;
setRelay(RELAYOFF);
}
//finally update write settings
writeSettings(false);
//Restart MQTT connection every "save settings"
if (settingMQTTenable) startMQTT();
} // updateSetting()
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
****************************************************************************
*/