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