-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXMLTool.cpp
268 lines (237 loc) · 8.46 KB
/
XMLTool.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* @file XMLTool.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include <Poco/NumberParser.h>
#include "device_table.h"
#include "XMLTool.h"
using namespace std;
using namespace Poco::XML;
using Poco::AutoPtr;
using Poco::Logger;
/**
* Constructor of XML for cases when we need to parse string and create Command structure from it.
*/
XMLTool::XMLTool() :
msg(),
log(Poco::Logger::get("Adaapp-XML"))
{
}
/**
* Constructor of XML for cases when we need to create message from incomming data.
*/
XMLTool::XMLTool(ServerMessage _msg) :
msg(_msg),
log(Poco::Logger::get("Adaapp-XML"))
{
}
/**
* Create message which can be sent to server.
* @param type Message type (0 - A_TO_S, 1 - INIT, 2 - PARAM)
* @return Created message in string
*/
string XMLTool::createXML(int type) {
stringstream stream;
Poco::UTF8Encoding utf8;
try {
XMLWriter writer(stream, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT, "UTF-8", &utf8); // Print XML header and add newline for each tag
writer.setNewLine("\n");
AttributesImpl attrs;
writer.startDocument();
attrs.addAttribute("", "", "adapter_id", "", msg.iotmessage.adapter_id); // TODO!! remove
attrs.addAttribute("", "", "state", "", msg.iotmessage.state);
attrs.addAttribute("", "", "protocol_version", "", msg.iotmessage.protocol_version);
attrs.addAttribute("", "", "fw_version", "", msg.iotmessage.fw_version);
attrs.addAttribute("", "", "time", "", toStringFromLongInt(msg.iotmessage.time));
if (msg.request_id != 0)
attrs.addAttribute("", "", "request_id", "", toStringFromLongInt(msg.request_id));
if (msg.response_id != 0)
attrs.addAttribute("", "", "response_id", "", toStringFromLongInt(msg.response_id));
writer.startElement("", "adapter_server", "", attrs);
if (type == A_TO_S) {
createDevice(&writer, msg.iotmessage.device); // TODO add fw_version etc?
}
else if (type == INIT) {
writer.characters(" ");
}
else if (type == PARAM) {
createParam(&writer, msg.iotmessage.params, msg.iotmessage.state);
}
writer.endElement("", "adapter_server", "");
writer.endDocument();
}
catch (Poco::Exception& ex) {
log.error("*** Exception: \n" + ex.displayText());
}
return stream.str();
}
/**
* Internal function for creating the <device> element filled with values
* @param w Pointer to XMLWriter
* @param dev Device data
* @param debug Flag - add debug mark
* @param proto Communication protocol version
* @param fw Firmware version of device
*/
void XMLTool::createDevice(XMLWriter* w, Device dev, bool debug, string proto, string fw) {
AttributesImpl att;
att.addAttribute("", "", "euid", "", toStringFromLongHex(dev.euid));
att.addAttribute("", "", "device_id", "", toStringFromLongHex(dev.device_id, 2));
if(dev.name != ""){
att.addAttribute("", "", "name", "", dev.name);
}
w->startElement("", "device", "", att);
if (debug)
w->dataElement("", "", "debug", "", "protocol_version", proto, "fw_version", fw);
TT_Table tt = fillDeviceTable();
auto tt_device = tt.find(dev.device_id);
if (tt_device == tt.end())
throw Poco::Exception("Missing device in types table"); // FIXME temporary "fix", do it properly
auto tt_dev = tt_device->second;
if (dev.values.size()) { // If there are some values
AttributesImpl attVal;
attVal.addAttribute("", "", "count", "", toStringFromInt(dev.values.size()));
w->startElement("", "values", "", attVal);
for (auto item : dev.values){
if(item.status == true){
w->dataElement("", "", "value", toStringFromFloat(item.value), "module_id", toStringFromHex(item.mid)); // convert to hex format (0x00)
} else {
w->dataElement("", "", "value", toStringFromFloat(item.value), "module_id", toStringFromHex(item.mid), "status", "unavailable");
}
}
w->endElement("", "values", "");
}
w->endElement("", "device", "");
}
void XMLTool::createParam(XMLWriter* w, CmdParam par, string state){
AttributesImpl att;
att.addAttribute("", "", "param_id", "", toStringFromInt(par.param_id));
if (par.euid > 0)
att.addAttribute("", "", "euid", "", toStringFromLongHex(par.euid));
if (par.module_id >= 0)
att.addAttribute("", "", "module_id", "", toStringFromLongHex(par.module_id));
w->startElement("", "parameter", "", att);
if (state == "parameters" && par.value.size()) {
for (auto item: par.value){
w->dataElement("", "", "value", item.first);
}
}
w->endElement("", "parameter", "");
}
/**
* Parse incomming message (from server).
* @param str Incomming server message
* @return Message in Command structure
*/
ServerCommand XMLTool::parseXML(string str) {
ServerCommand cmd;
try {
DOMParser parser = DOMParser();
AutoPtr<Document> pDoc = parser.parseString(str);
NodeIterator it(pDoc, NodeFilter::SHOW_ELEMENT);
Node* pNode = it.nextNode();
NamedNodeMap* attributes = NULL;
Node* attribute = NULL;
while (pNode) {
if (pNode->nodeName().compare("server_adapter") == 0) {
if (pNode->hasAttributes()) {
attributes = pNode->attributes();
for(unsigned int i = 0; i < attributes->length(); i++) {
attribute = attributes->item(i);
if (attribute->nodeName().compare("protocol_version") == 0) {
cmd.command.protocol_version = attribute->nodeValue();
}
else if (attribute->nodeName().compare("state") == 0) {
cmd.command.state = attribute->nodeValue();
}
else if (attribute->nodeName().compare("request_id") == 0) {
cmd.request_id = Poco::NumberParser::parse(attribute->nodeValue());
}
else if (attribute->nodeName().compare("response_id") == 0) {
cmd.response_id = Poco::NumberParser::parse(attribute->nodeValue());
}
// FIXME - id attribute is here only for backward compatibility, it should be removed in Q1/2016
else if (attribute->nodeName().compare("euid") == 0 || attribute->nodeName().compare("id") == 0) {
cmd.command.euid = stoull(attribute->nodeValue(), nullptr, 0);
}
else if (attribute->nodeName().compare("device_id") == 0) {
cmd.command.device_id = atoll(attribute->nodeValue().c_str());
}
else if (attribute->nodeName().compare("time") == 0) {
cmd.command.time = atoll(attribute->nodeValue().c_str());
}
else {
log.error("Unknow attribute for SERVER_ADAPTER : " + fromXMLString(attribute->nodeName()));
}
}
attributes->release();
}
}
else if (pNode->nodeName().compare("value") == 0) {
if(cmd.command.state == "getparameters" || cmd.command.state == "parameters"){
string inner = pNode->innerText();
string device_id = "";
if (pNode->hasAttributes()) {
attributes = pNode->attributes();
string device_id = "";
for(unsigned int i = 0; i < attributes->length(); i++) {
attribute = attributes->item(i);
if (attribute->nodeName().compare("device_id") == 0) {
device_id = toNumFromString(attribute->nodeValue());
}
if (attribute->nodeName().compare("module_id") == 0) {
cmd.command.params.module_id = toNumFromString(attribute->nodeValue());
}
}
attributes->release();
}
if (!inner.empty() || !device_id.empty())
cmd.command.params.value.push_back({inner, device_id});
}
else {
float val = atof(pNode->innerText().c_str());
if (pNode->hasAttributes()) {
int module_id = 0;
attributes = pNode->attributes();
for(unsigned int i = 0; i < attributes->length(); i++) {
attribute = attributes->item(i);
if (attribute->nodeName().compare("module_id") == 0) {
module_id = toNumFromString(attribute->nodeValue());
}
}
cmd.command.values.push_back({module_id, val}); //TODO Hex number is processed wrongly
attributes->release();
}
}
}
else if (pNode->nodeName().compare("parameter") == 0) {
if (pNode->hasAttributes()) {
attributes = pNode->attributes();
for(unsigned int i = 0; i < attributes->length(); i++) {
attribute = attributes->item(i);
if (attribute->nodeName().compare("param_id") == 0 || attribute->nodeName().compare("id") == 0) {
cmd.command.params.param_id = toIntFromString(attribute->nodeValue());
}
else if (attribute->nodeName().compare("euid") == 0) {
cmd.command.params.euid = toNumFromString(attribute->nodeValue());
}
}
attributes->release();
}
}
pNode = it.nextNode();
}
}
catch (Poco::Exception& e) {
log.error("Invalid format of incoming message!" + e.displayText());
cmd.command.state = "error";
}
return cmd;
}
/**
* Destructor
*/
XMLTool::~XMLTool() {
}