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::packets {
54 class Publish;
55}
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59#include <chrono>
60#include <core/timer/Timer.h>
61#include <cstdint>
62#include <list>
63#include <map>
64#include <memory>
65#include <string>
66
67#endif
68
69namespace mqtt::mqttbroker::lib {
70
71 class MqttModel {
72 private:
74 public:
75 MqttModelEntry(const Mqtt* mqtt);
76
78
79 MqttModelEntry(MqttModelEntry&&) noexcept = default;
80
81 const Mqtt* getMqtt() const;
82
83 std::string onlineSince() const;
84 std::string onlineDuration() const;
85
86 private:
87 const Mqtt* mqtt = nullptr;
88 };
89
91 public:
92 EventReceiver(const std::shared_ptr<express::Response>& response);
93
95
96 std::weak_ptr<express::Response> response;
97
98 bool operator==(const EventReceiver& other);
99
100 core::timer::Timer heartbeatTimer;
101 };
102
103 private:
104 MqttModel();
105
106 public:
107 static MqttModel& instance();
108
109 void addClient(const std::string& clientId, Mqtt* mqtt);
110 void delClient(const std::string& clientId);
111
112 std::map<std::string, MqttModelEntry>& getClients();
113
114 const Mqtt* getMqtt(const std::string& clientId);
115
116 std::string onlineSince();
117 std::string onlineDuration();
118
119 void addEventReceiver(const std::shared_ptr<express::Response>& response, const std::string& lastEventId);
120
121 void publish(const iot::mqtt::packets::Publish& publish);
122
123 private:
124 void sendEvent(const std::string& data, const std::string& event = "", const std::string& id = "");
125
126 static std::string timePointToString(const std::chrono::time_point<std::chrono::system_clock>& timePoint);
127 static std::string
128 durationToString(const std::chrono::time_point<std::chrono::system_clock>& bevore,
129 const std::chrono::time_point<std::chrono::system_clock>& later = std::chrono::system_clock::now());
130
131 protected:
132 std::map<std::string, MqttModelEntry> modelMap;
133
135
136 std::chrono::time_point<std::chrono::system_clock> onlineSinceTimePoint;
137
138 uint64_t id = 0;
139 };
140
141} // namespace mqtt::mqttbroker::lib
142
143#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:96
EventReceiver(const std::shared_ptr< express::Response > &response)
MqttModelEntry(MqttModelEntry &&) noexcept=default
void publish(const iot::mqtt::packets::Publish &publish)
void sendEvent(const std::string &data, const std::string &event="", const std::string &id="")
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
Definition MqttModel.h:136
void delClient(const std::string &clientId)
void addClient(const std::string &clientId, Mqtt *mqtt)
static MqttModel & instance()
Definition MqttModel.cpp:76
std::list< EventReceiver > eventReceiverList
Definition MqttModel.h:134
std::map< std::string, MqttModelEntry > modelMap
Definition MqttModel.h:132
void addEventReceiver(const std::shared_ptr< express::Response > &response, const std::string &lastEventId)
std::map< std::string, MqttModelEntry > & getClients()
const Mqtt * getMqtt(const std::string &clientId)
void publishMapping(const std::string &topic, const std::string &message, uint8_t qoS, bool retain) final
Definition Mqtt.cpp:82
void onPublish(const iot::mqtt::packets::Publish &publish) final
Definition Mqtt.cpp:70
void onConnect(const iot::mqtt::packets::Connect &connect) final
Definition Mqtt.cpp:64
void onDisconnected() final
Definition Mqtt.cpp:76