Blame view

test/helperclasses/PublisherClass.h 1.62 KB
5cd80369   Peter M. Groen   Setting up Sledge...
1
2
3
4
5
6
7
  /****************************************************************************
   * Copyright (c)2024 Open Systems Development B.V.
   ****************************************************************************/
  
  #include "mqttclient.h"
  
  #include <string>
5cd80369   Peter M. Groen   Setting up Sledge...
8
9
10
11
  
  using namespace osdev::components::mqtt;
  using namespace osdev::components::log;
  
5cd80369   Peter M. Groen   Setting up Sledge...
12
13
14
  class Publisher
  {
  public:
b7b0db4b   Peter M. Groen   Added tests
15
      Publisher(const std::string &unique_id) : m_mqtt_client("SledgeHammerTest" + unique_id){}
5cd80369   Peter M. Groen   Setting up Sledge...
16
17
      virtual ~Publisher() {}
  
b7b0db4b   Peter M. Groen   Added tests
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      void connect( const std::string &hostname,
                    int portnumber = 1883,
                    const std::string &username = std::string(),
                    const std::string &password = std::string(),
                    const std::string &lwt_topic = std::string(),
                    const std::string &lwt_message = std::string()
                  )
      {
          m_mqtt_client.connect(hostname, portnumber,
                                Credentials(username, password),
                                mqtt_LWT(lwt_topic, lwt_message),
                                true,
                                LogSettings
                                {
                                      LogLevel::Debug,
                                      LogMask::None
                                });
      }
  
      void disconnect()
      {
          m_mqtt_client.disconnect();
      }
  
      void publish(const std::string &message_topic, const std::string &message_payload)
      {
          MqttMessage message(message_topic, true, false, message_payload);
          Token t_result = m_mqtt_client.publish(message, 0);
      }
  
  private:
      osdev::components::mqtt::MqttClient     m_mqtt_client;
5cd80369   Peter M. Groen   Setting up Sledge...
50
  };