EMANE  1.2.1
locationevent.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016 - Adjacent Link LLC, Bridgewater,
3  * New Jersey
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 Adjacent Link 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 
35 #include "locationevent.pb.h"
36 
37 class EMANE::Events::LocationEvent::Implementation
38 {
39 public:
40  Implementation(const Locations & locations):
41  locations_{locations}{}
42 
43  const Locations & getLocations() const
44  {
45  return locations_;
46  }
47 
48 private:
49  Locations locations_;
50 };
51 
54 {
56 
57  if(!msg.ParseFromString(serialization))
58  {
59  throw SerializationException("unable to deserialize : LocationEvent");
60  }
61 
62  Locations locations;
63 
64  for(const auto & location : msg.locations())
65  {
66  const auto & positionMessage = location.position();
67  NEMId nemId{static_cast<EMANE::NEMId>(location.nemid())};
68 
69  Position position{positionMessage.latitudedegrees(),
70  positionMessage.longitudedegrees(),
71  positionMessage.altitudemeters()};
72 
73  // optional
74  Velocity velocity{};
75 
76  // optional
77  Orientation orientation{};
78 
79  if(location.has_velocity())
80  {
81  const auto & velocityMessage = location.velocity();
82 
83  velocity = Velocity{velocityMessage.azimuthdegrees(),
84  velocityMessage.elevationdegrees(),
85  velocityMessage.magnitudemeterspersecond()};
86  }
87 
88  if(location.has_orientation())
89  {
90  const auto & orientationMessage = location.orientation();
91 
92  orientation = Orientation{orientationMessage.rolldegrees(),
93  orientationMessage.pitchdegrees(),
94  orientationMessage.yawdegrees()};
95  }
96 
97  locations.push_back({nemId,
98  position,
99  {orientation,location.has_orientation()},
100  {velocity,location.has_velocity()}});
101  }
102 
103  pImpl_.reset(new Implementation{locations});
104 }
105 
107  Event{IDENTIFIER},
108  pImpl_{new Implementation{locations}}{}
109 
111  Event{IDENTIFIER},
112  pImpl_{new Implementation{rhs.getLocations()}}{}
113 
115 {
116  pImpl_.reset(new Implementation{rhs.getLocations()});
117  return *this;
118 }
119 
121  Event{IDENTIFIER},
122  pImpl_{new Implementation{{}}}
123 {
124  rval.pImpl_.swap(pImpl_);
125 }
126 
128 {
129  rval.pImpl_.swap(pImpl_);
130  return *this;
131 }
132 
134 
136 {
137  return pImpl_->getLocations();
138 }
139 
141 {
142  Serialization serialization;
143 
145 
146  for(auto & location : pImpl_->getLocations())
147  {
148  auto pLocationMessage = msg.add_locations();
149 
150  pLocationMessage->set_nemid(location.getNEMId());
151 
152  auto pPositionMessage = pLocationMessage->mutable_position();
153 
154  const auto & position = location.getPosition();
155 
156  pPositionMessage->set_latitudedegrees(position.getLatitudeDegrees());
157  pPositionMessage->set_longitudedegrees(position.getLongitudeDegrees());
158  pPositionMessage->set_altitudemeters(position.getAltitudeMeters());
159 
160  auto optionalVelocity = location.getVelocity();
161 
162  if(optionalVelocity.second)
163  {
164  auto pVelocityMessage = pLocationMessage->mutable_velocity();
165 
166  pVelocityMessage->set_azimuthdegrees(optionalVelocity.first.getAzimuthDegrees());
167  pVelocityMessage->set_elevationdegrees(optionalVelocity.first.getElevationDegrees());
168  pVelocityMessage->set_magnitudemeterspersecond(optionalVelocity.first.getMagnitudeMetersPerSecond());
169  }
170 
171  auto optionalOrientation = location.getOrientation();
172 
173  if(optionalOrientation.second)
174  {
175  auto pOrientationMessage = pLocationMessage->mutable_orientation();
176 
177  pOrientationMessage->set_yawdegrees(optionalOrientation.first.getYawDegrees());
178  pOrientationMessage->set_pitchdegrees(optionalOrientation.first.getPitchDegrees());
179  pOrientationMessage->set_rolldegrees(optionalOrientation.first.getRollDegrees());
180  }
181  }
182 
183  if(!msg.SerializeToString(&serialization))
184  {
185  throw SerializationException("unable to serialize : LocationEvent");
186  }
187 
188  return serialization;
189 }
std::string Serialization
Definition: serializable.h:42
Holds the velocity elements associated with an NEM&#39;s location information.
Definition: velocity.h:48
SerializationException is thrown when an exception occurs during serialization or deserialization of ...
Event interface is the base for all events.
Definition: event.h:46
A location event is usd to set the position, orientation and velocity information for one or more NEM...
Definition: locationevent.h:52
LocationEvent & operator=(const LocationEvent &rhs)
LocationEvent(const Serialization &serialization)
Serialization serialize() const override
std::uint16_t NEMId
Definition: types.h:52
std::list< Location > Locations
Definition: location.h:108
Holds pitch, yaw and roll.
Definition: orientation.h:45
Holds latitude, longitude and altitude.
Definition: position.h:47
const Locations & getLocations() const