34#include "rapidjson/document.h"
39#include "model/geodesic.h"
42bool CommDecoder::ParsePosition(
const LATLONG&
Position,
double& lat,
45 double llt =
Position.Latitude.Latitude;
47 if (!std::isnan(llt)) {
48 int lat_deg_int = (int)(llt / 100);
49 double lat_deg = lat_deg_int;
50 double lat_min = llt - (lat_deg * 100);
52 lat = lat_deg + (lat_min / 60.);
53 if (
Position.Latitude.Northing == South) lat = -lat;
57 double lln =
Position.Longitude.Longitude;
58 if (!std::isnan(lln)) {
59 int lon_deg_int = (int)(lln / 100);
60 double lon_deg = lon_deg_int;
61 double lon_min = lln - (lon_deg * 100);
63 lon = lon_deg + (lon_min / 60.);
64 if (
Position.Longitude.Easting == West) lon = -lon;
71bool CommDecoder::DecodeRMC(std::string s,
NavData& temp_data) {
72 wxString sentence(s.c_str());
74 m_NMEA0183 << sentence3;
76 if (!m_NMEA0183.PreParse())
return false;
77 if (!m_NMEA0183.Parse())
return false;
79 if (m_NMEA0183.Rmc.IsDataValid == NTrue) {
81 if (ParsePosition(m_NMEA0183.Rmc.Position, tlat, tlon)) {
82 temp_data.gLat = tlat;
83 temp_data.gLon = tlon;
89 if (!std::isnan(m_NMEA0183.Rmc.SpeedOverGroundKnots)) {
90 temp_data.gSog = m_NMEA0183.Rmc.SpeedOverGroundKnots;
92 if (!std::isnan(temp_data.gSog) && (temp_data.gSog > 0.05)) {
93 temp_data.gCog = m_NMEA0183.Rmc.TrackMadeGoodDegreesTrue;
101 if ((!std::isnan(m_NMEA0183.Rmc.MagneticVariation)) &&
102 0.0 != m_NMEA0183.Rmc.MagneticVariation) {
103 if (m_NMEA0183.Rmc.MagneticVariationDirection == East)
104 temp_data.gVar = m_NMEA0183.Rmc.MagneticVariation;
105 else if (m_NMEA0183.Rmc.MagneticVariationDirection == West)
106 temp_data.gVar = -m_NMEA0183.Rmc.MagneticVariation;
111 gRmcTime = m_NMEA0183.Rmc.UTCTime;
112 gRmcDate = m_NMEA0183.Rmc.Date;
119bool CommDecoder::DecodeHDM(std::string s,
NavData& temp_data) {
120 wxString sentence(s.c_str());
122 m_NMEA0183 << sentence3;
124 if (!m_NMEA0183.PreParse())
return false;
125 if (!m_NMEA0183.Parse())
return false;
127 temp_data.gHdm = m_NMEA0183.Hdm.DegreesMagnetic;
132bool CommDecoder::DecodeTHS(std::string s,
NavData& temp_data) {
133 wxString sentence(s.c_str());
135 m_NMEA0183 << sentence3;
137 if (!m_NMEA0183.PreParse())
return false;
138 if (!m_NMEA0183.Parse())
return false;
141 if (!(m_NMEA0183.Ths.ModeInd ==
"A"))
return false;
142 temp_data.gHdt = m_NMEA0183.Ths.TrueHeading;
147bool CommDecoder::DecodeHDT(std::string s,
NavData& temp_data) {
148 wxString sentence(s.c_str());
150 m_NMEA0183 << sentence3;
152 if (!m_NMEA0183.PreParse())
return false;
153 if (!m_NMEA0183.Parse())
return false;
155 temp_data.gHdt = m_NMEA0183.Hdt.DegreesTrue;
160bool CommDecoder::DecodeHDG(std::string s,
NavData& temp_data) {
161 wxString sentence(s.c_str());
163 m_NMEA0183 << sentence3;
165 if (!m_NMEA0183.PreParse())
return false;
166 if (!m_NMEA0183.Parse())
return false;
168 temp_data.gHdm = m_NMEA0183.Hdg.MagneticSensorHeadingDegrees;
173 if ((!std::isnan(m_NMEA0183.Hdg.MagneticVariationDegrees)) &&
174 0.0 != m_NMEA0183.Hdg.MagneticVariationDegrees) {
175 if (m_NMEA0183.Hdg.MagneticVariationDirection == East)
176 temp_data.gVar = m_NMEA0183.Hdg.MagneticVariationDegrees;
177 else if (m_NMEA0183.Hdg.MagneticVariationDirection == West)
178 temp_data.gVar = -m_NMEA0183.Hdg.MagneticVariationDegrees;
186bool CommDecoder::DecodeHVD(std::string s,
NavData& temp_data) {
187 wxString sentence(s.c_str());
189 m_NMEA0183 << sentence3;
191 if (!m_NMEA0183.PreParse())
return false;
192 if (!m_NMEA0183.Parse())
return false;
197 if ((!std::isnan(m_NMEA0183.Hvd.MagneticVariationDegrees)) &&
198 0.0 != m_NMEA0183.Hvd.MagneticVariationDegrees) {
199 if (m_NMEA0183.Hvd.MagneticVariationDirection == East)
200 temp_data.gVar = m_NMEA0183.Hvd.MagneticVariationDegrees;
201 else if (m_NMEA0183.Hvd.MagneticVariationDirection == West)
202 temp_data.gVar = -m_NMEA0183.Hvd.MagneticVariationDegrees;
210bool CommDecoder::DecodeVTG(std::string s,
NavData& temp_data) {
211 wxString sentence(s.c_str());
213 m_NMEA0183 << sentence3;
215 if (!m_NMEA0183.PreParse())
return false;
216 if (!m_NMEA0183.Parse())
return false;
220 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots))
221 temp_data.gSog = m_NMEA0183.Vtg.SpeedKnots;
223 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
224 !std::isnan(m_NMEA0183.Vtg.TrackDegreesTrue)) {
225 temp_data.gCog = m_NMEA0183.Vtg.TrackDegreesTrue;
230 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
231 !std::isnan(m_NMEA0183.Vtg.TrackDegreesMagnetic)) {
233 if (std::isnan(
gVar) && (g_UserVar != 0.0))
gVar = g_UserVar;
235 double cogt = m_NMEA0183.Vtg.TrackDegreesMagnetic +
gVar;
236 if (!std::isnan(cogt)) {
239 else if (cogt >= 360)
241 temp_data.gCog = cogt;
247bool CommDecoder::DecodeGLL(std::string s,
NavData& temp_data) {
248 wxString sentence(s.c_str());
250 m_NMEA0183 << sentence3;
252 if (!m_NMEA0183.PreParse())
return false;
253 if (!m_NMEA0183.Parse())
return false;
255 if (m_NMEA0183.Gll.IsDataValid == NTrue) {
257 if (ParsePosition(m_NMEA0183.Gll.Position, tlat, tlon)) {
258 temp_data.gLat = tlat;
259 temp_data.gLon = tlon;
268bool CommDecoder::DecodeGSV(std::string s,
NavData& temp_data) {
269 wxString sentence(s.c_str());
271 m_NMEA0183 << sentence3;
273 if (!m_NMEA0183.PreParse())
return false;
274 if (!m_NMEA0183.Parse())
return false;
276 if (m_NMEA0183.Gsv.MessageNumber == 1)
277 temp_data.n_satellites = m_NMEA0183.Gsv.SatsInView;
282bool CommDecoder::DecodeGGA(std::string s,
NavData& temp_data) {
283 wxString sentence(s.c_str());
285 m_NMEA0183 << sentence3;
287 if (!m_NMEA0183.PreParse())
return false;
288 if (!m_NMEA0183.Parse())
return false;
290 if (m_NMEA0183.Gga.GPSQuality > 0) {
292 if (ParsePosition(m_NMEA0183.Gga.Position, tlat, tlon)) {
293 temp_data.gLat = tlat;
294 temp_data.gLon = tlon;
298 temp_data.n_satellites = m_NMEA0183.Gga.NumberOfSatellitesInUse;
310bool CommDecoder::DecodePGN129026(std::vector<unsigned char> v,
313 tN2kHeadingReference ref;
316 if (ParseN2kPGN129026(v, SID, ref, COG, SOG)) {
317 temp_data.gCog = COG;
318 temp_data.gSog = SOG;
326bool CommDecoder::DecodePGN129029(std::vector<unsigned char> v,
329 uint16_t DaysSince1970;
330 double SecondsSinceMidnight;
331 double Latitude, Longitude, Altitude;
332 tN2kGNSStype GNSStype;
333 tN2kGNSSmethod GNSSmethod;
334 unsigned char nSatellites;
335 double HDOP, PDOP, GeoidalSeparation;
336 unsigned char nReferenceStations;
337 tN2kGNSStype ReferenceStationType;
338 uint16_t ReferenceSationID;
339 double AgeOfCorrection;
341 if (ParseN2kPGN129029(v, SID, DaysSince1970, SecondsSinceMidnight, Latitude,
342 Longitude, Altitude, GNSStype, GNSSmethod, nSatellites,
343 HDOP, PDOP, GeoidalSeparation, nReferenceStations,
344 ReferenceStationType, ReferenceSationID,
346 temp_data.gLat = Latitude;
347 temp_data.gLon = Longitude;
354 if ((GNSSmethod == N2kGNSSm_GNSSfix) || (GNSSmethod == N2kGNSSm_DGNSS) ||
355 (GNSSmethod == N2kGNSSm_PreciseGNSS)) {
356 if (nSatellites > 0) temp_data.n_satellites = nSatellites;
365bool CommDecoder::DecodePGN127250(std::vector<unsigned char> v,
368 double Heading, Deviation, Variation;
369 tN2kHeadingReference ref;
371 if (ParseN2kPGN127250(v, SID, Heading, Deviation, Variation, ref)) {
372 temp_data.gHdt = N2kDoubleNA;
373 temp_data.gHdm = N2kDoubleNA;
374 if (ref == tN2kHeadingReference::N2khr_true)
375 temp_data.gHdt = Heading;
376 else if (ref == tN2kHeadingReference::N2khr_magnetic)
377 temp_data.gHdm = Heading;
379 temp_data.gVar = Variation;
387bool CommDecoder::DecodePGN127258(std::vector<unsigned char> v,
390 tN2kMagneticVariation Source;
391 uint16_t DaysSince1970;
393 tN2kHeadingReference ref;
395 if (ParseN2kPGN127258(v, SID, Source, DaysSince1970, Variation)) {
396 temp_data.gVar = Variation;
405bool CommDecoder::DecodePGN129025(std::vector<unsigned char> v,
407 double Latitude, Longitude;
409 if (ParseN2kPGN129025(v, Latitude, Longitude)) {
410 temp_data.gLat = Latitude;
411 temp_data.gLon = Longitude;
418bool CommDecoder::DecodePGN129540(std::vector<unsigned char> v,
423 tN2kRangeResidualMode Mode;
425 if (ParseN2kPGN129540(v, SID, Mode, NumberOfSVs)) {
426 temp_data.n_satellites = NumberOfSVs;
434bool CommDecoder::DecodeSignalK(std::string s,
NavData& temp_data) {
435 rapidjson::Document root;
438 if (root.HasParseError())
return false;
440 if (root.HasMember(
"updates") && root[
"updates"].IsArray()) {
441 for (rapidjson::Value::ConstValueIterator itr = root[
"updates"].Begin();
442 itr != root[
"updates"].End(); ++itr) {
443 handleUpdate(*itr, temp_data);
450void CommDecoder::handleUpdate(
const rapidjson::Value& update,
452 wxString sfixtime =
"";
454 if (update.HasMember(
"timestamp")) {
455 sfixtime = update[
"timestamp"].GetString();
457 if (update.HasMember(
"source") && update[
"source"].HasMember(
"src")) {
458 src_string = update[
"source"][
"src"].GetString();
461 if (update.HasMember(
"values") && update[
"values"].IsArray()) {
462 for (rapidjson::Value::ConstValueIterator itr = update[
"values"].Begin();
463 itr != update[
"values"].End(); ++itr) {
464 updateItem(*itr, sfixtime, temp_data);
469void CommDecoder::updateItem(
const rapidjson::Value& item, wxString& sfixtime,
471 bool bposValid =
false;
472 if (item.HasMember(
"path") && item.HasMember(
"value")) {
473 const wxString& update_path = item[
"path"].GetString();
475 if (update_path ==
"navigation.gnss.methodQuality") {
477 if (src_string.size()) {
478 if (item[
"value"] ==
"no GPS") {
479 GNSS_quality_map[src_string] = 0;
481 GNSS_quality_map[src_string] = 1;
486 if (update_path ==
"navigation.position" && !item[
"value"].IsNull()) {
487 bposValid = updateNavigationPosition(item[
"value"], sfixtime, temp_data);
491 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
492 if (GNSS_quality_map[src_string] == 0) {
493 temp_data.gLat = NAN;
497 }
else if (update_path ==
"navigation.speedOverGround" &&
498 !item[
"value"].IsNull()) {
499 updateNavigationSpeedOverGround(item[
"value"], sfixtime, temp_data);
503 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
504 if (GNSS_quality_map[src_string] == 0) {
509 }
else if (update_path ==
"navigation.courseOverGroundTrue" &&
510 !item[
"value"].IsNull()) {
511 updateNavigationCourseOverGround(item[
"value"], sfixtime, temp_data);
512 }
else if (update_path ==
"navigation.courseOverGroundMagnetic") {
513 }
else if (update_path ==
514 "navigation.gnss.satellites")
516 updateGnssSatellites(item[
"value"], sfixtime, temp_data);
517 }
else if (update_path ==
518 "navigation.gnss.satellitesInView")
520 updateGnssSatellites(item[
"value"], sfixtime, temp_data);
521 }
else if (update_path ==
"navigation.headingTrue") {
522 if (!item[
"value"].IsNull())
523 updateHeadingTrue(item[
"value"], sfixtime, temp_data);
524 }
else if (update_path ==
"navigation.headingMagnetic") {
525 if (!item[
"value"].IsNull())
526 updateHeadingMagnetic(item[
"value"], sfixtime, temp_data);
527 }
else if (update_path ==
"navigation.magneticVariation") {
528 if (!item[
"value"].IsNull())
529 updateMagneticVariance(item[
"value"], sfixtime, temp_data);
537bool CommDecoder::updateNavigationPosition(
const rapidjson::Value& value,
538 const wxString& sfixtime,
540 if ((value.HasMember(
"latitude") && value[
"latitude"].IsDouble()) &&
541 (value.HasMember(
"longitude") && value[
"longitude"].IsDouble())) {
543 temp_data.gLat = value[
"latitude"].GetDouble();
544 temp_data.gLon = value[
"longitude"].GetDouble();
551void CommDecoder::updateNavigationSpeedOverGround(
const rapidjson::Value& value,
552 const wxString& sfixtime,
554 double sog_ms = value.GetDouble();
555 double sog_knot = sog_ms * 1.9438444924406;
557 temp_data.gSog = sog_knot;
560void CommDecoder::updateNavigationCourseOverGround(
561 const rapidjson::Value& value,
const wxString& sfixtime,
563 double cog_rad = value.GetDouble();
564 double cog_deg = GEODESIC_RAD2DEG(cog_rad);
566 temp_data.gCog = cog_deg;
569void CommDecoder::updateGnssSatellites(
const rapidjson::Value& value,
570 const wxString& sfixtime,
573 if (value.GetInt() > 0) {
574 temp_data.n_satellites = value.GetInt();
576 }
else if ((value.HasMember(
"count") && value[
"count"].IsInt())) {
577 temp_data.n_satellites = value[
"count"].GetInt();
580 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
581 if (GNSS_quality_map[src_string] == 0) {
582 temp_data.n_satellites = 0;
587void CommDecoder::updateHeadingTrue(
const rapidjson::Value& value,
588 const wxString& sfixtime,
590 temp_data.gHdt = GEODESIC_RAD2DEG(value.GetDouble());
593void CommDecoder::updateHeadingMagnetic(
const rapidjson::Value& value,
594 const wxString& sfixtime,
596 temp_data.gHdm = GEODESIC_RAD2DEG(value.GetDouble());
599void CommDecoder::updateMagneticVariance(
const rapidjson::Value& value,
600 const wxString& sfixtime,
602 temp_data.gVar = GEODESIC_RAD2DEG(value.GetDouble());
Incoming messages decoding support.
wxString ProcessNMEA4Tags(const wxString &msg)
Strip NMEA V4 tag blocks from NMEA0183 message.
Variables maintained by comm stack, read-only access for others.
double gVar
Magnetic variation in degrees.
Position, course, speed, etc.