esp8266_web_settings beta
Device.h
1/*
2 * Copyright (c) 2021, 2022 G. R. McDorman
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#pragma once
24
25#include <WString.h>
26#include <vector>
27#include <ArduinoJson.h>
28
29#include "grmcdorman/Setting.h"
30
31namespace grmcdorman::device
32{
33 class SettingInterface;
34
45 class Device
46 {
47 public:
54 Device(const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier);
59 virtual ~Device() {}
60
61 static constexpr int D0 = 16;
62 static constexpr int D1 = 5;
63 static constexpr int D2 = 4;
64 static constexpr int D3 = 0;
65 static constexpr int D4 = 2;
66 static constexpr int D5 = 14;
67 static constexpr int D6 = 12;
68 static constexpr int D7 = 13;
69 static constexpr int D8 = 15;
70
79 {
80 public:
89 virtual const __FlashStringHelper *get_name_suffix() const = 0;
98 virtual const __FlashStringHelper *get_value_template() const = 0;
99
109 virtual const __FlashStringHelper *get_unique_id_suffix() const = 0;
118 virtual const __FlashStringHelper *get_unit_of_measurement() const = 0;
130 virtual const __FlashStringHelper *get_json_attributes_template() const = 0;
139 virtual const __FlashStringHelper *get_icon() const = 0;
140 };
141
147 const __FlashStringHelper *name() const
148 {
149 return device_name;
150 }
151
160 virtual const __FlashStringHelper *identifier() const
161 {
162 return device_identifier;
163 }
164
188 static void set_system_identifiers(const __FlashStringHelper *firmware_name_value, const String &system_identifier_value = String());
189
195 static const __FlashStringHelper *get_firmware_name()
196 {
197 return firmware_name;
198 }
199
205 static const String &get_system_identifier()
206 {
207 return system_identifier;
208 }
209
218 virtual void set_defaults()
219 {
220 }
221
227 virtual void set_devices(const std::vector<Device *> &list)
228 {
229 }
230
240 virtual void setup() = 0;
241
251 virtual void loop() = 0;
252
268 virtual bool publish(DynamicJsonDocument &json) const
269 {
270 return false;
271 }
272
273
281 virtual DynamicJsonDocument as_json() const
282 {
283 // Return a null document.
284 return DynamicJsonDocument(8);
285 }
286
291 typedef std::vector<const Definition *> definition_list_t;
292
302 {
303 return definitions;
304 }
305
314 const ::grmcdorman::SettingInterface::settings_list_t &get_settings() const
315 {
316 return settings;
317 }
318
325 bool is_enabled() const
326 {
327 return enabled.get();
328 }
329
335 void set_enabled(bool state)
336 {
337 enabled.set(state);
338 }
339
354 void set(const String &setting, const String &value);
355
370 String get(const String &setting) const;
371
380 virtual String get_status() const
381 {
382 return String();
383 }
384
393 virtual bool get_is_published() const
394 {
395 return is_published;
396 }
397
404 {
405 is_published = true;
406 }
407
413 {
414 is_published = false;
415 }
416 static const ExclusiveOptionSetting::names_list_t data_line_names;
417 protected:
426 void initialize(definition_list_t &&definition_list, ::grmcdorman::SettingInterface::settings_list_t &&setting_list)
427 {
428 definitions = std::move(definition_list);
429 settings = std::move(setting_list);
430 }
431
442 static int index_to_dataline(int index)
443 {
444 return settingsMap[index];
445 }
446
455 static int dataline_to_index(int dataLine);
456
464 static const int settingsMap[6];
465
466 ToggleSetting enabled;
467 private:
468 const __FlashStringHelper *device_name;
469 const __FlashStringHelper *device_identifier;
470 bool is_published = true;
471
472 static const __FlashStringHelper *firmware_name;
473 static String system_identifier;
474
475
476 definition_list_t definitions;
477 ::grmcdorman::SettingInterface::settings_list_t settings;
478 };
479}
A sensor definition to be published to MQTT.
Definition: Device.h:79
virtual const __FlashStringHelper * get_unit_of_measurement() const =0
Get the unit of measurement.
virtual const __FlashStringHelper * get_value_template() const =0
Get the value template.
virtual const __FlashStringHelper * get_json_attributes_template() const =0
Get the json attributes template.
virtual const __FlashStringHelper * get_name_suffix() const =0
Get the name suffix.
virtual const __FlashStringHelper * get_unique_id_suffix() const =0
Get the unique id suffix.
virtual const __FlashStringHelper * get_icon() const =0
Get the icon.
The generic device interface.
Definition: Device.h:46
virtual bool publish(DynamicJsonDocument &json) const
Publish the value and attributes.
Definition: Device.h:268
void clear_is_published()
Set the device as not having published readings.
Definition: Device.h:412
const definition_list_t & get_definitions() const
Get the definitions list.
Definition: Device.h:301
const __FlashStringHelper * name() const
Get the device name; used for UI names and IDs.
Definition: Device.h:147
String get(const String &setting) const
Get a setting value, as a string.
Definition: Device.cpp:82
virtual ~Device()
Destroy the Device object.
Definition: Device.h:59
static constexpr int D0
D0 is GPIO16, HIGH at boot, not suitable for most usages.
Definition: Device.h:61
Device(const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier)
Construct a new Device object.
Definition: Device.cpp:43
virtual void set_devices(const std::vector< Device * > &list)
For devices that support it, add devices to manage.
Definition: Device.h:227
virtual void setup()=0
Setup the device.
static constexpr int D1
D1 is GPIO5; often used as SCL.
Definition: Device.h:62
static const ExclusiveOptionSetting::names_list_t data_line_names
Names for each configurable data line; see settingsMap.
Definition: Device.h:416
virtual void loop()=0
Main loop.
virtual DynamicJsonDocument as_json() const
Get the values, as a JSON document.
Definition: Device.h:281
void set(const String &setting, const String &value)
If possible, set a setting's value.
Definition: Device.cpp:69
static constexpr int D2
D2 is GPIO4; often used as SDA.
Definition: Device.h:63
static void set_system_identifiers(const __FlashStringHelper *firmware_name_value, const String &system_identifier_value=String())
Set the system identifier values.
Definition: Device.cpp:50
static int dataline_to_index(int dataLine)
Convert a data line to an index.
Definition: Device.cpp:98
static constexpr int D7
D7 is GPIO13; SPI (MOSI)
Definition: Device.h:68
const ::grmcdorman::SettingInterface::settings_list_t & get_settings() const
Get the settings list.
Definition: Device.h:314
static const String & get_system_identifier()
Get the system identifier.
Definition: Device.h:205
void set_enabled(bool state)
Set whether this device is enabled.
Definition: Device.h:335
static constexpr int D5
D5 is GPIO14; SPI (SCLK)
Definition: Device.h:66
virtual bool get_is_published() const
Get whether the device readings have been published.
Definition: Device.h:393
virtual void set_defaults()
Set defaults, if necessary.
Definition: Device.h:218
virtual const __FlashStringHelper * identifier() const
Get the device identifier.
Definition: Device.h:160
bool is_enabled() const
Get whether this device is enabled.
Definition: Device.h:325
static constexpr int D3
D3 is GPIO0; pulled up; connected to FLASH button; not for input.
Definition: Device.h:64
static constexpr int D4
D4 is GPIO2; pulled up; HIGH at boot; on-board LED; not for input.
Definition: Device.h:65
static const int settingsMap[6]
The set of data lines usable for communication.
Definition: Device.h:464
ToggleSetting enabled
Whether this device is enabled.
Definition: Device.h:466
virtual String get_status() const
Get a status report.
Definition: Device.h:380
static constexpr int D8
D8 is GPIO15; pulled to GND; SPI (CS); not recommended.
Definition: Device.h:69
void set_is_published()
Set the device as having published readings.
Definition: Device.h:403
void initialize(definition_list_t &&definition_list, ::grmcdorman::SettingInterface::settings_list_t &&setting_list)
Initialize the definition and setting lists.
Definition: Device.h:426
static constexpr int D6
D6 is GPIO12; SPI (MISO)
Definition: Device.h:67
static int index_to_dataline(int index)
Convert a data line index to a ESP data line.
Definition: Device.h:442
static const __FlashStringHelper * get_firmware_name()
Get the firmware prefix.
Definition: Device.h:195
std::vector< const Definition * > definition_list_t
The type containing a list of Definition objects.
Definition: Device.h:291