13std::unique_ptr<VHW> VHW::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"VHW::create";
15 if (baseMessage->getSentenceType() !=
"VHW") {
16 NMEALIB_RETURN_ERROR(NotVHWException(context,
"Expected sentence type 'VHW', 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(NotVHWException(context,
"Invalid fields in VHW payload: expected 8, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double headingTrue = 0.0;
41 double headingMagnetic = 0.0;
42 double speedKnots = 0.0;
43 double speedKph = 0.0;
51 char headingTrueType = fields[1].empty() ?
'\0' : fields[1][0];
52 char headingMagneticType = fields[3].empty() ?
'\0' : fields[3][0];
53 char speedKnotsType = fields[5].empty() ?
'\0' : fields[5][0];
54 char speedKphType = fields[7].empty() ?
'\0' : fields[7][0];
56 return std::unique_ptr<VHW>(
new VHW(std::move(*baseMessage),
67VHW::VHW(Message0183 baseMessage,
70 double headingMagnetic,
71 char headingMagneticType,
75 char speedKphType) noexcept
76 : Message0183(std::move(baseMessage)),
77 headingTrue_(headingTrue),
78 headingTrueType_(headingTrueType),
79 headingMagnetic_(headingMagnetic),
80 headingMagneticType_(headingMagneticType),
81 speedKnots_(speedKnots),
82 speedKnotsType_(speedKnotsType),
84 speedKphType_(speedKphType) {}
86VHW::VHW(std::string talkerId,
88 double headingMagnetic,
96 headingTrue_(headingTrue),
97 headingTrueType_(
'T'),
98 headingMagnetic_(headingMagnetic),
99 headingMagneticType_(
'M'),
100 speedKnots_(speedKnots),
101 speedKnotsType_(
'N'),
103 speedKphType_(
'K') {}
106 return std::unique_ptr<VHW>(
new VHW(*
this));
110 std::ostringstream ss;
111 ss << this->toString(verbose);
112 ss << std::fixed << std::setprecision(2);
115 ss <<
"\tHeading: " << headingTrue_ <<
" " << headingTrueType_ <<
"\n";
116 ss <<
"\tHeading: " << headingMagnetic_ <<
" " << headingMagneticType_ <<
"\n";
117 ss <<
"\tSpeed: " << speedKnots_ <<
"kts\n"
118 <<
"\tSpeed: " << speedKph_ <<
"kph";
121 ss <<
"True=" << headingTrue_
122 <<
", Magnetic=" << headingMagnetic_
123 <<
", Knots=" << speedKnots_
124 <<
", KPH=" << speedKph_;
130std::string VHW::composeRaw(
const std::string& talkerId,
132 double headingMagnetic,
135 std::ostringstream payloadStream;
136 payloadStream << talkerId <<
"VHW,";
137 payloadStream << std::fixed << std::setprecision(2) << headingTrue <<
",T,";
138 payloadStream << std::fixed << std::setprecision(2) << headingMagnetic <<
",M,";
139 payloadStream << std::fixed << std::setprecision(3) << speedKnots <<
",N,";
140 payloadStream << std::fixed << std::setprecision(3) << speedKph <<
",K";
142 std::string payload = payloadStream.str();
143 return "$" + payload +
"\r\n";
151 return headingTrueType_;
155 return headingMagnetic_;
159 return headingMagneticType_;
167 return speedKnotsType_;
175 return speedKphType_;
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 VHW (Water Speed and Heading) sentence.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this VHW message.
char getSpeedKphType() const noexcept
Get km/h unit indicator (typically 'K').
char getHeadingTrueType() const noexcept
Get true heading type indicator (typically 'T').
double getHeadingMagnetic() const noexcept
Get magnetic heading in degrees.
bool operator==(const VHW &other) const noexcept
Compare two VHW messages for equality.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
double getHeadingTrue() const noexcept
Get true heading in degrees.
char getHeadingMagneticType() const noexcept
Get magnetic heading type indicator (typically 'M').
double getSpeedKph() const noexcept
Get speed through water in kilometers per hour.
double getSpeedKnots() const noexcept
Get speed through water in knots.
VHW(std::string talkerId, double headingTrue, double headingMagnetic, double speedKnots, double speedKph)
Construct a VHW message from individual field values.
char getSpeedKnotsType() const noexcept
Get knots unit indicator (typically 'N').
#define NMEALIB_RETURN_ERROR(exceptionExpr)
bool parseOptionalDouble(const std::string &text, double &value) noexcept