esp8266_web_settings beta
|
The generic device interface. More...
#include <grmcdorman/device/Device.h>
Classes | |
class | Definition |
A sensor definition to be published to MQTT. More... | |
Public Types | |
typedef std::vector< const Definition * > | definition_list_t |
The type containing a list of Definition objects. | |
Public Member Functions | |
Device (const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier) | |
Construct a new Device object. More... | |
virtual | ~Device () |
Destroy the Device object. | |
const __FlashStringHelper * | name () const |
Get the device name; used for UI names and IDs. More... | |
virtual const __FlashStringHelper * | identifier () const |
Get the device identifier. More... | |
virtual void | set_defaults () |
Set defaults, if necessary. More... | |
virtual void | set_devices (const std::vector< Device * > &list) |
For devices that support it, add devices to manage. More... | |
virtual void | setup ()=0 |
Setup the device. More... | |
virtual void | loop ()=0 |
Main loop. More... | |
virtual bool | publish (DynamicJsonDocument &json) const |
Publish the value and attributes. More... | |
virtual DynamicJsonDocument | as_json () const |
Get the values, as a JSON document. More... | |
const definition_list_t & | get_definitions () const |
Get the definitions list. More... | |
const ::grmcdorman::SettingInterface::settings_list_t & | get_settings () const |
Get the settings list. More... | |
bool | is_enabled () const |
Get whether this device is enabled. More... | |
void | set_enabled (bool state) |
Set whether this device is enabled. More... | |
void | set (const String &setting, const String &value) |
If possible, set a setting's value. More... | |
String | get (const String &setting) const |
Get a setting value, as a string. More... | |
virtual String | get_status () const |
Get a status report. More... | |
virtual bool | get_is_published () const |
Get whether the device readings have been published. More... | |
void | set_is_published () |
Set the device as having published readings. More... | |
void | clear_is_published () |
Set the device as not having published readings. | |
Static Public Member Functions | |
static void | set_system_identifiers (const __FlashStringHelper *firmware_name_value, const String &system_identifier_value=String()) |
Set the system identifier values. More... | |
static const __FlashStringHelper * | get_firmware_name () |
Get the firmware prefix. More... | |
static const String & | get_system_identifier () |
Get the system identifier. More... | |
Protected Member Functions | |
void | initialize (definition_list_t &&definition_list, ::grmcdorman::SettingInterface::settings_list_t &&setting_list) |
Initialize the definition and setting lists. More... | |
Static Protected Member Functions | |
static int | index_to_dataline (int index) |
Convert a data line index to a ESP data line. More... | |
static int | dataline_to_index (int dataLine) |
Convert a data line to an index. More... | |
Protected Attributes | |
ToggleSetting | enabled |
Whether this device is enabled. | |
Static Protected Attributes | |
static const int | settingsMap [6] |
The set of data lines usable for communication. More... | |
The generic device interface.
A Device, generally, is something that needs to perform non-blocking operations in the main system loop, and can optionally publish to MQTT.
The device contains zero or more Descriptions, which are the individual sensors published to MQTT. It also contains zero or more Settings from the esp8266_web_settings library; these provide descriptions, configuration inputs, and status outputs.
grmcdorman::device::Device::Device | ( | const __FlashStringHelper * | device_name, |
const __FlashStringHelper * | device_identifier | ||
) |
|
inlinevirtual |
Get the values, as a JSON document.
The structure is identical to the document created inside publish
.
Reimplemented in grmcdorman::device::AbstractAnalog, grmcdorman::device::AbstractTemperaturePressureSensor, grmcdorman::device::InfoDisplay, grmcdorman::device::MqttPublisher, grmcdorman::device::SystemDetailsDisplay, grmcdorman::device::ThermistorSensor, grmcdorman::device::VindriktningAirQuality, grmcdorman::device::WifiDisplay, and grmcdorman::device::WifiSetup.
|
staticprotected |
Convert a data line to an index.
This will convert a data line, such as D1, into an index into the settings map.
dataLine | The data line. |
String grmcdorman::device::Device::get | ( | const String & | setting | ) | const |
Get a setting value, as a string.
The setting is located by name, and its value is converted to a string if necessary.
Internally, this search the list of settings for the named setting, and then returns the value of Setting::as_string
.
If the setting is not found, an empty string is returned.
setting | Name of setting. |
|
inline |
Get the definitions list.
This returns a static definition list of zero or more sensor definitions.
|
inlinestatic |
Get the firmware prefix.
|
inlinevirtual |
Get whether the device readings have been published.
The device is considered to be published if there are no readings since the last publish, or since system start.
Reimplemented in grmcdorman::device::WifiSetup.
|
inline |
Get the settings list.
This returns a list of zero or more individual settings for this device.
|
inlinevirtual |
Get a status report.
This is typically also reported in an info setting. By default this returns an empty string, i.e. no report.
Reimplemented in grmcdorman::device::BasicAnalog, grmcdorman::device::DhtSensor, grmcdorman::device::InfoDisplay, grmcdorman::device::MqttPublisher, grmcdorman::device::Sht31Sensor, grmcdorman::device::ThermistorSensor, and grmcdorman::device::VindriktningAirQuality.
|
inlinestatic |
Get the system identifier.
set_system_identifiers
is called.
|
inlinevirtual |
Get the device identifier.
This should be a unique identifier to be used in internal contexts, such as settings files.
Reimplemented in grmcdorman::device::SystemDetailsDisplay.
|
inlinestaticprotected |
Convert a data line index to a ESP data line.
The index is simply an offset into the settingsMap array, which contains usable data lines from the D0 ... D8 set defined above.
index | Index into the settings map. |
|
inlineprotected |
|
inline |
Get whether this device is enabled.
|
pure virtual |
Main loop.
This is called in the main application loop. This should avoid blocking as doing so will prevent the other devices from running.
An exception is a "device" which requires user interaction, e.g. WiFi association, before the rest of the system can run.
Implemented in grmcdorman::device::AbstractAnalog, grmcdorman::device::DhtSensor, grmcdorman::device::InfoDisplay, grmcdorman::device::MqttPublisher, grmcdorman::device::Sht31Sensor, grmcdorman::device::SystemDetailsDisplay, grmcdorman::device::VindriktningAirQuality, grmcdorman::device::WifiDisplay, and grmcdorman::device::WifiSetup.
|
inline |
Get the device name; used for UI names and IDs.
|
inlinevirtual |
Publish the value and attributes.
The values and attributes for all sensors, i.e. all items listed in the Description instances, should added to the supplied JSON object. If the value is not available, or this device does not publish, return false
.
The value, or values, and attributes, if any, are to be added as a single node to the json
parameter. This includes the case for multiple sensors.
[in,out] | json | JSON to receive the device values and attributes. |
true
if a value to be published was added; false
otherwise. Reimplemented in grmcdorman::device::AbstractTemperaturePressureSensor, grmcdorman::device::AbstractAnalog, grmcdorman::device::VindriktningAirQuality, and grmcdorman::device::WifiSetup.
void grmcdorman::device::Device::set | ( | const String & | setting, |
const String & | value | ||
) |
If possible, set a setting's value.
The setting is located by name, and the value is converted from the string if necessary.
If the setting cannot be found, nothing is done.
Internally, this searches the list of settings for the named setting, and then uses Setting::set_from_string
.
setting | Name of setting. |
value | New value for setting. |
|
inlinevirtual |
Set defaults, if necessary.
This must be called before loading any settings. It will, if necessary, set any defaults that are dynamic; typically these will be values that need the firmware prefix or system identifier.
Reimplemented in grmcdorman::device::MqttPublisher, and grmcdorman::device::WifiSetup.
|
inlinevirtual |
For devices that support it, add devices to manage.
list | List of devices to manage. |
Reimplemented in grmcdorman::device::InfoDisplay, and grmcdorman::device::MqttPublisher.
|
inline |
Set whether this device is enabled.
state | The new enabled/disabled state. |
|
inline |
Set the device as having published readings.
This should be called when the device is published.
|
static |
Set the system identifier values.
This must be called before load
; it sets default values. The system identifier is used for default values for things such as host names and SoftAP SSIDs.
The firmware name is a unique identifier for the firmware, or application, using the device.
The system identifier is a unique identifier for the specific board running the application. This is typically constructed at least in part from the hex value of ESP.getChipId()
or equivalent. If empty, an identifier is constructed by combining the firmware prefix, a hypen, and the ESP chip ID in hex.
Because these values are system wide, this is a static method.
If this is never called, the firmware prefix is unspecified_firmware
and the system identifier is constructed as described above.
firmware_name_value | The unique firmware prefix. |
system_identifier_value | The system identifier. |
|
pure virtual |
Setup the device.
Call once when the system boots. This should perform device-specific setup, if the device is enabled, as well as preparing UI settings (notably info settings) for communication.
This must be called after initial values are loaded.
Implemented in grmcdorman::device::AbstractAnalog, grmcdorman::device::DhtSensor, grmcdorman::device::InfoDisplay, grmcdorman::device::MqttPublisher, grmcdorman::device::Sht31Sensor, grmcdorman::device::SystemDetailsDisplay, grmcdorman::device::VindriktningAirQuality, grmcdorman::device::WifiDisplay, and grmcdorman::device::WifiSetup.
|
staticprotected |
The set of data lines usable for communication.
This array contains the usable data lines - D1, D2, D3, D5, D6, and D7. It thus is used to translate between indexes in, for example, settings and the internal data line constants.