esp8266_web_settings beta
MqttPublisher.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 <memory>
26#include <PubSubClient.h>
27#include <Ticker.h>
28
29#include "grmcdorman/device/Device.h"
30#include "grmcdorman/Setting.h"
31
32class Client;
33
34namespace grmcdorman::device
35{
48 class MqttPublisher: public Device
49 {
50 public:
51 static constexpr uint16_t CONNECTION_TRIES = 5;
52 static constexpr uint32_t CONNECTION_RETRY_INTERVAL = 5;
72 MqttPublisher(const __FlashStringHelper *manufacturer, const __FlashStringHelper *model, const __FlashStringHelper *software_version, Client *client = nullptr);
73
81 void set_defaults() override;
82
83 void setup() override;
84 void loop() override;
85
86 DynamicJsonDocument as_json() const override;
87
102 virtual void set_devices(const std::vector<Device *> &list) override
103 {
104 devices = &list;
105 }
106
118 virtual String get_status() const;
119
120 private:
127 void reconnect();
135 void publish_auto_config();
143 void publish();
144
153 String get_error_state_message(int state) const;
154
161 void set_timer();
162
163 const __FlashStringHelper *publish_manufacturer;
164 const __FlashStringHelper *publish_model;
165 const __FlashStringHelper *publish_software_version;
166
167 const std::vector<Device *> *devices = nullptr;
168
169 std::unique_ptr<Client> default_client;
170 Client *client;
171 std::unique_ptr<PubSubClient> mqttClient;
172 uint16_t retry_count = 0;
173 uint32_t previous_connection_attempt_ms = 0;
174 uint32_t previous_publish_ms = 0;
175 bool tried_publish = false;
176 bool last_publish_failed = false;
177 int last_state = -1;
178 uint32_t current_publish_seconds = 0;
179
180 String topicAvailability;
181 String topicState;
182 String topicCommand;
183
184 Ticker connect_ticker;
185 Ticker publish_ticker;
186 NoteSetting notes;
187 StringSetting server_address;
188 UnsignedIntegerSetting server_port;
189 UnsignedIntegerSetting update_interval;
190 UnsignedIntegerSetting reconnect_interval;
191 UnsignedIntegerSetting keepalive_interval;
192 UnsignedIntegerSetting buffer_size;
193 StringSetting username;
194 PasswordSetting password;
195 StringSetting prefix;
196 StringSetting identifier;
197 InfoSettingHtml device_status;
198 };
199}
The generic device interface.
Definition: Device.h:46
This class supports generic MQTT publishing.
Definition: MqttPublisher.h:49
static constexpr uint32_t CONNECTION_RETRY_INTERVAL
Second between attempts to retry establishing a connection.
Definition: MqttPublisher.h:52
DynamicJsonDocument as_json() const override
Get the values, as a JSON document.
Definition: MqttPublisher.cpp:337
void setup() override
Setup the device.
Definition: MqttPublisher.cpp:113
void set_defaults() override
Set defaults.
Definition: MqttPublisher.cpp:107
virtual String get_status() const
Get a status report.
Definition: MqttPublisher.cpp:407
void loop() override
Main loop.
Definition: MqttPublisher.cpp:149
static constexpr uint16_t CONNECTION_TRIES
Number of times to try establishing a connection.
Definition: MqttPublisher.h:51
virtual void set_devices(const std::vector< Device * > &list) override
Add a list of devices that will publish.
Definition: MqttPublisher.h:102
MqttPublisher(const __FlashStringHelper *manufacturer, const __FlashStringHelper *model, const __FlashStringHelper *software_version, Client *client=nullptr)
Construct a new Mqtt Device object.
Definition: MqttPublisher.cpp:37