-
Notifications
You must be signed in to change notification settings - Fork 1
/
IODaemonMsg.cpp
125 lines (106 loc) · 3.35 KB
/
IODaemonMsg.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* @file IODaemonMsg.cpp
* @Author BeeeOn team - Richard Wolfert (wolfert.richard@gmail.com)
* @date July 2016
* @brief IO message representation class
*/
#include <Poco/Dynamic/Var.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Net/SocketAddress.h>
#include "IODaemonMsg.h"
#define MSG_ACTION "Action"
#define MSG_EVENT_NAME "EventName"
#define MSG_LEDS_BLUE "Blue"
#define MSG_LEDS_CONFIGURATION "LedsConfiguration"
#define MSG_LEDS_GREEN "Green"
#define MSG_LEDS_RED "Red"
#define MSG_PRIORITY "Priority"
#define MSG_SENDER "Sender"
using namespace std;
using Poco::JSON::Object;
using Poco::JSON::Parser;
IODaemonMsg::IODaemonMsg():
m_priority(0),
m_validity(false)
{
}
void IODaemonMsg::debug_print(Poco::Logger& log)
{
log.debug(getEventName() + ", "
+ getAction() + ", "
+ getSenderName() + ", "
+ to_string(getPriority()) + ", "
+ to_string(isValid()) + ", "
+ to_string(getLedConf().m_red) + ", "
+ to_string(getLedConf().m_green) + ", "
+ to_string(getLedConf().m_blue));
}
void IODaemonMsg::parse(const string &data, Poco::Logger& log)
{
Object::Ptr parsedJson;
try {
parsedJson = parseJsonObject(data);
setEventName(extractString(parsedJson, MSG_EVENT_NAME));
setAction(extractString(parsedJson, MSG_ACTION));
setSenderName(extractString(parsedJson, MSG_SENDER));
setPriority(extractChar(parsedJson, MSG_PRIORITY));
if (m_action == "setLeds")
setLedConf(parseLedConfiguration(parsedJson, MSG_LEDS_CONFIGURATION));
if (getEventName() == "updateLeds")
setLedConf(parseLedConfiguration(parsedJson, MSG_LEDS_CONFIGURATION));
}
catch (Poco::Exception &e) {
log.log(e, __FILE__, __LINE__);
setValidity(false);
return;
}
setValidity(true);
}
Object::Ptr IODaemonMsg::parseJsonObject(const string &data)
{
Parser jsonParser;
jsonParser.parse(data);
Poco::Dynamic::Var parsedJsonResult = jsonParser.result();
return parsedJsonResult.extract<Object::Ptr>();
}
string IODaemonMsg::extractString(const Object::Ptr &jsonObject, const string &key)
{
Poco::Dynamic::Var item;
item = jsonObject->get(key);
return item.convert<string>();
}
unsigned char IODaemonMsg::extractChar(const Object::Ptr &jsonObject, const string &key)
{
Poco::Dynamic::Var item;
item = jsonObject->get(key);
return item.convert<unsigned char>();
}
ledConfiguration IODaemonMsg::parseLedConfiguration(const Object::Ptr &jsonObject, const string &key)
{
ledConfiguration ledConf;
Object::Ptr jsonLedsConf;
jsonLedsConf = jsonObject->getObject(key);
ledConf.m_red = (bool)jsonLedsConf->get(MSG_LEDS_RED).convert<unsigned char>();
ledConf.m_green = (bool)jsonLedsConf->get(MSG_LEDS_GREEN).convert<unsigned char>();
ledConf.m_blue = jsonLedsConf->get(MSG_LEDS_BLUE).convert<unsigned char>();
return ledConf;
}
/**
* @brief Method for creating JSON message for iodaemon
*/
string IODaemonMsg::createDaemonMsg() const
{
Poco::JSON::Object jsonMsg;
Poco::JSON::Object jsonLedConf;
jsonMsg.set(MSG_ACTION, m_action);
jsonMsg.set(MSG_EVENT_NAME, m_eventName);
jsonMsg.set(MSG_PRIORITY, m_priority);
jsonMsg.set(MSG_SENDER, m_senderName);
jsonLedConf.set(MSG_LEDS_RED, (int)m_ledConf.m_red);
jsonLedConf.set(MSG_LEDS_GREEN, (int)m_ledConf.m_green);
jsonLedConf.set(MSG_LEDS_BLUE, m_ledConf.m_blue);
jsonMsg.set(MSG_LEDS_CONFIGURATION, jsonLedConf);
Poco::Dynamic::Var jsonVar = jsonMsg;
return jsonVar.convert<string>();
}