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 <tuple>
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::tuple<std::vector<iot::mqtt::packets::Publish>, std::vector<ScheduledPublish>>;
84 using ConnectParameter = std::tuple<bool, std::string, std::string, uint8_t, bool, std::string, std::string>;
85
86 MqttMapper();
87 MqttMapper(const MqttMapper&) = delete;
88 MqttMapper& operator=(const MqttMapper&) = delete;
89
90 virtual ~MqttMapper();
91
92 static const std::string& getSchema();
93
94 bool setMapping(nlohmann::json mappingJson); // can throw
95 const nlohmann::json& getMapping() const;
96
97 std::string getClientId() const;
98 uint16_t getKeepAlive() const;
100
101 std::list<iot::mqtt::Topic> extractSubscriptions() const;
102 MappedPublishes getMappings(const iot::mqtt::packets::Publish& publish);
103
104 static const nlohmann::json validate(const nlohmann::json& json);
105 static const nlohmann::json validate(const nlohmann::json& json, nlohmann::json_schema::basic_error_handler& err);
106
107 private:
108 static void
109 extractSubscription(const nlohmann::json& topicLevelJson, const std::string& topic, std::list<iot::mqtt::Topic>& topicList);
110 static void
111 extractSubscriptions(const nlohmann::json& mappingJson, const std::string& topic, std::list<iot::mqtt::Topic>& topicList);
112
113 nlohmann::json findMatchingTopicLevel(const nlohmann::json& topicLevel, const std::string& topic) const;
114
115 void getMappedTemplate(const nlohmann::json& templateMapping, nlohmann::json& json, MappedPublishes& mappedPublishes) const;
116 void getTemplateMappings(const nlohmann::json& templateMapping,
117 nlohmann::json& json,
118 const iot::mqtt::packets::Publish& publish,
119 MappedPublishes& mappedPublishes) const;
120 static void getStaticMappings(const nlohmann::json& staticMapping,
121 const iot::mqtt::packets::Publish& publish,
122 MappedPublishes& mappedPublishes);
123
124 static void getMappedMessage(
125 const std::string& topic, const std::string& message, uint8_t qoS, bool retain, double delay, MappedPublishes& mappedPublishes);
126 static void
127 getMappedMessage(const nlohmann::json& staticMapping, const iot::mqtt::packets::Publish& publish, MappedPublishes& mappedPublishes);
128
131
132 std::list<void*> pluginHandles;
133
134 inja::Environment* injaEnvironment; // We need it as pointer as it must be destroyed befor unloading the plugin libraries
135
137
138 static const std::string mappingJsonSchemaString;
139 };
140
141} // namespace mqtt::lib
142
143#endif // MQTTBROKER_LIB_MQTTMAPPER_H
Class for changing the configuration.
Definition inja.hpp:2993
std::string getMapping(int indent=2) const
bool loadMapping(const std::string mapFilename)
std::string getMappingFilename() const
bool setMapping(const std::string &mapping) const
const std::shared_ptr< MqttMapper > getMqttMapper() const
bool setMappingFile(const std::string &mapFilename)
ConfigApplication & setSessionStore(const std::string &sessionStore)
ConfigApplication(utils::SubCommand *parent, ConcretConfigApplicationT *concretConfigApplication)
std::shared_ptr< MqttMapper > mqttMapper
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 void extractSubscription(const nlohmann::json &topicLevelJson, const std::string &topic, std::list< iot::mqtt::Topic > &topicList)
MqttMapper & operator=(const MqttMapper &)=delete
void getTemplateMappings(const nlohmann::json &templateMapping, nlohmann::json &json, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes) const
std::string getClientId() const
std::tuple< bool, std::string, std::string, uint8_t, bool, std::string, std::string > ConnectParameter
Definition MqttMapper.h:84
std::list< iot::mqtt::Topic > extractSubscriptions() const
MappedPublishes getMappings(const iot::mqtt::packets::Publish &publish)
nlohmann::json mappingJson
Definition MqttMapper.h:129
uint16_t getKeepAlive() const
bool setMapping(nlohmann::json mappingJson)
static const nlohmann::json validate(const nlohmann::json &json)
ConnectParameter getConnectPayload() const
static const nlohmann::json_schema::json_validator validator
Definition MqttMapper.h:136
std::list< void * > pluginHandles
Definition MqttMapper.h:132
static const nlohmann::json validate(const nlohmann::json &json, nlohmann::json_schema::basic_error_handler &err)
nlohmann::json mappingJsonUnpatched
Definition MqttMapper.h:130
static const std::string mappingJsonSchemaString
Definition MqttMapper.h:138
static void getMappedMessage(const std::string &topic, const std::string &message, uint8_t qoS, bool retain, double delay, MappedPublishes &mappedPublishes)
const nlohmann::json & getMapping() const
void getMappedTemplate(const nlohmann::json &templateMapping, nlohmann::json &json, MappedPublishes &mappedPublishes) const
std::tuple< std::vector< iot::mqtt::packets::Publish >, std::vector< ScheduledPublish > > MappedPublishes
Definition MqttMapper.h:83
inja::Environment * injaEnvironment
Definition MqttMapper.h:134
static void getStaticMappings(const nlohmann::json &staticMapping, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes)
static const std::string & getSchema()
static void getMappedMessage(const nlohmann::json &staticMapping, const iot::mqtt::packets::Publish &publish, MappedPublishes &mappedPublishes)
MqttMapper(const MqttMapper &)=delete
nlohmann::json findMatchingTopicLevel(const nlohmann::json &topicLevel, const std::string &topic) const
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