mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-31 16:52:10 +01:00
OP-1222 Added JSon serialization code.
This commit is contained in:
parent
9a13c49722
commit
1c2efe0868
@ -36,6 +36,8 @@
|
||||
#include <coreplugin/icore.h>
|
||||
#include <QKeySequence>
|
||||
#include <coreplugin/modemanager.h>
|
||||
#include <QJsonDocument>
|
||||
#include "stabilizationsettings.h"
|
||||
|
||||
SetupWizardPlugin::SetupWizardPlugin() : wizardRunning(false)
|
||||
{}
|
||||
@ -58,14 +60,26 @@ bool SetupWizardPlugin::initialize(const QStringList & args, QString *errMsg)
|
||||
Core::Constants::C_GLOBAL_ID);
|
||||
cmd->setDefaultKeySequence(QKeySequence("Ctrl+V"));
|
||||
cmd->action()->setText(tr("Vehicle Setup Wizard"));
|
||||
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(showSetupWizard()));
|
||||
|
||||
Core::ModeManager::instance()->addAction(cmd, 1);
|
||||
|
||||
ac->menu()->addSeparator();
|
||||
ac->appendGroup("Wizard");
|
||||
ac->addAction(cmd, "Wizard");
|
||||
|
||||
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(showSetupWizard()));
|
||||
cmd = am->registerAction(new QAction(this),
|
||||
"SetupWizardPlugin.ExportJSon",
|
||||
QList<int>() <<
|
||||
Core::Constants::C_GLOBAL_ID);
|
||||
cmd->action()->setText(tr("Export Stabilization Settings"));
|
||||
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(exportSettings()));
|
||||
|
||||
Core::ModeManager::instance()->addAction(cmd, 1);
|
||||
ac->menu()->addSeparator();
|
||||
ac->appendGroup("Wizard");
|
||||
ac->addAction(cmd, "Wizard");
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -87,6 +101,27 @@ void SetupWizardPlugin::showSetupWizard()
|
||||
}
|
||||
}
|
||||
|
||||
void SetupWizardPlugin::exportSettings()
|
||||
{
|
||||
QFile saveFile(QStringLiteral("/home/fredrik/export.json"));
|
||||
|
||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||
qWarning("Couldn't open save file.");
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject exportObject;
|
||||
|
||||
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
||||
UAVObjectManager *uavoManager = pm->getObject<UAVObjectManager>();
|
||||
StabilizationSettings* settings = StabilizationSettings::GetInstance(uavoManager);
|
||||
settings->toJson(exportObject);
|
||||
|
||||
QJsonDocument saveDoc(exportObject);
|
||||
saveFile.write(saveDoc.toJson());
|
||||
|
||||
}
|
||||
|
||||
void SetupWizardPlugin::wizardTerminated()
|
||||
{
|
||||
wizardRunning = false;
|
||||
|
@ -45,6 +45,7 @@ public:
|
||||
|
||||
private slots:
|
||||
void showSetupWizard();
|
||||
void exportSettings();
|
||||
void wizardTerminated();
|
||||
|
||||
private:
|
||||
|
@ -31,6 +31,7 @@
|
||||
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include <QtWidgets>
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
@ -523,7 +524,8 @@ void UAVObject::toXML(QXmlStreamWriter *xmlWriter)
|
||||
{
|
||||
xmlWriter->writeStartElement("object");
|
||||
xmlWriter->writeAttribute("name", getName());
|
||||
xmlWriter->writeAttribute("id", QString("%1-%2").arg(getObjID(), 1, 16).toUpper().arg(getInstID()));
|
||||
xmlWriter->writeAttribute("id", QString("%1").arg(getObjID(), 1, 16).toUpper());
|
||||
xmlWriter->writeAttribute("instance", QString("%1").arg(getInstID()));
|
||||
xmlWriter->writeStartElement("fields");
|
||||
foreach(UAVObjectField * field, fields) {
|
||||
field->toXML(xmlWriter);
|
||||
@ -534,17 +536,50 @@ void UAVObject::toXML(QXmlStreamWriter *xmlWriter)
|
||||
|
||||
void UAVObject::fromXML(QXmlStreamReader *xmlReader)
|
||||
{
|
||||
|
||||
// Check that we are the correct object by name, and that we are the same instance id
|
||||
// but dont check id since we want to be able to be forward compatible.
|
||||
Q_ASSERT(xmlReader->name() == "object");
|
||||
Q_ASSERT(xmlReader->attributes().value("name") == getName());
|
||||
Q_ASSERT(xmlReader->attributes().value("instance") == QString("%1").arg(getInstID()));
|
||||
QXmlStreamReader::TokenType token = xmlReader->readNext();
|
||||
if(token == QXmlStreamReader::StartElement && xmlReader->name() == "fields") {
|
||||
while (xmlReader->readNextStartElement()) {
|
||||
if (xmlReader->name() == "field") {
|
||||
QStringRef fieldName = xmlReader->attributes().value("name");
|
||||
if (fieldName != "") {
|
||||
getField(*fieldName.string())->fromXML(xmlReader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UAVObject::toJson(QJsonObject *jsonObject)
|
||||
void UAVObject::toJson(QJsonObject &jsonObject)
|
||||
{
|
||||
|
||||
jsonObject["name"] = getName();
|
||||
jsonObject["id"] = QString("%1").arg(getObjID(), 1, 16).toUpper();
|
||||
jsonObject["instance"] = (int)getInstID();
|
||||
QJsonArray jSonFields;
|
||||
foreach(UAVObjectField * field, fields) {
|
||||
QJsonObject jSonField;
|
||||
field->toJson(jSonField);
|
||||
jSonFields.append(jSonField);
|
||||
}
|
||||
jsonObject["fields"] = jSonFields;
|
||||
}
|
||||
|
||||
void UAVObject::fromJson(const QJsonObject *jsonObject)
|
||||
void UAVObject::fromJson(const QJsonObject &jsonObject)
|
||||
{
|
||||
|
||||
Q_ASSERT(jsonObject["name"].toString() == getName());
|
||||
Q_ASSERT(jsonObject["instance"].toInt() == (int)getInstID());
|
||||
QJsonArray jsonFields = jsonObject["fields"].toArray();
|
||||
for (int i = 0; i < jsonFields.size(); i++) {
|
||||
QJsonObject jsonField = jsonFields.at(i).toObject();
|
||||
UAVObjectField *field = getField(jsonField["name"].toString());
|
||||
if (field) {
|
||||
field->fromJson(jsonField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,8 +136,8 @@ public:
|
||||
void toXML(QXmlStreamWriter *xmlWriter);
|
||||
void fromXML(QXmlStreamReader *xmlReader);
|
||||
|
||||
void toJson(QJsonObject *jsonObject);
|
||||
void fromJson(const QJsonObject *jsonObject);
|
||||
void toJson(QJsonObject &jsonObject);
|
||||
void fromJson(const QJsonObject &jsonObject);
|
||||
|
||||
void emitTransactionCompleted(bool success);
|
||||
void emitNewInstance(UAVObject *);
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "uavobjectfield.h"
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include <QtWidgets>
|
||||
|
||||
UAVObjectField::UAVObjectField(const QString & name, const QString & units, FieldType type, quint32 numElements, const QStringList & options, const QString &limits)
|
||||
{
|
||||
@ -675,17 +676,46 @@ void UAVObjectField::toXML(QXmlStreamWriter *xmlWriter)
|
||||
|
||||
void UAVObjectField::fromXML(QXmlStreamReader *xmlReader)
|
||||
{
|
||||
|
||||
// Assert we have the correct field by name
|
||||
Q_ASSERT(xmlReader->name() == "field");
|
||||
Q_ASSERT(xmlReader->attributes().value("name") == getName());
|
||||
// Read values, skip overflowing ones if any
|
||||
while (xmlReader->readNextStartElement()) {
|
||||
if (xmlReader->name() == "value") {
|
||||
int index = getElementNames().indexOf(xmlReader->attributes().value("name").toString());
|
||||
if (index >= 0) {
|
||||
setValue(xmlReader->readElementText(), index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UAVObjectField::toJson(QJsonObject *jsonObject)
|
||||
void UAVObjectField::toJson(QJsonObject &jsonObject)
|
||||
{
|
||||
|
||||
jsonObject["name"] = getName();
|
||||
jsonObject["type"] = getTypeAsString();
|
||||
jsonObject["unit"] = getUnits();
|
||||
QJsonArray values;
|
||||
for (unsigned int n = 0; n < numElements; ++n) {
|
||||
QJsonObject value;
|
||||
value["name"] = getElementNames().at(n);
|
||||
value["value"] = QJsonValue::fromVariant(getValue(n));
|
||||
values.append(value);
|
||||
}
|
||||
jsonObject["values"] = values;
|
||||
}
|
||||
|
||||
void UAVObjectField::fromJson(const QJsonObject *jsonObject)
|
||||
void UAVObjectField::fromJson(const QJsonObject &jsonObject)
|
||||
{
|
||||
|
||||
Q_ASSERT(jsonObject["name"].toString() == getName());
|
||||
QJsonArray jsonValues = jsonObject["values"].toArray();
|
||||
for (int i = 0; i < jsonValues.size(); i++) {
|
||||
QJsonObject jsonValue = jsonValues.at(i).toObject();
|
||||
int index = getElementNames().indexOf(jsonValue["name"].toString());
|
||||
if (index >= 0) {
|
||||
setValue(((QJsonValue)jsonValue["value"]).toVariant(), index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
qint32 UAVObjectField::pack(quint8 *dataOut)
|
||||
|
@ -80,8 +80,8 @@ public:
|
||||
void toXML(QXmlStreamWriter *xmlWriter);
|
||||
void fromXML(QXmlStreamReader *xmlReader);
|
||||
|
||||
void toJson(QJsonObject *jsonObject);
|
||||
void fromJson(const QJsonObject *jsonObject);
|
||||
void toJson(QJsonObject &jsonObject);
|
||||
void fromJson(const QJsonObject &jsonObject);
|
||||
|
||||
bool isWithinLimits(QVariant var, quint32 index, int board = 0);
|
||||
QVariant getMaxLimit(quint32 index, int board = 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user