nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
hdm.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<HDM> HDM::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context = "HDM::create";
15 if (baseMessage->getSentenceType() != "HDM") {
16 NMEALIB_RETURN_ERROR(NotHDMException(context, "Expected sentence type 'HDM', 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() != 2) {
37 NMEALIB_RETURN_ERROR(NotHDMException(context, "Invalid fields in HDM payload: expected 2, got " + std::to_string(fields.size()) + ". Payload: " + payload));
38 }
39
40 double heading = 0.0;
41 if (!detail::parseOptionalDouble(fields[0], heading)) {
42 NMEALIB_RETURN_ERROR(NmeaException(context, "Error parsing HDM fields"));
43 }
44
45 char indicator = fields[1].empty() ? '\0' : fields[1][0];
46
47 return std::unique_ptr<HDM>(new HDM(std::move(*baseMessage),
48 heading,
49 indicator));
50}
51
52HDM::HDM(Message0183 baseMessage,
53 double heading,
54 char indicator) noexcept
55 : Message0183(std::move(baseMessage)),
56 heading_(heading),
57 indicator_(indicator) {}
58
59HDM::HDM(std::string talkerId,
60 double heading,
61 char indicator)
62 : Message0183(*Message0183::create(composeRaw(talkerId, heading, indicator))),
63 heading_(heading),
64 indicator_(indicator) {}
65
66std::unique_ptr<nmealib::Message> HDM::clone() const {
67 return std::unique_ptr<HDM>(new HDM(*this));
68}
69
70std::string HDM::getStringContent(bool verbose) const noexcept {
71 std::ostringstream ss;
72 ss << this->toString(verbose);
73 ss << std::fixed << std::setprecision(1);
74
75 if (verbose) {
76 ss << "\tHeading: " << heading_ << " " << indicator_;
77 ss << "\n";
78 } else {
79 ss << "Heading=" << heading_ << indicator_;
80 }
81
82 return ss.str();
83}
84
85std::string HDM::composeRaw(const std::string& talkerId,
86 double heading,
87 char indicator) {
88 std::ostringstream payloadStream;
89 payloadStream << talkerId << "HDM,";
90 payloadStream << std::fixed << std::setprecision(1) << heading << ",";
91 payloadStream << indicator;
92
93 std::string payload = payloadStream.str();
94 return "$" + payload + "\r\n";
95}
96
97double HDM::getHeading() const noexcept {
98 return heading_;
99}
100
101char HDM::getIndicator() const noexcept {
102 return indicator_;
103}
104
105bool HDM::operator==(const HDM& other) const noexcept {
106 return Message0183::operator==(other);
107}
108
109} // namespace nmea0183
110} // namespace nmealib
Represents a parsed NMEA 0183 HDM (Heading – Magnetic) sentence.
Definition hdm.h:47
double getHeading() const noexcept
Get the vessel heading in degrees magnetic.
Definition hdm.cpp:97
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
Definition hdm.cpp:70
char getIndicator() const noexcept
Get the magnetic heading indicator character.
Definition hdm.cpp:101
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this HDM message.
Definition hdm.cpp:66
bool operator==(const HDM &other) const noexcept
Compare two HDM messages for equality.
Definition hdm.cpp:105
HDM(std::string talkerId, double heading, char indicator)
Construct an HDM message from individual field values.
Definition hdm.cpp:59
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