src/MQTTHandler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 06 May 2022 21:54:32 +0200
branchv_0
changeset 0 7ef5ce9477c8
child 1 cb9577442d3b
permissions -rw-r--r--
establish project
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>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "MQTT.h"
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Configuration.h"
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Hex.h"
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace mqtt {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
class MQTTHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	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
    43
	Configuration configuration;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	shared_ptr<MQTT> mq;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	struct CurrentRelation {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		relpipe::common::type::StringX name;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		relpipe::common::type::Integer attributeIndex = 0;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::string currentValue;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	} currentRelation;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
public:
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	MQTTHandler(Configuration configuration) : configuration(configuration) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		// TODO: do not throw exception from the constructor: MQTT::open()
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		mq.reset(MQTT::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		currentRelation = CurrentRelation{name, attributes};
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	void attribute(const relpipe::common::type::StringX& value) override {
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
		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		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
    68
		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
    69
		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
    70
		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
    71
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		currentRelation.attributeIndex++;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			currentRelation.attributeIndex = 0;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			mq->send(currentRelation.currentValue);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		}
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
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	void endOfPipe() {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
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
};
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
}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}