author | František Kučera <franta-hg@frantovo.cz> |
Fri, 06 May 2022 23:06:44 +0200 | |
branch | v_0 |
changeset 1 | cb9577442d3b |
parent 0 | 7ef5ce9477c8 |
child 2 | 1a0fbd17db13 |
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> |
|
26 |
||
1
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
#include <mosquittopp.h> |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
|
0 | 29 |
#include <relpipe/common/type/typedefs.h> |
30 |
#include <relpipe/reader/TypeId.h> |
|
31 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
32 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
33 |
||
34 |
#include "Configuration.h" |
|
35 |
#include "Hex.h" |
|
36 |
||
37 |
namespace relpipe { |
|
38 |
namespace out { |
|
39 |
namespace mqtt { |
|
40 |
||
41 |
class MQTTHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
42 |
private: |
|
43 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
44 |
Configuration configuration; |
|
45 |
||
46 |
struct CurrentRelation { |
|
47 |
relpipe::common::type::StringX name; |
|
48 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
49 |
relpipe::common::type::Integer attributeIndex = 0; |
|
50 |
std::string currentValue; |
|
51 |
} currentRelation; |
|
52 |
||
53 |
public: |
|
54 |
||
55 |
MQTTHandler(Configuration configuration) : configuration(configuration) { |
|
1
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
|
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
{ |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
// TODO: remove |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
int major, minor, patch; |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
mosqpp::lib_version(&major, &minor, &patch); |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
std::cerr << "mosquitto version: " << major << "." << minor << "." << patch << std::endl; |
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
} |
0 | 63 |
} |
64 |
||
65 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
66 |
currentRelation = CurrentRelation{name, attributes}; |
|
67 |
} |
|
68 |
||
69 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
70 |
||
71 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
|
72 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value); |
|
73 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str(); |
|
74 |
else if (attributeName == L"text"); // keep empty or value from 'data' |
|
75 |
else if (attributeName == L"data"); // keep empty or value from 'text' |
|
76 |
||
77 |
currentRelation.attributeIndex++; |
|
78 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
|
79 |
currentRelation.attributeIndex = 0; |
|
1
cb9577442d3b
link to mosquittopp library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
// FIXME: send the message |
0 | 81 |
} |
82 |
||
83 |
} |
|
84 |
||
85 |
void endOfPipe() { |
|
86 |
||
87 |
} |
|
88 |
||
89 |
}; |
|
90 |
||
91 |
} |
|
92 |
} |
|
93 |
} |