Blame view

tools/publish_cli/main.cpp 3.87 KB
6a79aebe   Peter M. Groen   setting up the pu...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /* ****************************************************************************
   * Copyright 2024 Open Systems Development BV                                 *
   *                                                                            *
   * Permission is hereby granted, free of charge, to any person obtaining a    *
   * copy of this software and associated documentation files (the "Software"), *
   * to deal in the Software without restriction, including without limitation  *
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
   * and/or sell copies of the Software, and to permit persons to whom the      *
   * Software is furnished to do so, subject to the following conditions:       *
   *                                                                            *
   * The above copyright notice and this permission notice shall be included in *
   * all copies or substantial portions of the Software.                        *
   *                                                                            *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
   * DEALINGS IN THE SOFTWARE.                                                  *
   * ***************************************************************************/
  #include <iostream>
  
bf715122   Peter M. Groen   Added CLI Tools
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include "argumentparser.h"
  #include "filereader.h"
  #include "publisher.h"
  
  enum TIME_RES
  {
      T_MICRO,
      T_MILLI,
      T_SECONDS
  };
  
  std::uint64_t getEpochUSecs()
  {
      auto tsUSec =std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
      return static_cast<std::uint64_t>(tsUSec.time_since_epoch().count());
  }
  
  
  void sleepcp( int number, TIME_RES resolution = T_MILLI )    // Cross-platform sleep function
  {
      int factor = 0; // Should not happen..
  
      switch( resolution )
      {
          case T_MICRO:
              factor = 1;
              break;
  
          case T_MILLI:
              factor = 1000;
              break;
  
          case T_SECONDS:
              factor = 1000000;
          break;
      }
  
      usleep( number * factor );
  }
  
6a79aebe   Peter M. Groen   setting up the pu...
64
65
  int main(int argc, char* argv[])
  {
bf715122   Peter M. Groen   Added CLI Tools
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      ArgumentParser parser(argc, argv);
  
      std::string host = parser.getHost();
      std::string port = parser.getPort();
      std::string user = parser.getUserName();
      std::string pass = parser.getPassword();
      std::string topic = parser.getTopic();
      std::string file = parser.getFile();
      std::string message = parser.getMessage();
  
      if(!file.empty() && !message.empty())
      {
          std::cerr << "Error: Cannot specify both file and message" << std::endl;
          return -1;
      }
  
      if(file.empty() && message.empty())
      {
          std::cerr << "Error: Must specify either file or message" << std::endl;
          return -1;
      }
  
      std::string content = std::string();
      if(!file.empty())
      {
          content = FileReader::read_file(file);
          if(content.empty())
          {
              std::cerr << "Error: File is empty" << std::endl;
              return -1;
          }
      }
  
      if(!message.empty())
      {
          content = message;
      }
  
      // Ok, we have all the options, lets check the mandatory ones
      if(topic.empty())
      {
          std::cerr << "Error: Must specify a topic" << std::endl;
          return -1;
      }
  
      // Create the MQTT client
      Publisher oPublisher;
      oPublisher.connect(host, std::stoi(port), user, pass);
  
      // Wait a couple of seconds to make sure the connection is established
      sleepcp(2, T_SECONDS);
  
      oPublisher.publish(topic, content);
      sleepcp(2, T_SECONDS);
  
6a79aebe   Peter M. Groen   setting up the pu...
121
122
      return 0;
  }