Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel
ampel-firmware
Commits
8c4fc341
Commit
8c4fc341
authored
Feb 15, 2022
by
Eric Duminil
Browse files
IotWebConf in src, not in platformio
parent
9d0a0e38
Changes
27
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/src/lib/IotWebConf/src/IotWebConfParameter.h
0 → 100644
View file @
8c4fc341
/**
* 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>
#include <IotWebConfSettings.h>
#include <IotWebConfWebServerWrapper.h>
#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
ampel-firmware/src/lib/IotWebConf/src/IotWebConfSettings.h
0 → 100644
View file @
8c4fc341
/**
* IotWebConfSettings.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 IotWebConfSettings_h
#define IotWebConfSettings_h
// -- We might want to place the config in the EEPROM in an offset.
#ifndef IOTWEBCONF_CONFIG_START
# define IOTWEBCONF_CONFIG_START 0
#endif
// -- Maximal length of any string used in IotWebConfig configuration (e.g.
// ssid).
#ifndef IOTWEBCONF_WORD_LEN
# define IOTWEBCONF_WORD_LEN 33
#endif
// -- Maximal length of password used in IotWebConfig configuration.
#ifndef IOTWEBCONF_PASSWORD_LEN
# define IOTWEBCONF_PASSWORD_LEN 33
#endif
// -- IotWebConf tries to connect to the local network for an amount of time
// before falling back to AP mode.
#ifndef IOTWEBCONF_DEFAULT_WIFI_CONNECTION_TIMEOUT_MS
# define IOTWEBCONF_DEFAULT_WIFI_CONNECTION_TIMEOUT_MS 30000
#endif
// -- Thing will stay in AP mode for an amount of time on boot, before retrying
// to connect to a WiFi network.
#ifndef IOTWEBCONF_DEFAULT_AP_MODE_TIMEOUT_SECS
# define IOTWEBCONF_DEFAULT_AP_MODE_TIMEOUT_SECS "30"
#endif
// -- mDNS should allow you to connect to this device with a hostname provided
// by the device. E.g. mything.local
// (This is not very likely to work, and MDNS is not very well documented.)
#ifndef IOTWEBCONF_CONFIG_DONT_USE_MDNS
# define IOTWEBCONF_CONFIG_USE_MDNS 80
#endif
// -- Logs progress information to Serial if enabled.
#ifndef IOTWEBCONF_DEBUG_DISABLED
# define IOTWEBCONF_DEBUG_TO_SERIAL
#endif
// -- Logs passwords to Serial if enabled.
//#define IOTWEBCONF_DEBUG_PWD_TO_SERIAL
// -- Helper define for serial debug
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
# define IOTWEBCONF_DEBUG_LINE(MSG) Serial.println(MSG)
#else
# define IOTWEBCONF_DEBUG_LINE(MSG)
#endif
// -- EEPROM config starts with a special prefix of length defined here.
#ifndef IOTWEBCONF_CONFIG_VERSION_LENGTH
# define IOTWEBCONF_CONFIG_VERSION_LENGTH 4
#endif
#ifndef IOTWEBCONF_DNS_PORT
# define IOTWEBCONF_DNS_PORT 53
#endif
#endif
\ No newline at end of file
ampel-firmware/src/lib/IotWebConf/src/IotWebConfTParameter.h
0 → 100644
View file @
8c4fc341
/**
* IotWebConfTParameter.h -- 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>
* rovo89
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#ifndef IotWebConfTParameter_h
#define IotWebConfTParameter_h
// TODO: This file is a mess. Help wanted to organize thing!
#include <IotWebConfParameter.h>
#include <Arduino.h>
#include <IPAddress.h>
#include <errno.h>
// At least in PlatformIO, strtoimax/strtoumax are defined, but not implemented.
#if 1
#define strtoimax strtoll
#define strtoumax strtoull
#endif
namespace
iotwebconf
{
/**
* This class is to hide web related properties from the
* data manipulation.
*/
class
ConfigItemBridge
:
public
ConfigItem
{
public:
virtual
void
update
(
WebRequestWrapper
*
webRequestWrapper
)
override
{
if
(
webRequestWrapper
->
hasArg
(
this
->
getId
()))
{
String
newValue
=
webRequestWrapper
->
arg
(
this
->
getId
());
this
->
update
(
newValue
);
}
}
void
debugTo
(
Stream
*
out
)
override
{
out
->
print
(
"'"
);
out
->
print
(
this
->
getId
());
out
->
print
(
"' with value: '"
);
out
->
print
(
this
->
toString
());
out
->
println
(
"'"
);
}
protected:
ConfigItemBridge
(
const
char
*
id
)
:
ConfigItem
(
id
)
{
}
virtual
int
getInputLength
()
{
return
0
;
};
virtual
bool
update
(
String
newValue
,
bool
validateOnly
=
false
)
=
0
;
virtual
String
toString
()
=
0
;
};
///////////////////////////////////////////////////////////////////////////
/**
* DataType is the data related part of the parameter.
* It does not care about web and visualization, but takes care of the
* data validation and storing.
*/
template
<
typename
ValueType
,
typename
_DefaultValueType
=
ValueType
>
class
DataType
:
virtual
public
ConfigItemBridge
{
public:
using
DefaultValueType
=
_DefaultValueType
;
DataType
(
const
char
*
id
,
DefaultValueType
defaultValue
)
:
ConfigItemBridge
(
id
),
_defaultValue
(
defaultValue
)
{
}
/**
* value() can be used to get the value, but it can also
* be used set it like this: p.value() = newValue
*/
ValueType
&
value
()
{
return
this
->
_value
;
}
ValueType
&
operator
*
()
{
return
this
->
_value
;
}
protected:
int
getStorageSize
()
override
{
return
sizeof
(
ValueType
);
}
virtual
bool
update
(
String
newValue
,
bool
validateOnly
=
false
)
=
0
;
bool
validate
(
String
newValue
)
{
return
update
(
newValue
,
true
);
}
virtual
String
toString
()
override
{
return
String
(
this
->
_value
);
}
ValueType
_value
;
const
DefaultValueType
_defaultValue
;
};
///////////////////////////////////////////////////////////////////////////
class
StringDataType
:
public
DataType
<
String
>
{
public:
using
DataType
<
String
>::
DataType
;
protected:
virtual
bool
update
(
String
newValue
,
bool
validateOnly
)
override
{
if
(
!
validateOnly
)
{
this
->
_value
=
newValue
;
}
return
true
;
}
virtual
String
toString
()
override
{
return
this
->
_value
;
}
};
///////////////////////////////////////////////////////////////////////////
template
<
size_t
len
>
class
CharArrayDataType
:
public
DataType
<
char
[
len
],
const
char
*>
{
public:
using
DataType
<
char
[
len
],
const
char
*>::
DataType
;
CharArrayDataType
(
const
char
*
id
,
const
char
*
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
DataType
<
char
[
len
],
const
char
*>::
DataType
(
id
,
defaultValue
)
{
};
virtual
void
applyDefaultValue
()
override
{
strncpy
(
this
->
_value
,
this
->
_defaultValue
,
len
);
}
protected:
virtual
bool
update
(
String
newValue
,
bool
validateOnly
)
override
{
if
(
newValue
.
length
()
+
1
>
len
)
{
return
false
;
}
if
(
!
validateOnly
)
{
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
print
(
this
->
getId
());
Serial
.
print
(
": "
);
Serial
.
println
(
newValue
);
#endif
strncpy
(
this
->
_value
,
newValue
.
c_str
(),
len
);
}
return
true
;
}
void
storeValue
(
std
::
function
<
void
(
SerializationData
*
serializationData
)
>
doStore
)
override
{
SerializationData
serializationData
;
serializationData
.
length
=
len
;
serializationData
.
data
=
(
byte
*
)
this
->
_value
;
doStore
(
&
serializationData
);
}
void
loadValue
(
std
::
function
<
void
(
SerializationData
*
serializationData
)
>
doLoad
)
override
{
SerializationData
serializationData
;
serializationData
.
length
=
len
;
serializationData
.
data
=
(
byte
*
)
this
->
_value
;
doLoad
(
&
serializationData
);
}
virtual
int
getInputLength
()
override
{
return
len
;
};
};
///////////////////////////////////////////////////////////////////////////
/**
* All non-complex types should be inherited from this base class.
*/
template
<
typename
ValueType
>
class
PrimitiveDataType
:
public
DataType
<
ValueType
>
{
public:
using
DataType
<
ValueType
>::
DataType
;
PrimitiveDataType
(
const
char
*
id
,
ValueType
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
DataType
<
ValueType
>::
DataType
(
id
,
defaultValue
)
{
};
void
setMax
(
ValueType
val
)
{
this
->
_max
=
val
;
this
->
_maxDefined
=
true
;
}
void
setMin
(
ValueType
val
)
{
this
->
_min
=
val
;
this
->
_minDefined
=
true
;
}
virtual
void
applyDefaultValue
()
override
{
this
->
_value
=
this
->
_defaultValue
;
}
protected:
virtual
bool
update
(
String
newValue
,
bool
validateOnly
)
override
{
errno
=
0
;
ValueType
val
=
fromString
(
newValue
);
if
((
errno
==
ERANGE
)
||
(
this
->
_minDefined
&&
(
val
<
this
->
_min
))
||
(
this
->
_maxDefined
&&
(
val
>
this
->
_max
)))
{
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
print
(
this
->
getId
());
Serial
.
print
(
" value not accepted: "
);
Serial
.
println
(
val
);
#endif
return
false
;
}
if
(
!
validateOnly
)
{
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
print
(
this
->
getId
());
Serial
.
print
(
": "
);
Serial
.
println
((
ValueType
)
val
);
#endif
this
->
_value
=
(
ValueType
)
val
;
}
return
true
;
}
void
storeValue
(
std
::
function
<
void
(
SerializationData
*
serializationData
)
>
doStore
)
override
{
SerializationData
serializationData
;
serializationData
.
length
=
this
->
getStorageSize
();
serializationData
.
data
=
reinterpret_cast
<
byte
*>
(
&
this
->
_value
);
doStore
(
&
serializationData
);
}
void
loadValue
(
std
::
function
<
void
(
SerializationData
*
serializationData
)
>
doLoad
)
override
{
byte
buf
[
this
->
getStorageSize
()];
SerializationData
serializationData
;
serializationData
.
length
=
this
->
getStorageSize
();
serializationData
.
data
=
buf
;
doLoad
(
&
serializationData
);
ValueType
*
valuePointer
=
reinterpret_cast
<
ValueType
*>
(
buf
);
this
->
_value
=
*
valuePointer
;
}
virtual
ValueType
fromString
(
String
stringValue
)
=
0
;
ValueType
getMax
()
{
return
this
->
_max
;
}
ValueType
getMin
()
{
return
this
->
_min
;
}
ValueType
isMaxDefined
()
{
return
this
->
_maxDefined
;
}
ValueType
isMinDefined
()
{
return
this
->
_minDefined
;
}
private:
ValueType
_min
;
ValueType
_max
;
bool
_minDefined
=
false
;
bool
_maxDefined
=
false
;
};
///////////////////////////////////////////////////////////////////////////
template
<
typename
ValueType
,
int
base
=
10
>
class
SignedIntDataType
:
public
PrimitiveDataType
<
ValueType
>
{
public:
SignedIntDataType
(
const
char
*
id
,
ValueType
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
PrimitiveDataType
<
ValueType
>::
PrimitiveDataType
(
id
,
defaultValue
)
{
};
protected:
virtual
ValueType
fromString
(
String
stringValue
)
{
return
(
ValueType
)
strtoimax
(
stringValue
.
c_str
(),
nullptr
,
base
);
}
};
template
<
typename
ValueType
,
int
base
=
10
>
class
UnsignedIntDataType
:
public
PrimitiveDataType
<
ValueType
>
{
public:
UnsignedIntDataType
(
const
char
*
id
,
ValueType
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
PrimitiveDataType
<
ValueType
>::
PrimitiveDataType
(
id
,
defaultValue
)
{
};
protected:
virtual
ValueType
fromString
(
String
stringValue
)
{
return
(
ValueType
)
strtoumax
(
stringValue
.
c_str
(),
nullptr
,
base
);
}
};
class
BoolDataType
:
public
PrimitiveDataType
<
bool
>
{
public:
BoolDataType
(
const
char
*
id
,
bool
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
PrimitiveDataType
<
bool
>::
PrimitiveDataType
(
id
,
defaultValue
)
{
};
protected:
virtual
bool
fromString
(
String
stringValue
)
{
return
stringValue
.
c_str
()[
0
]
==
1
;
}
};
class
FloatDataType
:
public
PrimitiveDataType
<
float
>
{
public:
FloatDataType
(
const
char
*
id
,
float
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
PrimitiveDataType
<
float
>::
PrimitiveDataType
(
id
,
defaultValue
)
{
};
protected:
virtual
float
fromString
(
String
stringValue
)
{
return
atof
(
stringValue
.
c_str
());
}
};
class
DoubleDataType
:
public
PrimitiveDataType
<
double
>
{
public:
DoubleDataType
(
const
char
*
id
,
double
defaultValue
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
PrimitiveDataType
<
double
>::
PrimitiveDataType
(
id
,
defaultValue
)
{
};
protected:
virtual
double
fromString
(
String
stringValue
)
{
return
strtod
(
stringValue
.
c_str
(),
nullptr
);
}
};
/////////////////////////////////////////////////////////////////////////
class
IpDataType
:
public
DataType
<
IPAddress
>
{
using
DataType
<
IPAddress
>::
DataType
;
protected:
virtual
bool
update
(
String
newValue
,
bool
validateOnly
)
override
{
if
(
validateOnly
)
{
IPAddress
ip
;
return
ip
.
fromString
(
newValue
);
}
else
{
return
this
->
_value
.
fromString
(
newValue
);
}
}
virtual
String
toString
()
override
{
return
this
->
_value
.
toString
();
}
};
///////////////////////////////////////////////////////////////////////////
/**
* Input parameter is the part of the parameter that is responsible
* for the appearance of the parameter in HTML.
*/
class
InputParameter
:
virtual
public
ConfigItemBridge
{
public:
InputParameter
(
const
char
*
id
,
const
char
*
label
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
label
(
label
)
{
}
virtual
void
renderHtml
(
bool
dataArrived
,
WebRequestWrapper
*
webRequestWrapper
)
override
{
String
content
=
this
->
renderHtml
(
dataArrived
,
webRequestWrapper
->
hasArg
(
this
->
getId
()),
webRequestWrapper
->
arg
(
this
->
getId
()));
webRequestWrapper
->
sendContent
(
content
);
}
const
char
*
label
;
/**
* This variable is meant to store a value that is displayed in an empty
* (not filled) field.
*/
const
char
*
placeholder
=
nullptr
;
virtual
void
setPlaceholder
(
const
char
*
placeholder
)
{
this
->
placeholder
=
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
=
nullptr
;
/**
* Used when rendering the input field. Is is overridden by different
* implementations.
*/
virtual
String
getCustomHtml
()
{
return
String
(
customHtml
==
nullptr
?
""
:
customHtml
);
}
const
char
*
errorMessage
=
nullptr
;
protected:
void
clearErrorMessage
()
override
{
this
->
errorMessage
=
nullptr
;
}
virtual
String
renderHtml
(
bool
dataArrived
,
bool
hasValueFromPost
,
String
valueFromPost
)
{
String
pitem
=
String
(
this
->
getHtmlTemplate
());
pitem
.
replace
(
"{b}"
,
this
->
label
);
pitem
.
replace
(
"{t}"
,
this
->
getInputType
());
pitem
.
replace
(
"{i}"
,
this
->
getId
());
pitem
.
replace
(
"{p}"
,
this
->
placeholder
==
nullptr
?
""
:
this
->
placeholder
);
int
length
=
this
->
getInputLength
();
if
(
length
>
0
)
{
char
parLength
[
11
];
snprintf
(
parLength
,
11
,
"%d"
,
length
);
String
maxLength
=
String
(
"maxlength="
)
+
parLength
;
pitem
.
replace
(
"{l}"
,
maxLength
);
}
else
{
pitem
.
replace
(
"{l}"
,
""
);
}
if
(
hasValueFromPost
)
{
// -- Value from previous submit
pitem
.
replace
(
"{v}"
,
valueFromPost
);
}
else
{
// -- Value from config
pitem
.
replace
(
"{v}"
,
this
->
toString
());
}
pitem
.
replace
(
"{c}"
,
this
->
getCustomHtml
());
pitem
.
replace
(
"{s}"
,
this
->
errorMessage
==
nullptr
?
""
:
"de"
);
// Div style class.
pitem
.
replace
(
"{e}"
,
this
->
errorMessage
==
nullptr
?
""
:
this
->
errorMessage
);
return
pitem
;
}
/**
* 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
);
};
virtual
const
char
*
getInputType
()
=
0
;
};
template
<
size_t
len
>
class
TextTParameter
:
public
CharArrayDataType
<
len
>
,
public
InputParameter
{
public:
using
CharArrayDataType
<
len
>::
CharArrayDataType
;
TextTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
CharArrayDataType
<
len
>::
CharArrayDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"text"
;
}
};
class
CheckboxTParameter
:
public
BoolDataType
,
public
InputParameter
{
public:
CheckboxTParameter
(
const
char
*
id
,
const
char
*
label
,
const
bool
defaultValue
)
:
ConfigItemBridge
(
id
),
BoolDataType
::
BoolDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
bool
isChecked
()
{
return
this
->
value
();
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"checkbox"
;
}
virtual
void
update
(
WebRequestWrapper
*
webRequestWrapper
)
override
{
bool
selected
=
false
;
if
(
webRequestWrapper
->
hasArg
(
this
->
getId
()))
{
String
valueFromPost
=
webRequestWrapper
->
arg
(
this
->
getId
());
selected
=
valueFromPost
.
equals
(
"selected"
);
}
// this->update(String(selected ? "1" : "0"));
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
print
(
this
->
getId
());
Serial
.
print
(
": "
);
Serial
.
println
(
selected
?
"selected"
:
"not selected"
);
#endif
this
->
_value
=
selected
;
}
virtual
String
renderHtml
(
bool
dataArrived
,
bool
hasValueFromPost
,
String
valueFromPost
)
override
{
bool
checkSelected
=
false
;
if
(
dataArrived
)
{
if
(
hasValueFromPost
&&
valueFromPost
.
equals
(
"selected"
))
{
checkSelected
=
true
;
}
}
else
{
if
(
this
->
isChecked
())
{
checkSelected
=
true
;
}
}
if
(
checkSelected
)
{
this
->
customHtml
=
CheckboxTParameter
::
_checkedStr
;
}
else
{
this
->
customHtml
=
nullptr
;
}
return
InputParameter
::
renderHtml
(
dataArrived
,
true
,
String
(
"selected"
));
}
private:
const
char
*
_checkedStr
=
"checked='checked'"
;
};
template
<
size_t
len
>
class
PasswordTParameter
:
public
CharArrayDataType
<
len
>
,
public
InputParameter
{
public:
using
CharArrayDataType
<
len
>::
CharArrayDataType
;
PasswordTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
CharArrayDataType
<
len
>::
CharArrayDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
this
->
customHtml
=
_customHtmlPwd
;
}
void
debugTo
(
Stream
*
out
)
{
out
->
print
(
"'"
);
out
->
print
(
this
->
getId
());
out
->
print
(
"' with value: "
);
#ifdef IOTWEBCONF_DEBUG_PWD_TO_SERIAL
out
->
print
(
"'"
);
out
->
print
(
this
->
_value
);
out
->
println
(
"'"
);
#else
out
->
println
(
F
(
"<hidden>"
));
#endif
}
virtual
bool
update
(
String
newValue
,
bool
validateOnly
)
override
{
if
(
newValue
.
length
()
+
1
>
len
)
{
return
false
;
}
if
(
validateOnly
)
{
return
true
;
}
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
print
(
this
->
getId
());
Serial
.
print
(
": "
);
#endif
if
(
newValue
.
length
()
>
0
)
{
// -- Value was set.
strncpy
(
this
->
_value
,
newValue
.
c_str
(),
len
);
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
# ifdef IOTWEBCONF_DEBUG_PWD_TO_SERIAL
Serial
.
println
(
this
->
_value
);
# else
Serial
.
println
(
"<updated>"
);
# endif
#endif
}
else
{
#ifdef IOTWEBCONF_DEBUG_TO_SERIAL
Serial
.
println
(
"<was not changed>"
);
#endif
}
return
true
;
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"password"
;
}
virtual
String
renderHtml
(
bool
dataArrived
,
bool
hasValueFromPost
,
String
valueFromPost
)
override
{
return
InputParameter
::
renderHtml
(
dataArrived
,
true
,
String
(
""
));
}
private:
const
char
*
_customHtmlPwd
=
"ondblclick=
\"
pw(this.id)
\"
"
;
};
/**
* All non-complex type input parameters should be inherited from this
* base class.
*/
template
<
typename
ValueType
>
class
PrimitiveInputParameter
:
public
InputParameter
{
public:
PrimitiveInputParameter
(
const
char
*
id
,
const
char
*
label
)
:
ConfigItemBridge
::
ConfigItemBridge
(
id
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
virtual
String
getCustomHtml
()
override
{
String
modifiers
=
String
(
this
->
customHtml
);
if
(
this
->
isMinDefined
())
{
modifiers
+=
" min='"
;
modifiers
+=
this
->
getMin
();
modifiers
+=
"'"
;
}
if
(
this
->
isMaxDefined
())
{
modifiers
+=
" max='"
;
modifiers
+=
this
->
getMax
();
modifiers
+=
"'"
;
}
if
(
this
->
step
!=
0
)
{
modifiers
+=
" step='"
;
modifiers
+=
this
->
step
;
modifiers
+=
"'"
;
}
return
modifiers
;
}
ValueType
step
=
0
;
void
setStep
(
ValueType
step
)
{
this
->
step
=
step
;
}
virtual
ValueType
getMin
()
=
0
;
virtual
ValueType
getMax
()
=
0
;
virtual
bool
isMinDefined
()
=
0
;
virtual
bool
isMaxDefined
()
=
0
;
};
template
<
typename
ValueType
,
int
base
=
10
>
class
IntTParameter
:
public
virtual
SignedIntDataType
<
ValueType
,
base
>
,
public
PrimitiveInputParameter
<
ValueType
>
{
public:
IntTParameter
(
const
char
*
id
,
const
char
*
label
,
ValueType
defaultValue
)
:
ConfigItemBridge
(
id
),
SignedIntDataType
<
ValueType
,
base
>::
SignedIntDataType
(
id
,
defaultValue
),
PrimitiveInputParameter
<
ValueType
>::
PrimitiveInputParameter
(
id
,
label
)
{
}
// TODO: somehow organize these methods into common parent.
virtual
ValueType
getMin
()
override
{
return
PrimitiveDataType
<
ValueType
>::
getMin
();
}
virtual
ValueType
getMax
()
override
{
return
PrimitiveDataType
<
ValueType
>::
getMax
();
}
virtual
bool
isMinDefined
()
override
{
return
PrimitiveDataType
<
ValueType
>::
isMinDefined
();
}
virtual
bool
isMaxDefined
()
override
{
return
PrimitiveDataType
<
ValueType
>::
isMaxDefined
();
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"number"
;
}
};
template
<
typename
ValueType
,
int
base
=
10
>
class
UIntTParameter
:
public
virtual
UnsignedIntDataType
<
ValueType
,
base
>
,
public
PrimitiveInputParameter
<
ValueType
>
{
public:
UIntTParameter
(
const
char
*
id
,
const
char
*
label
,
ValueType
defaultValue
)
:
ConfigItemBridge
(
id
),
UnsignedIntDataType
<
ValueType
,
base
>::
UnsignedIntDataType
(
id
,
defaultValue
),
PrimitiveInputParameter
<
ValueType
>::
PrimitiveInputParameter
(
id
,
label
)
{
}
// TODO: somehow organize these methods into common parent.
virtual
ValueType
getMin
()
override
{
return
PrimitiveDataType
<
ValueType
>::
getMin
();
}
virtual
ValueType
getMax
()
override
{
return
PrimitiveDataType
<
ValueType
>::
getMax
();
}
virtual
bool
isMinDefined
()
override
{
return
PrimitiveDataType
<
ValueType
>::
isMinDefined
();
}
virtual
bool
isMaxDefined
()
override
{
return
PrimitiveDataType
<
ValueType
>::
isMaxDefined
();
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"number"
;
}
};
class
FloatTParameter
:
public
FloatDataType
,
public
PrimitiveInputParameter
<
float
>
{
public:
FloatTParameter
(
const
char
*
id
,
const
char
*
label
,
float
defaultValue
)
:
ConfigItemBridge
(
id
),
FloatDataType
::
FloatDataType
(
id
,
defaultValue
),
PrimitiveInputParameter
<
float
>::
PrimitiveInputParameter
(
id
,
label
)
{
}
virtual
float
getMin
()
override
{
return
PrimitiveDataType
<
float
>::
getMin
();
}
virtual
float
getMax
()
override
{
return
PrimitiveDataType
<
float
>::
getMax
();
}
virtual
bool
isMinDefined
()
override
{
return
PrimitiveDataType
<
float
>::
isMinDefined
();
}
virtual
bool
isMaxDefined
()
override
{
return
PrimitiveDataType
<
float
>::
isMaxDefined
();
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"number"
;
}
};
/**
* Options parameter is a structure, that handles multiple values when redering
* the HTML representation.
*/
template
<
size_t
len
>
class
OptionsTParameter
:
public
TextTParameter
<
len
>
{
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!)
*/
OptionsTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
,
const
char
*
optionValues
,
const
char
*
optionNames
,
size_t
optionCount
,
size_t
nameLength
)
:
ConfigItemBridge
(
id
),
TextTParameter
<
len
>
(
id
,
label
,
defaultValue
)
{
this
->
_optionValues
=
optionValues
;
this
->
_optionNames
=
optionNames
;
this
->
_optionCount
=
optionCount
;
this
->
_nameLength
=
nameLength
;
}
// TODO: make these protected
void
setOptionValues
(
const
char
*
optionValues
)
{
this
->
_optionValues
=
optionValues
;
}
void
setOptionNames
(
const
char
*
optionNames
)
{
this
->
_optionNames
=
optionNames
;
}
void
setOptionCount
(
size_t
optionCount
)
{
this
->
_optionCount
=
optionCount
;
}
void
setNameLength
(
size_t
nameLength
)
{
this
->
_nameLength
=
nameLength
;
}
protected:
OptionsTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
TextTParameter
<
len
>
(
id
,
label
,
defaultValue
)
{
}
const
char
*
_optionValues
;
const
char
*
_optionNames
;
size_t
_optionCount
;
size_t
_nameLength
;
};
///////////////////////////////////////////////////////////////////////////////
/**
* Select parameter is an option parameter, that rendered as HTML SELECT.
* Basically it is a dropdown combobox.
*/
template
<
size_t
len
>
class
SelectTParameter
:
public
OptionsTParameter
<
len
>
{
public:
/**
* Create a select parameter for the config portal.
*
* (See OptionsParameter for arguments!)
*/
SelectTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
,
const
char
*
optionValues
,
const
char
*
optionNames
,
size_t
optionCount
,
size_t
nameLength
)
:
ConfigItemBridge
(
id
),
OptionsTParameter
<
len
>
(
id
,
label
,
defaultValue
,
optionValues
,
optionNames
,
optionCount
,
nameLength
)
{
}
// TODO: make this protected
SelectTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
OptionsTParameter
<
len
>
(
id
,
label
,
defaultValue
)
{
}
protected:
// Overrides
virtual
String
renderHtml
(
bool
dataArrived
,
bool
hasValueFromPost
,
String
valueFromPost
)
override
{
String
options
=
""
;
for
(
size_t
i
=
0
;
i
<
this
->
_optionCount
;
i
++
)
{
const
char
*
optionValue
=
(
this
->
_optionValues
+
(
i
*
len
)
);
const
char
*
optionName
=
(
this
->
_optionNames
+
(
i
*
this
->
_nameLength
)
);
String
oitem
=
FPSTR
(
IOTWEBCONF_HTML_FORM_OPTION
);
oitem
.
replace
(
"{v}"
,
optionValue
);
// if (sizeof(this->_optionNames) > i)
{
oitem
.
replace
(
"{n}"
,
optionName
);
}
// else
// {
// oitem.replace("{n}", "?");
// }
if
((
hasValueFromPost
&&
(
valueFromPost
==
optionValue
))
||
(
strncmp
(
this
->
value
(),
optionValue
,
len
)
==
0
))
{
// -- Value from previous submit
oitem
.
replace
(
"{s}"
,
" selected"
);
}
else
{
// -- Value from config
oitem
.
replace
(
"{s}"
,
""
);
}
options
+=
oitem
;
}
String
pitem
=
FPSTR
(
IOTWEBCONF_HTML_FORM_SELECT_PARAM
);
pitem
.
replace
(
"{b}"
,
this
->
label
);
pitem
.
replace
(
"{i}"
,
this
->
getId
());
pitem
.
replace
(
"{c}"
,
this
->
customHtml
==
nullptr
?
""
:
this
->
customHtml
);
pitem
.
replace
(
"{s}"
,
this
->
errorMessage
==
nullptr
?
""
:
"de"
);
// Div style class.
pitem
.
replace
(
"{e}"
,
this
->
errorMessage
==
nullptr
?
""
:
this
->
errorMessage
);
pitem
.
replace
(
"{o}"
,
options
);
return
pitem
;
}
private:
};
///////////////////////////////////////////////////////////////////////////////
/**
* Color chooser.
*/
class
ColorTParameter
:
public
CharArrayDataType
<
8
>
,
public
InputParameter
{
public:
using
CharArrayDataType
<
8
>::
CharArrayDataType
;
ColorTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
CharArrayDataType
<
8
>::
CharArrayDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"color"
;
}
};
///////////////////////////////////////////////////////////////////////////////
/**
* Date chooser.
*/
class
DateTParameter
:
public
CharArrayDataType
<
11
>
,
public
InputParameter
{
public:
using
CharArrayDataType
<
11
>::
CharArrayDataType
;
DateTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
CharArrayDataType
<
11
>::
CharArrayDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"date"
;
}
};
///////////////////////////////////////////////////////////////////////////////
/**
* Time chooser.
*/
class
TimeTParameter
:
public
CharArrayDataType
<
6
>
,
public
InputParameter
{
public:
using
CharArrayDataType
<
6
>::
CharArrayDataType
;
TimeTParameter
(
const
char
*
id
,
const
char
*
label
,
const
char
*
defaultValue
)
:
ConfigItemBridge
(
id
),
CharArrayDataType
<
6
>::
CharArrayDataType
(
id
,
defaultValue
),
InputParameter
::
InputParameter
(
id
,
label
)
{
}
protected:
virtual
const
char
*
getInputType
()
override
{
return
"time"
;
}
};
}
// end namespace
#include <IotWebConfTParameterBuilder.h>
#endif
ampel-firmware/src/lib/IotWebConf/src/IotWebConfTParameterBuilder.h
0 → 100644
View file @
8c4fc341
/**
* IotWebConfTParameter.h -- 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>
* rovo89
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#ifndef IotWebConfTParameterBuilder_h
#define IotWebConfTParameterBuilder_h
#include <IotWebConfTParameter.h>
namespace
iotwebconf
{
template
<
typename
ParamType
>
class
Builder
;
template
<
typename
ParamType
>
class
AbstractBuilder
{
public:
AbstractBuilder
(
const
char
*
id
)
:
_id
(
id
)
{
};
virtual
ParamType
build
()
const
{
ParamType
instance
=
std
::
move
(
ParamType
(
this
->
_id
,
this
->
_label
,
this
->
_defaultValue
));
this
->
apply
(
&
instance
);
return
instance
;
}
Builder
<
ParamType
>&
label
(
const
char
*
label
)
{
this
->
_label
=
label
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
Builder
<
ParamType
>&
defaultValue
(
typename
ParamType
::
DefaultValueType
defaultValue
)
{
this
->
_defaultValue
=
defaultValue
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
protected:
virtual
ParamType
*
apply
(
ParamType
*
instance
)
const
{
return
instance
;
}
const
char
*
_label
;
const
char
*
_id
;
typename
ParamType
::
DefaultValueType
_defaultValue
;
};
template
<
typename
ParamType
>
class
Builder
:
public
AbstractBuilder
<
ParamType
>
{
public:
Builder
(
const
char
*
id
)
:
AbstractBuilder
<
ParamType
>
(
id
)
{
};
};
///////////////////////////////////////////////////////////////////////////
template
<
typename
ValueType
,
typename
ParamType
>
class
PrimitiveBuilder
:
public
AbstractBuilder
<
ParamType
>
{
public:
PrimitiveBuilder
<
ValueType
,
ParamType
>
(
const
char
*
id
)
:
AbstractBuilder
<
ParamType
>
(
id
)
{
};
Builder
<
ParamType
>&
min
(
ValueType
min
)
{
this
->
_minDefined
=
true
;
this
->
_min
=
min
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
Builder
<
ParamType
>&
max
(
ValueType
max
)
{
this
->
_maxDefined
=
true
;
this
->
_max
=
max
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
Builder
<
ParamType
>&
step
(
ValueType
step
)
{
this
->
_step
=
step
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
Builder
<
ParamType
>&
placeholder
(
const
char
*
placeholder
)
{
this
->
_placeholder
=
placeholder
;
return
static_cast
<
Builder
<
ParamType
>&>
(
*
this
);
}
protected:
virtual
ParamType
*
apply
(
ParamType
*
instance
)
const
override
{
if
(
this
->
_minDefined
)
{
instance
->
setMin
(
this
->
_min
);
}
if
(
this
->
_maxDefined
)
{
instance
->
setMax
(
this
->
_max
);
}
instance
->
setStep
(
this
->
_step
);
instance
->
setPlaceholder
(
this
->
_placeholder
);
return
instance
;
}
bool
_minDefined
=
false
;
bool
_maxDefined
=
false
;
ValueType
_min
;
ValueType
_max
;
ValueType
_step
=
0
;
const
char
*
_placeholder
=
nullptr
;
};
template
<
typename
ValueType
,
int
base
>
class
Builder
<
IntTParameter
<
ValueType
,
base
>>
:
public
PrimitiveBuilder
<
ValueType
,
IntTParameter
<
ValueType
,
base
>>
{
public:
Builder
<
IntTParameter
<
ValueType
,
base
>>
(
const
char
*
id
)
:
PrimitiveBuilder
<
ValueType
,
IntTParameter
<
ValueType
,
base
>>
(
id
)
{
};
};
template
<
typename
ValueType
,
int
base
>
class
Builder
<
UIntTParameter
<
ValueType
,
base
>>
:
public
PrimitiveBuilder
<
ValueType
,
UIntTParameter
<
ValueType
,
base
>>
{
public:
Builder
<
UIntTParameter
<
ValueType
,
base
>>
(
const
char
*
id
)
:
PrimitiveBuilder
<
ValueType
,
UIntTParameter
<
ValueType
,
base
>>
(
id
)
{
};
};
template
<
>
class
Builder
<
FloatTParameter
>
:
public
PrimitiveBuilder
<
float
,
FloatTParameter
>
{
public:
Builder
<
FloatTParameter
>
(
const
char
*
id
)
:
PrimitiveBuilder
<
float
,
FloatTParameter
>
(
id
)
{
};
};
template
<
size_t
len
>
class
Builder
<
SelectTParameter
<
len
>>
:
public
AbstractBuilder
<
SelectTParameter
<
len
>>
{
public:
Builder
<
SelectTParameter
<
len
>>
(
const
char
*
id
)
:
AbstractBuilder
<
SelectTParameter
<
len
>>
(
id
)
{
};
virtual
SelectTParameter
<
len
>
build
()
const
override
{
return
SelectTParameter
<
len
>
(
this
->
_id
,
this
->
_label
,
this
->
_defaultValue
,
this
->
_optionValues
,
this
->
_optionNames
,
this
->
_optionCount
,
this
->
_nameLength
);
}
Builder
<
SelectTParameter
<
len
>>&
optionValues
(
const
char
*
optionValues
)
{
this
->
_optionValues
=
optionValues
;
return
*
this
;
}
Builder
<
SelectTParameter
<
len
>>&
optionNames
(
const
char
*
optionNames
)
{
this
->
_optionNames
=
optionNames
;
return
*
this
;
}
Builder
<
SelectTParameter
<
len
>>&
optionCount
(
size_t
optionCount
)
{
this
->
_optionCount
=
optionCount
;
return
*
this
;
}
Builder
<
SelectTParameter
<
len
>>&
nameLength
(
size_t
nameLength
)
{
this
->
_nameLength
=
nameLength
;
return
*
this
;
}
protected:
virtual
SelectTParameter
<
len
>*
apply
(
SelectTParameter
<
len
>*
instance
)
const
override
{
instance
->
setOptionValues
(
this
->
_optionValues
);
instance
->
setOptionNames
(
this
->
_optionNames
);
instance
->
setOptionCount
(
this
->
_optionCount
);
instance
->
setNameLength
(
this
->
_nameLength
);
return
instance
;
}
private:
const
char
*
_optionValues
;
const
char
*
_optionNames
;
size_t
_optionCount
;
size_t
_nameLength
;
};
}
// End namespace
#endif
ampel-firmware/src/lib/IotWebConf/src/IotWebConfUsing.h
0 → 100644
View file @
8c4fc341
/**
* IotWebConfUsing.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 IotWebConfUsing_h
#define IotWebConfUsing_h
// This "using" lines are just aliases, and should avoided.
using
IotWebConfParameterGroup
=
iotwebconf
::
ParameterGroup
;
using
IotWebConfTextParameter
=
iotwebconf
::
TextParameter
;
using
IotWebConfPasswordParameter
=
iotwebconf
::
PasswordParameter
;
using
IotWebConfNumberParameter
=
iotwebconf
::
NumberParameter
;
using
IotWebConfCheckboxParameter
=
iotwebconf
::
CheckboxParameter
;
using
IotWebConfSelectParameter
=
iotwebconf
::
SelectParameter
;
#endif
\ No newline at end of file
ampel-firmware/src/lib/IotWebConf/src/IotWebConfWebServerWrapper.h
0 → 100644
View file @
8c4fc341
/**
* IotWebConfWebServerWrapper.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 WebServerWrapper_h
#define WebServerWrapper_h
#include <Arduino.h>
#include <IPAddress.h>
namespace
iotwebconf
{
class
WebRequestWrapper
{
public:
virtual
const
String
hostHeader
()
const
;
virtual
IPAddress
localIP
();
virtual
uint16_t
localPort
();
virtual
const
String
uri
()
const
;
virtual
bool
authenticate
(
const
char
*
username
,
const
char
*
password
);
virtual
void
requestAuthentication
();
virtual
bool
hasArg
(
const
String
&
name
);
virtual
String
arg
(
const
String
name
);
virtual
void
sendHeader
(
const
String
&
name
,
const
String
&
value
,
bool
first
=
false
);
virtual
void
setContentLength
(
const
size_t
contentLength
);
virtual
void
send
(
int
code
,
const
char
*
content_type
=
nullptr
,
const
String
&
content
=
String
(
""
));
virtual
void
sendContent
(
const
String
&
content
);
virtual
void
stop
();
};
class
WebServerWrapper
{
public:
virtual
void
handleClient
();
virtual
void
begin
();
};
}
// end namespace
#endif
\ No newline at end of file
platformio.ini
View file @
8c4fc341
...
@@ -16,13 +16,6 @@ platform = espressif8266
...
@@ -16,13 +16,6 @@ platform = espressif8266
board
=
esp12e
board
=
esp12e
framework
=
arduino
framework
=
arduino
monitor_speed
=
115200
monitor_speed
=
115200
lib_deps
=
EEPROM
DNSServer
prampec/IotWebConf@^3.2.0
build_flags
=
; Disabling debug helps a bit with available memory.
-D
IOTWEBCONF_DEBUG_DISABLED
[env:esp32]
[env:esp32]
platform
=
espressif32
platform
=
espressif32
...
@@ -31,9 +24,6 @@ framework = arduino
...
@@ -31,9 +24,6 @@ framework = arduino
monitor_speed
=
115200
monitor_speed
=
115200
lib_deps
=
lib_deps
=
MCCI
LoRaWAN
LMIC
library
MCCI
LoRaWAN
LMIC
library
EEPROM
DNSServer
prampec/IotWebConf@^3.2.0
build_flags
=
build_flags
=
-D
ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
-D
ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
-D
CFG_eu868
=
1
-D
CFG_eu868
=
1
...
...
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment