13std::unique_ptr<DBT> DBT::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"DBT::create";
15 if (baseMessage->getSentenceType() !=
"DBT") {
16 NMEALIB_RETURN_ERROR(NotDBTException(context,
"Expected sentence type 'DBT', 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() != 6) {
37 NMEALIB_RETURN_ERROR(NotDBTException(context,
"Invalid fields in DBT payload: expected 6, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double depthFeet = 0.0;
41 double depthMeters = 0.0;
42 double depthFathoms = 0.0;
49 char feetUnit = fields[1].empty() ?
'\0' : fields[1][0];
50 char metersUnit = fields[3].empty() ?
'\0' : fields[3][0];
51 char fathomsUnit = fields[5].empty() ?
'\0' : fields[5][0];
53 return std::unique_ptr<DBT>(
new DBT(std::move(*baseMessage),
62DBT::DBT(Message0183 baseMessage,
68 char fathomsUnit) noexcept :
69 Message0183(std::move(baseMessage)),
70 depthFeet_(depthFeet),
72 depthMeters_(depthMeters),
73 metersUnit_(metersUnit),
74 depthFathoms_(depthFathoms),
75 fathomsUnit_(fathomsUnit) {}
77DBT::DBT(std::string talkerId,
91 depthFeet_(depthFeet),
93 depthMeters_(depthMeters),
94 metersUnit_(metersUnit),
95 depthFathoms_(depthFathoms),
96 fathomsUnit_(fathomsUnit) {}
99 return std::unique_ptr<DBT>(
new DBT(*
this));
103 std::ostringstream ss;
104 ss << this->toString(verbose);
105 ss << std::fixed << std::setprecision(2);
108 ss <<
"\tDepth: " << depthFeet_ <<
" " << feetUnit_ <<
"\n";
109 ss <<
"\tDepth (meters): " << depthMeters_ <<
" " << metersUnit_ <<
"\n";
110 ss <<
"\tDepth (fathoms): " << depthFathoms_ <<
" " << fathomsUnit_;
113 ss <<
"Depth=" << depthFeet_ << feetUnit_
114 <<
", " << depthMeters_ << metersUnit_
115 <<
", " << depthFathoms_ << fathomsUnit_;
120std::string DBT::composeRaw(
const std::string& talkerId,
127 std::ostringstream payloadStream;
128 payloadStream << talkerId <<
"DBT,";
129 payloadStream << std::fixed << std::setprecision(1) << depthFeet <<
",";
130 payloadStream << feetUnit <<
",";
131 payloadStream << std::fixed << std::setprecision(1) << depthMeters <<
",";
132 payloadStream << metersUnit <<
",";
133 payloadStream << std::fixed << std::setprecision(1) << depthFathoms <<
",";
134 payloadStream << fathomsUnit;
136 std::string payload = payloadStream.str();
137 return "$" + payload +
"\r\n";
157 return depthFathoms_;
Represents a parsed NMEA 0183 DBT (Depth Below Transducer) sentence.
char getMetersUnit() const noexcept
Get the meters unit indicator.
DBT(std::string talkerId, double depthFeet, char feetUnit, double depthMeters, char metersUnit, double depthFathoms, char fathomsUnit)
Construct a DBT message from individual field values.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this DBT message.
bool operator==(const DBT &other) const noexcept
Compare two DBT messages for equality.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
char getFeetUnit() const noexcept
Get the feet unit indicator.
char getFathomsUnit() const noexcept
Get the fathoms unit indicator.
double getDepthFeet() const noexcept
Get the depth in feet.
double getDepthMeters() const noexcept
Get the depth in meters.
double getDepthFathoms() const noexcept
Get the depth in fathoms.
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