nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
mwv.cpp
Go to the documentation of this file.
2
5
6#include <iomanip>
7#include <sstream>
8#include <vector>
9
10namespace nmealib {
11namespace nmea0183 {
12
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()));
17 }
18
19 std::string payload = baseMessage->getPayload();
20 std::istringstream ss(payload);
21 std::string token;
22 std::vector<std::string> fields;
23
24 while (std::getline(ss, token, ',')) {
25 fields.push_back(token);
26 }
27
28 if (!payload.empty() && payload.back() == ',') {
29 fields.push_back("");
30 }
31
32 if (!fields.empty()) {
33 fields.erase(fields.begin());
34 }
35
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));
38 }
39
40 double windAngle = 0.0;
41 double windSpeed = 0.0;
42 if (!detail::parseOptionalDouble(fields[0], windAngle) ||
43 !detail::parseOptionalDouble(fields[2], windSpeed)) {
44 NMEALIB_RETURN_ERROR(NmeaException(context, "Error parsing MWV fields"));
45 }
46
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];
50
51 return std::unique_ptr<MWV>(new MWV(std::move(*baseMessage),
52 windAngle,
53 reference,
54 windSpeed,
55 windSpeedUnits,
56 status));
57}
58
59MWV::MWV(Message0183 baseMessage,
60 double windAngle,
61 char reference,
62 double windSpeed,
63 char windSpeedUnits,
64 char status) noexcept
65 : Message0183(std::move(baseMessage)),
66 windAngle_(windAngle),
67 reference_(reference),
68 windSpeed_(windSpeed),
69 windSpeedUnits_(windSpeedUnits),
70 status_(status) {}
71
72MWV::MWV(std::string talkerId,
73 double windAngle,
74 char reference,
75 double windSpeed,
76 char windSpeedUnits,
77 char status)
78 : Message0183(*Message0183::create(composeRaw(talkerId,
79 windAngle,
80 reference,
81 windSpeed,
82 windSpeedUnits,
83 status))),
84 windAngle_(windAngle),
85 reference_(reference),
86 windSpeed_(windSpeed),
87 windSpeedUnits_(windSpeedUnits),
88 status_(status) {}
89
90std::unique_ptr<nmealib::Message> MWV::clone() const {
91 return std::unique_ptr<MWV>(new MWV(*this));
92}
93
94std::string MWV::getStringContent(bool verbose) const noexcept {
95 std::ostringstream ss;
96 ss << this->toString(verbose);
97 ss << std::fixed << std::setprecision(2);
98
99 if (verbose) {
100 ss << "\tWind Angle: " << windAngle_ << " " << reference_ << "\n";
101 ss << "\tWind Speed: " << windSpeed_ << " " << windSpeedUnits_ << "\n";
102 ss << "\tStatus: " << status_;
103 ss << "\n";
104 } else {
105 ss << "Angle=" << windAngle_ << reference_
106 << ", Speed=" << windSpeed_ << windSpeedUnits_
107 << ", Status=" << status_;
108 }
109
110 return ss.str();
111}
112
113std::string MWV::composeRaw(const std::string& talkerId,
114 double windAngle,
115 char reference,
116 double windSpeed,
117 char windSpeedUnits,
118 char status) {
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;
126
127 std::string payload = payloadStream.str();
128 return "$" + payload + "\r\n";
129}
130
131double MWV::getWindAngle() const noexcept {
132 return windAngle_;
133}
134
135char MWV::getReference() const noexcept {
136 return reference_;
137}
138
139double MWV::getWindSpeed() const noexcept {
140 return windSpeed_;
141}
142
143char MWV::getWindSpeedUnits() const noexcept {
144 return windSpeedUnits_;
145}
146
147char MWV::getStatus() const noexcept {
148 return status_;
149}
150
151bool MWV::operator==(const MWV& other) const noexcept {
152 return Message0183::operator==(other);
153}
154
155} // namespace nmea0183
156} // namespace nmealib
Represents a parsed NMEA 0183 MWV (Wind Speed and Angle) sentence.
Definition mwv.h:37
char getReference() const noexcept
Get reference indicator.
Definition mwv.cpp:135
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this MWV message.
Definition mwv.cpp:90
double getWindSpeed() const noexcept
Get wind speed value.
Definition mwv.cpp:139
double getWindAngle() const noexcept
Get wind angle in degrees.
Definition mwv.cpp:131
char getStatus() const noexcept
Get data status indicator.
Definition mwv.cpp:147
bool operator==(const MWV &other) const noexcept
Compare two MWV messages for equality.
Definition mwv.cpp:151
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
Definition mwv.cpp:94
char getWindSpeedUnits() const noexcept
Get wind speed unit indicator.
Definition mwv.cpp:143
MWV(std::string talkerId, double windAngle, char reference, double windSpeed, char windSpeedUnits, char status)
Construct an MWV message from individual field values.
Definition mwv.cpp:72
Represents an NMEA 0183 sentence.
Definition nmea0183.h:98
bool operator==(const Message0183 &other) const noexcept
Compares two Message0183 objects for equality based on their content and timestamp.
Definition nmea0183.cpp:204
#define NMEALIB_RETURN_ERROR(exceptionExpr)
bool parseOptionalDouble(const std::string &text, double &value) noexcept
Definition parse.h:86