Commit c55eb07aa154fc26cf3a04991af9bf82a8369d38
1 parent
51ef1a6f
Added ControlMessage tool
Showing
6 changed files
with
373 additions
and
0 deletions
examples/ctrlmsg/CMakeLists.txt
0 โ 100644
| 1 | +cmake_minimum_required(VERSION 3.0) | |
| 2 | +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) | |
| 3 | + | |
| 4 | +include(projectheader) | |
| 5 | +project_header(ctrlmsg) | |
| 6 | + | |
| 7 | +include_directories( SYSTEM | |
| 8 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../include | |
| 9 | +) | |
| 10 | + | |
| 11 | +include(compiler) | |
| 12 | +set(SRC_LIST | |
| 13 | + ${CMAKE_CURRENT_SOURCE_DIR}/subscriber.h | |
| 14 | + ${CMAKE_CURRENT_SOURCE_DIR}/subscriber.cpp | |
| 15 | + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.cpp | |
| 16 | + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.h | |
| 17 | + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp | |
| 18 | +) | |
| 19 | + | |
| 20 | +add_executable( ${PROJECT_NAME} | |
| 21 | + ${SRC_LIST} | |
| 22 | +) | |
| 23 | + | |
| 24 | +target_link_libraries( | |
| 25 | + ${PROJECT_NAME} | |
| 26 | + mqtt-cpp | |
| 27 | +) | |
| 28 | + | |
| 29 | +set_target_properties( ${PROJECT_NAME} PROPERTIES | |
| 30 | + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin | |
| 31 | + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib | |
| 32 | + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib | |
| 33 | +) | |
| 34 | + | |
| 35 | +include(installation) | |
| 36 | +install_application() | ... | ... |
examples/ctrlmsg/main.cpp
0 โ 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2019 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | + | |
| 23 | +// std | |
| 24 | +#include <iostream> | |
| 25 | +#include <unistd.h> | |
| 26 | +#include <chrono> | |
| 27 | +#include <vector> | |
| 28 | + | |
| 29 | +#include "publisher.h" | |
| 30 | +#include "subscriber.h" | |
| 31 | + | |
| 32 | +enum TIME_RES | |
| 33 | +{ | |
| 34 | + T_MICRO, | |
| 35 | + T_MILLI, | |
| 36 | + T_SECONDS | |
| 37 | +}; | |
| 38 | + | |
| 39 | +std::vector<std::string> positionTable = { | |
| 40 | + "1", "99", "2", "98", "3", "97", "4", "96", "5", "95", | |
| 41 | + "6", "94", "7", "93", "8", "92", "9", "91", | |
| 42 | + "10", "90", "11", "89", "12", "88", "13", "87", "14", "86", | |
| 43 | + "15", "85", "16", "84", "17", "83", "18", "82", "19", "81", | |
| 44 | + "20", "80", "21", "79", "22", "78", "23", "77", "24", "76", | |
| 45 | + "25", "75", "26", "74", "27", "73", "28", "72", "29", "71", | |
| 46 | + "30", "70", "31", "69", "32", "68", "33", "67", "34", "66", | |
| 47 | + "35", "65", "36", "64", "37", "63", "38", "62", "39", "61", | |
| 48 | + "40", "60", "41", "59", "42", "58", "43", "57", "44", "56", | |
| 49 | + "45", "55", "46", "54", "47", "53", "48", "52", "49", "51", | |
| 50 | + "50", "50", "51", "49", "52", "48", "53", "47", "54", "46", | |
| 51 | + "55", "45", "56", "44", "57", "43", "58", "42", "59", "41", | |
| 52 | + "60", "40", "61", "39", "62", "38", "63", "37", "64", "36", | |
| 53 | + "65", "35", "66", "34", "67", "33", "68", "32", "69", "31", | |
| 54 | + "70", "30", "71", "29", "72", "28", "73", "27", "74", "26", | |
| 55 | + "75", "25", "76", "24", "77", "23", "78", "22", "79", "21", | |
| 56 | + "80", "20", "81", "19", "82", "18", "83", "17", "84", "16", | |
| 57 | + "85", "15", "86", "14", "87", "13", "88", "12", "89", "11", | |
| 58 | + "90", "10", "91", "9", "92", "8", "93", "7", "94", "6", | |
| 59 | + "95", "5", "96", "4", "97", "3", "98", "2", "99", "1", | |
| 60 | + "10", "90", "20", "80", "30", "70", "40", "60", "50", "50", | |
| 61 | + "60", "40", "70", "30", "80", "20", "90", "10" | |
| 62 | +}; | |
| 63 | + | |
| 64 | +std::uint64_t getEpochUSecs() | |
| 65 | +{ | |
| 66 | + auto tsUSec =std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()); | |
| 67 | + return static_cast<std::uint64_t>(tsUSec.time_since_epoch().count()); | |
| 68 | +} | |
| 69 | + | |
| 70 | + | |
| 71 | +void sleepcp( int number, TIME_RES resolution = T_MILLI ) // Cross-platform sleep function | |
| 72 | +{ | |
| 73 | + int factor = 0; // Should not happen.. | |
| 74 | + | |
| 75 | + switch( resolution ) | |
| 76 | + { | |
| 77 | + case T_MICRO: | |
| 78 | + factor = 1; | |
| 79 | + break; | |
| 80 | + | |
| 81 | + case T_MILLI: | |
| 82 | + factor = 1000; | |
| 83 | + break; | |
| 84 | + | |
| 85 | + case T_SECONDS: | |
| 86 | + factor = 1000000; | |
| 87 | + break; | |
| 88 | + } | |
| 89 | + | |
| 90 | + usleep( number * factor ); | |
| 91 | +} | |
| 92 | + | |
| 93 | +int main( int argc, char* argv[] ) | |
| 94 | +{ | |
| 95 | + // We're not using the command parameters, so we just want to keep the compiler happy. | |
| 96 | + (void)argc; | |
| 97 | + (void)argv; | |
| 98 | + | |
| 99 | + | |
| 100 | + // Create the publisher, run it and publish a control message every 35 sec. | |
| 101 | + int publishInterval = 30; | |
| 102 | + std::string ld_topic = "priva/0/devices/actuators/geiger-lamel-1"; | |
| 103 | + | |
| 104 | + std::cout << "Create the subscriber object : "; | |
| 105 | + Subscriber *pSubscriber = new Subscriber( "TestSubscriber" ); | |
| 106 | + if( pSubscriber != nullptr ) | |
| 107 | + { | |
| 108 | + std::cout << "{OK}" << std::endl; | |
| 109 | + std::cout << "Connecting to the broker : "; | |
| 110 | + pSubscriber->connect( "192.168.1.110", 1883, "", "", "LWT-subscriber", "connection disrupted.." ); | |
| 111 | + pSubscriber->subscribe( ld_topic ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + std::cout << "Create the publisher object : "; | |
| 115 | + Publisher *pPublisher = new Publisher(); | |
| 116 | + if( pPublisher != nullptr ) | |
| 117 | + { | |
| 118 | + std::cout << "{OK}" << std::endl; | |
| 119 | + std::cout << "Connecting to the broker : "; | |
| 120 | + pPublisher->connect( "192.168.1.110", 1883, "", "", "LWT-publisher", "connection disrupted.." ); | |
| 121 | + | |
| 122 | + // Assume we are connected now, start publishing. | |
| 123 | + while( 1 ) | |
| 124 | + { | |
| 125 | + // Follow different patterns with bigger jumps. | |
| 126 | + for( unsigned int nCount = 0; nCount < positionTable.size(); nCount++ ) | |
| 127 | + { | |
| 128 | + std::cout << "==============================================================================" << std::endl; | |
| 129 | + std::string payload = "{\"version\": 1, \"id\": \"geiger-lamel-1\", \"requesterId\": \"me\", \"instruction\": \"SETPOINT\", \"desiredValue\": " + positionTable[nCount] + ", \"requestedAt\": " + std::to_string( getEpochUSecs() ) + "}"; | |
| 130 | + pPublisher->publish( std::string( "priva/1100000916/devices/actuators/geiger-lamel-1/control"), payload ); | |
| 131 | + sleepcp(publishInterval, T_SECONDS); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + } | |
| 135 | + else | |
| 136 | + return -1; | |
| 137 | + | |
| 138 | +} | ... | ... |
examples/ctrlmsg/publisher.cpp
0 โ 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2019 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | + | |
| 23 | +// mqtt_tests | |
| 24 | +#include "publisher.h" | |
| 25 | + | |
| 26 | +Publisher::Publisher() | |
| 27 | + : m_mqtt_client( "TestPublisher" ) | |
| 28 | +{ | |
| 29 | + | |
| 30 | +} | |
| 31 | + | |
| 32 | +void Publisher::connect(const std::string &hostname, int portnumber, const std::string &username, const std::string &password, const std::string &lwt_topic, const std::string &lwt_message) | |
| 33 | +{ | |
| 34 | + m_mqtt_client.connect( hostname, portnumber, | |
| 35 | + osdev::components::mqtt::Credentials( username, password ), | |
| 36 | + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true, | |
| 37 | + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } ); | |
| 38 | + | |
| 39 | + std::cout << "Client state : " << m_mqtt_client.state() << std::endl; | |
| 40 | +} | |
| 41 | + | |
| 42 | +void Publisher::publish( const std::string &message_topic, const std::string &message_payload ) | |
| 43 | +{ | |
| 44 | + osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload ); | |
| 45 | + std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl; | |
| 46 | + osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 ); | |
| 47 | +} | ... | ... |
examples/ctrlmsg/publisher.h
0 โ 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2019 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#pragma once | |
| 23 | + | |
| 24 | +// std | |
| 25 | +#include <memory> | |
| 26 | +#include <string> | |
| 27 | + | |
| 28 | +// osdev::components::mqtt | |
| 29 | +#include "mqttclient.h" | |
| 30 | +#include "compat-c++14.h" | |
| 31 | + | |
| 32 | +class Publisher | |
| 33 | +{ | |
| 34 | +public: | |
| 35 | + Publisher(); | |
| 36 | + | |
| 37 | + virtual ~Publisher() {} | |
| 38 | + | |
| 39 | + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string() | |
| 40 | + , const std::string &lwt_topic = std::string(), const std::string &lwt_message = std::string() ); | |
| 41 | + | |
| 42 | + void publish( const std::string &message_topic, const std::string &message_payload ); | |
| 43 | + | |
| 44 | +private: | |
| 45 | + osdev::components::mqtt::MqttClient m_mqtt_client; | |
| 46 | +}; | ... | ... |
examples/ctrlmsg/subscriber.cpp
0 โ 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2019 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#include "subscriber.h" | |
| 23 | + | |
| 24 | +#include <iostream> | |
| 25 | + | |
| 26 | +Subscriber::Subscriber( const std::string &client_id ) | |
| 27 | + : m_mqtt_client( client_id ) | |
| 28 | +{ | |
| 29 | + | |
| 30 | +} | |
| 31 | + | |
| 32 | +void Subscriber::connect( const std::string &hostname, int portnumber, const std::string &username, const std::string &password, const std::string &lwt_topic, const std::string &lwt_message ) | |
| 33 | +{ | |
| 34 | + m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) ); | |
| 35 | + std::cout << "Client state : " << m_mqtt_client.state() << std::endl; | |
| 36 | +} | |
| 37 | + | |
| 38 | +void Subscriber::subscribe( const std::string &message_topic ) | |
| 39 | +{ | |
| 40 | + m_mqtt_client.subscribe( message_topic, 1, [this](const osdev::components::mqtt::MqttMessage &message ) | |
| 41 | + { | |
| 42 | + this->receive_data( message.topic(), message.payload() ); | |
| 43 | + }); | |
| 44 | +} | |
| 45 | + | |
| 46 | +void Subscriber::unsubscribe( const std::string &message_topic ) | |
| 47 | +{ | |
| 48 | + m_mqtt_client.unsubscribe( message_topic, 1 ); | |
| 49 | +} | |
| 50 | + | |
| 51 | +void Subscriber::receive_data( const std::string &message_topic, const std::string &message_payload ) | |
| 52 | +{ | |
| 53 | + std::cout << "[Subscriber::receive_data] - Received message : " << message_payload << " from topic : " << message_topic << std::endl; | |
| 54 | +} | ... | ... |
examples/ctrlmsg/subscriber.h
0 โ 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2019 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#pragma once | |
| 23 | + | |
| 24 | +// std | |
| 25 | +#include <memory> | |
| 26 | +#include <string> | |
| 27 | + | |
| 28 | +// mqtt-cpp | |
| 29 | +#include "mqttclient.h" | |
| 30 | +#include "compat-c++14.h" | |
| 31 | + | |
| 32 | +class Subscriber | |
| 33 | +{ | |
| 34 | +public: | |
| 35 | + Subscriber(const std::string &client_id); | |
| 36 | + | |
| 37 | + virtual ~Subscriber() {} | |
| 38 | + | |
| 39 | + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string(), | |
| 40 | + const std::string &lwt_topic = std::string(), const std::string &lwt_message = std::string() ); | |
| 41 | + | |
| 42 | + void subscribe( const std::string &message_topic ); | |
| 43 | + | |
| 44 | + void unsubscribe( const std::string &message_topic ); | |
| 45 | + | |
| 46 | +protected: | |
| 47 | + void receive_data( const std::string &message_topic, const std::string &message_payload ); | |
| 48 | + | |
| 49 | +private: | |
| 50 | + osdev::components::mqtt::MqttClient m_mqtt_client; | |
| 51 | + | |
| 52 | +}; | ... | ... |