-
Notifications
You must be signed in to change notification settings - Fork 6
/
PX4ParameterMetaData.cpp
185 lines (164 loc) · 7.01 KB
/
PX4ParameterMetaData.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
#include "PX4ParameterMetaData.h"
#include <QFile>
#include <QDir>
#include <QXmlStreamReader>
#include <QDebug>
#include <QRegExp>
PX4ParameterMetaData::PX4ParameterMetaData(QObject *parent) :
QObject(parent),
_parameterMetaDataLoaded(false)
{
}
PX4ParameterMetaData::~PX4ParameterMetaData()
{
}
void PX4ParameterMetaData::addMetaDataToFact(Fact* fact)
{
if(_mapParameterName2FactMetaData.contains(fact->name())) {
fact->setMetaData(_mapParameterName2FactMetaData[fact->name()]);
}
}
void PX4ParameterMetaData::loadParamFactMetaDataFile(QString &metaDataFile)
{
_mapParameterName2FactMetaData.clear();
_loadParameterFactMetaDataFile(metaDataFile);
}
void PX4ParameterMetaData::_loadParameterFactMetaDataFile(const QString& metaDataFile)
{
qDebug()<<"PX4ParameterMetaData::loadParameterFactMetaDataFile."<<metaDataFile;
if(_parameterMetaDataLoaded){
qWarning() << "Internal error: parameter meta data loaded more than once";
return;
}
_parameterMetaDataLoaded = true;
qDebug()<<"Loading parameter meta data......";
QFile xmlFile(metaDataFile);
if(!xmlFile.exists()){
qWarning()<<"Internal error:metaDataFile missing"<<metaDataFile;
return;
}
if(!xmlFile.open(QIODevice::ReadWrite)) {
qWarning()<<"Internal error: Unable to open parameter file:"<<metaDataFile<<xmlFile.errorString();
return;
}
QXmlStreamReader xml(xmlFile.readAll());
xmlFile.close();
if(xml.hasError()) {
qWarning()<<"Badly formed xml"<<xml.errorString();
return;
}
int xmlState = XMLStateNone;
FactMetaData* metaData = NULL;
bool badMetaData = false;
QString errorString;
while(!xml.atEnd()) {
if(xml.isStartElement()) {
QString elementName = xml.name().toString();
if(elementName == "parameters") {
if(xmlState!=XMLStateNone) {
qWarning() << "parameters: Badly formed XML";
return;
}
xmlState = XMLStateFoundParameters;
}
else if(elementName == "parameter") {
if(xmlState!=XMLStateFoundParameters) {
qDebug() << "parameter: Badly formed XML";
return;
}
xmlState = XMLStateFoundParameter;
if(!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("type")) {
qDebug() << "name & type: Badly formed XML";
return;
}
QString name = xml.attributes().value("name").toString();
QString type = xml.attributes().value("type").toString();
QString strDefault= xml.attributes().value("default").toString();
//qDebug()<<"Found parameter name:"<<name<<" type:"<<type<<" default:"<<strDefault;
// Convert type from string to FactMetaData::ValueType_t
bool unknownType;
FactMetaData::ValueType_t foundType = FactMetaData::stringToType(type,unknownType);
if(unknownType) {
qWarning()<<"Parameter meta data with bad type: "<<type<<" name: "<<name;
return;
}
metaData = new FactMetaData(foundType);
//add to FactMetaData
if(_mapParameterName2FactMetaData.contains(name)) {
badMetaData = true;
qWarning()<<"Duplicate parameter found: "<<name;
//reset to default meta data
_mapParameterName2FactMetaData[name] = metaData;
}
else {
_mapParameterName2FactMetaData[name] = metaData;
metaData->setName(name);
if(xml.attributes().hasAttribute("default") && !strDefault.isEmpty()) {
QVariant varDefault;
if(metaData->convertAndValidateRaw(strDefault,false,varDefault,errorString)) {
metaData->setRawDefaultValue(varDefault);
}
else {
qDebug()<< "Invalid default value, name:" << name << " type:" << type << " default:" << strDefault << " error:" << errorString;
}
}
}
}
else {
//be getting meta data now
if(xmlState!=XMLStateFoundParameter) {
qDebug() << "Badly formed XML"<<xmlState;
return;
}
if(!badMetaData) {
if(elementName == "short_desc") {
QString text = xml.readElementText();
text = text.replace("\n"," ");
//qDebug()<<"Short Description: "<<text;
metaData->setShortDescription(text);
}
else if(elementName == "min") {
QString text = xml.readElementText();
//qDebug()<<"Min: "<<text;
QVariant varMin;
//convertOnly, i.e. only convertOk matters
if(metaData->convertAndValidateRaw(text,true,varMin,errorString)) {
metaData->setRawMin(varMin);
}
else {
qWarning()<< "Invalid min value, name:" << metaData->name() << " type:" << metaData->type() << " min:" << text << " error:" << errorString;
}
}
else if(elementName == "max") {
QString text = xml.readElementText();
//qDebug()<<"Max: "<<text;
QVariant varMax;
//convertOnly, i.e. only convertOk matters
if(metaData->convertAndValidateRaw(text,true,varMax,errorString)) {
metaData->setRawMax(varMax);
}
else {
qWarning()<< "Invalid max value, name:" << metaData->name() << " type:" << metaData->type() << " min:" << text << " error:" << errorString;
}
}
else {
//qDebug()<< "Unknown element in XML: " << elementName;
}
}
}
}
else if (xml.isEndElement()) {
QString elementName = xml.name().toString();
if(elementName == "parameter") {
// Reset for next parameter
metaData = NULL;
badMetaData = false;
xmlState = XMLStateFoundParameters;
}
else if (elementName == "parameters") {
xmlState = XMLStateFoundParameters;
}
}
xml.readNext();
}
}