nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
dbt.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<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()));
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() != 6) {
37 NMEALIB_RETURN_ERROR(NotDBTException(context, "Invalid fields in DBT payload: expected 6, got " + std::to_string(fields.size()) + ". Payload: " + payload));
38 }
39
40 double depthFeet = 0.0;
41 double depthMeters = 0.0;
42 double depthFathoms = 0.0;
43 if (!detail::parseOptionalDouble(fields[0], depthFeet) ||
44 !detail::parseOptionalDouble(fields[2], depthMeters) ||
45 !detail::parseOptionalDouble(fields[4], depthFathoms)) {
46 NMEALIB_RETURN_ERROR(NmeaException(context, "Error parsing DBT fields"));
47 }
48
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];
52
53 return std::unique_ptr<DBT>(new DBT(std::move(*baseMessage),
54 depthFeet,
55 feetUnit,
56 depthMeters,
57 metersUnit,
58 depthFathoms,
59 fathomsUnit));
60}
61
62DBT::DBT(Message0183 baseMessage,
63 double depthFeet,
64 char feetUnit,
65 double depthMeters,
66 char metersUnit,
67 double depthFathoms,
68 char fathomsUnit) noexcept :
69 Message0183(std::move(baseMessage)),
70 depthFeet_(depthFeet),
71 feetUnit_(feetUnit),
72 depthMeters_(depthMeters),
73 metersUnit_(metersUnit),
74 depthFathoms_(depthFathoms),
75 fathomsUnit_(fathomsUnit) {}
76
77DBT::DBT(std::string talkerId,
78 double depthFeet,
79 char feetUnit,
80 double depthMeters,
81 char metersUnit,
82 double depthFathoms,
83 char fathomsUnit) :
84 Message0183(*Message0183::create(composeRaw(talkerId,
85 depthFeet,
86 feetUnit,
87 depthMeters,
88 metersUnit,
89 depthFathoms,
90 fathomsUnit))),
91 depthFeet_(depthFeet),
92 feetUnit_(feetUnit),
93 depthMeters_(depthMeters),
94 metersUnit_(metersUnit),
95 depthFathoms_(depthFathoms),
96 fathomsUnit_(fathomsUnit) {}
97
98std::unique_ptr<nmealib::Message> DBT::clone() const {
99 return std::unique_ptr<DBT>(new DBT(*this));
100}
101
102std::string DBT::getStringContent(bool verbose) const noexcept {
103 std::ostringstream ss;
104 ss << this->toString(verbose);
105 ss << std::fixed << std::setprecision(2);
106
107 if (verbose) {
108 ss << "\tDepth: " << depthFeet_ << " " << feetUnit_ << "\n";
109 ss << "\tDepth (meters): " << depthMeters_ << " " << metersUnit_ << "\n";
110 ss << "\tDepth (fathoms): " << depthFathoms_ << " " << fathomsUnit_;
111 ss << "\n";
112 } else {
113 ss << "Depth=" << depthFeet_ << feetUnit_
114 << ", " << depthMeters_ << metersUnit_
115 << ", " << depthFathoms_ << fathomsUnit_;
116 }
117 return ss.str();
118}
119
120std::string DBT::composeRaw(const std::string& talkerId,
121 double depthFeet,
122 char feetUnit,
123 double depthMeters,
124 char metersUnit,
125 double depthFathoms,
126 char fathomsUnit) {
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;
135
136 std::string payload = payloadStream.str();
137 return "$" + payload + "\r\n";
138}
139
140double DBT::getDepthFeet() const noexcept {
141 return depthFeet_;
142}
143
144char DBT::getFeetUnit() const noexcept {
145 return feetUnit_;
146}
147
148double DBT::getDepthMeters() const noexcept {
149 return depthMeters_;
150}
151
152char DBT::getMetersUnit() const noexcept {
153 return metersUnit_;
154}
155
156double DBT::getDepthFathoms() const noexcept {
157 return depthFathoms_;
158}
159
160char DBT::getFathomsUnit() const noexcept {
161 return fathomsUnit_;
162}
163
164bool DBT::operator==(const DBT& other) const noexcept {
165 return Message0183::operator==(other);
166}
167
168} // namespace nmea0183
169} // namespace nmealib
Represents a parsed NMEA 0183 DBT (Depth Below Transducer) sentence.
Definition dbt.h:37
char getMetersUnit() const noexcept
Get the meters unit indicator.
Definition dbt.cpp:152
DBT(std::string talkerId, double depthFeet, char feetUnit, double depthMeters, char metersUnit, double depthFathoms, char fathomsUnit)
Construct a DBT message from individual field values.
Definition dbt.cpp:77
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this DBT message.
Definition dbt.cpp:98
bool operator==(const DBT &other) const noexcept
Compare two DBT messages for equality.
Definition dbt.cpp:164
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
Definition dbt.cpp:102
char getFeetUnit() const noexcept
Get the feet unit indicator.
Definition dbt.cpp:144
char getFathomsUnit() const noexcept
Get the fathoms unit indicator.
Definition dbt.cpp:160
double getDepthFeet() const noexcept
Get the depth in feet.
Definition dbt.cpp:140
double getDepthMeters() const noexcept
Get the depth in meters.
Definition dbt.cpp:148
double getDepthFathoms() const noexcept
Get the depth in fathoms.
Definition dbt.cpp:156
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