-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlServerStore.cpp
More file actions
131 lines (120 loc) · 3.49 KB
/
XmlServerStore.cpp
File metadata and controls
131 lines (120 loc) · 3.49 KB
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
126
127
128
129
130
131
#include "XmlServerStore.h"
#include "Parameters.h"
// Case insensitive equals
static bool iequals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(),
b.begin(), b.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
});
}
/*
* Serializes to something like:
*
* {
* "T": "P",
* "Bool" : [
* {"Name" : <Param Name>, "Default" : <Param Default>, "Description" : <Param Description>, "Value" : <Current Param Value>},
* ...
* ],
* "String" : [
* {"Name" : <Param Name>, "Default" : <Param Default>, "Description" : <Param Description>, "Value" : <Current Param Value>},
* ...
* ],
* "Long" : [
* {"Name" : <Param Name>, "Default" : <Param Default>, "Description" : <Param Description>, "Value" : <Current Param Value>},
* ...
* ]
* }
*
*/
void XmlServerStore::SendParameterListToClient(const std::shared_ptr<WebsocketServer>& DasherServer, const websocketpp::connection_hdl& Connection)
{
rapidjson::StringBuffer Buffer;
rapidjson::Writer<rapidjson::StringBuffer> Writer(Buffer);
Writer.StartObject();
Writer.Key("T");
Writer.String("P");
// Bools
Writer.Key("Bool");
Writer.StartArray();
for(const Dasher::Settings::bp_table& entry : Dasher::Settings::boolparamtable)
{
Writer.StartObject();
Writer.Key("Name");
Writer.String(entry.regName);
Writer.Key("Default");
Writer.Bool(entry.defaultValue);
Writer.Key("Description");
Writer.String(entry.humanReadable);
Writer.Key("Value");
Writer.Bool(GetBoolParameter(entry.key));
Writer.EndObject();
}
Writer.EndArray();
// Strings
Writer.Key("String");
Writer.StartArray();
for(const Dasher::Settings::sp_table& entry : Dasher::Settings::stringparamtable)
{
Writer.StartObject();
Writer.Key("Name");
Writer.String(entry.regName);
Writer.Key("Default");
Writer.String(entry.defaultValue);
Writer.Key("Description");
Writer.String(entry.humanReadable);
Writer.Key("Value");
Writer.String(GetStringParameter(entry.key).c_str());
Writer.EndObject();
}
Writer.EndArray();
// Longs
Writer.Key("Long");
Writer.StartArray();
for(const Dasher::Settings::lp_table& entry : Dasher::Settings::longparamtable)
{
Writer.StartObject();
Writer.Key("Name");
Writer.String(entry.regName);
Writer.Key("Default");
Writer.Int64(entry.defaultValue);
Writer.Key("Description");
Writer.String(entry.humanReadable);
Writer.Key("Value");
Writer.Int64(GetLongParameter(entry.key));
Writer.EndObject();
}
Writer.EndArray();
Writer.EndObject();
//Send Parameters to Client
DasherServer->send(Connection, Buffer.GetString(), Buffer.GetSize(), websocketpp::frame::opcode::TEXT);
}
void XmlServerStore::SetParameterBasedOnRequest(const std::string& ParameterName, rapidjson::Value& Value)
{
for(const Dasher::Settings::bp_table& entry : Dasher::Settings::boolparamtable)
{
if(iequals(ParameterName, entry.regName))
{
SetBoolParameter(entry.key, Value.GetBool());
return;
}
}
for(const Dasher::Settings::lp_table& entry : Dasher::Settings::longparamtable)
{
if(iequals(ParameterName, entry.regName))
{
SetLongParameter(entry.key, static_cast<long>(Value.GetInt64()));
return;
}
}
for(const Dasher::Settings::sp_table& entry : Dasher::Settings::stringparamtable)
{
if(iequals(ParameterName, entry.regName))
{
SetStringParameter(entry.key, Value.GetString());
return;
}
}
}