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 void registerChannelHandler(MqttChannelHandler *handler);
125 void unregisterChannelHandler(MqttChannelHandler *handler);
126 const char *getPrefix() const;
127 const char *getHostname() const;
128 void subscribe(const char *topic, int qos = -1);
129 bool isUpdatePending() override;
130 bool isRegisteredAndReady() override;
131 void notifyConfigChange(int channelNumber) override;
132
133 void sendActionTrigger(uint8_t channelNumber, uint32_t actionId) override;
134 void sendChannelValueChanged(uint8_t channelNumber, int8_t *value,
135 unsigned char offline, uint32_t validityTimeSec) override;
136 void sendExtendedChannelValueChanged(uint8_t channelNumber,
137 TSuplaChannelExtendedValue *value) override;
138
139 bool processData(const char *topic, const char *payload);
140 void processRelayRequest(const char *topic,
141 const char *payload,
142 Supla::Element *element);
143 void processRGBWRequest(const char *topic,
144 const char *payload,
145 Supla::Element *element);
146 void processRGBRequest(const char *topic,
147 const char *payload,
148 Supla::Element *element);
149 void processDimmerRequest(const char *topic,
150 const char *payload,
151 Supla::Element *element);
152 void processRollerShutterRequest(const char *topic,
153 const char *payload,
154 Supla::Element *element);
155
156 protected:
157 void generateClientId(char result[MQTT_CLIENTID_MAX_SIZE]);
158 void generateObjectId(char result[30], int channelNumber, int subId);
159 MqttTopic getHADiscoveryTopic(const char *sensor, char *objectId);
160 void publishDeviceStatus(bool onRegistration = false);
161 void publishHADiscovery(int channel);
162 void publishHADiscoveryRelay(Supla::Element *);
163 void publishHADiscoveryRelayImpulse(Supla::Element *);
164 void publishHADiscoveryThermometer(Supla::Element *);
165 void publishHADiscoveryHumidity(Supla::Element *);
166 void publishHADiscoveryActionTrigger(Supla::Element *);
167 void publishHADiscoveryEM(Supla::Element *);
168 void publishHADiscoveryRGB(Supla::Element *);
169 void publishHADiscoveryDimmer(Supla::Element *);
170 void publishHADiscoveryBinarySensor(Supla::Element *);
171 void publishHADiscoveryRollerShutter(Supla::Element *);
172
173 // parameterName has to be ASCII string with small caps and underscores
174 // between words i.e. "total_forward_active_energy".
175 // Name of parameter will be generated in following way:
176 // "Total forward active energy"
177 void publishHADiscoveryEMParameter(
178 Supla::Element *element, int parameterId, const char *parameterName,
179 const char *units, Supla::Protocol::HAStateClass stateClass,
180 Supla::Protocol::HADeviceClass deviceClass);
181 const char *getActionTriggerType(uint8_t actionIdx);
182 bool isActionTriggerEnabled(Supla::Channel *ch, uint8_t actionIdx);
183 virtual void publishImp(const char *topic,
184 const char *payload,
185 int qos,
186 bool retain) = 0;
187 virtual void subscribeImp(const char *topic, int qos) = 0;
188 const char *getStateClassStr(Supla::Protocol::HAStateClass stateClass);
189 const char *getDeviceClassStr(Supla::Protocol::HADeviceClass deviceClass);
190
191 const char *getRelayChannelName(int channelFunction) const;
192 const char *getBinarySensorChannelName(int channelFunction) const;
193
194 bool isPayloadOn(const char *);
195 bool isOpenClosedBinarySensorFunction(int channelFunction) const;
196 MqttChannelHandler *findChannelHandler(int channelType) const;
197
198 friend class HvacMqttHandler;
199
200 char server[SUPLA_SERVER_NAME_MAXSIZE] = {};
201 int32_t port = -1;
202 char user[MQTT_USERNAME_MAX_SIZE] = {};
203 char password[MQTT_PASSWORD_MAX_SIZE] = {};
204 char hostname[32] = {};
205 uint8_t qosCfg = 0;
206 bool useTls = false;
207 bool useAuth = true;
208 bool retainCfg = false;
209 bool enabled = true;
210 bool connecting = false;
211 bool connected = false;
212 bool error = false;
213 uint16_t prefixLen = 0;
214 uint16_t channelsCount = 0;
215 uint16_t buttonNumber = 0;
216 char *prefix = nullptr;
217 MqttChannelHandler *channelHandlers = nullptr;
218 // Button number is incremented on each publishHADiscoveryActionTrigger call
219 // and it is reset on publishDeviceStatus. So we publish button numbers
220 // starting from 1 and incrementing on each ActionTrigger channel found
221 // in current setup.
222 // It is important to call publishDeviceStatus first, then to call
223 // publishHADiscoveryActionTrigger for each AT channel.
224 uint8_t configChangedBit[8] = {};
225 Supla::Uptime uptime;
226};
227} // namespace Protocol
228} // namespace Supla
229
230#endif // SRC_SUPLA_PROTOCOL_MQTT_H_
Definition SuplaDevice.h:163
Definition channel.h:33
Base class for all elements of SuplaDevice.
Definition element.h:37
Definition hvac_mqtt.cpp:31
Definition uptime.h:24
Definition proto.h:782