MQTTSuite
Loading...
Searching...
No Matches
mqtt::mqttbroker::lib::MqttModel Class Reference

#include <MqttModel.h>

Collaboration diagram for mqtt::mqttbroker::lib::MqttModel:

Classes

class  MqttModelEntry
class  EventReceiver

Public Member Functions

void addClient (const std::string &clientId, Mqtt *mqtt)
void delClient (const std::string &clientId)
std::map< std::string, MqttModelEntry > & getClients ()
const MqttgetMqtt (const std::string &clientId)
std::string onlineSince ()
std::string onlineDuration ()
void addEventReceiver (const std::shared_ptr< express::Response > &response, const std::string &lastEventId)
void publish (const iot::mqtt::packets::Publish &publish)

Static Public Member Functions

static MqttModelinstance ()

Protected Attributes

std::map< std::string, MqttModelEntrymodelMap
std::list< EventReceivereventReceiverList
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
uint64_t id = 0

Private Member Functions

 MqttModel ()
void sendEvent (const std::string &data, const std::string &event="", const std::string &id="")

Static Private Member Functions

static std::string timePointToString (const std::chrono::time_point< std::chrono::system_clock > &timePoint)
static std::string durationToString (const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())

Detailed Description

Definition at line 71 of file MqttModel.h.

Constructor & Destructor Documentation

◆ MqttModel()

mqtt::mqttbroker::lib::MqttModel::MqttModel ( )
private

Definition at line 72 of file MqttModel.cpp.

73 : onlineSinceTimePoint(std::chrono::system_clock::now()) {
74 }
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
Definition MqttModel.h:136

References onlineSinceTimePoint.

Member Function Documentation

◆ addClient()

void mqtt::mqttbroker::lib::MqttModel::addClient ( const std::string & clientId,
Mqtt * mqtt )

Definition at line 116 of file MqttModel.cpp.

116 {
117 MqttModelEntry mqttModelEntry(mqtt);
118
119 nlohmann::json json({href(mqtt->getClientId(), "/client?" + mqtt->getClientId(), windowId(mqtt->getClientId()), 450, 900),
120 mqttModelEntry.onlineSince(),
121 "<duration>" + mqttModelEntry.onlineDuration() + "</duration>",
122 mqtt->getConnectionName(),
123 mqtt->getMqttContext()->getSocketConnection()->getLocalAddress().toString(),
124 mqtt->getMqttContext()->getSocketConnection()->getRemoteAddress().toString(),
125 "<button class=\"red-btn\" onClick=\"disconnectClient('" + mqtt->getClientId() + "')\">Disconnect</button>"});
126
127 sendEvent(json.dump(), "connect", std::to_string(id++));
128
129 modelMap.emplace(clientId, std::move(mqttModelEntry));
130 }
nlohmann::json json
void sendEvent(const std::string &data, const std::string &event="", const std::string &id="")
std::map< std::string, MqttModelEntry > modelMap
Definition MqttModel.h:132
static std::string href(const std::string &text, const std::string &url, const std::string &windowId, uint16_t width, uint16_t height)
Definition MqttModel.cpp:96
static std::string windowId(const std::string &clientId)
Definition MqttModel.cpp:82

References mqtt::mqttbroker::lib::href(), id, modelMap, mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::MqttModelEntry(), mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineDuration(), mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineSince(), sendEvent(), and mqtt::mqttbroker::lib::windowId().

Referenced by mqtt::mqttbroker::lib::Mqtt::onConnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addEventReceiver()

void mqtt::mqttbroker::lib::MqttModel::addEventReceiver ( const std::shared_ptr< express::Response > & response,
const std::string & lastEventId )

Definition at line 161 of file MqttModel.cpp.

161 {
162 auto& eventReceiver = eventReceiverList.emplace_back(response);
163
164 response->getSocketContext()->onDisconnected([this, &eventReceiver]() {
165 eventReceiverList.remove(eventReceiver);
166 });
167
168 for (auto& mqttModel : modelMap) {
169 nlohmann::json json({href(mqttModel.first, "/client?" + mqttModel.first, windowId(mqttModel.first), 450, 900),
170 mqttModel.second.onlineSince(),
171 "<duration>" + mqttModel.second.onlineDuration() + "</duration>",
172 mqttModel.second.getMqtt()->getConnectionName(),
173 mqttModel.second.getMqtt()->getMqttContext()->getSocketConnection()->getLocalAddress().toString(),
174 mqttModel.second.getMqtt()->getMqttContext()->getSocketConnection()->getRemoteAddress().toString(),
175 "<button class=\"red-btn\" onClick=\"disconnectClient('" + mqttModel.second.getMqtt()->getClientId() +
176 "')\">Disconnect</button>"});
177
178 sendEvent(json.dump(), "connect", std::to_string(id++));
179 }
180 }
std::list< EventReceiver > eventReceiverList
Definition MqttModel.h:134

References eventReceiverList, mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::getMqtt(), mqtt::mqttbroker::lib::href(), id, modelMap, mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineDuration(), mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::onlineSince(), sendEvent(), and mqtt::mqttbroker::lib::windowId().

Referenced by getRouter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ delClient()

void mqtt::mqttbroker::lib::MqttModel::delClient ( const std::string & clientId)

Definition at line 132 of file MqttModel.cpp.

132 {
133 sendEvent(clientId, "disconnect", std::to_string(id++));
134
135 modelMap.erase(clientId);
136 }

References id, modelMap, and sendEvent().

Referenced by mqtt::mqttbroker::lib::Mqtt::onDisconnected().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ durationToString()

std::string mqtt::mqttbroker::lib::MqttModel::durationToString ( const std::chrono::time_point< std::chrono::system_clock > & bevore,
const std::chrono::time_point< std::chrono::system_clock > & later = std::chrono::system_clock::now() )
staticprivate

Definition at line 241 of file MqttModel.cpp.

242 {
243 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
244
245 seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
246
247 // Compute days, hours, minutes, and seconds
248 seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
249 seconds_duration_type remainder = totalSeconds % 86400;
250 seconds_duration_type hours = remainder / 3600;
251 remainder = remainder % 3600;
252 seconds_duration_type minutes = remainder / 60;
253 seconds_duration_type seconds = remainder % 60;
254
255 // Format the components into a string using stringstream
256 std::ostringstream oss;
257 if (days > 0) {
258 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
259 }
260 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
261 << std::setfill('0') << seconds;
262
263 return oss.str();
264 }

◆ getClients()

std::map< std::string, MqttModel::MqttModelEntry > & mqtt::mqttbroker::lib::MqttModel::getClients ( )

Definition at line 138 of file MqttModel.cpp.

138 {
139 return modelMap;
140 }

References modelMap.

◆ getMqtt()

const Mqtt * mqtt::mqttbroker::lib::MqttModel::getMqtt ( const std::string & clientId)

Definition at line 142 of file MqttModel.cpp.

142 {
143 const Mqtt* mqtt = nullptr;
144
145 auto modelIt = modelMap.find(clientId);
146 if (modelIt != modelMap.end()) {
147 mqtt = modelIt->second.getMqtt();
148 }
149
150 return mqtt;
151 }

References mqtt::mqttbroker::lib::MqttModel::MqttModelEntry::getMqtt(), and modelMap.

Referenced by getRouter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instance()

MqttModel & mqtt::mqttbroker::lib::MqttModel::instance ( )
static

Definition at line 76 of file MqttModel.cpp.

76 {
77 static MqttModel mqttModel;
78
79 return mqttModel;
80 }

Referenced by getRouter(), mqtt::mqttbroker::lib::Mqtt::onConnect(), mqtt::mqttbroker::lib::Mqtt::onDisconnected(), and mqtt::mqttbroker::lib::Mqtt::onPublish().

Here is the caller graph for this function:

◆ onlineDuration()

std::string mqtt::mqttbroker::lib::MqttModel::onlineDuration ( )

Definition at line 157 of file MqttModel.cpp.

157 {
159 }
static std::string durationToString(const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())

References onlineSinceTimePoint.

Referenced by getOverviewPage().

Here is the caller graph for this function:

◆ onlineSince()

std::string mqtt::mqttbroker::lib::MqttModel::onlineSince ( )

Definition at line 153 of file MqttModel.cpp.

153 {
155 }
static std::string timePointToString(const std::chrono::time_point< std::chrono::system_clock > &timePoint)

References onlineSinceTimePoint.

Referenced by getOverviewPage().

Here is the caller graph for this function:

◆ publish()

void mqtt::mqttbroker::lib::MqttModel::publish ( const iot::mqtt::packets::Publish & publish)

Definition at line 182 of file MqttModel.cpp.

182 {
183 /*
184 if (publish.getRetain()) {
185 sendEvent(publish.getTopic() + " : " + publish.getMessage(), "retain", std::to_string(id++));
186 }
187 */
188 }

Referenced by mqtt::mqttbroker::lib::Mqtt::onPublish().

Here is the caller graph for this function:

◆ sendEvent()

void mqtt::mqttbroker::lib::MqttModel::sendEvent ( const std::string & data,
const std::string & event = "",
const std::string & id = "" )
private

Definition at line 209 of file MqttModel.cpp.

209 {
210 for (auto& eventReceiver : eventReceiverList) {
211 if (const auto& response = eventReceiver.response.lock()) {
212 if (response->isConnected()) {
213 if (!event.empty()) {
214 response->sendFragment("event:" + event);
215 }
216 if (!id.empty()) {
217 response->sendFragment("id:" + id);
218 }
219 response->sendFragment("data:" + data);
220 response->sendFragment();
221 }
222 }
223 }
224 }

References eventReceiverList, and mqtt::mqttbroker::lib::MqttModel::EventReceiver::response.

Referenced by addClient(), addEventReceiver(), and delClient().

Here is the caller graph for this function:

◆ timePointToString()

std::string mqtt::mqttbroker::lib::MqttModel::timePointToString ( const std::chrono::time_point< std::chrono::system_clock > & timePoint)
staticprivate

Definition at line 226 of file MqttModel.cpp.

226 {
227 std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
228 std::tm* tm_ptr = std::gmtime(&time);
229
230 char buffer[100];
231 std::string onlineSince = "Formatting error";
232
233 // Format: "2025-02-02 14:30:00"
234 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
235 onlineSince = std::string(buffer) + " UTC";
236 }
237
238 return onlineSince;
239 }

Member Data Documentation

◆ eventReceiverList

std::list<EventReceiver> mqtt::mqttbroker::lib::MqttModel::eventReceiverList
protected

Definition at line 134 of file MqttModel.h.

Referenced by addEventReceiver(), and sendEvent().

◆ id

uint64_t mqtt::mqttbroker::lib::MqttModel::id = 0
protected

Definition at line 138 of file MqttModel.h.

Referenced by addClient(), addEventReceiver(), and delClient().

◆ modelMap

std::map<std::string, MqttModelEntry> mqtt::mqttbroker::lib::MqttModel::modelMap
protected

Definition at line 132 of file MqttModel.h.

Referenced by addClient(), addEventReceiver(), delClient(), getClients(), and getMqtt().

◆ onlineSinceTimePoint

std::chrono::time_point<std::chrono::system_clock> mqtt::mqttbroker::lib::MqttModel::onlineSinceTimePoint
protected

Definition at line 136 of file MqttModel.h.

Referenced by MqttModel(), onlineDuration(), and onlineSince().


The documentation for this class was generated from the following files: