author | František Kučera <franta-hg@frantovo.cz> |
Sun, 08 May 2022 21:42:14 +0200 | |
branch | v_0 |
changeset 2 | 0799eaf338b9 |
parent 1 | 4993a084b8ba |
child 3 | 610783d70ae9 |
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 |
#include <cstdlib> |
|
18 |
#include <vector> |
|
19 |
#include <memory> |
|
20 |
#include <locale> |
|
21 |
#include <regex> |
|
22 |
#include <algorithm> |
|
23 |
#include <unistd.h> |
|
24 |
#include <sstream> |
|
25 |
#include <iomanip> |
|
2 | 26 |
#include <random> |
0 | 27 |
|
1
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
#include <mosquittopp.h> |
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
#include <relpipe/writer/RelationalWriter.h> |
31 |
#include <relpipe/writer/RelpipeWriterException.h> |
|
32 |
#include <relpipe/writer/AttributeMetadata.h> |
|
33 |
#include <relpipe/writer/Factory.h> |
|
34 |
#include <relpipe/writer/TypeId.h> |
|
35 |
||
36 |
#include <relpipe/cli/CLI.h> |
|
37 |
||
38 |
#include "MQTTCommand.h" |
|
39 |
#include "Hex.h" |
|
40 |
||
41 |
using namespace relpipe::cli; |
|
42 |
using namespace relpipe::writer; |
|
43 |
||
44 |
namespace relpipe { |
|
45 |
namespace in { |
|
46 |
namespace mqtt { |
|
47 |
||
2 | 48 |
class MQTTClient : public mosqpp::mosquittopp { |
49 |
private: |
|
50 |
std::shared_ptr<writer::RelationalWriter> writer; |
|
51 |
Configuration& configuration; |
|
52 |
int messageCount = 0; |
|
53 |
std::string clientId; |
|
0 | 54 |
|
2 | 55 |
/** |
56 |
* @return unique (random) client ID for MQTT to allow multiple simultaneous connections |
|
57 |
*/ |
|
58 |
static std::string generateClientID() { |
|
59 |
std::stringstream result; |
|
60 |
// result << "relpipe-in-"; |
|
61 |
std::string symbols("0123456789abcdef"); |
|
62 |
||
63 |
std::random_device dev; |
|
64 |
std::mt19937 rng(dev()); |
|
65 |
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, symbols.size()); |
|
66 |
||
67 |
for (int i = 0; i < 8; i++) result << symbols[dist6(rng)]; |
|
68 |
||
69 |
// std::cerr << "generated clien ID = " << result.str() << std::endl; |
|
70 |
return result.str(); |
|
71 |
} |
|
72 |
public: |
|
73 |
||
74 |
MQTTClient(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : mosqpp::mosquittopp((generateClientID()).c_str()), writer(writer), configuration(configuration) { |
|
1
4993a084b8ba
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
} |
0 | 76 |
|
2 | 77 |
void on_message(const mosquitto_message* message) override { |
78 |
// std::cerr << "got MQTT message: length=" << message->payloadlen << std::endl; |
|
79 |
std::string payload = std::string((const char*) message->payload, message->payloadlen); |
|
80 |
writer->writeAttribute(configuration.stream); |
|
81 |
writer->writeAttribute(Hex::toTxt(payload)); |
|
82 |
writer->writeAttribute(Hex::toHex(payload)); |
|
83 |
messageCount++; |
|
84 |
} |
|
85 |
||
86 |
int popMessageCount() { |
|
87 |
int count = messageCount; |
|
88 |
messageCount = 0; |
|
89 |
return count; |
|
90 |
} |
|
91 |
||
92 |
}; |
|
93 |
||
94 |
void MQTTCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) { |
|
95 |
std::shared_ptr<MQTTClient> mq = std::make_shared<MQTTClient>(writer, configuration); |
|
96 |
||
0 | 97 |
writer->startRelation(configuration.relation,{ |
2 | 98 |
{L"stream", TypeId::STRING}, |
0 | 99 |
{L"text", TypeId::STRING}, |
100 |
{L"data", TypeId::STRING} |
|
101 |
}, true); |
|
102 |
||
2 | 103 |
mq->max_inflight_messages_set(1); |
104 |
mq->connect("localhost", 1883); |
|
105 |
int mid; |
|
106 |
mq->subscribe(&mid, convertor.to_bytes(configuration.stream).c_str()); |
|
0 | 107 |
|
2 | 108 |
//for (int i = configuration.messageCount; continueProcessing && i > 0; i--) { |
109 |
for (int i = configuration.messageCount; continueProcessing && i > 0; i = i - mq->popMessageCount()) { |
|
110 |
// std::cerr << "loop(): i=" << i << std::endl; |
|
111 |
||
112 |
//mq->loop(); |
|
113 |
mq->loop(1000, 1); |
|
114 |
//mq->loop(1000, -1); |
|
115 |
//mq->loop_forever(); |
|
116 |
//mq->loop_write(); |
|
0 | 117 |
} |
118 |
||
2 | 119 |
// FIXME: move do destructor |
120 |
mq->disconnect(); |
|
0 | 121 |
} |
122 |
||
123 |
MQTTCommand::~MQTTCommand() { |
|
124 |
} |
|
125 |
||
126 |
} |
|
127 |
} |
|
128 |
} |