dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavobjectstest.cpp
Go to the documentation of this file.
1 #include "uavobjectstest.h"
2 
4  : sout(stdout)
5  , done(false)
6 {
7  // Create object manager
8  objMngr = new UAVObjectManager();
9  connect(objMngr, SIGNAL(newObject(UAVObject *)), this, SLOT(newObject(UAVObject *)));
10  connect(objMngr, SIGNAL(newInstance(UAVObject *)), this, SLOT(newInstance(UAVObject *)));
11 
12  // Create test objects
13  obj1 = new ExampleObject1();
14  connect(obj1, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(objectUpdated(UAVObject *)));
15  connect(obj1, SIGNAL(objectUpdatedAuto(UAVObject *)), this,
16  SLOT(objectUpdatedAuto(UAVObject *)));
17  connect(obj1, SIGNAL(objectUpdatedManual(UAVObject *)), this,
18  SLOT(objectUpdatedManual(UAVObject *)));
19  connect(obj1, SIGNAL(objectUnpacked(UAVObject *)), this, SLOT(objectUnpacked(UAVObject *)));
20  connect(obj1, SIGNAL(updateRequested(UAVObject *)), this, SLOT(updateRequested(UAVObject *)));
21  objMngr->registerObject(obj1);
22 
23  // Setup timer
24  timer = new QTimer();
25  connect(timer, SIGNAL(timeout()), this, SLOT(runTest()));
26  timer->start(1000);
27 }
28 
29 void UAVObjectsTest::objectUpdated(UAVObject *obj)
30 {
31  sout << QString("[Object Updated]\n%1").arg(obj->toString());
32 }
33 
34 void UAVObjectsTest::objectUpdatedAuto(UAVObject *obj)
35 {
36  sout << QString("[Object Updated AUTO]\n%1").arg(obj->toString());
37 }
38 
39 void UAVObjectsTest::objectUpdatedManual(UAVObject *obj)
40 {
41  sout << QString("[Object Updated MANUAL]\n%1").arg(obj->toString());
42 }
43 
44 void UAVObjectsTest::objectUnpacked(UAVObject *obj)
45 {
46  sout << QString("[Object Updated UNPACKED]\n%1").arg(obj->toString());
47 }
48 
49 void UAVObjectsTest::updateRequested(UAVObject *obj)
50 {
51  sout << QString("[Object Update Requested]\n%1").arg(obj->toString());
52 }
53 
54 void UAVObjectsTest::newObject(UAVObject *obj)
55 {
56  sout << QString("[New object]\n%1").arg(obj->toString());
57 }
58 
59 void UAVObjectsTest::newInstance(UAVObject *obj)
60 {
61  sout << QString("[New instance]\n%1").arg(obj->toString());
62 }
63 
64 void UAVObjectsTest::runTest()
65 {
66  if (!done) {
67  // Create a new instance
68  ExampleObject1 *obj2 = new ExampleObject1();
69  objMngr->registerObject(obj2);
70 
71  // Set data
72  ExampleObject1::DataFields data = obj1->getData();
73  data.Field1 = 1;
74  data.Field2 = 2;
75  data.Field3 = 3;
76  data.Field4[0] = 4.1;
77  data.Field4[1] = 4.2;
78  data.Field4[2] = 4.3;
79  obj1->setData(data);
80 
81  // Set metadata
82  UAVObject::Metadata mdata = obj1->getMetadata();
83  mdata.gcsTelemetryUpdatePeriod = 123;
84  obj1->setMetadata(mdata);
85  // Print metadata of both instances
86  sout << "[Meta of obj1]\n";
87  sout << obj1->getMetaObject()->toString();
88  sout << "[Meta of obj2]\n";
89  sout << obj2->getMetaObject()->toString();
90 
91  // Create a new instance using clone() and an out of order instance ID
92  UAVDataObject *obj3 = obj2->clone(5);
93  objMngr->registerObject(obj3);
94 
95  // Pack, unpack testing
96  quint8 *buf = new quint8[obj1->getNumBytes()];
97  obj1->pack(buf);
98  data.Field1 = 10;
99  data.Field2 = 20;
100  data.Field3 = 30;
101  data.Field4[0] = 40.1;
102  data.Field4[1] = 40.2;
103  data.Field4[2] = 40.3;
104  obj1->setData(data);
105  obj1->unpack(buf);
106 
107  // Save, load testing
108  obj1->save();
109  data.Field1 = 10;
110  data.Field2 = 20;
111  data.Field3 = 30;
112  data.Field4[0] = 40.1;
113  data.Field4[1] = 40.2;
114  data.Field4[2] = 40.3;
115  obj1->setData(data);
116  obj1->load();
117 
118  // Get all instances
119  QList<UAVObject *> objs = objMngr->getObjectInstances(ExampleObject1::OBJID);
120  for (int n = 0; n < objs.length(); ++n) {
121  sout << "[Printing object instances]\n";
122  sout << objs[n]->toString();
123  }
124 
125  // Get object fields
126  QString objname("ExampleObject1");
127  UAVObject *obj = objMngr->getObject(objname);
128  QList<UAVObjectField *> fields = obj->getFields();
129  // qint8
130  UAVObjectFieldInt8 *fieldint8 = dynamic_cast<UAVObjectFieldInt8 *>(fields[0]);
131  if (fieldint8 != NULL) {
132  fieldint8->setValue(10);
133  qint8 value = fieldint8->getValue();
134  sout << value;
135  }
136  // enum
137  UAVObjectFieldEnum *fieldenum = dynamic_cast<UAVObjectFieldEnum *>(fields[7]);
138  if (fieldenum != NULL) {
139  QStringList options = fieldenum->getOptions();
140  fieldenum->setSelected(options[1]);
141  QString selected = fieldenum->getSelected();
142  sout << selected;
143  }
144 
145  // Done
146  done = true;
147  }
148  sout.flush();
149 }
bool registerObject(UAVDataObject *obj)
DataFields data
Eccentricity n
Definition: OPPlots.m:137
QList< UAVObjectField * > getFields()
Definition: uavobject.cpp:196
QString toString()
Definition: uavobject.cpp:287
Definition: icore.h:39
virtual UAVDataObject * clone(quint32 instID=0)=0
UAVObject * getObject(const QString &name, quint32 instId=0)