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