IotWebConfParameter.h 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * IotWebConfParameter.h -- IotWebConf is an ESP8266/ESP32
 *   non blocking WiFi/AP web configuration library for Arduino.
 *   https://github.com/prampec/IotWebConf
 *
 * Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#ifndef IotWebConfParameter_h
#define IotWebConfParameter_h

#include <Arduino.h>
#include <functional>
Eric Duminil's avatar
Eric Duminil committed
17
18
#include "IotWebConfSettings.h"
#include "IotWebConfWebServerWrapper.h"
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

#ifdef IOTWEBCONF_ENABLE_JSON
# include <ArduinoJson.h>
#endif

const char IOTWEBCONF_HTML_FORM_GROUP_START[] PROGMEM =
  "<fieldset id='{i}'><legend>{b}</legend>\n";
const char IOTWEBCONF_HTML_FORM_GROUP_END[] PROGMEM =
  "</fieldset>\n";

const char IOTWEBCONF_HTML_FORM_PARAM[] PROGMEM =
  "<div class='{s}'><label for='{i}'>{b}</label><input type='{t}' id='{i}' "
  "name='{i}' {l} placeholder='{p}' value='{v}' {c}/>"
  "<div class='em'>{e}</div></div>\n";

const char IOTWEBCONF_HTML_FORM_SELECT_PARAM[] PROGMEM =
  "<div class='{s}'><label for='{i}'>{b}</label><select id='{i}' "
  "name='{i}' {c}/>\n{o}"
  "</select><div class='em'>{e}</div></div>\n";
const char IOTWEBCONF_HTML_FORM_OPTION[] PROGMEM =
  "<option value='{v}'{s}>{n}</option>\n";

namespace iotwebconf
{

typedef struct SerializationData
{
  byte* data;
  int length;
} SerializationData;

class ConfigItem
{
public:
  bool visible = true;
  const char* getId() { return this->_id; }

  /**
   * Calculate the size of bytes should be stored in the EEPROM.
   */
  virtual int getStorageSize() = 0;

  /**
   * On initial startup (when no data was saved), it may be required to apply a default value
   *   to the parameter.
   */
  virtual void applyDefaultValue() = 0;

  /**
   * Save data.
   * @doStore - A method is passed as a parameter, that will performs the actual EEPROM access.
   *   The argument 'serializationData' of this referenced method should be pre-filled with
   *   the size and the serialized data before calling the method.
   */
  virtual void storeValue(std::function<void(SerializationData* serializationData)> doStore) = 0;

  /**
   * Load data.
   * @doLoad - A method is passed as a parameter, that will performs the actual EEPROM access.
   *   The argument 'serializationData' of this referenced method should be pre-filled with
   *   the size of the expected data, and the data buffer should be allocated with this size.
   *   The doLoad will fill the data from the EEPROM.
   */
  virtual void loadValue(std::function<void(SerializationData* serializationData)> doLoad) = 0;

  /**
   * This method will create the HTML form item for the config portal.
   * 
   * @dataArrived - True if there was a form post, where (some) form
   *   data arrived from the client.
   * @webRequestWrapper - The webRequestWrapper, that will send the rendered content to the client.
   *   The webRequestWrapper->sendContent() method should be used in the implementations.
   */
  virtual void renderHtml(bool dataArrived, WebRequestWrapper* webRequestWrapper) = 0;

  /**
   * New value arrived from the form post. The value should be stored in the
   *   in this config item.
   * 
   * @webRequestWrapper - The webRequestWrapper, that will send the rendered content to the client.
   *   The webRequestWrapper->hasArg() and webRequestWrapper->arg() methods should be used in the
   *   implementations.
   */
  virtual void update(WebRequestWrapper* webRequestWrapper) = 0;

  /**
   * Before validating the form post, it is required to clear previous error messages.
   */
  virtual void clearErrorMessage() = 0;

  /**
   * This method should display information to Serial containing the parameter
   *   ID and the current value of the parameter (if it is confidential).
   *   Will only be called if debug is enabled.
   */
  virtual void debugTo(Stream* out) = 0;

#ifdef IOTWEBCONF_ENABLE_JSON
  /**
   * 
   */
  virtual void loadFromJson(JsonObject jsonObject) = 0;
#endif

protected:
  ConfigItem(const char* id) { this->_id = id; };

private:
  const char* _id = 0;
  ConfigItem* _parentItem = nullptr;
  ConfigItem* _nextItem = nullptr;
  friend class ParameterGroup; // Allow ParameterGroup to access _nextItem.
};

class ParameterGroup : public ConfigItem
{
public:
  ParameterGroup(const char* id, const char* label = nullptr);
  void addItem(ConfigItem* configItem);
  const char *label;
  void applyDefaultValue() override;
#ifdef IOTWEBCONF_ENABLE_JSON
  virtual void loadFromJson(JsonObject jsonObject) override;
#endif

protected:
  int getStorageSize() override;
  void storeValue(std::function<void(
    SerializationData* serializationData)> doStore) override;
  void loadValue(std::function<void(
    SerializationData* serializationData)> doLoad) override;
  void renderHtml(bool dataArrived, WebRequestWrapper* webRequestWrapper) override;
  void update(WebRequestWrapper* webRequestWrapper) override;
  void clearErrorMessage() override;
  void debugTo(Stream* out) override;
  /**
   * One can override this method in case a specific HTML template is required
   * for a group.
   */
  virtual String getStartTemplate() { return FPSTR(IOTWEBCONF_HTML_FORM_GROUP_START); };
  /**
   * One can override this method in case a specific HTML template is required
   * for a group.
   */
  virtual String getEndTemplate() { return FPSTR(IOTWEBCONF_HTML_FORM_GROUP_END); };

  ConfigItem* _firstItem = nullptr;
  ConfigItem* getNextItemOf(ConfigItem* parent) { return parent->_nextItem; };

  friend class IotWebConf; // Allow IotWebConf to access protected members.

private:
};

/**
 * Parameters is a configuration item of the config portal.
 * The parameter will have its input field on the configuration page,
 * and the provided value will be saved to the EEPROM.
 */
class Parameter : public ConfigItem
{
public:
  /**
   * Create a parameter for the config portal.
   *
   * @label - Displayable label at the config portal.
   * @id - Identifier used for HTTP queries and as configuration key. Must not
   *   contain spaces nor other special characters.
   * @valueBuffer - Configuration value will be loaded to this buffer from the
   *   EEPROM.
   * @length - The buffer should have a length provided here.
   * @defaultValue - Defalt value set on startup, when no configuration ever saved
   *   with the current config-version.
   */
  Parameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* defaultValue = nullptr);

  const char* label;
  char* valueBuffer;
  const char* defaultValue;
  const char* errorMessage;

  int getLength() { return this->_length; }
  void applyDefaultValue() override;
#ifdef IOTWEBCONF_ENABLE_JSON
  virtual void loadFromJson(JsonObject jsonObject) override;
#endif

protected:
  // Overrides
  int getStorageSize() override;
  void storeValue(std::function<void(SerializationData* serializationData)> doStore) override;
  void loadValue(std::function<void(SerializationData* serializationData)> doLoad) override;
  virtual void update(WebRequestWrapper* webRequestWrapper) override;
  virtual void update(String newValue) = 0;
  void clearErrorMessage() override;

private:
  int _length;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * TexParameters is to store text based parameters.
 */
class TextParameter : public Parameter
{
public:
  /**
   * Create a text parameter for the config portal.
   *
   * @placeholder (optional) - Text appear in an empty input box.
   * @customHtml (optional) - The text of this parameter will be added into
   *   the HTML INPUT field.
   * (See Parameter for arguments!)
   */
  TextParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* defaultValue = nullptr,
    const char* placeholder = nullptr,
    const char* customHtml = nullptr);

  /**
   * This variable is meant to store a value that is displayed in an empty
   *   (not filled) field.
   */
  const char* placeholder;

  /**
   * Usually this variable is used when rendering the form input field
   *   so one can customize the rendered outcome of this particular item.
   */
  const char* customHtml;

protected:
  virtual String renderHtml(
    bool dataArrived, bool hasValueFromPost, String valueFromPost);
  // Overrides
  virtual void renderHtml(bool dataArrived, WebRequestWrapper* webRequestWrapper) override;
  virtual void update(String newValue) override;
  virtual void debugTo(Stream* out) override;
  /**
   * One can override this method in case a specific HTML template is required
   * for a parameter.
   */
  virtual String getHtmlTemplate() { return FPSTR(IOTWEBCONF_HTML_FORM_PARAM); };

  /**
   * Renders a standard HTML form INPUT.
   * @type - The type attribute of the html input field.
   */
  virtual String renderHtml(const char* type, bool hasValueFromPost, String valueFromPost);

private:
  friend class IotWebConf;
  friend class WifiParameterGroup;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * The Password parameter has a special handling, as the value will be
 * overwritten in the EEPROM only if value was provided on the config portal.
 * Because of this logic, "password" type field with length more then
 * IOTWEBCONF_PASSWORD_LEN characters are not supported.
 */
class PasswordParameter : public TextParameter
{
public:
  /**
   * Create a password parameter for the config portal.
   *
   * (See TextParameter for arguments!)
   */
  PasswordParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* defaultValue = nullptr,
    const char* placeholder = nullptr,
    const char* customHtml = "ondblclick=\"pw(this.id)\"");

protected:
  // Overrides
  virtual String renderHtml(
    bool dataArrived, bool hasValueFromPost, String valueFromPost) override;
  virtual void update(String newValue) override;
  virtual void debugTo(Stream* out) override;

private:
  friend class IotWebConf;
  friend class WifiParameterGroup;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * This is just a text parameter, that is rendered with type 'number'.
 */
class NumberParameter : public TextParameter
{
public:
  /**
   * Create a numeric parameter for the config portal.
   *
   * (See TextParameter for arguments!)
   */
  NumberParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* defaultValue = nullptr,
    const char* placeholder = nullptr,
    const char* customHtml = nullptr);

protected:
  // Overrides
  virtual String renderHtml(
    bool dataArrived, bool hasValueFromPost, String valueFromPost) override;

private:
  friend class IotWebConf;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Checkbox parameter is represended as a text parameter but has a special
 * handling. As the value is either empty or has the word "selected".
 * Note, that form post will not send value if checkbox was not selected.
 */
class CheckboxParameter : public TextParameter
{
public:
  /**
   * Create a checkbox parameter for the config portal.
   *
   * (See TextParameter for arguments!)
   */
  CheckboxParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    bool defaultValue = false);
  bool isChecked() { return strncmp(this->valueBuffer, "selected", this->getLength()) == 0; }

protected:
  // Overrides
  virtual String renderHtml(
    bool dataArrived, bool hasValueFromPost, String valueFromPost) override;
  virtual void update(WebRequestWrapper* webRequestWrapper) override;

private:
  friend class IotWebConf;
  bool _checked;
  const char* _checkedStr = "checked='checked'";
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Options parameter is a structure, that handles multiple values when redering
 * the HTML representation.
 */
class OptionsParameter : public TextParameter
{
public:
  /**
   * @optionValues - List of values to choose from with, where each value
   *   can have a maximal size of 'length'. Contains 'optionCount' items.
   * @optionNames - List of names to render for the values, where each
   *   name can have a maximal size of 'nameLength'. Contains 'optionCount'
   *   items.
   * @optionCount - Size of both 'optionValues' and 'optionNames' lists.
   * @nameLength - Size of any item in optionNames list.
   * (See TextParameter for arguments!)
   */
  OptionsParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* optionValues, const char* optionNames, size_t optionCount, size_t nameLength,
    const char* defaultValue = nullptr);

protected:
  const char* _optionValues;
  const char* _optionNames;
  size_t _optionCount;
  size_t _nameLength;

private:
  friend class IotWebConf;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Select parameter is an option parameter, that rendered as HTML SELECT.
 * Basically it is a dropdown combobox.
 */
class SelectParameter : public OptionsParameter
{
public:
  /**
   * Create a select parameter for the config portal.
   *
   * (See OptionsParameter for arguments!)
   */
  SelectParameter(
    const char* label, const char* id, char* valueBuffer, int length,
    const char* optionValues, const char* optionNames, size_t optionCount, size_t namesLenth,
    const char* defaultValue = nullptr);

protected:
  // Overrides
  virtual String renderHtml(
    bool dataArrived, bool hasValueFromPost, String valueFromPost) override;

private:
  friend class IotWebConf;
};

/**
 * This class is here just to make some nice indents on debug output
 *   for group tree.
 */
class PrefixStreamWrapper : public Stream
{
public:
  PrefixStreamWrapper(
    Stream* originalStream,
    std::function<size_t(Stream* stream)> prefixWriter);
  size_t write(uint8_t) override;
  size_t write(const uint8_t *buffer, size_t size) override;
  int available() override { return _originalStream->available(); };
  int read() override { return _originalStream->read(); };
  int peek() override { return _originalStream->peek(); };
  void flush() override { return _originalStream->flush(); };

private:
  Stream* _originalStream;
  std::function<size_t(Stream* stream)> _prefixWriter;
  bool _newLine = true;

  size_t checkNewLine();
};

} // end namespace

#endif