Uniot Core
0.8.1
Loading...
Searching...
No Matches
MQTTDevice.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2023 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <Bytes.h>
22#include <IterableQueue.h>
23
24namespace uniot {
25class MQTTKit;
26
38 friend class MQTTKit;
39
40 public:
44 MQTTDevice() : mpKit(nullptr) {}
45
49 virtual ~MQTTDevice();
50
56 const String &getDeviceId() const;
57
63 const String &getOwnerId() const;
64
70 const String &subscribe(const String &topic);
71
78 const String &subscribeDevice(const String &subTopic);
79
87 const String &subscribeGroup(const String &groupId, const String &subTopic);
88
95 bool unsubscribe(const String &topic);
96
104 virtual void syncSubscriptions() = 0;
105
109 void unsubscribeFromAll();
110
117 bool isSubscribed(const String &topic);
118
129 bool isTopicMatch(const String &storedTopic, const String &incomingTopic) const;
130
138 void publish(const String &topic, const Bytes &payload, bool retained = false, bool sign = false);
139
147 void publishDevice(const String &subTopic, const Bytes &payload, bool retained = false, bool sign = false);
148
157 void publishGroup(const String &groupId, const String &subTopic, const Bytes &payload, bool retained = false, bool sign = false);
158
165 void publishEmptyDevice(const String &subTopic);
166
167 protected:
175 virtual void handle(const String &topic, const Bytes &payload) = 0;
176
177 private:
182 IterableQueue<String> *topics() {
183 return &mTopics;
184 }
185
190 void kit(MQTTKit *kit) {
191 mpKit = kit;
192 }
193
195 IterableQueue<String> mTopics;
196
198 MQTTKit *mpKit;
199
201 static const String sEmptyString;
202};
203
204} // namespace uniot
Definition Bytes.h:38
Definition IterableQueue.h:36
const String & getOwnerId() const
Gets the owner identifier.
Definition MQTTDevice.cpp:46
const String & subscribe(const String &topic)
Subscribes to a specific MQTT topic.
Definition MQTTDevice.cpp:74
MQTTDevice()
Constructs a new MQTTDevice instance.
Definition MQTTDevice.h:44
virtual void syncSubscriptions()=0
Reconstructs subscriptions after reconnection or credential changes.
bool isTopicMatch(const String &storedTopic, const String &incomingTopic) const
Determines if a stored topic matches an incoming topic string using MQTT wildcards.
Definition MQTTDevice.cpp:137
void publishGroup(const String &groupId, const String &subTopic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a group-specific subtopic.
Definition MQTTDevice.cpp:114
bool isSubscribed(const String &topic)
Checks if the device is subscribed to a given topic.
Definition MQTTDevice.cpp:126
const String & subscribeGroup(const String &groupId, const String &subTopic)
Subscribes to a group-specific subtopic.
Definition MQTTDevice.cpp:92
void unsubscribeFromAll()
Unsubscribes from all subscribed topics.
Definition MQTTDevice.cpp:55
const String & getDeviceId() const
Gets the device identifier.
Definition MQTTDevice.cpp:37
bool unsubscribe(const String &topic)
Unsubscribes from a specific topic.
Definition MQTTDevice.cpp:64
void publishEmptyDevice(const String &subTopic)
Publishes an empty message to a device-specific subtopic with retained flag set.
Definition MQTTDevice.cpp:120
void publishDevice(const String &subTopic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a device-specific subtopic.
Definition MQTTDevice.cpp:108
void publish(const String &topic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a specific topic.
Definition MQTTDevice.cpp:101
virtual ~MQTTDevice()
Virtual destructor that handles cleanup and unregistration from MQTTKit.
Definition MQTTDevice.cpp:30
friend class MQTTKit
Definition MQTTDevice.h:38
const String & subscribeDevice(const String &subTopic)
Subscribes to a device-specific subtopic.
Definition MQTTDevice.cpp:83
virtual void handle(const String &topic, const Bytes &payload)=0
Handles incoming MQTT messages.
Definition MQTTKit.h:75
Contains all classes and functions related to the Uniot Core.