nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
dpt.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<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()));
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() != 3) {
37 NMEALIB_RETURN_ERROR(NotDPTException(context, "Invalid fields in DPT payload: expected 3, got " + std::to_string(fields.size()) + ". Payload: " + payload));
38 }
39
40 double waterDepthMeters = 0.0;
41 double transducerOffset = 0.0;
42 double maxRangeScale = 0.0;
43 if (!detail::parseOptionalDouble(fields[0], waterDepthMeters) ||
44 !detail::parseOptionalDouble(fields[1], transducerOffset) ||
45 !detail::parseOptionalDouble(fields[2], maxRangeScale)) {
46 NMEALIB_RETURN_ERROR(NmeaException(context, "Error parsing DPT fields"));
47 }
48
49 return std::unique_ptr<DPT>(new DPT(std::move(*baseMessage),
50 waterDepthMeters,
51 transducerOffset,
52 maxRangeScale));
53}
54
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) {}
63
64DPT::DPT(std::string talkerId,
65 double waterDepthMeters,
66 double transducerOffset,
67 double maxRangeScale) :
68 Message0183(*Message0183::create(composeRaw(talkerId,
69 waterDepthMeters,
70 transducerOffset,
71 maxRangeScale))),
72 waterDepthMeters_(waterDepthMeters),
73 transducerOffset_(transducerOffset),
74 maxRangeScale_(maxRangeScale) {}
75
76std::unique_ptr<nmealib::Message> DPT::clone() const {
77 return std::unique_ptr<DPT>(new DPT(*this));
78}
79
80std::string DPT::getStringContent(bool verbose) const noexcept {
81 std::ostringstream ss;
82 ss << this->toString(verbose);
83 ss << std::fixed << std::setprecision(2);
84
85 if (verbose) {
86 ss << "\tWater depth: " << waterDepthMeters_ << " m\n";
87 ss << "\tTransducer offset: " << transducerOffset_ << " m\n";
88 ss << "\tMax range scale: " << maxRangeScale_ << " m\n";
89 } else {
90 ss << "Depth=" << waterDepthMeters_ << "m"
91 << ", Offset=" << transducerOffset_ << "m"
92 << ", MaxRange=" << maxRangeScale_ << "m";
93 }
94 return ss.str();
95}
96
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;
106
107 std::string payload = payloadStream.str();
108 return "$" + payload + "\r\n";
109}
110
111double DPT::getWaterDepthMeters() const noexcept {
112 return waterDepthMeters_;
113}
114
115double DPT::getTransducerOffset() const noexcept {
116 return transducerOffset_;
117}
118
119double DPT::getMaxRangeScale() const noexcept {
120 return maxRangeScale_;
121}
122
123bool DPT::operator==(const DPT& other) const noexcept {
124 return Message0183::operator==(other) &&
125 waterDepthMeters_ == other.waterDepthMeters_ &&
126 transducerOffset_ == other.transducerOffset_ &&
127 maxRangeScale_ == other.maxRangeScale_;
128}
129
130} // namespace nmea0183
131} // namespace nmealib
Represents a parsed NMEA 0183 DPT (Depth of Water) sentence.
Definition dpt.h:37
double getMaxRangeScale() const noexcept
Get maximum range scale in meters.
Definition dpt.cpp:119
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
Definition dpt.cpp:80
double getTransducerOffset() const noexcept
Get transducer offset in meters.
Definition dpt.cpp:115
bool operator==(const DPT &other) const noexcept
Compare two DPT messages for equality.
Definition dpt.cpp:123
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this DPT message.
Definition dpt.cpp:76
double getWaterDepthMeters() const noexcept
Get water depth in meters.
Definition dpt.cpp:111
DPT(std::string talkerId, double waterDepthMeters, double transducerOffset, double maxRangeScale)
Construct a DPT message from individual field values.
Definition dpt.cpp:64
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