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()));
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() != 2) {
37 NMEALIB_RETURN_ERROR(NotMTWException(context,
"Invalid fields in MTW payload: expected 2, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double temperature = 0.0;
45 char unit = fields[1].empty() ?
'\0' : fields[1][0];
47 return std::unique_ptr<MTW>(
new MTW(std::move(*baseMessage),
52MTW::MTW(Message0183 baseMessage,
55 : Message0183(std::move(baseMessage)),
56 temperature_(temperature),
59MTW::MTW(std::string talkerId,
63 temperature_(temperature),
67 return std::unique_ptr<MTW>(
new MTW(*
this));
71 std::ostringstream ss;
72 ss << this->toString(verbose);
73 ss << std::fixed << std::setprecision(2);
76 ss <<
"\tTemperature: " << temperature_ <<
" " << unit_;
79 ss <<
"Temperature=" << temperature_ << unit_;
85std::string MTW::composeRaw(
const std::string& talkerId,
88 std::ostringstream payloadStream;
89 payloadStream << talkerId <<
"MTW,";
90 payloadStream << std::fixed << std::setprecision(1) << temperature <<
",";
91 payloadStream << unit;
93 std::string payload = payloadStream.str();
94 return "$" + payload +
"\r\n";
Represents a parsed NMEA 0183 MTW (Mean Temperature of Water) sentence.
double getTemperature() const noexcept
Get the water temperature value.
char getUnit() const noexcept
Get the temperature unit character.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this MTW message.
bool operator==(const MTW &other) const noexcept
Compare two MTW messages for equality.
MTW(std::string talkerId, double temperature, char unit)
Construct an MTW 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