2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

RESTSensorGroup.cpp 4.26 KB
Newer Older
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
//================================================================================
// Name        : RESTSensorGroup.cpp
// Author      : Michael Ott
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Source file for REST sensor group class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2021 Leibniz Supercomputing Centre
//
// This program 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 2
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

#include "RESTSensorGroup.h"

#include <sstream>

#include <boost/foreach.hpp>
#include <boost/property_tree/xml_parser.hpp>

RESTSensorGroup::RESTSensorGroup(const std::string &name)
    : SensorGroupTemplateEntity(name) {
}

RESTSensorGroup::RESTSensorGroup(const RESTSensorGroup &other)
    : SensorGroupTemplateEntity(other),
      _request(other._request) {}

RESTSensorGroup::~RESTSensorGroup() {}

RESTSensorGroup &RESTSensorGroup::operator=(const RESTSensorGroup &other) {
	SensorGroupTemplate::operator=(other);
	_request = other._request;

	return *this;
}

void RESTSensorGroup::read() {
	//send request
	std::string response;
	if (!_entity->sendRequest(_request, response)) {
		LOG(error) << _groupName << " could not send request!";
		return;
	}

	//parse response
	boost::property_tree::ptree ptree;
	std::string                 xml = response.substr(response.find("<"));
	std::istringstream          treeStream(xml);
	try {
		boost::property_tree::read_xml(treeStream, ptree);
	} catch (const std::exception &e) {
		LOG(error) << _groupName << " got malformed XML response";
		return;
	}

	//read values for every sensor from response
	reading_t reading;
	reading.timestamp = getTimestamp();

	for (const auto &s : _sensors) {
		try {
			std::string                 readStr;
			const xmlPathVector_t &     xmlPath = s->getXMLPath();
			boost::property_tree::ptree node = ptree;
			for (size_t i = 0; i < xmlPath.size(); i++) {
				const std::string &       path = std::get<0>(xmlPath[i]);
				const std::string &       child = std::get<1>(xmlPath[i]);
				const attributesVector_t &attVec = std::get<2>(xmlPath[i]);

				unsigned matchCount;
				if (child != "") {
					BOOST_FOREACH (boost::property_tree::ptree::value_type &v, node.get_child(path)) {
						if (v.first == child) {
							matchCount = 0;
							for (size_t j = 0; j < attVec.size(); j++) {
								std::string attributeVal = v.second.get_child("<xmlattr>." + attVec[j].first).data();

								if (attributeVal != attVec[j].second) { //attribute values don't match
									break;
								} else {
									matchCount++;
								}
							}
							if (matchCount == attVec.size()) { //all attributes matched
								readStr = v.second.data();
								node = v.second;
								break;
							}
						}
					}
				} else { //child == ""
					readStr = node.get(path, "");
					break; //last (part of the) path
				}
			}

			if (readStr == "") {
				throw std::runtime_error("Value not found!");
			}
			reading.value = stoll(readStr);

#ifdef DEBUG
			LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif
			s->storeReading(reading);
		} catch (const std::exception &e) {
			LOG(error) << _groupName << "::" << s->getName() << " could not read value: " << e.what();
			continue;
		}
	}
}

void RESTSensorGroup::printGroupConfig(LOG_LEVEL ll, unsigned int leadingSpaces) {
	std::string leading(leadingSpaces, ' ');
	LOG_VAR(ll) << leading << "Request: " << _request;
}