source: xplra/test/multigettest.cc@ 113:14139defbd04

Last change on this file since 113:14139defbd04 was 112:58892a32a039, checked in by István Váradi <ivaradi@…>, 2 years ago

The test programs can be parameterized to connect over TCP

File size: 11.6 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29//------------------------------------------------------------------------------
30
31#include <hu/varadiistvan/xplra/XPlane.h>
32#include <hu/varadiistvan/xplra/MultiGetter.h>
33
34#include <hu/varadiistvan/scpl/Thread.h>
35
36#include "common.h"
37
38#include <cstdio>
39#include <cstring>
40
41//------------------------------------------------------------------------------
42
43using hu::varadiistvan::xplra::XPlane;
44using hu::varadiistvan::xplra::MultiGetter;
45using hu::varadiistvan::xplra::Exception;
46using hu::varadiistvan::xplra::ProtocolException;
47
48using hu::varadiistvan::scpl::Thread;
49
50//------------------------------------------------------------------------------
51
52int main(int argc, char* argv[])
53{
54 bool dontregister = false;
55 for(int i = 1; i<argc; ++i) {
56 if (strcmp(argv[i], "dontregister")==0) {
57 dontregister = true;
58 }
59 }
60
61 try {
62 XPlane xplane;
63
64 printf("Connecting to X-Plane...\n");
65 connectXPlane(xplane, argc, argv);
66 printf("Connected to X-Plane.\n\n");
67
68 MultiGetter& wrongGetter = xplane.createMultiGetter();
69 wrongGetter.addInt("sim/time/local_date_days");
70 wrongGetter.addInt("sim/time/paused");
71 wrongGetter.addInt("sim/time/hello_te_lo");
72 wrongGetter.addFloat("sim/time/zulu_time_sec");
73 wrongGetter.addInt("sim/operation/prefs/replay_mode");
74
75 printf("Executing a multi-dataref getter with an invalid dataref...\n");
76 try {
77 wrongGetter.execute();
78 printf(">>>>>>>>>>>>>>>>>>>>> Succeeded, but it should not have!!!!\n\n");
79 } catch(const ProtocolException& e) {
80 printf("Caught protocol exception: %s\n\n", e.what());
81 }
82
83 printf("Executing a registered multi-dataref getter with an invalid dataref...\n");
84 wrongGetter.registerInXPlane();
85 try {
86 wrongGetter.execute();
87 printf(">>>>>>>>>>>>>>>>>>>>> Succeeded, but it should not have!!!!\n\n");
88 } catch(const ProtocolException& e) {
89 printf("Caught protocol exception: %s\n\n", e.what());
90 }
91
92 MultiGetter& getter = xplane.createMultiGetter();
93
94 const size_t tailnumID = getter.addByteArray("sim/aircraft/view/acf_tailnum", 40);
95 const size_t daysID = getter.addInt("sim/time/local_date_days");
96 const size_t zuluSecID = getter.addFloat("sim/time/zulu_time_sec");
97 const size_t pausedID = getter.addInt("sim/time/paused");
98 const size_t latitudeID = getter.addDouble("sim/flightmodel/position/latitude");
99 const size_t longitudeID = getter.addDouble("sim/flightmodel/position/longitude");
100 const size_t replayID = getter.addInt("sim/operation/prefs/replay_mode");
101 const size_t overspeedID = getter.addInt("sim/flightmodel/failures/over_vne");
102 const size_t stalledID = getter.addInt("sim/flightmodel/failures/stallwarning");
103 const size_t onTheGroundID = getter.addInt("sim/flightmodel/failures/onground_any");
104 const size_t emptyWeightID = getter.addFloat("sim/aircraft/weight/acf_m_empty");
105 const size_t payloadWeightID = getter.addFloat("sim/flightmodel/weight/m_fixed");
106 const size_t grossWeightID = getter.addFloat("sim/flightmodel/weight/m_total");
107 const size_t headingID = getter.addFloat("sim/flightmodel/position/psi");
108 const size_t pitchID = getter.addFloat("sim/flightmodel/position/theta");
109 const size_t bankID = getter.addFloat("sim/flightmodel/position/phi");
110 const size_t iasID = getter.addFloat("sim/flightmodel/position/indicated_airspeed2");
111 const size_t machID = getter.addFloat("sim/flightmodel/misc/machno");
112 const size_t groundSpeedID = getter.addFloat("sim/flightmodel/position/groundspeed");
113 const size_t vsID = getter.addFloat("sim/flightmodel/position/vh_ind_fpm2");
114 const size_t radioAltitudeID = getter.addFloat("sim/flightmodel/position/y_agl");
115 const size_t altitudeID = getter.addFloat("sim/flightmodel/position/elevation");
116 const size_t gLoadID = getter.addFloat("sim/flightmodel/forces/g_nrml");
117 const size_t flapsControlID = getter.addFloat("sim/flightmodel/controls/flaprqst");
118 const size_t flapsLeftID = getter.addFloat("sim/flightmodel/controls/flaprat");
119 const size_t flapsRightID = getter.addFloat("sim/flightmodel/controls/flap2rat");
120 const size_t navLightsID = getter.addInt("sim/cockpit/electrical/nav_lights_on");
121 const size_t beaconLightsID = getter.addInt("sim/cockpit/electrical/beacon_lights_on");
122 const size_t strobeLightsID = getter.addInt("sim/cockpit/electrical/strobe_lights_on");
123 const size_t landingLightsID = getter.addInt("sim/cockpit/electrical/landing_lights_on");
124 const size_t pitotID = getter.addInt("sim/cockpit/switches/pitot_heat_on");
125 const size_t parkingID = getter.addFloat("sim/flightmodel/controls/parkbrake");
126 //const size_t gearControlID = getter.addInt("sim/cockpit/switches/gear_handle_status");
127 const size_t gearControlID = getter.addInt("sim/cockpit2/controls/gear_handle_down");
128 const size_t noseGearID = getter.addFloatArray("sim/flightmodel2/gear/deploy_ratio", 1);
129 const size_t altimeterID = getter.addFloat("sim/cockpit/misc/barometer_setting");
130 const size_t qnhID = getter.addFloat("sim/flightmodel/misc/Qstatic");
131 const size_t navTypesID = getter.addIntArray("sim/cockpit/radios/nav_type", 6);
132 const size_t busVoltsID = getter.addFloatArray("sim/cockpit2/electrical/bus_volts", 4);
133
134 if (dontregister) {
135 getter.finalize();
136 } else {
137 printf("Registering getter...\n");
138 getter.registerInXPlane();
139 printf("Registered getter.\n\n");
140 }
141
142 const int32_t& days = getter.getIntRef(daysID);
143 const float& zuluSec = getter.getFloatRef(zuluSecID);
144 const int32_t& paused = getter.getIntRef(pausedID);
145 const double& latitude = getter.getDoubleRef(latitudeID);
146 const double& longitude = getter.getDoubleRef(longitudeID);
147 // frozen
148 // slew
149 const int32_t& replay = getter.getIntRef(replayID);
150 const int32_t& overspeed = getter.getIntRef(overspeedID);
151 const int32_t& stalled = getter.getIntRef(stalledID);
152 const int32_t& onTheGround = getter.getIntRef(onTheGroundID);
153 const float& emptyWeight = getter.getFloatRef(emptyWeightID);
154 const float& payloadWeight = getter.getFloatRef(payloadWeightID);
155 const float& grossWeight = getter.getFloatRef(grossWeightID);
156 const float& heading = getter.getFloatRef(headingID);
157 const float& pitch = getter.getFloatRef(pitchID);
158 const float& bank = getter.getFloatRef(bankID);
159 const float& ias = getter.getFloatRef(iasID);
160 const float& mach = getter.getFloatRef(machID);
161 const float& groundSpeed = getter.getFloatRef(groundSpeedID);
162 const float& vs = getter.getFloatRef(vsID);
163 const float& radioAltitude = getter.getFloatRef(radioAltitudeID);
164 const float& altitude = getter.getFloatRef(altitudeID);
165 const float& gLoad = getter.getFloatRef(gLoadID);
166 const float& flapsControl = getter.getFloatRef(flapsControlID);
167 const float& flapsLeft = getter.getFloatRef(flapsLeftID);
168 const float& flapsRight = getter.getFloatRef(flapsRightID);
169 const int32_t& navLights = getter.getIntRef(navLightsID);
170 const int32_t& beaconLights = getter.getIntRef(beaconLightsID);
171 const int32_t& strobeLights = getter.getIntRef(strobeLightsID);
172 const int32_t& landingLights = getter.getIntRef(landingLightsID);
173 const int32_t& pitot = getter.getIntRef(pitotID);
174 const float& parking = getter.getFloatRef(parkingID);
175 const int32_t& gearControl = getter.getIntRef(gearControlID);
176 const float& noseGear = getter.getFloatArray(noseGearID)[0];
177 const float& altimeter = getter.getFloatRef(altimeterID);
178 const float& qnh = getter.getFloatRef(qnhID);
179 const int32_t* navTypes = getter.getIntArray(navTypesID);
180 const float* busVolts = getter.getFloatArray(busVoltsID);
181
182 while(true) {
183 getter.execute();
184 printf("tail number: '%s', days: %d, zuluSec: %f\n",
185 getter.getString(tailnumID).c_str(), days, zuluSec);
186 printf("paused=%d, replay=%d, overspeed=%d, stalled=%d, onTheGround=%d\n",
187 paused, replay, overspeed, stalled, onTheGround);
188 printf("emptyWeight=%f, payloadWeight=%f, grossWeight=%f\n",
189 emptyWeight, payloadWeight, grossWeight);
190 printf("latitude=%f, longitude=%f, radioAltitude=%f, altitude=%f, gLoad=%f\n",
191 latitude, longitude, radioAltitude/.3048, altitude/.3048, gLoad);
192 printf("heading=%f, pitch=%f, bank=%f, ias=%f, mach=%f, groundSpeed=%f, vs=%f\n",
193 heading, pitch,bank, ias, mach, groundSpeed, vs);
194 printf("flapsControl=%f, flapsLeft=%f, flapsRight=%f\n",
195 flapsControl, flapsLeft, flapsRight);
196 printf("Lights: NAV: %d, beacon: %d, strobe: %d, landing: %d\n",
197 navLights, beaconLights, strobeLights, landingLights);
198 printf("pitot: %d, parking: %f, gearControl: %d, noseGear: %f\n",
199 pitot, parking, gearControl, noseGear);
200 printf("altimeter: %f, qnh: %f\n",
201 altimeter, qnh);
202 printf("NAV types: %d %d %d %d %d %d\n",
203 navTypes[0], navTypes[1], navTypes[2],
204 navTypes[3], navTypes[4], navTypes[5]);
205 printf("bus voltages: %f %f %f %f\n",
206 busVolts[0], busVolts[1], busVolts[2], busVolts[3]);
207 printf("\n");
208 Thread::sleep(1000);
209 }
210
211 //xplane.destroyMultiBuffer(getter);
212
213 return 0;
214 } catch(Exception& exception) {
215 printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
216
217 return 1;
218 }
219}
220
221//------------------------------------------------------------------------------
222
223// Local Variables:
224// mode: C++
225// c-basic-offset: 4
226// indent-tabs-mode: nil
227// End:
Note: See TracBrowser for help on using the repository browser.