YAODAQ
Message.hpp
1 #pragma once
2 
3 #include "States.hpp"
4 #include "json/json.h"
5 
6 #include <memory>
7 #include <set>
8 
9 enum class Types
10 {
11  Trace,
12  Info,
13  Debug,
14  Warning,
15  Critical,
16  Error,
17  State,
18  Action,
19  Command,
20  Response,
21  Data,
22  Unknow,
23 };
24 
25 enum class Actions
26 {
27  INITIALIZE,
28  CONNECT,
29  CONFIGURE,
30  START,
31  PAUSE,
32  STOP,
33  CLEAR,
34  DISCONNECT,
35  RELEASE,
36  QUIT
37 };
38 
39 class Message
40 {
41 public:
42  explicit Message(const Types& type = Types::Info, const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
43  explicit Message(const Types& type, const Json::Value& content, const std::string& to = "ALL", const std::string& from = "");
44  void parse(const std::string&);
45  void setFrom(const std::string&);
46  void setTo(const std::string&);
47  void addKey(const std::string& key, const std::string& value);
48  void setContent(const std::string&);
49  void setContent(const Json::Value&);
50  std::string getFrom();
51  std::string getTo();
52  std::string getContent();
53  std::string getContent() const;
54  std::string get();
55  std::string get() const;
56  std::string getType();
57  std::string getStyled(const std::string& indent = "\t");
58  void setType(const Types&);
59  bool isEmpty();
60  std::size_t getContentSize();
61  Json::Value getContentAsJson() const;
62  Json::Value getContentAsJson();
63 
64 protected:
65  Json::Value m_Value{};
66 
67 private:
68  static Json::StreamWriterBuilder m_StreamWriterBuilder;
69  std::unique_ptr<Json::StreamWriter> m_Writer{nullptr};
70  std::unique_ptr<Json::CharReader> m_Reader{nullptr};
71  static Json::CharReaderBuilder m_CharReaderBuilder;
72 };
73 
74 class Command: public Message
75 {
76 public:
77  Command(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
78  std::string getCommand() const;
79  std::string getCommand();
80  template<typename T> void addParameter(const std::string& name, const T& value) { m_Value["Content"]["Parameters"][name] = value; }
81  Json::Value getParameter(const std::string& parameter) const
82  {
83  if(m_Value["Content"]["Parameters"].isMember(parameter)) return m_Value["Content"]["Parameters"][parameter];
84  else
85  return Json::Value{"ERROR"};
86  }
87 };
88 
89 class Critical: public Message
90 {
91 public:
92  Critical(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
93 };
94 
95 class Warning: public Message
96 {
97 public:
98  Warning(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
99 };
100 
101 class Debug: public Message
102 {
103 public:
104  Debug(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
105 };
106 
107 class Trace: public Message
108 {
109 public:
110  Trace(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
111 };
112 
113 class Error: public Message
114 {
115 public:
116  Error(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
117 };
118 
119 class Info: public Message
120 {
121 public:
122  Info(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
123 };
124 
125 class State: public Message
126 {
127 public:
128  State(States state, const std::string& to = "ALL", const std::string& from = "");
129 };
130 
131 class Action: public Message
132 {
133 public:
134  Action(const Actions& action, const std::string& to = "ALL", const std::string& from = "");
135 };
136 
137 class Data: public Message
138 {
139 public:
140  Data(const std::string& content, const std::string& to = "ALL", const std::string& from = "");
141  Data(const Json::Value& content = {}, const std::string& to = "ALL", const std::string& from = "");
142 };
143 
144 class Response: public Message
145 {
146 public:
147  Response(const std::string& content = "", const std::string& to = "ALL", const std::string& from = "");
148 };
Definition: Message.hpp:144
Definition: Message.hpp:113
Definition: Message.hpp:39
Definition: Message.hpp:131
Definition: Message.hpp:95
Definition: Message.hpp:89
Definition: Message.hpp:107
Definition: Message.hpp:125
Definition: Message.hpp:74
Definition: Message.hpp:101
Definition: Data.hpp:15
Definition: Message.hpp:119