13std::unique_ptr<MWV> MWV::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"MWV::create";
15 if (baseMessage->getSentenceType() !=
"MWV") {
16 NMEALIB_RETURN_ERROR(NotMWVException(context,
"Expected sentence type 'MWV', 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() != 5) {
37 NMEALIB_RETURN_ERROR(NotMWVException(context,
"Invalid fields in MWV payload: expected 5, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double windAngle = 0.0;
41 double windSpeed = 0.0;
47 char reference = fields[1].empty() ?
'\0' : fields[1][0];
48 char windSpeedUnits = fields[3].empty() ?
'\0' : fields[3][0];
49 char status = fields[4].empty() ?
'\0' : fields[4][0];
51 return std::unique_ptr<MWV>(
new MWV(std::move(*baseMessage),
59MWV::MWV(Message0183 baseMessage,
65 : Message0183(std::move(baseMessage)),
66 windAngle_(windAngle),
67 reference_(reference),
68 windSpeed_(windSpeed),
69 windSpeedUnits_(windSpeedUnits),
72MWV::MWV(std::string talkerId,
84 windAngle_(windAngle),
85 reference_(reference),
86 windSpeed_(windSpeed),
87 windSpeedUnits_(windSpeedUnits),
91 return std::unique_ptr<MWV>(
new MWV(*
this));
95 std::ostringstream ss;
96 ss << this->toString(verbose);
97 ss << std::fixed << std::setprecision(2);
100 ss <<
"\tWind Angle: " << windAngle_ <<
" " << reference_ <<
"\n";
101 ss <<
"\tWind Speed: " << windSpeed_ <<
" " << windSpeedUnits_ <<
"\n";
102 ss <<
"\tStatus: " << status_;
105 ss <<
"Angle=" << windAngle_ << reference_
106 <<
", Speed=" << windSpeed_ << windSpeedUnits_
107 <<
", Status=" << status_;
113std::string MWV::composeRaw(
const std::string& talkerId,
119 std::ostringstream payloadStream;
120 payloadStream << talkerId <<
"MWV,";
121 payloadStream << std::fixed << std::setprecision(1) << windAngle <<
",";
122 payloadStream << reference <<
",";
123 payloadStream << std::fixed << std::setprecision(1) << windSpeed <<
",";
124 payloadStream << windSpeedUnits <<
",";
125 payloadStream << status;
127 std::string payload = payloadStream.str();
128 return "$" + payload +
"\r\n";
144 return windSpeedUnits_;
Represents a parsed NMEA 0183 MWV (Wind Speed and Angle) sentence.
char getReference() const noexcept
Get reference indicator.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this MWV message.
double getWindSpeed() const noexcept
Get wind speed value.
double getWindAngle() const noexcept
Get wind angle in degrees.
char getStatus() const noexcept
Get data status indicator.
bool operator==(const MWV &other) const noexcept
Compare two MWV messages for equality.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
char getWindSpeedUnits() const noexcept
Get wind speed unit indicator.
MWV(std::string talkerId, double windAngle, char reference, double windSpeed, char windSpeedUnits, char status)
Construct an MWV message from individual field values.
Represents an NMEA 0183 sentence.
bool operator==(const Message0183 &other) const noexcept
Compares two Message0183 objects for equality based on their content and timestamp.
#define NMEALIB_RETURN_ERROR(exceptionExpr)
bool parseOptionalDouble(const std::string &text, double &value) noexcept