13std::unique_ptr<VWR> VWR::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"VWR::create";
15 if (baseMessage->getSentenceType() !=
"VWR") {
16 NMEALIB_RETURN_ERROR(NotVWRException(context,
"Expected sentence type 'VWR', got " + baseMessage->getSentenceType()));
19 std::string payload = baseMessage->getPayload();
20 std::istringstream ss(payload);
22 std::vector<std::string> fields;
24 while (std::getline(ss, token,
',')) {
25 fields.push_back(token);
28 if (!payload.empty() && payload.back() ==
',') {
32 if (!fields.empty()) {
33 fields.erase(fields.begin());
36 if (fields.size() != 8) {
37 NMEALIB_RETURN_ERROR(NotVWRException(context,
"Invalid fields in VWR payload: expected 8, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double windAngle = 0.0;
41 double speedKnots = 0.0;
42 double speedMps = 0.0;
43 double speedKph = 0.0;
51 char windSide = fields[1].empty() ?
'\0' : fields[1][0];
52 char speedKnotsUnit = fields[3].empty() ?
'\0' : fields[3][0];
53 char speedMpsUnit = fields[5].empty() ?
'\0' : fields[5][0];
54 char speedKphUnit = fields[7].empty() ?
'\0' : fields[7][0];
56 return std::unique_ptr<VWR>(
new VWR(std::move(*baseMessage),
67VWR::VWR(Message0183 baseMessage,
75 char speedKphUnit) noexcept
76 : Message0183(std::move(baseMessage)),
77 windAngle_(windAngle),
79 speedKnots_(speedKnots),
80 speedKnotsUnit_(speedKnotsUnit),
82 speedMpsUnit_(speedMpsUnit),
84 speedKphUnit_(speedKphUnit) {}
86VWR::VWR(std::string talkerId,
98 windAngle_(windAngle),
100 speedKnots_(speedKnots),
101 speedKnotsUnit_(
'N'),
105 speedKphUnit_(
'K') {}
108 return std::unique_ptr<VWR>(
new VWR(*
this));
112 std::ostringstream ss;
113 ss << this->toString(verbose);
114 ss << std::fixed << std::setprecision(2);
117 ss <<
"\tWind Angle: " << windAngle_ <<
"\n";
118 ss <<
"\tWind Side: " << windSide_ <<
"\n";
119 ss <<
"\tSpeed: " << speedKnots_ <<
"kts\n";
120 ss <<
"\tSpeed: " << speedMps_ <<
" m/s\n";
121 ss <<
"\tSpeed: " << speedKph_ <<
" kph";
124 ss <<
"Angle=" << windAngle_ << windSide_
125 <<
", Knots=" << speedKnots_
126 <<
", m/s=" << speedMps_
127 <<
", KPH=" << speedKph_;
133std::string VWR::composeRaw(
const std::string& talkerId,
139 std::ostringstream payloadStream;
140 payloadStream << talkerId <<
"VWR,";
141 payloadStream << std::fixed << std::setprecision(1) << windAngle <<
",";
142 payloadStream << windSide <<
",";
143 payloadStream << std::fixed << std::setprecision(1) << speedKnots <<
",N,";
144 payloadStream << std::fixed << std::setprecision(1) << speedMps <<
",M,";
145 payloadStream << std::fixed << std::setprecision(1) << speedKph <<
",K";
147 std::string payload = payloadStream.str();
148 return "$" + payload +
"\r\n";
164 return speedKnotsUnit_;
172 return speedMpsUnit_;
180 return speedKphUnit_;
Represents an NMEA 0183 sentence.
bool operator==(const Message0183 &other) const noexcept
Compares two Message0183 objects for equality based on their content and timestamp.
Represents a parsed NMEA 0183 VWR sentence (Relative Wind Speed and Angle).
double getSpeedKph() const noexcept
Returns the wind speed in kilometers per hour.
double getWindAngle() const noexcept
Returns the wind direction magnitude in degrees (0–180).
bool operator==(const VWR &other) const noexcept
char getSpeedKnotsUnit() const noexcept
Returns the knots unit indicator character.
VWR(std::string talkerId, double windAngle, char windSide, double speedKnots, double speedMps, double speedKph)
Construct a VWR sentence from individual field values.
char getSpeedMpsUnit() const noexcept
Returns the meters-per-second unit indicator character.
char getWindSide() const noexcept
Returns the wind direction side relative to the vessel.
std::string getStringContent(bool verbose) const noexcept override
Returns a human-readable string representation of the message content.
double getSpeedKnots() const noexcept
Returns the wind speed in knots.
std::unique_ptr< nmealib::Message > clone() const override
Creates a polymorphic deep copy of this Message0183.
double getSpeedMps() const noexcept
Returns the wind speed in meters per second.
char getSpeedKphUnit() const noexcept
Returns the kilometers-per-hour unit indicator character.
#define NMEALIB_RETURN_ERROR(exceptionExpr)
bool parseOptionalDouble(const std::string &text, double &value) noexcept