MQTTSuite
Loading...
Searching...
No Matches
MqttMapper.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, 2026
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_MQTTMAPPER_H
43#define MQTTBROKER_LIB_MQTTMAPPER_H
44
45namespace iot::mqtt {
46 class Topic;
47} // namespace iot::mqtt
48
49#include <iot/mqtt/packets/Publish.h>
50#include <utils/Timeval.h>
51
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54namespace inja {
55 class Environment;
56}
57
58#include <cstdint>
59#include <list>
60#include <nlohmann/json.hpp> // IWYU pragma: export
61#include <string>
62#include <utility>
63#include <vector>
64
65namespace nlohmann::json_schema {
66 class json_validator;
68} // namespace nlohmann::json_schema
69
70// IWYU pragma: no_include <nlohmann/json_fwd.hpp>
71
72#endif // DOXYGEN_SHOULD_SKIP_THIS
73
74namespace mqtt::lib {
75
76 class MqttMapper {
77 public:
79 utils::Timeval delay;
80 iot::mqtt::packets::Publish publish;
81 };
82
83 using MappedPublishes = std::pair<std::vector<iot::mqtt::packets::Publish>, std::vector<ScheduledPublish>>;
84
85 MqttMapper();
86 MqttMapper(const MqttMapper&) = delete;
87 MqttMapper& operator=(const MqttMapper&) = delete;
88
89 virtual ~MqttMapper();
90
91 static const std::string& getSchema();
92
93 bool setMapping(nlohmann::json mappingJson); // can throw
94 const nlohmann::json& getMapping() const;
95
96 std::string dump();
97
98 const nlohmann::json& getConnection() const;
99
100 std::list<iot::mqtt::Topic> extractSubscriptions() const;
101 MappedPublishes getMappings(const iot::mqtt::packets::Publish& publish);
102
103 static const nlohmann::json validate(const nlohmann::json& json);
104 static const nlohmann::json validate(const nlohmann::json& json, nlohmann::json_schema::basic_error_handler& err);
105
106 private:
107 static void
108 extractSubscription(const nlohmann::json& topicLevelJson, const std::string& topic, std::list<iot::mqtt::Topic>& topicList);
109 static void
110 extractSubscriptions(const nlohmann::json& mappingJson, const std::string& topic, std::list<iot::mqtt::Topic>& topicList);
111
112 nlohmann::json findMatchingTopicLevel(const nlohmann::json& topicLevel, const std::string& topic);
113
114 void publishMappedTemplate(const nlohmann::json& templateMapping, nlohmann::json& json, MappedPublishes& mappedPublishes);
115 void getTemplateMappings(const nlohmann::json& templateMapping,
116 nlohmann::json& json,
117 const iot::mqtt::packets::Publish& publish,
118 MappedPublishes& mappedPublishes);
119
120 static void publishMappedMessage(
121 const std::string& topic, const std::string& message, uint8_t qoS, bool retain, double delay, MappedPublishes& mappedPublishes);
122 static void publishMappedMessage(const nlohmann::json& staticMapping,
123 const iot::mqtt::packets::Publish& publish,
124 MappedPublishes& mappedPublishes);
125 static void getStaticMappings(const nlohmann::json& staticMapping,
126 const iot::mqtt::packets::Publish& publish,
127 MappedPublishes& mappedPublishes);
128
130
131 std::list<void*> pluginHandles;
132
133 inja::Environment* injaEnvironment; // We need it as pointer as it must be removed befor unloading the plugin libraries
134
136
137 static const std::string mappingJsonSchemaString;
138 };
139
140} // namespace mqtt::lib
141
142#endif // MQTTBROKER_LIB_MQTTMAPPER_H
Class for changing the configuration.
Definition inja.hpp:2993
std::string getMappingFilename() const
const std::shared_ptr< MqttMapper > getMqttMapper() const
bool setMappingFile(const std::string &mappingFile)
ConfigApplication & setSessionStore(const std::string &sessionStore)
ConfigApplication(utils::SubCommand *parent, ConcretConfigApplicationT *concretConfigApplication)
std::shared_ptr< MqttMapper > mqttMapper
bool setMapping(const nlohmann::json &json)
std::string getSessionStore() const
ConfigMqttBroker & setHtmlRoot(const std::string &htmlRoot)
static constexpr std::string_view NAME
ConfigMqttBroker(utils::SubCommand *parent)
static constexpr std::string_view DESCRIPTION
static constexpr std::string_view DESCRIPTION
static constexpr std::string_view NAME
ConfigMqttIntegrator(utils::SubCommand *parent)
static nlohmann::json deployDraft(const std::string &mapFilePath)
static nlohmann::json rollbackTo(const std::string &mapFilePath, const std::string &versionId)
static void discardDraft(const std::string &mapFilePath)
static std::vector< VersionEntry > getHistory(const std::string &mapFilePath)
static nlohmann::json readMappingFromFile(const std::string &mapFilePath)
static nlohmann::json readDraftOrActive(const std::string &mapFilePath)
static std::string getDraftPath(const std::string &mapFilePath)
static void saveDraft(const std::string &mapFilePath, const nlohmann::json &content)
static nlohmann::json getDefaultPatch(const nlohmann::json &inputJson)
static void extractSubscription(const nlohmann::json &topicLevelJson, const std::string &topic, std::list< iot::mqtt::Topic > &topicList)
MqttMapper & operator=(const MqttMapper &)=delete
std::list< iot::mqtt::Topic > extractSubscriptions() const
MappedPublishes getMappings(const iot::mqtt::packets::Publish &publish)
nlohmann::json findMatchingTopicLevel(const nlohmann::json &topicLevel, const std::string &topic)
nlohmann::json mappingJson
Definition MqttMapper.h:129
bool setMapping(nlohmann::json mappingJson)
static const nlohmann::json validate(const nlohmann::json &json)
std::pair< std::vector< iot::mqtt::packets::Publish >, std::vector< ScheduledPublish > > MappedPublishes
Definition MqttMapper.h:83
void getTemplateMappings(const nlohmann::json &templateMapping, nlohmann::json &json, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes)
static const nlohmann::json_schema::json_validator validator
Definition MqttMapper.h:135
std::list< void * > pluginHandles
Definition MqttMapper.h:131
static const nlohmann::json validate(const nlohmann::json &json, nlohmann::json_schema::basic_error_handler &err)
static void publishMappedMessage(const nlohmann::json &staticMapping, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes)
static const std::string mappingJsonSchemaString
Definition MqttMapper.h:137
const nlohmann::json & getMapping() const
inja::Environment * injaEnvironment
Definition MqttMapper.h:133
static void getStaticMappings(const nlohmann::json &staticMapping, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes)
static const std::string & getSchema()
MqttMapper(const MqttMapper &)=delete
const nlohmann::json & getConnection() const
static void publishMappedMessage(const std::string &topic, const std::string &message, uint8_t qoS, bool retain, double delay, MappedPublishes &mappedPublishes)
void publishMappedTemplate(const nlohmann::json &templateMapping, nlohmann::json &json, MappedPublishes &mappedPublishes)
static void extractSubscriptions(const nlohmann::json &mappingJson, const std::string &topic, std::list< iot::mqtt::Topic > &topicList)
iot::mqtt::packets::Publish publish
Definition MqttMapper.h:80