LinearPathTelemetrySimulator.cpp
Go to the documentation of this file.
2 #include <swarmio/data/Variant.pb.h>
3 #include <swarmio/data/discovery/Schema.pb.h>
4 
5 using namespace swarmio;
6 using namespace swarmio::simulator;
7 using namespace std::chrono_literals;
8 
9 LinearPathTelemetrySimulator::LinearPathTelemetrySimulator(services::telemetry::Service& telemetryService, const std::string& name, SimulatedLocation firstPoint, SimulatedLocation secondPoint, std::chrono::seconds duration)
10  : _telemetryService(telemetryService), _firstPoint(firstPoint), _secondPoint(secondPoint), _duration(duration), _shouldStop(false), _name(name)
11 {
12  // Set schema
13  data::discovery::Field field;
14  auto& subfields = *field.mutable_schema()->mutable_fields();
15  subfields["longitude"].set_type(data::discovery::Type::DOUBLE);
16  subfields["latitude"].set_type(data::discovery::Type::DOUBLE);
17  subfields["altitude"].set_type(data::discovery::Type::DOUBLE);
18  telemetryService.SetFieldDefinitionForKey(name, field, false);
19 
20  // Start thread
21  _thread = std::make_unique<std::thread>(&LinearPathTelemetrySimulator::Worker, this);
22 }
23 
25 {
26  uint64_t unit = _duration.count() * 20;
27  uint64_t cycle = 0;
28  while(!_shouldStop)
29  {
30  // Calculate current stage
31  double stage = (cycle % unit) / (double)unit * 2.0;
32  if (stage > 1.0)
33  {
34  stage = 2.0 - stage;
35  }
36 
37  // Build value
38  data::Variant value;
39  auto& pairs = *value.mutable_map_value()->mutable_pairs();
40  pairs["longitude"].set_double_value(_firstPoint.GetLongitude() + (_secondPoint.GetLongitude() - _firstPoint.GetLongitude()) * stage);
41  pairs["latitude"].set_double_value(_firstPoint.GetLatitude() + (_secondPoint.GetLatitude() - _firstPoint.GetLatitude()) * stage);
42  pairs["altitude"].set_double_value(_firstPoint.GetAltitude() + (_secondPoint.GetAltitude() - _firstPoint.GetAltitude()) * stage);
44 
45  // Next cycle
46  ++cycle;
47  std::this_thread::sleep_for(100ms);
48  }
49 }
Telemetry Service can subscribe to receive updates from remote nodes on named values.
std::unique_ptr< std::thread > _thread
Worker thread.
void SetValue(const std::string &key, const data::Variant &value)
Add or update a value in the local telemetry cache.
void SetFieldDefinitionForKey(const std::string &key, const data::discovery::Field &field, bool includeInStatus)
Add field to the schema.
A location described by a latitude, a longitude and an altitude.
services::telemetry::Service & _telemetryService
Telemetry service.
std::chrono::seconds _duration
Time it takes to complete one trip.
LinearPathTelemetrySimulator(services::telemetry::Service &telemetryService, const std::string &name, SimulatedLocation firstPoint, SimulatedLocation secondPoint, std::chrono::seconds duration)
Constructor.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48