MQTTSuite
Loading...
Searching...
No Matches
MqttModel.h
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef MQTTBROKER_LIB_MQTTMODEL_H
43#define MQTTBROKER_LIB_MQTTMODEL_H
44
45namespace mqtt::mqttbroker::lib {
46 class Mqtt;
47}
48
49namespace express {
50 class Response;
51}
52
53namespace iot::mqtt {
54 namespace server::broker {
55 class Broker;
56 }
57} // namespace iot::mqtt
58
59#ifndef DOXYGEN_SHOULD_SKIP_THIS
60
61#include <chrono>
62#include <core/timer/Timer.h>
63#include <cstdint>
64#include <list>
65#include <map>
66#include <memory>
67#include <nlohmann/json_fwd.hpp>
68#include <string>
69
70#endif
71
72namespace mqtt::mqttbroker::lib {
73
74 class MqttModel {
75 private:
77 public:
78 MqttModelEntry() = default;
79 MqttModelEntry(Mqtt* mqtt);
80
82
83 MqttModelEntry(MqttModelEntry&&) noexcept = default;
84
85 Mqtt* getMqtt() const;
86
87 std::string onlineSince() const;
88 std::string onlineDuration() const;
89
90 private:
91 Mqtt* mqtt = nullptr;
92 };
93
95 public:
96 EventReceiver(const std::shared_ptr<express::Response>& response);
97
99
100 std::weak_ptr<express::Response> response;
101
102 bool operator==(const EventReceiver& other);
103
104 core::timer::Timer heartbeatTimer;
105 };
106
107 private:
108 MqttModel();
109
110 public:
111 static MqttModel& instance();
112
113 void addEventReceiver(const std::shared_ptr<express::Response>& response,
114 const std::string& lastEventId,
115 const std::shared_ptr<iot::mqtt::server::broker::Broker>& broker);
116
117 void connectClient(Mqtt* mqtt);
118 void disconnectClient(const std::string& clientId);
119 void subscribeClient(const std::string& clientId, const std::string& topic, const uint8_t qos);
120 void unsubscribeClient(const std::string& clientId, const std::string& topic);
121 void publishMessage(const std::string& topic, const std::string& message, uint8_t qoS, bool retain);
122
123 const std::map<std::string, MqttModelEntry>& getClients() const;
124
125 Mqtt* getMqtt(const std::string& clientId) const;
126
127 std::string onlineSince() const;
128 std::string onlineDuration() const;
129
130 private:
131 static void sendEvent(const std::shared_ptr<express::Response>& response,
132 const std::string& data,
133 const std::string& event,
134 const std::string& id);
135 static void sendJsonEvent(const std::shared_ptr<express::Response>& response,
136 const nlohmann::json& json,
137 const std::string& event = "",
138 const std::string& id = "");
139 void sendEvent(const std::string& data, const std::string& event = "", const std::string& id = "") const;
140 void sendJsonEvent(const nlohmann::json& json, const std::string& event = "", const std::string& id = "") const;
141
142 static std::string timePointToString(const std::chrono::time_point<std::chrono::system_clock>& timePoint);
143 static std::string
144 durationToString(const std::chrono::time_point<std::chrono::system_clock>& bevore,
145 const std::chrono::time_point<std::chrono::system_clock>& later = std::chrono::system_clock::now());
146
147 std::map<std::string, MqttModelEntry> modelMap;
149 std::chrono::time_point<std::chrono::system_clock> onlineSinceTimePoint;
150 uint64_t id = 0;
151
152 friend void to_json(nlohmann::json& j, const MqttModel::MqttModelEntry& mqttModelEntry);
153 };
154
155} // namespace mqtt::mqttbroker::lib
156
157#endif // MQTTBROKER_LIB_MQTTMODEL_H
void publishMappings(const iot::mqtt::packets::Publish &publish)
MqttMapper(const nlohmann::json &mappingJson)
bool operator==(const EventReceiver &other)
std::weak_ptr< express::Response > response
Definition MqttModel.h:100
EventReceiver(const std::shared_ptr< express::Response > &response)
MqttModelEntry(MqttModelEntry &&) noexcept=default
void sendEvent(const std::string &data, const std::string &event="", const std::string &id="") const
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
Definition MqttModel.h:149
void publishMessage(const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
void addEventReceiver(const std::shared_ptr< express::Response > &response, const std::string &lastEventId, const std::shared_ptr< iot::mqtt::server::broker::Broker > &broker)
static MqttModel & instance()
void unsubscribeClient(const std::string &clientId, const std::string &topic)
std::list< EventReceiver > eventReceiverList
Definition MqttModel.h:148
std::map< std::string, MqttModelEntry > modelMap
Definition MqttModel.h:147
Mqtt * getMqtt(const std::string &clientId) const
void subscribeClient(const std::string &clientId, const std::string &topic, const uint8_t qos)
void disconnectClient(const std::string &clientId)
static void sendEvent(const std::shared_ptr< express::Response > &response, const std::string &data, const std::string &event, const std::string &id)
const std::map< std::string, MqttModelEntry > & getClients() const
std::string onlineDuration() const
std::string onlineSince() const
void unsubscribe(const std::string &topic)
Definition Mqtt.cpp:73
void publishMapping(const std::string &topic, const std::string &message, uint8_t qoS, bool retain) final
Definition Mqtt.cpp:115
void onPublish(const iot::mqtt::packets::Publish &publish) final
Definition Mqtt.cpp:85
void onConnect(const iot::mqtt::packets::Connect &connect) final
Definition Mqtt.cpp:79
void subscribe(const std::string &topic, uint8_t qoS)
Definition Mqtt.cpp:67
void onDisconnected() final
Definition Mqtt.cpp:109
void onSubscribe(const iot::mqtt::packets::Subscribe &subscribe) final
Definition Mqtt.cpp:93
void onUnsubscribe(const iot::mqtt::packets::Unsubscribe &unsubscribe) final
Definition Mqtt.cpp:101
void to_json(nlohmann::json &j, const MqttModel::MqttModelEntry &mqttModelEntry)
Definition MqttModel.cpp:99