-
Notifications
You must be signed in to change notification settings - Fork 2
/
exchangemessages.cpp
101 lines (88 loc) · 3.3 KB
/
exchangemessages.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
/*
* Copyright (C) 2019 Alessandro Arrabito
*/
/*
* This file is part of QtExchangeDemo.
*
* QtExchangeDemo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtExchangeDemo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtExchangeDemo. If not, see <http://www.gnu.org/licenses/>.
*/
#include "exchangemessages.h"
#include <QDebug>
// factory method
std::unique_ptr<ExchangeMessage> ExchangeMessage::createFromJson(QByteArray &&json_data)
{
std::unique_ptr<ExchangeMessage> msg_ptr;
QJsonDocument json_doc = QJsonDocument::fromJson(json_data);
MessageType type = static_cast<MessageType>(json_doc["type"].toInt());
switch(type)
{
case MessageType::Parameters:
{
int refresh_interval = json_doc["refresh"].toInt();
QString provider_url = json_doc["url"].toString();
QString provider_field = json_doc["field"].toString();
msg_ptr.reset(new ExchangeMessageParameters(refresh_interval, provider_url, provider_field));
break;
}
case MessageType::SetActiveStatus:
{
bool is_active = json_doc["is_active"].toBool();
msg_ptr.reset(new ExchangeMessageStatus(is_active));
break;
}
case MessageType::Data:
{
double data = json_doc["data"].toDouble();
msg_ptr.reset(new ExchangeMessageData(data));
break;
}
default:
qWarning() << __FUNCTION__ << " bad message type";
}
return msg_ptr;
}
ExchangeMessage::~ExchangeMessage(){}
ExchangeMessageParameters::ExchangeMessageParameters(int _refresh_interval, QString _provider_url, QString _provider_field):
refresh_interval(_refresh_interval),
provider_url(_provider_url),
provider_field(_provider_field)
{}
QJsonDocument ExchangeMessageParameters::toJsonDoc()
{
QJsonDocument json_doc;
json_doc.setObject(QJsonObject({{"type", QString::number(static_cast<int>(MessageType::Parameters))},
{"refresh", refresh_interval},
{"url", provider_url},
{"field", provider_field}}));
return json_doc;
}
ExchangeMessageStatus::ExchangeMessageStatus(bool _is_active):
is_active(_is_active)
{}
QJsonDocument ExchangeMessageStatus::toJsonDoc()
{
QJsonDocument json_doc;
json_doc.setObject(QJsonObject({{"type", QJsonValue(static_cast<int>(MessageType::SetActiveStatus))},
{"is_active", QJsonValue(is_active)}}));
return json_doc;
}
ExchangeMessageData::ExchangeMessageData(double _data):data(_data)
{}
QJsonDocument ExchangeMessageData::toJsonDoc()
{
QJsonDocument json_doc;
json_doc.setObject(QJsonObject({{"type", QJsonValue(static_cast<int>(MessageType::Data))},
{"data", QJsonValue(data)}}));
return json_doc;
}