src/MQTTCommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 May 2022 21:42:14 +0200
branchv_0
changeset 2 0799eaf338b9
parent 1 4993a084b8ba
child 3 610783d70ae9
permissions -rw-r--r--
first version

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <cstdlib>
#include <vector>
#include <memory>
#include <locale>
#include <regex>
#include <algorithm>
#include <unistd.h>
#include <sstream>
#include <iomanip>
#include <random>

#include <mosquittopp.h>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>

#include "MQTTCommand.h"
#include "Hex.h"

using namespace relpipe::cli;
using namespace relpipe::writer;

namespace relpipe {
namespace in {
namespace mqtt {

class MQTTClient : public mosqpp::mosquittopp {
private:
	std::shared_ptr<writer::RelationalWriter> writer;
	Configuration& configuration;
	int messageCount = 0;
	std::string clientId;

	/**
	 * @return unique (random) client ID for MQTT to allow multiple simultaneous connections
	 */
	static std::string generateClientID() {
		std::stringstream result;
		// result << "relpipe-in-";
		std::string symbols("0123456789abcdef");

		std::random_device dev;
		std::mt19937 rng(dev());
		std::uniform_int_distribution<std::mt19937::result_type> dist6(0, symbols.size());

		for (int i = 0; i < 8; i++) result << symbols[dist6(rng)];

		// std::cerr << "generated clien ID = " << result.str() << std::endl;
		return result.str();
	}
public:

	MQTTClient(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : mosqpp::mosquittopp((generateClientID()).c_str()), writer(writer), configuration(configuration) {
	}

	void on_message(const mosquitto_message* message) override {
		// std::cerr << "got MQTT message: length=" << message->payloadlen << std::endl;
		std::string payload = std::string((const char*) message->payload, message->payloadlen);
		writer->writeAttribute(configuration.stream);
		writer->writeAttribute(Hex::toTxt(payload));
		writer->writeAttribute(Hex::toHex(payload));
		messageCount++;
	}

	int popMessageCount() {
		int count = messageCount;
		messageCount = 0;
		return count;
	}

};

void MQTTCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
	std::shared_ptr<MQTTClient> mq = std::make_shared<MQTTClient>(writer, configuration);

	writer->startRelation(configuration.relation,{
		{L"stream", TypeId::STRING},
		{L"text", TypeId::STRING},
		{L"data", TypeId::STRING}
	}, true);

	mq->max_inflight_messages_set(1);
	mq->connect("localhost", 1883);
	int mid;
	mq->subscribe(&mid, convertor.to_bytes(configuration.stream).c_str());

	//for (int i = configuration.messageCount; continueProcessing && i > 0; i--) {
	for (int i = configuration.messageCount; continueProcessing && i > 0; i = i - mq->popMessageCount()) {
		// std::cerr << "loop(): i=" << i << std::endl;

		//mq->loop();
		mq->loop(1000, 1);
		//mq->loop(1000, -1);
		//mq->loop_forever();
		//mq->loop_write();
	}

	// FIXME: move do destructor
	mq->disconnect();
}

MQTTCommand::~MQTTCommand() {
}

}
}
}