author | František Kučera <franta-hg@frantovo.cz> |
Sun, 08 May 2022 21:42:25 +0200 | |
branch | v_0 |
changeset 2 | 1a0fbd17db13 |
parent 1 | cb9577442d3b |
child 3 | 62ced7c41c67 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
2 | 26 |
#include <random> |
0 | 27 |
|
1
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
#include <mosquittopp.h> |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
#include <relpipe/common/type/typedefs.h> |
31 |
#include <relpipe/reader/TypeId.h> |
|
32 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
33 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
34 |
||
35 |
#include "Configuration.h" |
|
36 |
#include "Hex.h" |
|
37 |
||
38 |
namespace relpipe { |
|
39 |
namespace out { |
|
40 |
namespace mqtt { |
|
41 |
||
42 |
class MQTTHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
43 |
private: |
|
44 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
45 |
Configuration configuration; |
|
2 | 46 |
std::shared_ptr<mosqpp::mosquittopp> mq = std::make_shared<mosqpp::mosquittopp>(generateClientID().c_str()); |
47 |
||
48 |
/** |
|
49 |
* @return unique (random) client ID for MQTT to allow multiple simultaneous connections |
|
50 |
*/ |
|
51 |
static std::string generateClientID() { |
|
52 |
std::stringstream result; |
|
53 |
// result << "relpipe-out-"; |
|
54 |
std::string symbols("0123456789abcdef"); |
|
55 |
||
56 |
std::random_device dev; |
|
57 |
std::mt19937 rng(dev()); |
|
58 |
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, symbols.size()); |
|
59 |
||
60 |
for (int i = 0; i < 8; i++) result << symbols[dist6(rng)]; |
|
61 |
||
62 |
// std::cerr << "generated clien ID = " << result.str() << std::endl; |
|
63 |
return result.str(); |
|
64 |
} |
|
0 | 65 |
|
66 |
struct CurrentRelation { |
|
67 |
relpipe::common::type::StringX name; |
|
68 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
69 |
relpipe::common::type::Integer attributeIndex = 0; |
|
70 |
std::string currentValue; |
|
71 |
} currentRelation; |
|
72 |
||
73 |
public: |
|
74 |
||
75 |
MQTTHandler(Configuration configuration) : configuration(configuration) { |
|
2 | 76 |
mq->connect("localhost", 1883); |
0 | 77 |
} |
78 |
||
79 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
2 | 80 |
// TODO: check relation name according to the configuration |
0 | 81 |
currentRelation = CurrentRelation{name, attributes}; |
82 |
} |
|
83 |
||
84 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
85 |
||
86 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
|
87 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value); |
|
88 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str(); |
|
89 |
else if (attributeName == L"text"); // keep empty or value from 'data' |
|
90 |
else if (attributeName == L"data"); // keep empty or value from 'text' |
|
91 |
||
92 |
currentRelation.attributeIndex++; |
|
93 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
|
94 |
currentRelation.attributeIndex = 0; |
|
2 | 95 |
int mid = -1; |
96 |
mq->publish(&mid, convertor.to_bytes(configuration.stream).c_str(), currentRelation.currentValue.size(), currentRelation.currentValue.c_str()); |
|
97 |
// std::cerr << "MQTT message enqueued: " << mid << std::endl; |
|
0 | 98 |
} |
99 |
||
100 |
} |
|
101 |
||
102 |
void endOfPipe() { |
|
2 | 103 |
mq->disconnect(); |
0 | 104 |
} |
105 |
||
106 |
}; |
|
107 |
||
108 |
} |
|
109 |
} |
|
110 |
} |