supla-device
Loading...
Searching...
No Matches
mqtt.h
1/*
2 * Copyright (C) AC SOFTWARE SP. Z O.O
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#ifndef SRC_SUPLA_PROTOCOL_MQTT_H_
20#define SRC_SUPLA_PROTOCOL_MQTT_H_
21
22#include <supla/storage/config.h>
23#include <supla/storage/storage.h>
24#include <supla-common/proto.h>
25#include <supla/uptime.h>
26#include <supla/protocol/mqtt_topic.h>
27#include <supla/protocol/mqtt_channel_handler.h>
28#include <supla/element.h>
29
30#include "protocol_layer.h"
31
32// max client id length limit in mqtt 3.1
33#define MQTT_CLIENTID_MAX_SIZE 23
34#define MQTT_MAX_PAYLOAD_LEN 50
35
36namespace Supla {
37
38namespace Protocol {
39
40// https://developers.home-assistant.io/docs/core/entity/sensor/
41enum HAStateClass {
42 HAStateClass_None,
43 HAStateClass_Measurement,
44 HAStateClass_Total,
45 HAStateClass_TotalIncreasing
46};
47
48// https://www.home-assistant.io/integrations/sensor/#device-class
49// Not all devices classes are implemented here. Only those which we use are
50// present. Add more if needed.
51// Also not all parameters used in Supla have corresponding device class in
52// HA, so in such case we don't set the device class.
53enum HADeviceClass {
54 HADeviceClass_None,
55 HADeviceClass_ApparentPower,
56 HADeviceClass_Current,
57 HADeviceClass_Energy,
58 HADeviceClass_ReactiveEnergy,
59 HADeviceClass_Frequency,
60 HADeviceClass_PowerFactor,
61 HADeviceClass_Power,
62 HADeviceClass_ReactivePower,
63 HADeviceClass_Voltage,
64 HADeviceClass_Outlet,
65 HADeviceClass_Gate,
66 HADeviceClass_Door,
67 HADeviceClass_Garage,
68 HADeviceClass_Moisture,
69 HADeviceClass_Window,
70 HADeviceClass_Awning,
71 HADeviceClass_Blind,
72 HADeviceClass_Curtain,
73 HADeviceClass_Shutter,
74 HADeviceClass_Shade,
75};
76
77class HvacMqttHandler;
78
79class Mqtt : public ProtocolLayer {
80 public:
81 explicit Mqtt(SuplaDeviceClass *sdc);
82 virtual ~Mqtt();
83
84 void onInit() override;
85 bool onLoadConfig() override;
86 bool verifyConfig() override;
87 bool isEnabled() override;
88// void disconnect() override;
89// void iterate(uint32_t _millis) override;
90 bool isNetworkRestartRequested() override;
91 uint32_t getConnectionFailTime() override;
92 bool isConnectionError() override;
93 bool isConnecting() override;
94 bool isMqtt() const override;
95 void publish(const char *topic,
96 const char *payload,
97 int qos = -1,
98 int retain = -1,
99 bool ignorePrefix = false);
100 void publishInt(const char *topic,
101 int payload,
102 int qos = -1,
103 int retain = -1);
104 void publishBool(const char *topic,
105 bool payload,
106 int qos = -1,
107 int retain = -1);
108 void publishOnOff(const char *topic, bool payload, int qos, int retain);
109 void publishOpenClosed(const char *topic, bool payload, int qos, int retain);
110 void publishDouble(const char *topic,
111 double payload,
112 int qos = -1,
113 int retain = -1,
114 int precision = 2);
115 void publishColor(const char *topic,
116 uint8_t red,
117 uint8_t green,
118 uint8_t blue,
119 int qos = -1,
120 int retain = -1);
121 void publishChannelState(int channel);
122 void publishExtendedChannelState(int channel);
123 void subscribeChannel(int channel);
124 const char *getPrefix() const;
125 const char *getHostname() const;
126 void subscribe(const char *topic, int qos = -1);
127 bool isUpdatePending() override;
128 bool isRegisteredAndReady() override;
129 void notifyConfigChange(int channelNumber) override;
130
131 void sendActionTrigger(uint8_t channelNumber, uint32_t actionId) override;
132 void sendChannelValueChanged(uint8_t channelNumber, int8_t *value,
133 unsigned char offline, uint32_t validityTimeSec) override;
134 void sendExtendedChannelValueChanged(uint8_t channelNumber,
135 TSuplaChannelExtendedValue *value) override;
136
137 bool processData(const char *topic, const char *payload);
138 void processRelayRequest(const char *topic,
139 const char *payload,
140 Supla::Element *element,
141 Supla::Channel *channel);
142 void processRGBWRequest(const char *topic,
143 const char *payload,
144 Supla::Element *element,
145 int channelNumber);
146 void processRGBRequest(const char *topic,
147 const char *payload,
148 Supla::Element *element,
149 int channelNumber);
150 void processDimmerRequest(const char *topic,
151 const char *payload,
152 Supla::Element *element,
153 int channelNumber);
154 void processRollerShutterRequest(const char *topic,
155 const char *payload,
156 Supla::Element *element,
157 int channelNumber);
158
159 protected:
160 void generateClientId(char result[MQTT_CLIENTID_MAX_SIZE]);
161 void generateObjectId(char result[30], int channelNumber, int subId);
162 MqttTopic getHADiscoveryTopic(const char *sensor, char *objectId);
163 void publishDeviceStatus(bool onRegistration = false);
164 void publishHADiscovery(int channel);
165 void publishHADiscoveryRelay(Supla::Element *);
166 void publishHADiscoveryRelay(Supla::Element *, Supla::Channel *);
167 void publishHADiscoveryRelayImpulse(Supla::Element *);
168 void publishHADiscoveryRelayImpulse(Supla::Element *, Supla::Channel *);
169 void clearHADiscoveryRelayAlternativeTypes(Supla::Channel *channel,
170 const char *currentType);
171 void clearHADiscoveryForChannel(Supla::Channel *channel);
172 void clearRelayAlternativeStateTopics(Supla::Channel *channel,
173 bool currentIsRoller);
174 void clearStateForChannel(Supla::Channel *channel);
175 void publishHADiscoveryThermometer(Supla::Element *);
176 void publishHADiscoveryHumidity(Supla::Element *);
177 void publishHADiscoveryActionTrigger(Supla::Element *);
178 void publishHADiscoveryEM(Supla::Element *);
179 void publishHADiscoveryRGB(Supla::Element *);
180 void publishHADiscoveryDimmer(Supla::Element *);
181 void publishHADiscoveryBinarySensor(Supla::Element *);
182 void publishHADiscoveryRollerShutter(Supla::Element *);
183
184 // parameterName has to be ASCII string with small caps and underscores
185 // between words i.e. "total_forward_active_energy".
186 // Name of parameter will be generated in following way:
187 // "Total forward active energy"
188 void publishHADiscoveryEMParameter(
189 Supla::Element *element, int parameterId, const char *parameterName,
190 const char *units, Supla::Protocol::HAStateClass stateClass,
191 Supla::Protocol::HADeviceClass deviceClass);
192 const char *getActionTriggerType(uint8_t actionIdx);
193 bool isActionTriggerEnabled(Supla::Channel *ch, uint8_t actionIdx);
194 virtual void publishImp(const char *topic,
195 const char *payload,
196 int qos,
197 bool retain) = 0;
198 virtual void subscribeImp(const char *topic, int qos) = 0;
199 const char *getStateClassStr(Supla::Protocol::HAStateClass stateClass);
200 const char *getDeviceClassStr(Supla::Protocol::HADeviceClass deviceClass);
201
202 const char *getRelayChannelName(int channelFunction) const;
203 const char *getBinarySensorChannelName(int channelFunction) const;
204
205 bool isPayloadOn(const char *);
206 bool isOpenClosedBinarySensorFunction(int channelFunction) const;
207
208 MqttChannelHandler *findChannelHandler(int channelType) const;
209
210 friend class HvacMqttHandler;
211
212 char server[SUPLA_SERVER_NAME_MAXSIZE] = {};
213 int32_t port = -1;
214 char user[MQTT_USERNAME_MAX_SIZE] = {};
215 char password[MQTT_PASSWORD_MAX_SIZE] = {};
216 char hostname[32] = {};
217 uint8_t qosCfg = 0;
218 bool useTls = false;
219 bool useAuth = true;
220 bool retainCfg = false;
221 bool enabled = true;
222 bool connecting = false;
223 bool connected = false;
224 bool error = false;
225 uint16_t prefixLen = 0;
226 uint16_t channelsCount = 0;
227 uint16_t buttonNumber = 0;
228 char *prefix = nullptr;
229 // Button number is incremented on each publishHADiscoveryActionTrigger call
230 // and it is reset on publishDeviceStatus. So we publish button numbers
231 // starting from 1 and incrementing on each ActionTrigger channel found
232 // in current setup.
233 // It is important to call publishDeviceStatus first, then to call
234 // publishHADiscoveryActionTrigger for each AT channel.
235 uint8_t configChangedBit[8] = {};
236 Supla::Uptime uptime;
237};
238} // namespace Protocol
239} // namespace Supla
240
241#endif // SRC_SUPLA_PROTOCOL_MQTT_H_
Definition SuplaDevice.h:177
Definition channel.h:33
Base class for all elements of SuplaDevice.
Definition element.h:42
Definition hvac_mqtt.cpp:31
Definition uptime.h:24
Definition proto.h:785