Blame view

include/mqtttypeconverter.h 4.43 KB
f45c31d8   Peter M. Groen   MOve headers to i...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
  /* ****************************************************************************
   * Copyright 2019 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.                                                  *
   * ***************************************************************************/
  #ifndef OSDEV_COMPONENTS_MQTT_MLOGICTYPECONVERTER_H
  #define OSDEV_COMPONENTS_MQTT_MLOGICTYPECONVERTER_H
  
  // std
  #include <chrono>
  #include <ctime>
  #include "date.h"
  #include <string>
  
  // boost
  #include <boost/uuid/uuid.hpp>
  
  #include "commondefs.h"
  
  namespace osdev {
  namespace components {
  namespace mqtt {
  
  /**
   * @brief Utility namespace to convert between mqtt common types and other frequently used types.
   */
  namespace MqttTypeConverter {
  
  /**
   * @brief Converts from MqttId to std::string.
   * @param mqttId The mqttId to convert.
   * @return The std::string with contents of the provided mqttId. Format is 12345678-9abc-def0-1234-56789abcdef0.
   */
  std::string toStdString(const MqttId& mqttId);
  
  /**
   * @brief Converts from system clock timepoint to std::string.
   * @tparam Duration std::chrono::duration instance.
   *         Duration::Period is used to determine the precision
   *         of the subsecond part of the returned ISO8601 string.
   *         Uses the duration of the StdTime type by default.
   * @param tp The timepoint to converter.
   * @return ISO8601 string representation of stdTime.
   */
  template <typename Duration>
  std::string toStdString(const std::chrono::time_point<std::chrono::system_clock, Duration>& tp)
  {
      return date::format("%FT%T%Ez", tp);
  }
  
  /**
   * @brief Converts from std::string to MqttId.
   * @param mqttId The MqttId string to convert.
   * @return the converted string to MqttId.
   */
  MqttId toMqttId(const std::string& mqttId);
  
  /**
   * @brief Creates a descriptive string based on the specified input parameters.
   * @param str The prefix of the string.
   * @param mqttId The id of which to use the first 4 characters.
   * @return str + "-" + <first 4 characters of mqttId>
   * Example: "Unassigned-a2c4".
   */
  std::string toShortGuidAppendedString(const std::string& str, const MqttId& mqttId);
  
  /**
   * @brief Converts from PosixTime (time_t) to StdTime.
   * @param posixTime The Posix Time (time_t) to convert.
   * @return The StdTime with same value as the provided posixTime.
   */
  StdTime toStdTime(const std::time_t& posixTime);
  
  /**
   * @brief Converts from StdTime to PosixTime (time_t).
   * @param stdTime The StdTime to convert.
   * @return The PosixTime with the same value as the provided stdTime.
   */
  time_t toPosixTime(const osdev::components::mqtt::StdTime& stdTime);
  
  /**
   * @brief Converts the specified posixTimeString to an OptionalTime.
   * @param posixTimeString A posix time as string.
   * @return The converted posixTimeString.
   * @retval boost::none if the specified posixTimeString could not be converted to a StdTime.
   */
  osdev::components::mqtt::OptionalTime toOptionalTime(const std::string& posixTimeString);
  
  }       // End namespace MqttTypeConverter
  }       // End namespace mqtt
  }       // End namespace components
  }       // End namespace osdev
  
  #endif  // OSDEV_COMPONENTS_MQTT_MQTTTYPECONVERTER_H