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-common/proto.h>
24#include <supla/uptime.h>
25#include <supla/protocol/mqtt_topic.h>
26#include <supla/element.h>
27
28#include "protocol_layer.h"
29
30// max client id length limit in mqtt 3.1
31#define MQTT_CLIENTID_MAX_SIZE 23
32#define MQTT_MAX_PAYLOAD_LEN 50
33
34namespace Supla {
35
36namespace Protocol {
37
38// https://developers.home-assistant.io/docs/core/entity/sensor/
39enum HAStateClass {
40 HAStateClass_None,
41 HAStateClass_Measurement,
42 HAStateClass_Total,
43 HAStateClass_TotalIncreasing
44};
45
46// https://www.home-assistant.io/integrations/sensor/#device-class
47// Not all devices classes are implemented here. Only those which we use are
48// present. Add more if needed.
49// Also not all parameters used in Supla have corresponding device class in
50// HA, so in such case we don't set the device class.
51enum HADeviceClass {
52 HADeviceClass_None,
53 HADeviceClass_ApparentPower,
54 HADeviceClass_Current,
55 HADeviceClass_Energy,
56 HADeviceClass_Frequency,
57 HADeviceClass_PowerFactor,
58 HADeviceClass_Power,
59 HADeviceClass_ReactivePower,
60 HADeviceClass_Voltage,
61 HADeviceClass_Outlet,
62 HADeviceClass_Gate,
63 HADeviceClass_Door,
64 HADeviceClass_Garage,
65 HADeviceClass_Moisture,
66 HADeviceClass_Window,
67 HADeviceClass_Awning,
68 HADeviceClass_Blind,
69 HADeviceClass_Curtain,
70 HADeviceClass_Shutter,
71 HADeviceClass_Shade,
72};
73
74class Mqtt : public ProtocolLayer {
75 public:
76 explicit Mqtt(SuplaDeviceClass *sdc);
77 ~Mqtt();
78
79 void onInit() override;
80 bool onLoadConfig() override;
81 bool verifyConfig() override;
82 bool isEnabled() override;
83// void disconnect() override;
84// void iterate(uint32_t _millis) override;
85 bool isNetworkRestartRequested() override;
86 uint32_t getConnectionFailTime() override;
87 bool isConnectionError() override;
88 bool isConnecting() override;
89 void publish(const char *topic,
90 const char *payload,
91 int qos = -1,
92 int retain = -1,
93 bool ignorePrefix = false);
94 void publishInt(const char *topic,
95 int payload,
96 int qos = -1,
97 int retain = -1);
98 void publishBool(const char *topic,
99 bool payload,
100 int qos = -1,
101 int retain = -1);
102 void publishOnOff(const char *topic, bool payload, int qos, int retain);
103 void publishOpenClosed(const char *topic, bool payload, int qos, int retain);
104 void publishDouble(const char *topic,
105 double payload,
106 int qos = -1,
107 int retain = -1,
108 int precision = 2);
109 void publishColor(const char *topic,
110 uint8_t red,
111 uint8_t green,
112 uint8_t blue,
113 int qos = -1,
114 int retain = -1);
115 void publishChannelState(int channel);
116 void publishExtendedChannelState(int channel);
117 void subscribeChannel(int channel);
118 void subscribe(const char *topic, int qos = -1);
119 bool isUpdatePending() override;
120 bool isRegisteredAndReady() override;
121 void notifyConfigChange(int channelNumber) override;
122
123 void sendActionTrigger(uint8_t channelNumber, uint32_t actionId) override;
124 void sendChannelValueChanged(uint8_t channelNumber, int8_t *value,
125 unsigned char offline, uint32_t validityTimeSec) override;
126 void sendExtendedChannelValueChanged(uint8_t channelNumber,
127 TSuplaChannelExtendedValue *value) override;
128
129 bool processData(const char *topic, const char *payload);
130 void processRelayRequest(const char *topic,
131 const char *payload,
132 Supla::Element *element);
133 void processRGBWRequest(const char *topic,
134 const char *payload,
135 Supla::Element *element);
136 void processRGBRequest(const char *topic,
137 const char *payload,
138 Supla::Element *element);
139 void processDimmerRequest(const char *topic,
140 const char *payload,
141 Supla::Element *element);
142 void processHVACRequest(const char *topic,
143 const char *payload,
144 Supla::Element *element);
145 void processRollerShutterRequest(const char *topic,
146 const char *payload,
147 Supla::Element *element);
148
149 protected:
150 void generateClientId(char result[MQTT_CLIENTID_MAX_SIZE]);
151 void generateObjectId(char result[30], int channelNumber, int subId);
152 MqttTopic getHADiscoveryTopic(const char *sensor, char *objectId);
153 void publishDeviceStatus(bool onRegistration = false);
154 void publishHADiscovery(int channel);
155 void publishHADiscoveryRelay(Supla::Element *);
156 void publishHADiscoveryRelayImpulse(Supla::Element *);
157 void publishHADiscoveryThermometer(Supla::Element *);
158 void publishHADiscoveryHumidity(Supla::Element *);
159 void publishHADiscoveryActionTrigger(Supla::Element *);
160 void publishHADiscoveryEM(Supla::Element *);
161 void publishHADiscoveryRGB(Supla::Element *);
162 void publishHADiscoveryDimmer(Supla::Element *);
163 void publishHADiscoveryHVAC(Supla::Element *);
164 void publishHADiscoveryBinarySensor(Supla::Element *);
165 void publishHADiscoveryRollerShutter(Supla::Element *);
166
167 // parameterName has to be ASCII string with small caps and underscores
168 // between words i.e. "total_forward_active_energy".
169 // Name of parameter will be generated in following way:
170 // "Total forward active energy"
171 void publishHADiscoveryEMParameter(
172 Supla::Element *element, int parameterId, const char *parameterName,
173 const char *units, Supla::Protocol::HAStateClass stateClass,
174 Supla::Protocol::HADeviceClass deviceClass);
175 const char *getActionTriggerType(uint8_t actionIdx);
176 bool isActionTriggerEnabled(Supla::Channel *ch, uint8_t actionIdx);
177 virtual void publishImp(const char *topic,
178 const char *payload,
179 int qos,
180 bool retain) = 0;
181 virtual void subscribeImp(const char *topic, int qos) = 0;
182 const char *getStateClassStr(Supla::Protocol::HAStateClass stateClass);
183 const char *getDeviceClassStr(Supla::Protocol::HADeviceClass deviceClass);
184
185 const char *getRelayChannelName(int channelFunction) const;
186 const char *getBinarySensorChannelName(int channelFunction) const;
187
188 bool isPayloadOn(const char *);
189 bool isOpenClosedBinarySensorFunction(int channelFunction) const;
190
191 char server[SUPLA_SERVER_NAME_MAXSIZE] = {};
192 int32_t port = -1;
193 char user[MQTT_USERNAME_MAX_SIZE] = {};
194 char password[MQTT_PASSWORD_MAX_SIZE] = {};
195 char hostname[32] = {};
196 uint8_t qosCfg = 0;
197 bool useTls = false;
198 bool useAuth = true;
199 bool retainCfg = false;
200 bool enabled = true;
201 bool connecting = false;
202 bool connected = false;
203 bool error = false;
204 char *prefix = nullptr;
205 int prefixLen = 0;
206 Supla::Uptime uptime;
207 int channelsCount = 0;
208 // Button number is incremented on each publishHADiscoveryActionTrigger call
209 // and it is reset on publishDeviceStatus. So we publish button numbers
210 // starting from 1 and incrementing on each ActionTrigger channel found
211 // in current setup.
212 // It is important to call publishDeviceStatus first, then to call
213 // publishHADiscoveryActionTrigger for each AT channel.
214 int buttonNumber = 0;
215 uint8_t configChangedBit[8] = {};
216};
217} // namespace Protocol
218} // namespace Supla
219
220#endif // SRC_SUPLA_PROTOCOL_MQTT_H_
Definition SuplaDevice.h:93
Definition channel.h:33
Base class for all elements of SuplaDevice.
Definition element.h:33
Definition uptime.h:24
Definition proto.h:749