summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/encode/XHeartbeatFieldsImp.cpp
blob: 23ffa92be38dca634d782bfabcc281fc16d2b43a (plain)
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
#include "XHeartbeatFieldsImp.h"

const std::string XEnumHeartbeatFieldsHeartbeatFieldsVersionMapping[] = {
    "3.0"
};

XHeartbeatFieldsImp::XHeartbeatFieldsImp(std::shared_ptr<XCommonEventHeader> header, XInteger heartbeatInterval):
    header_(header),
    heartbeatFieldsVersion_(XEnumHeartbeatFieldsHeartbeatFieldsVersion3_0),
    heartbeatInterval_(heartbeatInterval)
{

}

void XHeartbeatFieldsImp::setHeartbeatInterval(XInteger heartbeatInterval)
{
    comVals_["heartbeatInterval"] = heartbeatInterval;
}
            
void XHeartbeatFieldsImp::setAdditionalFields(const XHashMap& additionalFields)
{
    additionalFields_ = additionalFields;
}

std::shared_ptr<XCommonEventHeader> XHeartbeatFieldsImp::getCommonHeader()
{
    return header_;
}

std::string XHeartbeatFieldsImp::toString()
{
    try
    {
        auto js = toJson();
        return js.dump();
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to dump XHeartbeatFields to json string:{}.", e.what());
        return "";
    }
}

json XHeartbeatFieldsImp::toJson()
{
    try
    {
        json field = {
            {"heartbeatInterval", heartbeatInterval_},
            {"heartbeatFieldsVersion", XEnumHeartbeatFieldsHeartbeatFieldsVersionMapping[heartbeatFieldsVersion_]}
        };

        //optional val
        if (!additionalFields_.empty())
        {
            field["additionalFields"] = additionalFields_;
        }

        json jsEvent;
        header_->setDomain(XEnumCommonEventHeaderDomainHeartbeat);
        jsEvent["event"]["commonEventHeader"] = dynamic_pointer_cast<XJsonable>(header_)->toJson();
        jsEvent["event"]["heartbeatFields"] = field;

        return jsEvent;
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to build XHeartbeatFields to json object:{}.", e.what());
        return json();
    }
}