From 38bc0369e74ff822c35ba0c92f0b9acd2e22a209 Mon Sep 17 00:00:00 2001 From: Dennis Eisold <de@itstall.de> Date: Tue, 28 Jan 2020 05:18:32 +0100 Subject: [PATCH] Working again --- .gitignore | 6 +-- MqttClient.h | 118 ++++++++++++++++++++++++------------------------ compatibility.h | 12 ++--- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index 3ea3afb..7f142c5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,6 @@ ################################################################################ /.vs -/Debug -/x64/Release -/Release +/Debug +/x64/Release +/Release diff --git a/MqttClient.h b/MqttClient.h index 423413a..e30bcfb 100644 --- a/MqttClient.h +++ b/MqttClient.h @@ -1,60 +1,60 @@ -#pragma once - -#include <mosquittopp.h> -#include <cstring> -#include <cstdio> - -#define MAX_PAYLOAD 50 -#define DEFAULT_KEEP_ALIVE 60 -#define PUBLISH_TOPIC "EXAMPLE_TOPIC" - -class MqttClient : public mosqpp::mosquittopp { -public: - MqttClient(const char* id, const char* host, int port) : mosquittopp(id) { - int keepalive = DEFAULT_KEEP_ALIVE; - connect(host, port, keepalive); - } - - ~MqttClient() {} - - void on_connect(int rc) { - if (!rc) std::cout << "Connected - code " << rc << std::endl; - } - - void on_subscribe(int mid, int qos_count, const int* granted_qos){ - std::cout << "Subscription succeeded." << std::endl; - } - - void on_message(const struct mosquitto_message* message) { - int payload_size = MAX_PAYLOAD + 1; - char buf[payload_size]; - - if (!strcmp(message->topic, PUBLISH_TOPIC)) { - memset(buf, 0, payload_size * sizeof(char)); - - /* Copy N-1 bytes to ensure always 0 terminated. */ - memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char)); - - std::cout << buf << std::endl; - - // Examples of messages for M2M communications... - if (!strcmp(buf, "STATUS")) { - snprintf(buf, payload_size, "This is a Status Message..."); - publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); - std::cout << "Status Request Recieved." << std::endl; - } - - if (!strcmp(buf, "ON")) { - snprintf(buf, payload_size, "Turning on..."); - publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); - std::cout << "Request to turn on." << std::endl; - } - - if (!strcmp(buf, "OFF")) { - snprintf(buf, payload_size, "Turning off..."); - publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); - std::cout << "Request to turn off." << std::endl; - } - } - } +#pragma once + +#include <mosquittopp.h> +#include <cstring> +#include <cstdio> + +#define MAX_PAYLOAD 50 +#define DEFAULT_KEEP_ALIVE 60 +#define PUBLISH_TOPIC "EXAMPLE_TOPIC" + +class MqttClient : public mosqpp::mosquittopp { +public: + MqttClient(const char* id, const char* host, int port) : mosquittopp(id) { + int keepalive = DEFAULT_KEEP_ALIVE; + connect(host, port, keepalive); + } + + ~MqttClient() {} + + void on_connect(int rc) { + if (!rc) std::cout << "Connected - code " << rc << std::endl; + } + + void on_subscribe(int mid, int qos_count, const int* granted_qos){ + std::cout << "Subscription succeeded." << std::endl; + } + + void on_message(const struct mosquitto_message* message) { + int payload_size = MAX_PAYLOAD + 1; + char buf[payload_size]; + + if (!strcmp(message->topic, PUBLISH_TOPIC)) { + memset(buf, 0, payload_size * sizeof(char)); + + /* Copy N-1 bytes to ensure always 0 terminated. */ + memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char)); + + std::cout << buf << std::endl; + + // Examples of messages for M2M communications... + if (!strcmp(buf, "STATUS")) { + snprintf(buf, payload_size, "This is a Status Message..."); + publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); + std::cout << "Status Request Recieved." << std::endl; + } + + if (!strcmp(buf, "ON")) { + snprintf(buf, payload_size, "Turning on..."); + publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); + std::cout << "Request to turn on." << std::endl; + } + + if (!strcmp(buf, "OFF")) { + snprintf(buf, payload_size, "Turning off..."); + publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); + std::cout << "Request to turn off." << std::endl; + } + } + } } \ No newline at end of file diff --git a/compatibility.h b/compatibility.h index 2b9134a..e776f42 100644 --- a/compatibility.h +++ b/compatibility.h @@ -1,7 +1,7 @@ -void csleep(int time) { -#if defined(WIN32) || defined(WIN64) - Sleep(time); -#else - usleep((long) time); -#endif +void csleep(int time) { +#if defined(WIN32) || defined(WIN64) + Sleep(time); +#else + usleep((long) time); +#endif } \ No newline at end of file -- GitLab