13std::unique_ptr<DPT> DPT::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"DPT::create";
15 if (baseMessage->getSentenceType() !=
"DPT") {
16 NMEALIB_RETURN_ERROR(NotDPTException(context,
"Expected sentence type 'DPT', 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() != 3) {
37 NMEALIB_RETURN_ERROR(NotDPTException(context,
"Invalid fields in DPT payload: expected 3, got " + std::to_string(fields.size()) +
". Payload: " + payload));
40 double waterDepthMeters = 0.0;
41 double transducerOffset = 0.0;
42 double maxRangeScale = 0.0;
49 return std::unique_ptr<DPT>(
new DPT(std::move(*baseMessage),
55DPT::DPT(Message0183 baseMessage,
56 double waterDepthMeters,
57 double transducerOffset,
58 double maxRangeScale) noexcept :
59 Message0183(std::move(baseMessage)),
60 waterDepthMeters_(waterDepthMeters),
61 transducerOffset_(transducerOffset),
62 maxRangeScale_(maxRangeScale) {}
64DPT::DPT(std::string talkerId,
65 double waterDepthMeters,
66 double transducerOffset,
67 double maxRangeScale) :
72 waterDepthMeters_(waterDepthMeters),
73 transducerOffset_(transducerOffset),
74 maxRangeScale_(maxRangeScale) {}
77 return std::unique_ptr<DPT>(
new DPT(*
this));
81 std::ostringstream ss;
82 ss << this->toString(verbose);
83 ss << std::fixed << std::setprecision(2);
86 ss <<
"\tWater depth: " << waterDepthMeters_ <<
" m\n";
87 ss <<
"\tTransducer offset: " << transducerOffset_ <<
" m\n";
88 ss <<
"\tMax range scale: " << maxRangeScale_ <<
" m\n";
90 ss <<
"Depth=" << waterDepthMeters_ <<
"m"
91 <<
", Offset=" << transducerOffset_ <<
"m"
92 <<
", MaxRange=" << maxRangeScale_ <<
"m";
97std::string DPT::composeRaw(
const std::string& talkerId,
98 double waterDepthMeters,
99 double transducerOffset,
100 double maxRangeScale) {
101 std::ostringstream payloadStream;
102 payloadStream << talkerId <<
"DPT,";
103 payloadStream << std::fixed << std::setprecision(1) << waterDepthMeters <<
",";
104 payloadStream << std::fixed << std::setprecision(1) << transducerOffset <<
",";
105 payloadStream << std::fixed << std::setprecision(1) << maxRangeScale;
107 std::string payload = payloadStream.str();
108 return "$" + payload +
"\r\n";
112 return waterDepthMeters_;
116 return transducerOffset_;
120 return maxRangeScale_;
125 waterDepthMeters_ == other.waterDepthMeters_ &&
126 transducerOffset_ == other.transducerOffset_ &&
127 maxRangeScale_ == other.maxRangeScale_;
Represents a parsed NMEA 0183 DPT (Depth of Water) sentence.
double getMaxRangeScale() const noexcept
Get maximum range scale in meters.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
double getTransducerOffset() const noexcept
Get transducer offset in meters.
bool operator==(const DPT &other) const noexcept
Compare two DPT messages for equality.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this DPT message.
double getWaterDepthMeters() const noexcept
Get water depth in meters.
DPT(std::string talkerId, double waterDepthMeters, double transducerOffset, double maxRangeScale)
Construct a DPT 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