AutoConnectAux.cpp 34.2 KB
Newer Older
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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/**
 * Implementation of AutoConnectAux class.
 * @file AutoConnectAux.cpp
 * @author hieromon@gmail.com
 * @version  1.2.0
 * @date 2020-10-30
 * @copyright  MIT license.
 */
#include <algorithm>
#include "AutoConnect.h"
#include "AutoConnectAux.h"
#include "AutoConnectAuxImpl.h"
#include "AutoConnectUploadImpl.h"
#include "AutoConnectElementBasisImpl.h"
#ifdef AUTOCONNECT_USE_JSON
#include "AutoConnectElementJsonImpl.h"
#endif

/**
 * Template for auxiliary page composed with AutoConnectAux of user sketch.
 * The structure of the auxiliary page depends on this template for 
 * the purpose to be incorporated into the AutoConnect Menu.
 * The page element implemented by AutoConnectElement is placed at the 
 * position of {{AUX_ELEMENT}} token. This token is contained in a 
 * <div> block with a class defined in 'base-panel' and is held by a 
 * <form> element with an ID '_aux'.
 * The JavaScript that named 'sa' at the end of the template determines 
 * the behavior of AutoConnectSubmit.
 */
const char AutoConnectAux::_PAGE_AUX[] PROGMEM = {
  "{{HEAD}}"
  "<title>{{AUX_TITLE}}</title>"
  "<style type=\"text/css\">"
  "{{CSS_BASE}}"
  "{{CSS_UL}}"
  "{{CSS_INPUT_BUTTON}}"
  "{{CSS_INPUT_TEXT}}"
  "{{CSS_LUXBAR}}"
  "{{AUX_CSS}}"
  "</style>"
  "</head>"
  "<body style=\"padding-top:58px;\">"
  "<div class=\"container\">"
  "{{MENU_PRE}}"
  "{{MENU_AUX}}"
  "{{MENU_POST}}"
  "<div class=\"base-panel\"><div class=\"aux-page\">"
  "<form id='_aux' method=\"post\" onsubmit=\"return false;\" {{ENC_TYPE}}>"
  "<ul class=\"noorder\">"
  "{{AUX_ELEMENT}}"
  "</ul>"
  "</form>"
  "</div></div>"
  "</div>"
  "<script>"
  "function _bu(url) {"
  "var uri=document.createElement('input');"
  "uri.setAttribute('type','hidden');"
  "uri.setAttribute('name','" AUTOCONNECT_AUXURI_PARAM "');"
  "uri.setAttribute('value','{{AUX_URI}}');"
  "var fm=document.getElementById('_aux');"
  "fm.appendChild(uri);"
  "fm.action=url;"
  "return fm;"
  "}"
  "function _sa(url) {"
  "_bu(url).submit();"
  "}"
  "</script>"
  "</body>"
  "</html>"
};

/**
 * Destructs container of AutoConnectElement and release a unique
 * pointer of AutoConnect instance.
 */
AutoConnectAux::~AutoConnectAux() {
  _addonElm.clear();
  _addonElm.swap(_addonElm);
}

/**
 * Returns a null element as static storage.
 * This static element is referred by invalid JSON data.
 * @return A reference of a static element defined by name as null.
 */
AutoConnectElement& AutoConnectAux::_nullElement() {
  static AutoConnectElement nullElement("", "");
  return nullElement;
}

/**
 * Add an AutoConnectElement
 * @param  addon A reference of AutoConnectElement.
 */
void AutoConnectAux::add(AutoConnectElement& addon) {
  _addonElm.push_back(addon);
  AC_DBG("%s placed on %s\n", addon.name.length() ? addon.name.c_str() : "*noname", uri());
}

/**
 * Add an AutoConnectElement vector container to the AutoConnectAux page.
 * @param  addons  AutoConnectElementVT collection.
 */
void AutoConnectAux::add(AutoConnectElementVT addons) {
  for (AutoConnectElement& element : addons)
    add(element);
}

/**
 * Parses the query parameters contained in the http request and fetches
 * the value of AutoConnectElements carried by AutoConnectAux.
 */
void AutoConnectAux::fetchElement(void) {
  WebServerClass*  _webServer = _ac->_webServer.get();
  if (_webServer->hasArg(String(F(AUTOCONNECT_AUXURI_PARAM)))) {
    _ac->_auxUri = _webServer->arg(String(F(AUTOCONNECT_AUXURI_PARAM)));
    _ac->_auxUri.replace("&#47;", "/");
    AC_DBG("fetch %s", _ac->_auxUri.c_str());
    AutoConnectAux* aux = _ac->_aux;
    while (aux) {
      if (aux->_uriStr == _ac->_auxUri) {
        // Save the value owned by each element contained in the POST body
        // of a current HTTP request to AutoConnectElements.
        aux->_storeElements(_webServer);
        break;
      }
      aux = aux->_next;
    }
  }
}

/**
 * Get already registered AutoConnectElement.
 * @param  name  Element name
 * @return A pointer to the registered AutoConnectElement.
 */
AutoConnectElement* AutoConnectAux::getElement(const String& name) {
  for (AutoConnectElement& elm : _addonElm)
    if (elm.name.equalsIgnoreCase(name))
      return &elm;
  AC_DBG("Element<%s> not registered\n", name.c_str());
  return nullptr;
}

/**
 * Validate all AutoConnectInputs value.
 * @return true  Validation successfull
 * @return false Some elements failed validation.
 */
bool AutoConnectAux::isValid(void) const {
  bool  rc = true;
  for (AutoConnectElement& elm : _addonElm)
    if (elm.typeOf() == AC_Input) {
      AutoConnectInput& elmInput = reinterpret_cast<AutoConnectInput&>(elm);
      rc &= elmInput.isValid();
    }
  return rc;
}

/**
 * Releases the AutoConnectElements with the specified name from 
 * the AutoConnectAux page. Releases all AutoConnectElements with 
 * the same name in AutoConnectAux.
 * @param  name  
 * @return true  The specified AutoConnectElements have been released.
 * @return false The specified AutoConnectElement not found in AutoConnectAux.
 */
bool AutoConnectAux::release(const String& name) {
  auto itr = std::remove_if(_addonElm.begin(), _addonElm.end(),
    [&](std::reference_wrapper<AutoConnectElement> const elm) {
      return elm.get().name.equalsIgnoreCase(name);
    });
  return _addonElm.erase(itr, _addonElm.end()) != _addonElm.end();
}

/**
 * Set the value to specified element.
 * @param  name  A string of element name to set the value.
 * @param  value Setting value. (String)
 * @return true  The value was set.
 * @return false An element specified name is not registered,
 * or its element value does not match storage type.
 */
bool AutoConnectAux::setElementValue(const String& name, const String value) {
  AutoConnectElement* elm = getElement(name);
  if (elm) {
    if (elm->typeOf() == AC_Select) {
      AutoConnectSelect* elmSelect = reinterpret_cast<AutoConnectSelect*>(elm);
      elmSelect->select(value);
    }
    else {
      if (elm->typeOf() == AC_Checkbox) {
        if (value == "checked") {
          AutoConnectCheckbox* elmCheckbox = reinterpret_cast<AutoConnectCheckbox*>(elm);
          elmCheckbox->checked = true;
        }
      }
      else if (elm->typeOf() == AC_Radio) {
        AutoConnectRadio* elmRadio = reinterpret_cast<AutoConnectRadio*>(elm);
        elmRadio->check(value);
      }
      else
        elm->value = value;
      return true;
    }
  }
  return false;
}

/**
 * Set the value to specified element.
 * @param  name  A string of element name to set the value.
 * @param  value Setting value. (String)
 * @return true  The value was set.
 * @return false An element specified name is not registered,
 * or its element value must be array.
 */
bool AutoConnectAux::setElementValue(const String& name, std::vector<String> const& values) {
  bool  rc = false;

  AutoConnectElement* elm = getElement(name);
  if (elm) {
    switch (elm->typeOf()) {
    case AC_Radio: {
      AutoConnectRadio* elmRadio = reinterpret_cast<AutoConnectRadio*>(elm);
      elmRadio->empty();
      for (String v : values)
        elmRadio->add(v);
      rc = true;
      break;
    }
    case AC_Select: {
      AutoConnectSelect* elmSelect = reinterpret_cast<AutoConnectSelect*>(elm);
      elmSelect->empty();
      for (String o : values)
        elmSelect->add(o);
      rc = true;
      break;
    }
    default: {
      AC_DBG("Element<%s> value type mismatch\n", name.c_str());
      break;
    }
    }
  }
  return rc;
}

/**
 * The upload function that overrides the RequestHandler class
 * attached with ESP8266WebServer.
 * This function invokes the upload handler registered by the onUpload
 * function which will be implemented by the user sketch.
 */
void AutoConnectAux::upload(const String& requestUri, const HTTPUpload& upload) {
  if (upload.status == UPLOAD_FILE_START) {
    AC_DBG("%s requests upload to %s\n", requestUri.c_str(), _uriStr.c_str());
    // Selects a valid upload handler before uploading starts.
    // Identify AutoConnectFile with the current upload request and
    // save the value and mimeType attributes.
    AC_DBG("ACFile %s ", upload.name.c_str());
    String  logContext = "missing";

    AutoConnectElementVT  addons;
    AutoConnectAux* aux = _ac->_aux;
    while (aux) {
      if (aux->_uriStr == requestUri) {
        addons = aux->_addonElm;
        break;
      }
      aux = aux->_next;
    }

    _currentUpload = nullptr;
    for (AutoConnectElement& elm : addons) {
      if (elm.typeOf() == AC_File) {
        _currentUpload = reinterpret_cast<AutoConnectFile*>(&elm);
        // Reset previous value
        _currentUpload->value = String("");
        _currentUpload->mimeType = String("");
        _currentUpload->size = 0;
        // Overwrite with current upload request
        if (upload.name.equalsIgnoreCase(_currentUpload->name)) {
          _currentUpload->value = upload.filename;
          _currentUpload->mimeType = upload.type;
          logContext = "accepted " + _currentUpload->value;
          break;
        }
      }
    }
    AC_DBG_DUMB("%s, handler ", logContext.c_str());

    // If the current upload request is AutoConnectFile without default
    // AutoConnectUpload (i.e. the store attribute is AC_File_Ex),
    // enable the user-owned upload handler activated by the onUpload.
    _upload = nullptr;
    if (_currentUpload)
      if (_currentUpload->attach(_currentUpload->store)) {
        _upload = std::bind(&AutoConnectUploadHandler::upload, _currentUpload->upload(), std::placeholders::_1, std::placeholders::_2);
        AC_DBG_DUMB("attached(%d)\n", (int)_currentUpload->store);
      }

    if (!_upload) {
      if (_uploadHandler) {
        _upload = _uploadHandler;
        AC_DBG_DUMB("enabled\n");
      }
      else {
        AC_DBG_DUMB("missing\n");
      }
    }
  }

  // Invokes upload handler
  if (_upload) {
    _upload(requestUri, upload);
    if (_currentUpload)
      _currentUpload->size = upload.totalSize;
    // Upload ended, purge handler
    if (upload.status == UPLOAD_FILE_END || upload.status == UPLOAD_FILE_ABORTED) {
      if (_currentUpload)
        _currentUpload->detach();
      AC_DBG("%d bytes uploaded\n", upload.totalSize);
    }
  }
}

/**
 * Concatenates subsequent AutoConnectAux pages starting from oneself 
 * to the chain list. 
 * AutoConnectAux is collected in the chain list and each object is 
 * chained by the "_next". AutoConnect follows the "_next" to manage 
 * auxiliary pages. The _concat function concatenates subsequent 
 * AutoConnectAuxes.
 * @param  aux   A reference of AutoConnectAux.
 */
void AutoConnectAux::_concat(AutoConnectAux& aux) {
  if (_next)
    _next->_concat(aux);
  else
    _next = &aux;
}

/**
 * Register the AutoConnect that owns itself.
 * AutoConnectAux needs to access the AutoConnect member. Also 
 * AutoConnectAux is cataloged by chain list. The _join function 
 * registers AutoConnect in the following AutoConnectAux chain list.
 * @param  ac    A reference of AutoConnect.
 */
void AutoConnectAux::_join(AutoConnect& ac) {
  _ac = &ac;

  // Chain to subsequent AutoConnectAux in the list.
  if (_next)
    _next->_join(ac);
}

/**
 * Inject the <li> element depending on the "luxbar-item" attribute
 * for implementing the AutoConnect menu.
 * @param  args  A reference of PageArgument but it's only used for
 * interface alignment and is not actually used.
 * @return A concatenated string of <li> elements for the menu item of
 * AutoConnect.
 */
const String AutoConnectAux::_injectMenu(PageArgument& args) {
  String  menuItem;

  if (_menu)
    menuItem = String(FPSTR("<li class=\"lb-item\"><a href=\"")) + String(_uri) + String("\">") + _title + String(FPSTR("</a></li>"));
  if (_next)
    menuItem += _next->_injectMenu(args);
  return menuItem;
}

/**
 * Insert the uri that caused the request to the aux.
 */
const String AutoConnectAux::_indicateUri(PageArgument& args) {
  AC_UNUSED(args);
  String  lastUri = _uriStr;
  // The following code contains adding and trimming a blank that is
  // wasteful for this function. It exists for avoiding the bug of
  // WString::replace of ESP8266 arduino core 2.5.2.
  // https://github.com/esp8266/Arduino/issues/6192
  String  reps = "/";
  String  replacement = "&#47;";
  if (lastUri.length() == reps.length() + replacement.length())
    lastUri += " ";
  lastUri.replace(reps, replacement);
  lastUri.trim();
  return lastUri;
}

/**
 * Modifying the form of attribute depending on the type of `input` tag
 * contained. If the custom web page contains `input type=file` then
 * allows multipart as ENCTYPE attribute.
 * @param  args  A reference of PageArgument but unused.
 * @return HTML string that should be inserted.
 */
const String AutoConnectAux::_indicateEncType(PageArgument& args) {
  AC_UNUSED(args);
  String  encType = String("");
  for (AutoConnectElement& elm : _addonElm)
    if (elm.typeOf() == AC_File) {
      return String(F("enctype='multipart/form-data'"));
    }
  return AutoConnect::_emptyString;
}

/**
 * Insert the token handler of PageBuilder. This handler inserts HTML
 * elements generated by the whole AutoConnectElements to the auxiliary page.
 * @param  args  A reference of PageArgument but unused.
 * @return HTML string that should be inserted.
 */
const String AutoConnectAux::_insertElement(PageArgument& args) {
  String  body = String("");

  // When WebServerClass::handleClient calls RequestHandler, the parsed
  //  http argument has been prepared.
  // If the current request argument contains AutoConnectElement, it is
  // the form data of the AutoConnectAux page and with this timing save
  // the value of each element.
  fetchElement();

  // Call user handler before HTML generation.
  if (_handler) {
    if (_order & AC_EXIT_AHEAD) {
      AC_DBG("CB in AHEAD %s\n", uri());
      body += _handler(*this, args);
    }
  }

  // Generate HTML for all AutoConnectElements contained in the page.
  for (AutoConnectElement& addon : _addonElm) {
    // Since the style sheet has already drained at the time of the
    // _insertElement function call, it skips the call to the HTML
    // generator by each element.
    if (addon.typeOf() != AC_Style)
      // Invoke an HTML generator by each element
      body += addon.toHTML();
  }

  // Call user handler after HTML generation.
  if (_handler) {
    if (_order & AC_EXIT_LATER) {
      AC_DBG("CB in LATER %s\n", uri());
      body += _handler(*this, args);
    }
  }
  return body;
}

/**
 * Insert user defined CSS code to AutoConnectAux page.
 * @param  args  A reference of PageArgument but unused.
 * @return HTML string that should be inserted.
 */
const String AutoConnectAux::_insertStyle(PageArgument& args) {
  String  css = String("");

  for (AutoConnectElement& elm : _addonElm) {
    if (elm.typeOf() == AC_Style)
      css += elm.toHTML();
  }
  return css;
}

/**
 * Generate an auxiliary page assembled with the AutoConnectElement.
 * This function is the core procedure of AutoConnectAux, and uses 
 * PageBuilder from the _PAGE_AUX template to build an AutoConnect 
 * menu and insert HTML elements. A template of an auxiliary page is 
 * fixed and its structure inherits from the AutoConnect.
 * @param  uri   An uri of the auxiliary page.
 * @return A PageElement of auxiliary page.
 */
PageElement* AutoConnectAux::_setupPage(const String& uri) {
  PageElement*  elm = nullptr;

  if (_ac) {
    if (uri != String(_uri)) {
      if (_next) {
        elm = _next->_setupPage(uri);
      }
    } else {
      AutoConnect*  mother = _ac;
      // Overwrite actual AutoConnectMenu title to the Aux. page title
      if (_title.length())
        mother->_menuTitle = _title;

      elm = new PageElement();
      // Construct the auxiliary page
      elm->setMold(_PAGE_AUX);
      elm->addToken(String(FPSTR("HEAD")), std::bind(&AutoConnect::_token_HEAD, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("AUX_TITLE")), std::bind(&AutoConnectAux::_injectTitle, this, std::placeholders::_1));
      elm->addToken(String(FPSTR("CSS_BASE")), std::bind(&AutoConnect::_token_CSS_BASE, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("CSS_UL")), std::bind(&AutoConnect::_token_CSS_UL, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("CSS_INPUT_BUTTON")), std::bind(&AutoConnect::_token_CSS_INPUT_BUTTON, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("CSS_INPUT_TEXT")), std::bind(&AutoConnect::_token_CSS_INPUT_TEXT, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("CSS_LUXBAR")), std::bind(&AutoConnect::_token_CSS_LUXBAR, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("AUX_CSS")), std::bind(&AutoConnectAux::_insertStyle, this, std::placeholders::_1));
      elm->addToken(String(FPSTR("MENU_PRE")), std::bind(&AutoConnect::_token_MENU_PRE, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("MENU_AUX")), std::bind(&AutoConnect::_token_MENU_AUX, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, mother, std::placeholders::_1));
      elm->addToken(String(FPSTR("AUX_URI")), std::bind(&AutoConnectAux::_indicateUri, this, std::placeholders::_1));
      elm->addToken(String(FPSTR("ENC_TYPE")), std::bind(&AutoConnectAux::_indicateEncType, this, std::placeholders::_1));
      elm->addToken(String(FPSTR("AUX_ELEMENT")), std::bind(&AutoConnectAux::_insertElement, this, std::placeholders::_1));
      // Restore transfer mode by each page
      mother->_responsePage->chunked(chunk);

      // Register authentication
      // Determine the necessity of authentication from the conditions of
      // AutoConnectConfig::authScope and derive the method.
      bool  auth = ((mother->_apConfig.authScope & AC_AUTHSCOPE_AUX) && (mother->_apConfig.auth != AC_AUTH_NONE))
                || ((mother->_apConfig.authScope & AC_AUTHSCOPE_PARTIAL) && (_httpAuth != AC_AUTH_NONE));
      HTTPAuthMethod  method;
      if (mother->_apConfig.authScope & AC_AUTHSCOPE_PARTIAL)
        method = _httpAuth == AC_AUTH_BASIC ? HTTPAuthMethod::BASIC_AUTH : HTTPAuthMethod::DIGEST_AUTH;
      else
        method = mother->_apConfig.auth == AC_AUTH_BASIC ? HTTPAuthMethod::BASIC_AUTH : HTTPAuthMethod::DIGEST_AUTH;
      mother->_authentication(auth, method);
    }
  }
  return elm;
}

/**
 * Store element values owned by AutoConnectAux that caused the request.
 * Save the current arguments remaining in the Web server object when
 * this function invoked.
 * @param webServer A pointer to the class object of WebServerClass
 */
void AutoConnectAux::_storeElements(WebServerClass* webServer) {
  // Retrieve each element value, Overwrites the value of all cataloged
  // AutoConnectElements with arguments inherited from last http request.
  for (AutoConnectElement& elm : _addonElm) {

    // The POST body does not contain the value of the AutoConnectFile,
    // so it can not be obtained with the WebServerClass::arg function.
    // The AutoConnectFile value will be restored from least recent
    // upload request.
    if (elm.typeOf() == AC_File)
      continue;

    // Relies on AutoConnectRadio, it restores to false at the being
    // because the checkbox argument will not pass if it is not checked.
    if (elm.typeOf() == AC_Checkbox)
      reinterpret_cast<AutoConnectCheckbox&>(elm).checked = false;

    // Seek by argument, store the value to its element.
    for (int8_t n = 0; n < static_cast<int8_t>(webServer->args()); n++) {
      if (webServer->argName(n).equalsIgnoreCase(elm.name)) {
        String  elmValue = webServer->arg(n);
        if (elm.typeOf() == AC_Checkbox)
          elmValue = "checked";
        setElementValue(elm.name, elmValue);

        // Copy a value to other elements declared as global.
        if (elm.global) {
          AutoConnectAux* aux = _ac->_aux;
          while (aux) {
            if (aux != this)
              aux->setElementValue(elm.name, elmValue);
            aux = aux->_next;
          }
        }
      }
    }
  }
  AC_DBG_DUMB(",elements stored\n");
}

#ifdef AUTOCONNECT_USE_JSON

/**
 * Load AutoConnectAux page from JSON description stored in PROGMEM.
 * This function can load AutoConnectAux for multiple AUX pages written
 * in JSON and is registered in AutoConnect.
 * @param  aux  JSON description to be load.
 * @return true Successfully loaded.
 */
bool AutoConnect::load(PGM_P aux) {
  return _parseJson<const __FlashStringHelper*>(reinterpret_cast<const __FlashStringHelper*>(aux));
}

/**
 * Load AutoConnectAux page from JSON description stored in PROGMEM.
 * This function can load AutoConnectAux for multiple AUX pages written
 * in JSON and is registered in AutoConnect.
 * @param  aux  JSON description to be load.
 * @return true Successfully loaded.
 */
bool AutoConnect::load(const __FlashStringHelper* aux) {
  return _parseJson<const __FlashStringHelper*>(aux);
}

/**
 * Load AutoConnectAux page from JSON description stored in the sketch.
 * This function can load AutoConnectAux for multiple AUX pages written
 * in JSON and is registered in AutoConnect.
 * @param  aux  JSON description to be load.
 * @return true Successfully loaded.
 */
bool AutoConnect::load(const String& aux) {
  return _parseJson<const String&>(aux);
}

/**
 * Load AutoConnectAux page from JSON description from the stream.
 * This function can load AutoConnectAux for multiple AUX pages written
 * in JSON and is registered in AutoConnect.
 * @param  aux  Stream for read AutoConnectAux elements.
 * @return true Successfully loaded.
 */
bool AutoConnect::load(Stream& aux) {
  return _parseJson<Stream&>(aux);
}

/**
 * Load AutoConnectAux page from JSON object.
 * @param  aux  A JsonVariant object that stores each element of AutoConnectAux.
 * @return true Successfully loaded.
 */
bool AutoConnect::_load(JsonVariant& auxJson) {
  bool  rc = true;
  if (auxJson.is<JsonArray>()) {
    ArduinoJsonArray  jb = auxJson.as<JsonArray>();
    for (ArduinoJsonObject  auxJson : jb) {
      AutoConnectAux* newAux = new AutoConnectAux;
      if (newAux->_load(auxJson))
        join(*newAux);
      else {
        delete newAux;
        rc = false;
        break;
      }
    }
  }
  else {
    ArduinoJsonObject jb = auxJson.as<JsonObject>();
    AutoConnectAux* newAux = new AutoConnectAux;
    if (newAux->_load(jb))
      join(*newAux);
    else {
      delete newAux;
      rc = false;
    }
  }
  return rc;
}

/**
 * Create an instance from the AutoConnectElement of the JSON object.
 * @param  json  A reference of JSON
 * @return A pointer of created AutoConnectElement instance.
 */
AutoConnectElement* AutoConnectAux::_createElement(const JsonObject& json) {
  AutoConnectElement* elm = nullptr;
  String  type = json[F(AUTOCONNECT_JSON_KEY_TYPE)].as<String>();

  switch (_asElementType(type)) {
  case AC_Element:
  case AC_Unknown:
    elm = new AutoConnectElement;
    break;
  case AC_Button: {
    AutoConnectButton*  cert_elm = new AutoConnectButton;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Checkbox: {
    AutoConnectCheckbox*  cert_elm = new AutoConnectCheckbox;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_File: {
    AutoConnectFile* cert_elm = new AutoConnectFile;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Input: {
    AutoConnectInput* cert_elm = new AutoConnectInput;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Radio: {
    AutoConnectRadio*  cert_elm = new AutoConnectRadio;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Select: {
    AutoConnectSelect*  cert_elm = new AutoConnectSelect;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Style: {
    AutoConnectStyle*  cert_elm = new AutoConnectStyle;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Submit: {
    AutoConnectSubmit*  cert_elm = new AutoConnectSubmit;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  case AC_Text: {
    AutoConnectText*  cert_elm = new AutoConnectText;
    return reinterpret_cast<AutoConnectElement*>(cert_elm);
  }
  }
  return elm;
}

/**
 * Constructs an AutoConnectAux instance by reading all the
 * AutoConnectElements of the specified URI from the elements defined 
 * JSON stored in a constant character string.
 * @param  in    AutoConnectAux element data which is described by JSON.
 * @return true  The element collection successfully loaded.
 * @return false Invalid JSON data occurred. 
 */
bool AutoConnectAux::load(const String& in) {
  return _parseJson<const String&>(in);
}

/**
 * Constructs an AutoConnectAux instance by reading all the
 * AutoConnectElements of the specified URI from the elements passing
 * pointer to JSON stored in pgm_data array.
 * @param  in    AutoConnectAux element data which is described by JSON.
 * @return true  The element collection successfully loaded.
 * @return false Invalid JSON data occurred. 
 */
bool AutoConnectAux::load(PGM_P in) {
  return _parseJson<const __FlashStringHelper*>(reinterpret_cast<const __FlashStringHelper*>(in));
}

/**
 * Constructs an AutoConnectAux instance by reading all the
 * AutoConnectElements of the specified URI from the elements defined
 * JSON stored in pgm_data array.
 * @param  in    AutoConnectAux element data which is described by JSON.
 * @return true  The element collection successfully loaded.
 * @return false Invalid JSON data occurred. 
 */
bool AutoConnectAux::load(const __FlashStringHelper* in) {
  return _parseJson<const __FlashStringHelper*>(in);
}

/**
 * Constructs an AutoConnectAux instance by reading all the
 * AutoConnectElements of the specified URI from the elements defined
 * JSON stored in a Stream.
 * @param  in    AutoConnectAux element data which is described by JSON.
 * @return true  The element collection successfully loaded.
 * @return false Invalid JSON data occurred. 
 */
bool AutoConnectAux::load(Stream& in) {
  return _parseJson<Stream&>(in);
}

/**
 * Load all elements of AutoConectAux page from JSON object.
 * @param  jb    Reference of JSON object
 * @return true  Successfully loaded.
 * @return false loading unsuccessful, JSON parsing error occurred.
 */
bool AutoConnectAux::_load(JsonObject& jb) {
  _title = jb[F(AUTOCONNECT_JSON_KEY_TITLE)].as<String>();
  _uriStr = jb[F(AUTOCONNECT_JSON_KEY_URI)].as<String>();
  _uri = _uriStr.c_str();
  _menu = jb[F(AUTOCONNECT_JSON_KEY_MENU)].as<bool>();
  String  auth = jb[F(AUTOCONNECT_JSON_KEY_AUTH)].as<String>();
  if (auth.equalsIgnoreCase(F(AUTOCONNECT_JSON_VALUE_BASIC)))
    _httpAuth = AC_AUTH_BASIC;
  else if (auth.equalsIgnoreCase(F(AUTOCONNECT_JSON_VALUE_DIGEST)))
    _httpAuth = AC_AUTH_DIGEST;
  if (auth.equalsIgnoreCase(F(AUTOCONNECT_JSON_VALUE_NONE)))
    _httpAuth = AC_AUTH_NONE;
  JsonVariant elements = jb[F(AUTOCONNECT_JSON_KEY_ELEMENT)];
  (void)_loadElement(elements, "");
  return true;
}

/**
 * Load element specified by the name parameter from the stream
 * described by JSON. Usually, the Stream is specified a storm file of
 * SD or SPIFFS. The Stream must be opened before invoking the function.
 * @param  in    Reference of the Stream which contains the parameter
 * data described by JSON.
 * @param  name  The element name to be loaded. '*'specifies that all
 * elements are to be loaded.
 * @return A reference of loaded AutoConnectElement instance.
 */
bool AutoConnectAux::loadElement(PGM_P in, const String& name) {
  return _parseElement<const __FlashStringHelper*, const String&>(reinterpret_cast<const __FlashStringHelper*>(in), name);
}

bool AutoConnectAux::loadElement(const __FlashStringHelper* in, const String& name) {
  return _parseElement<const __FlashStringHelper*, const String&>(in, name);
}

bool AutoConnectAux::loadElement(const String& in, const String& name) {
  return _parseElement<const String&, const String&>(in, name);
}
bool AutoConnectAux::loadElement(Stream& in, const String& name) {
  return _parseElement<Stream&, const String&>(in, name);
}

bool AutoConnectAux::loadElement(PGM_P in, std::vector<String> const& names) {
  return _parseElement<const __FlashStringHelper*, std::vector<String> const&>(reinterpret_cast<const __FlashStringHelper*>(in), names);
}

bool AutoConnectAux::loadElement(const __FlashStringHelper* in, std::vector<String> const& names) {
  return _parseElement<const __FlashStringHelper*, std::vector<String> const&>(in, names);
}

bool AutoConnectAux::loadElement(const String& in, std::vector<String> const& names) {
  return _parseElement<const String&, std::vector<String> const&>(in, names);
}

bool AutoConnectAux::loadElement(Stream& in, std::vector<String> const& names) {
  return _parseElement<Stream&, std::vector<String> const&>(in, names);
}

bool AutoConnectAux::_loadElement(JsonVariant& jb, std::vector<String> const& names) {
  bool  rc = true;
  for (const String& name : names)
    rc &= _loadElement(jb, name);
  return rc;
}

bool AutoConnectAux::_loadElement(JsonVariant& jb, const String& name) {
  bool  rc = false;
  if (jb.is<JsonArray>()) {
    ArduinoJsonArray  elements = jb.as<JsonArray>();
    for (ArduinoJsonObject  element : elements) {
      if (name.length()) {
        //Finds an element with the specified name in the JSON array and loads it.
        if (!name.equalsIgnoreCase(element[F(AUTOCONNECT_JSON_KEY_NAME)].as<String>()))
          continue;
      }
      AutoConnectElement& elm = _loadElement(element, name);
      if (elm.name.length())
        rc = true;
      if (name.length())
        break;
    }
  }
  else {
    ArduinoJsonObject element = jb.as<JsonObject>();
    AutoConnectElement& elm = _loadElement(element, name);
    if (elm.name.length())
      rc = true;
  }
  return rc;
}

AutoConnectElement& AutoConnectAux::_loadElement(JsonObject& element, const String& name) {
  AutoConnectElement* auxElm = nullptr;
  String  elmName = element[F(AUTOCONNECT_JSON_KEY_NAME)].as<String>();
  if (!name.length() || name.equalsIgnoreCase(elmName)) {
    // The specified element is defined in the JSON stream.
    // Loads from JSON object.
    auxElm = getElement(elmName);
    // The element is not created yet, create new one.
    if (!auxElm) {
      if (elmName.length()) {
        if ((auxElm = _createElement(element))) {
          AC_DBG("%s<%d> of %s created\n", elmName.c_str(), (int)(auxElm->typeOf()), uri());
          add(*auxElm);   // Insert to AutoConnect
        }
        else {
          AC_DBG("%s unknown element type\n", elmName.c_str());
        }
      }
      else {
        AC_DBG("Element name missing\n");
      }
    }
    if (auxElm) {
      if (auxElm->loadMember(element))
        AC_DBG("%s<%d> of %s loaded\n", auxElm->name.c_str(), (int)auxElm->typeOf(), uri());
      else {
        // Element type mismatch
        AC_DBG("Type of %s element mismatched\n", elmName.c_str());
      }
    }
  }
  return auxElm ? *auxElm : _nullElement();
}

/**
 * Serialize an element specified the name into the stream.
 * @param  name  An element name to be output.
 * @return Number of bytes output
 */
size_t AutoConnectAux::saveElement(Stream& out, std::vector<String> const& names) {
  size_t  bufferSize = 0;
  size_t  amount = names.size();
  size_t  size_n = 0;

  // Calculate JSON buffer size
  if (amount == 0) {
    bufferSize += JSON_OBJECT_SIZE(4);
    bufferSize += sizeof(AUTOCONNECT_JSON_KEY_TITLE) + _title.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_URI) + _uriStr.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_MENU) + sizeof(AUTOCONNECT_JSON_KEY_ELEMENT) + sizeof(AUTOCONNECT_JSON_KEY_AUTH) + sizeof(AUTOCONNECT_JSON_VALUE_DIGEST);
    bufferSize += JSON_ARRAY_SIZE(_addonElm.size());
  }
  else
    bufferSize += JSON_ARRAY_SIZE(amount);

  for (AutoConnectElement& elmEach : _addonElm) {
    AutoConnectElement* elm = &elmEach;
    if (amount > 0) {
      String& elmName = elm->name;
      auto aim = std::find_if(names.begin(), names.end(), [&](const String& n) { return n.equalsIgnoreCase(elmName); });
      if (aim == names.end())
        continue;
    }
    bufferSize += elm->getObjectSize();
  }
  // Round up to 16 boundary
  bufferSize = bufferSize > 0 ? ((bufferSize + 16) & (~0xf)) : bufferSize;
  AC_DBG("JSON buffer size:%d\n", bufferSize);

  // Serialization
  if (bufferSize > 0) {
    ArduinoJsonBuffer jb(bufferSize);
    if (amount == 1) {
      ArduinoJsonObject element = ARDUINOJSON_CREATEOBJECT(jb);
      for (AutoConnectElement& elm : _addonElm)
        if (elm.name.equalsIgnoreCase(names[0])) {
          elm.serialize(element);
          break;
        }
      size_n = ARDUINOJSON_PRINT(element, out);
    }
    else if (amount == 0) {
      ArduinoJsonObject json = ARDUINOJSON_CREATEOBJECT(jb);
      json[F(AUTOCONNECT_JSON_KEY_TITLE)] = _title;
      json[F(AUTOCONNECT_JSON_KEY_URI)] = _uriStr;
      json[F(AUTOCONNECT_JSON_KEY_MENU)] = _menu;
      if (_httpAuth == AC_AUTH_BASIC)
        json[F(AUTOCONNECT_JSON_KEY_AUTH)] = String(F(AUTOCONNECT_JSON_VALUE_BASIC));
      else if (_httpAuth == AC_AUTH_DIGEST)
        json[F(AUTOCONNECT_JSON_KEY_AUTH)] = String(F(AUTOCONNECT_JSON_VALUE_DIGEST));
      ArduinoJsonArray  elements = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_ELEMENT));
      for (AutoConnectElement& elm : _addonElm) {
        ArduinoJsonObject element = elements.createNestedObject();
        elm.serialize(element);
      }
      size_n = ARDUINOJSON_PRETTYPRINT(json, out);
    }
    else if (amount >= 2) {
      ArduinoJsonArray  elements = ARDUINOJSON_CREATEARRAY(jb);
      for (String name : names)
        for (AutoConnectElement& elm : _addonElm)
          if (elm.name.equalsIgnoreCase(name)) {
            ArduinoJsonObject element = elements.createNestedObject();
            elm.serialize(element);
            break;
          }
      size_n = ARDUINOJSON_PRETTYPRINT(elements, out);
    }
  }
  return size_n;
}

/**
 * Convert element type from type as String.
 * @param  type  An element type as String
 * @return A type of ACElement_t
 */
ACElement_t AutoConnectAux::_asElementType(const String& type) {
  typedef struct {
    const char* tName;
    ACElement_t tEnum;
  } ACElementType_t;
  static const ACElementType_t  types[] PROGMEM = {
    { AUTOCONNECT_JSON_TYPE_ACBUTTON, AC_Button },
    { AUTOCONNECT_JSON_TYPE_ACCHECKBOX, AC_Checkbox },
    { AUTOCONNECT_JSON_TYPE_ACELEMENT, AC_Element },
    { AUTOCONNECT_JSON_TYPE_ACFILE, AC_File },
    { AUTOCONNECT_JSON_TYPE_ACINPUT, AC_Input },
    { AUTOCONNECT_JSON_TYPE_ACRADIO, AC_Radio },
    { AUTOCONNECT_JSON_TYPE_ACSELECT, AC_Select },
    { AUTOCONNECT_JSON_TYPE_ACSTYLE, AC_Style },
    { AUTOCONNECT_JSON_TYPE_ACSUBMIT, AC_Submit },
    { AUTOCONNECT_JSON_TYPE_ACTEXT, AC_Text }
  };

  ACElement_t  t = AC_Unknown;
  for (size_t n = 0; n < (sizeof(types) / sizeof(ACElementType_t)); n++) {
    if (type.equalsIgnoreCase(String(FPSTR(types[n].tName))))
      return types[n].tEnum;
  }
  return t;
}

#endif // !AUTOCONNECT_USE_JSON