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