EMANE  1.2.1
eelloaderlocation.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2010-2012 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include "eelloaderlocation.h"
39 
40 #include <sstream>
41 #include <cstring>
42 
44 {}
45 
47 {}
48 
50  const ModuleId & moduleId,
51  const EventType & eventType,
52  const InputArguments & args)
53 {
54  if(moduleType == "nem")
55  {
56  if(eventType == "location")
57  {
58  if(args[0] == "gps")
59  {
60  InputArguments params;
61 
62  size_t posStart = 0;
63  size_t posEnd = 0;
64 
65  while(posEnd != std::string::npos)
66  {
67  posEnd = args[1].find_first_of(",",posStart);
68 
69  params.push_back(args[1].substr(posStart, posEnd - posStart));
70 
71  posStart = posEnd + 1;
72  }
73 
74  double dLatitudeDegress{};
75  double dLongitudeDegress{};
76  double dAltitudeMeters{};
77 
78  try
79  {
80  for(size_t i = 0; i < params.size(); ++i)
81  {
82  switch(i)
83  {
84  case 0:
85  dLatitudeDegress = Utils::ParameterConvert(params[i]).toDouble();
86  break;
87 
88  case 1:
89  dLongitudeDegress = Utils::ParameterConvert(params[i]).toDouble();
90  break;
91 
92  case 2:
93  dAltitudeMeters = Utils::ParameterConvert(params[i]).toDouble();
94  break;
95 
96  case 3:
97  if(params[i] != "msl" && params[i] != "agl")
98  {
99  throw FormatException("LoaderLocation gps unkown altitude type");
100  }
101  break;
102 
103  default:
104  throw FormatException("LoaderLocation too many arguments");
105  }
106  }
107  }
109  {
110  std::stringstream sstream;
111  sstream<<"EELEventGenerator: Parameter conversion error. "<<exp.what()<<std::ends;
112  throw FormatException(sstream.str());
113  }
114 
115  LocationEntry locationEntry{};
116 
117  Position position{dLatitudeDegress,dLongitudeDegress,dAltitudeMeters};
118 
119  locationEntry.setPosition(position);
120 
121  // update the full cache
122  auto ret = locationEntryMap_.insert(std::make_pair(moduleId,locationEntry));
123 
124  if(!ret.second)
125  {
126  ret.first->second.setPosition(position);
127  }
128 
129  // update the delta cache
130  ret = locationEntryDeltaMap_.insert(std::make_pair(moduleId,ret.first->second));
131 
132  if(!ret.second)
133  {
134  // update gps position information only
135  ret.first->second.setPosition(position);
136  }
137  }
138  else
139  {
140  throw FormatException("EELLoaderLocation only support 'gps' location type");
141  }
142  }
143  else if(eventType == "orientation")
144  {
145  InputArguments params;
146 
147  size_t posStart = 0;
148  size_t posEnd = 0;
149 
150  while(posEnd != std::string::npos)
151  {
152  posEnd = args[0].find_first_of(",",posStart);
153 
154  params.push_back(args[0].substr(posStart, posEnd - posStart));
155 
156  posStart = posEnd + 1;
157  }
158 
159  double dPitchDegrees{};
160  double dRollDegrees{};
161  double dYawDegrees{};
162 
163  try
164  {
165  for(size_t i = 0; i < params.size(); ++i)
166  {
167  switch(i)
168  {
169  case 0:
170  dPitchDegrees = Utils::ParameterConvert(params[i]).toDouble();
171  break;
172 
173  case 1:
174  dRollDegrees = Utils::ParameterConvert(params[i]).toDouble();
175  break;
176 
177  case 2:
178  dYawDegrees = Utils::ParameterConvert(params[i]).toDouble();
179  break;
180 
181  default:
182  if(params[i] != "degrees" && params[i] != "relative")
183  {
184  throw FormatException("EELLoaderLocation orientation unkown or unsupported keyword. "
185  "Only degrees and relative keywords supported.");
186  }
187  }
188  }
189  }
191  {
192  std::stringstream sstream;
193  sstream<<"EELEventGenerator: Parameter conversion error. "<<exp.what()<<std::ends;
194  throw FormatException(sstream.str());
195  }
196 
197  LocationEntry locationEntry{};
198 
199  Orientation orientation{dRollDegrees,dPitchDegrees,dYawDegrees};
200 
201  locationEntry.setOrientation(orientation);
202 
203  // update the full cache
204  auto ret = locationEntryMap_.insert(std::make_pair(moduleId,locationEntry));
205 
206  if(!ret.second)
207  {
208  // update orientation information only
209  ret.first->second.setOrientation(orientation);
210  }
211 
212  // update the delta cache
213  ret = locationEntryDeltaMap_.insert(std::make_pair(moduleId,ret.first->second));
214 
215  if(!ret.second)
216  {
217  // update orientation position information only
218  ret.first->second.setOrientation(orientation);
219  }
220  }
221  else if(eventType == "velocity")
222  {
223  InputArguments params;
224 
225  size_t posStart = 0;
226  size_t posEnd = 0;
227 
228  while(posEnd != std::string::npos)
229  {
230  posEnd = args[0].find_first_of(",",posStart);
231 
232  params.push_back(args[0].substr(posStart, posEnd - posStart));
233 
234  posStart = posEnd + 1;
235  }
236 
237  double dAzimuthDegrees{};
238  double dElevationDegrees{};
239  double dMagnitudeMetersPerSecond{};
240 
241  try
242  {
243  for(size_t i = 0; i < params.size(); ++i)
244  {
245  switch(i)
246  {
247  case 0:
248  dAzimuthDegrees = Utils::ParameterConvert(params[i]).toDouble();
249  break;
250 
251  case 1:
252  dElevationDegrees = Utils::ParameterConvert(params[i]).toDouble();
253  break;
254 
255  case 2:
256  dMagnitudeMetersPerSecond = Utils::ParameterConvert(params[i]).toDouble();
257  break;
258 
259  default:
260  if(params[i] != "degrees" &&
261  params[i] != "mps" &&
262  params[i] != "azimuth" &&
263  params[i] != "relative")
264  {
265  throw FormatException("EELLoaderLocation velocity unkown or unsupported keyword. "
266  "Only degrees, relative, mps and azimuth keywords supported.");
267  }
268  }
269  }
270  }
272  {
273  std::stringstream sstream;
274  sstream<<"EELEventGenerator: Parameter conversion error. "<<exp.what()<<std::ends;
275  throw FormatException(sstream.str());
276  }
277 
278 
279  LocationEntry locationEntry{};
280 
281  Velocity velocity{dAzimuthDegrees,dElevationDegrees,dMagnitudeMetersPerSecond};
282 
283  locationEntry.setVelocity(velocity);
284 
285 
286  // update the full cache
287  auto ret = locationEntryMap_.insert(std::make_pair(moduleId,locationEntry));
288 
289  if(!ret.second)
290  {
291  // update velocity information only
292  ret.first->second.setVelocity(velocity);
293  }
294 
295 
296  // update the delta cache
297  ret = locationEntryDeltaMap_.insert(std::make_pair(moduleId,ret.first->second));
298 
299  if(!ret.second)
300  {
301  // update velocity information only
302  ret.first->second.setVelocity(velocity);
303  }
304  }
305  }
306 }
307 
308 
310 {
311  EventInfoList eventInfoList;
312  LocationEntryMap * pCache = 0;
313 
314 
315  if(!locationEntryDeltaMap_.empty())
316  {
317  if(mode == DELTA)
318  {
319  pCache = &locationEntryDeltaMap_;
320  }
321  else
322  {
323  pCache = &locationEntryMap_;
324  }
325 
326  Events::Locations locations;
327 
328  for(const auto & entry : *pCache)
329  {
330  const Position & position = entry.second.getPosition();
331  auto optionalOrientation = entry.second.getOrientation();
332  auto optionalVelocity = entry.second.getVelocity();
333 
334  locations.push_back({entry.first,position,optionalOrientation,optionalVelocity});
335  }
336 
337  if(!locations.empty())
338  {
339  eventInfoList.push_back(EventInfo{0,
341  Events::LocationEvent(locations).serialize()});
342  }
343 
344  locationEntryDeltaMap_.clear();
345  }
346 
347  return eventInfoList;
348 }
349 
EventInfoList getEvents(EventPublishMode mode) override
Holds the velocity elements associated with an NEM&#39;s location information.
Definition: velocity.h:48
std::vector< std::string > InputArguments
#define DECLARE_EEL_LOADER_PLUGIN(X)
Definition: loaderplugin.h:91
Parameter conversion exception class.
double toDouble(double dMin=std::numeric_limits< double >::lowest(), double dMax=std::numeric_limits< double >::max()) const
const char * what() const
Definition: exception.h:62
std::list< Location > Locations
Definition: location.h:108
Contains event information to be published.
Definition: eventinfo.h:52
Holds pitch, yaw and roll.
Definition: orientation.h:45
void load(const ModuleType &modelType, const ModuleId &moduleId, const EventType &eventType, const InputArguments &args) override
std::list< EventInfo > EventInfoList
Parameter conversion class with range checks.
Holds latitude, longitude and altitude.
Definition: position.h:47