IotWebConfMultipleWifi.cpp 2.85 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
/**
 * IotWebConfMultipleWifi.cpp -- IotWebConf is an ESP8266/ESP32
 *   non blocking WiFi/AP web configuration library for Arduino.
 *   https://github.com/prampec/IotWebConf
 *
 * Copyright (C) 2021 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.
 */

#include "IotWebConfMultipleWifi.h"

namespace iotwebconf
{

MultipleWifiAddition::MultipleWifiAddition(
  IotWebConf* iotWebConf,
  ChainedWifiParameterGroup sets[],
  size_t setsSize)
{
  this->_iotWebConf = iotWebConf;
  this->_firstSet = &sets[0];
  this->_currentSet = &sets[0];

  ChainedWifiParameterGroup* set = &sets[0];
  for(size_t i=1; i<setsSize; i++)
  {
    set->setNext(&sets[i]);
    set = &sets[i];
  }
}

void MultipleWifiAddition::init()
{
  // -- Add parameter groups.
  ChainedWifiParameterGroup* set = this->_firstSet;
  while(set != nullptr)
  {
    this->_iotWebConf->addSystemParameter(set);
    set = (ChainedWifiParameterGroup*)set->getNext();
  }

  // -- Add custom format provider.
  this->_iotWebConf->setHtmlFormatProvider(
    &this->_optionalGroupHtmlFormatProvider);

  // -- Set up handler, that will selects next connection info to use.
  this->_iotWebConf->setFormValidator([&](WebRequestWrapper* webRequestWrapper)
    {
      return this->formValidator(webRequestWrapper);
    });

  // -- Set up handler, that will selects next connection info to use.
  this->_iotWebConf->setWifiConnectionFailedHandler([&]()
    {
        WifiAuthInfo* result;
        while (true)
        {
          if (this->_currentSet == nullptr)
          {
            this->_currentSet = this->_firstSet;
            this->_iotWebConf->resetWifiAuthInfo();
            result = nullptr;
            break;
          }
          else
          {
            if (this->_currentSet->isActive())
            {
              result = &this->_currentSet->wifiAuthInfo;
              this->_currentSet =
                (iotwebconf::ChainedWifiParameterGroup*)this->_currentSet->getNext();
              break;
            }
            else
            {
              this->_currentSet =
                (iotwebconf::ChainedWifiParameterGroup*)this->_currentSet->getNext();
            }
          }
        }
        return result;
    });
};

bool MultipleWifiAddition::formValidator(
  WebRequestWrapper* webRequestWrapper)
{
  ChainedWifiParameterGroup* set = this->_firstSet;
  bool valid = true;

  while(set != nullptr)
  {
    if (set->isActive())
    {
      PasswordParameter* pwdParam = &set->wifiPasswordParameter;
      int l = webRequestWrapper->arg(pwdParam->getId()).length();
      if ((0 < l) && (l < 8))
      {
        pwdParam->errorMessage = "Password length must be at least 8 characters.";
        valid = false;
      }
    }

    set = (ChainedWifiParameterGroup*)set->getNext();
  }

  return valid;
};

}