Example #1
0
/**
 * Parse incomming message (from server).
 * @param str Incomming server message
 * @return Message in Command structure
 */
Command XMLTool::parseXML(string str) {
	Command cmd;

	try  {
		DOMParser parser = DOMParser(0);
		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.protocol_version = attribute->nodeValue();
						}

						else if (attribute->nodeName().compare("state") == 0) {
							cmd.state = 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.euid = stoull(attribute->nodeValue(), nullptr, 0);
						}

						else if (attribute->nodeName().compare("device_id") == 0) {
							cmd.device_id = atoll(attribute->nodeValue().c_str());
						}

						else if (attribute->nodeName().compare("time") == 0) {
							cmd.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.state == "getparameters" || cmd.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());
							}
						}
						attributes->release();
					}
					cmd.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.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.params.param_id = toNumFromString(attribute->nodeValue());
						}
						else if (attribute->nodeName().compare("euid") == 0) {
							cmd.params.euid = toNumFromString(attribute->nodeValue());
						}
					}
					attributes->release();
				}
			}
			pNode = it.nextNode();
		}
	}
	catch (Poco::Exception& e) {
		log.error("Invalid format of incoming message!" + e.displayText());
		cmd.state = "error";
	}
	return cmd;
}