36#include "rapidjson/document.h"
38#include "model/comm_decoder.h"
39#include "model/comm_util.h"
40#include "model/comm_vars.h"
41#include "model/geodesic.h"
42#include "model/own_ship.h"
44bool CommDecoder::ParsePosition(
const LATLONG&
Position,
double& lat,
47 double llt =
Position.Latitude.Latitude;
49 if (!std::isnan(llt)) {
50 int lat_deg_int = (int)(llt / 100);
51 double lat_deg = lat_deg_int;
52 double lat_min = llt - (lat_deg * 100);
54 lat = lat_deg + (lat_min / 60.);
55 if (
Position.Latitude.Northing == South) lat = -lat;
59 double lln =
Position.Longitude.Longitude;
60 if (!std::isnan(lln)) {
61 int lon_deg_int = (int)(lln / 100);
62 double lon_deg = lon_deg_int;
63 double lon_min = lln - (lon_deg * 100);
65 lon = lon_deg + (lon_min / 60.);
66 if (
Position.Longitude.Easting == West) lon = -lon;
73bool CommDecoder::DecodeRMC(std::string s,
NavData& temp_data) {
74 wxString sentence(s.c_str());
75 wxString sentence3 = ProcessNMEA4Tags(sentence);
76 m_NMEA0183 << sentence3;
78 if (!m_NMEA0183.PreParse())
return false;
79 if (!m_NMEA0183.Parse())
return false;
81 if (m_NMEA0183.Rmc.IsDataValid == NTrue) {
83 if (ParsePosition(m_NMEA0183.Rmc.Position, tlat, tlon)) {
84 temp_data.gLat = tlat;
85 temp_data.gLon = tlon;
91 if (!std::isnan(m_NMEA0183.Rmc.SpeedOverGroundKnots)) {
92 temp_data.gSog = m_NMEA0183.Rmc.SpeedOverGroundKnots;
94 if (!std::isnan(temp_data.gSog) && (temp_data.gSog > 0.05)) {
95 temp_data.gCog = m_NMEA0183.Rmc.TrackMadeGoodDegreesTrue;
103 if ((!std::isnan(m_NMEA0183.Rmc.MagneticVariation)) &&
104 0.0 != m_NMEA0183.Rmc.MagneticVariation) {
105 if (m_NMEA0183.Rmc.MagneticVariationDirection == East)
106 temp_data.gVar = m_NMEA0183.Rmc.MagneticVariation;
107 else if (m_NMEA0183.Rmc.MagneticVariationDirection == West)
108 temp_data.gVar = -m_NMEA0183.Rmc.MagneticVariation;
113 gRmcTime = m_NMEA0183.Rmc.UTCTime;
114 gRmcDate = m_NMEA0183.Rmc.Date;
121bool CommDecoder::DecodeHDM(std::string s,
NavData& temp_data) {
122 wxString sentence(s.c_str());
123 wxString sentence3 = ProcessNMEA4Tags(sentence);
124 m_NMEA0183 << sentence3;
126 if (!m_NMEA0183.PreParse())
return false;
127 if (!m_NMEA0183.Parse())
return false;
129 temp_data.gHdm = m_NMEA0183.Hdm.DegreesMagnetic;
134bool CommDecoder::DecodeHDT(std::string s,
NavData& temp_data) {
135 wxString sentence(s.c_str());
136 wxString sentence3 = ProcessNMEA4Tags(sentence);
137 m_NMEA0183 << sentence3;
139 if (!m_NMEA0183.PreParse())
return false;
140 if (!m_NMEA0183.Parse())
return false;
142 temp_data.gHdt = m_NMEA0183.Hdt.DegreesTrue;
147bool CommDecoder::DecodeHDG(std::string s,
NavData& temp_data) {
148 wxString sentence(s.c_str());
149 wxString sentence3 = ProcessNMEA4Tags(sentence);
150 m_NMEA0183 << sentence3;
152 if (!m_NMEA0183.PreParse())
return false;
153 if (!m_NMEA0183.Parse())
return false;
155 temp_data.gHdm = m_NMEA0183.Hdg.MagneticSensorHeadingDegrees;
160 if ((!std::isnan(m_NMEA0183.Hdg.MagneticVariationDegrees)) &&
161 0.0 != m_NMEA0183.Hdg.MagneticVariationDegrees) {
162 if (m_NMEA0183.Hdg.MagneticVariationDirection == East)
163 temp_data.gVar = m_NMEA0183.Hdg.MagneticVariationDegrees;
164 else if (m_NMEA0183.Hdg.MagneticVariationDirection == West)
165 temp_data.gVar = -m_NMEA0183.Hdg.MagneticVariationDegrees;
173bool CommDecoder::DecodeVTG(std::string s,
NavData& temp_data) {
174 wxString sentence(s.c_str());
175 wxString sentence3 = ProcessNMEA4Tags(sentence);
176 m_NMEA0183 << sentence3;
178 if (!m_NMEA0183.PreParse())
return false;
179 if (!m_NMEA0183.Parse())
return false;
183 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots))
184 temp_data.gSog = m_NMEA0183.Vtg.SpeedKnots;
186 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
187 !std::isnan(m_NMEA0183.Vtg.TrackDegreesTrue)) {
188 temp_data.gCog = m_NMEA0183.Vtg.TrackDegreesTrue;
193 if (!std::isnan(m_NMEA0183.Vtg.SpeedKnots) &&
194 !std::isnan(m_NMEA0183.Vtg.TrackDegreesMagnetic)) {
196 if (std::isnan(gVar) && (g_UserVar != 0.0)) gVar = g_UserVar;
198 double cogt = m_NMEA0183.Vtg.TrackDegreesMagnetic + gVar;
199 if (!std::isnan(cogt)) {
202 else if (cogt >= 360)
204 temp_data.gCog = cogt;
210bool CommDecoder::DecodeGLL(std::string s,
NavData& temp_data) {
211 wxString sentence(s.c_str());
212 wxString sentence3 = ProcessNMEA4Tags(sentence);
213 m_NMEA0183 << sentence3;
215 if (!m_NMEA0183.PreParse())
return false;
216 if (!m_NMEA0183.Parse())
return false;
218 if (m_NMEA0183.Gll.IsDataValid == NTrue) {
220 if (ParsePosition(m_NMEA0183.Gll.Position, tlat, tlon)) {
221 temp_data.gLat = tlat;
222 temp_data.gLon = tlon;
231bool CommDecoder::DecodeGSV(std::string s,
NavData& temp_data) {
232 wxString sentence(s.c_str());
233 wxString sentence3 = ProcessNMEA4Tags(sentence);
234 m_NMEA0183 << sentence3;
236 if (!m_NMEA0183.PreParse())
return false;
237 if (!m_NMEA0183.Parse())
return false;
239 if (m_NMEA0183.Gsv.MessageNumber == 1)
240 temp_data.n_satellites = m_NMEA0183.Gsv.SatsInView;
245bool CommDecoder::DecodeGGA(std::string s,
NavData& temp_data) {
246 wxString sentence(s.c_str());
247 wxString sentence3 = ProcessNMEA4Tags(sentence);
248 m_NMEA0183 << sentence3;
250 if (!m_NMEA0183.PreParse())
return false;
251 if (!m_NMEA0183.Parse())
return false;
253 if (m_NMEA0183.Gga.GPSQuality > 0) {
255 if (ParsePosition(m_NMEA0183.Gga.Position, tlat, tlon)) {
256 temp_data.gLat = tlat;
257 temp_data.gLon = tlon;
261 temp_data.n_satellites = m_NMEA0183.Gga.NumberOfSatellitesInUse;
273bool CommDecoder::DecodePGN129026(std::vector<unsigned char> v,
276 tN2kHeadingReference ref;
279 if (ParseN2kPGN129026(v, SID, ref, COG, SOG)) {
280 temp_data.gCog = COG;
281 temp_data.gSog = SOG;
289bool CommDecoder::DecodePGN129029(std::vector<unsigned char> v,
292 uint16_t DaysSince1970;
293 double SecondsSinceMidnight;
294 double Latitude, Longitude, Altitude;
295 tN2kGNSStype GNSStype;
296 tN2kGNSSmethod GNSSmethod;
297 unsigned char nSatellites;
298 double HDOP, PDOP, GeoidalSeparation;
299 unsigned char nReferenceStations;
300 tN2kGNSStype ReferenceStationType;
301 uint16_t ReferenceSationID;
302 double AgeOfCorrection;
304 if (ParseN2kPGN129029(v, SID, DaysSince1970, SecondsSinceMidnight, Latitude,
305 Longitude, Altitude, GNSStype, GNSSmethod, nSatellites,
306 HDOP, PDOP, GeoidalSeparation, nReferenceStations,
307 ReferenceStationType, ReferenceSationID,
309 temp_data.gLat = Latitude;
310 temp_data.gLon = Longitude;
317 if ((GNSSmethod == N2kGNSSm_GNSSfix) || (GNSSmethod == N2kGNSSm_DGNSS) ||
318 (GNSSmethod == N2kGNSSm_PreciseGNSS)) {
319 if (nSatellites > 0) temp_data.n_satellites = nSatellites;
328bool CommDecoder::DecodePGN127250(std::vector<unsigned char> v,
331 double Heading, Deviation, Variation;
332 tN2kHeadingReference ref;
334 if (ParseN2kPGN127250(v, SID, Heading, Deviation, Variation, ref)) {
335 temp_data.gHdt = N2kDoubleNA;
336 temp_data.gHdm = N2kDoubleNA;
337 if (ref == tN2kHeadingReference::N2khr_true)
338 temp_data.gHdt = Heading;
339 else if (ref == tN2kHeadingReference::N2khr_magnetic)
340 temp_data.gHdm = Heading;
342 temp_data.gVar = Variation;
350bool CommDecoder::DecodePGN129025(std::vector<unsigned char> v,
352 double Latitude, Longitude;
354 if (ParseN2kPGN129025(v, Latitude, Longitude)) {
355 temp_data.gLat = Latitude;
356 temp_data.gLon = Longitude;
363bool CommDecoder::DecodePGN129540(std::vector<unsigned char> v,
368 tN2kRangeResidualMode Mode;
370 if (ParseN2kPGN129540(v, SID, Mode, NumberOfSVs)) {
371 temp_data.n_satellites = NumberOfSVs;
379bool CommDecoder::DecodeSignalK(std::string s,
NavData& temp_data) {
380 rapidjson::Document root;
383 if (root.HasParseError())
return false;
385 if (root.HasMember(
"updates") && root[
"updates"].IsArray()) {
386 for (rapidjson::Value::ConstValueIterator itr = root[
"updates"].Begin();
387 itr != root[
"updates"].End(); ++itr) {
388 handleUpdate(*itr, temp_data);
395void CommDecoder::handleUpdate(
const rapidjson::Value& update,
397 wxString sfixtime =
"";
399 if (update.HasMember(
"timestamp")) {
400 sfixtime = update[
"timestamp"].GetString();
402 if (update.HasMember(
"source") && update[
"source"].HasMember(
"src")) {
403 src_string = update[
"source"][
"src"].GetString();
406 if (update.HasMember(
"values") && update[
"values"].IsArray()) {
407 for (rapidjson::Value::ConstValueIterator itr = update[
"values"].Begin();
408 itr != update[
"values"].End(); ++itr) {
409 updateItem(*itr, sfixtime, temp_data);
414void CommDecoder::updateItem(
const rapidjson::Value& item, wxString& sfixtime,
416 bool bposValid =
false;
417 if (item.HasMember(
"path") && item.HasMember(
"value")) {
418 const wxString& update_path = item[
"path"].GetString();
420 if (update_path == _T(
"navigation.gnss.methodQuality")) {
422 if (src_string.size()) {
423 if (item[
"value"] ==
"no GPS") {
424 GNSS_quality_map[src_string] = 0;
426 GNSS_quality_map[src_string] = 1;
431 if (update_path == _T(
"navigation.position") && !item[
"value"].IsNull()) {
432 bposValid = updateNavigationPosition(item[
"value"], sfixtime, temp_data);
436 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
437 if (GNSS_quality_map[src_string] == 0) {
438 temp_data.gLat = NAN;
442 }
else if (update_path == _T(
"navigation.speedOverGround") &&
443 !item[
"value"].IsNull()) {
444 updateNavigationSpeedOverGround(item[
"value"], sfixtime, temp_data);
448 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
449 if (GNSS_quality_map[src_string] == 0) {
454 }
else if (update_path == _T(
"navigation.courseOverGroundTrue") &&
455 !item[
"value"].IsNull()) {
456 updateNavigationCourseOverGround(item[
"value"], sfixtime, temp_data);
457 }
else if (update_path == _T(
"navigation.courseOverGroundMagnetic")) {
458 }
else if (update_path ==
459 _T(
"navigation.gnss.satellites"))
461 updateGnssSatellites(item[
"value"], sfixtime, temp_data);
462 }
else if (update_path ==
463 _T(
"navigation.gnss.satellitesInView"))
465 updateGnssSatellites(item[
"value"], sfixtime, temp_data);
466 }
else if (update_path == _T(
"navigation.headingTrue")) {
467 if (!item[
"value"].IsNull())
468 updateHeadingTrue(item[
"value"], sfixtime, temp_data);
469 }
else if (update_path == _T(
"navigation.headingMagnetic")) {
470 if (!item[
"value"].IsNull())
471 updateHeadingMagnetic(item[
"value"], sfixtime, temp_data);
472 }
else if (update_path == _T(
"navigation.magneticVariation")) {
473 if (!item[
"value"].IsNull())
474 updateMagneticVariance(item[
"value"], sfixtime, temp_data);
482bool CommDecoder::updateNavigationPosition(
const rapidjson::Value& value,
483 const wxString& sfixtime,
485 if ((value.HasMember(
"latitude") && value[
"latitude"].IsDouble()) &&
486 (value.HasMember(
"longitude") && value[
"longitude"].IsDouble())) {
488 temp_data.gLat = value[
"latitude"].GetDouble();
489 temp_data.gLon = value[
"longitude"].GetDouble();
496void CommDecoder::updateNavigationSpeedOverGround(
const rapidjson::Value& value,
497 const wxString& sfixtime,
499 double sog_ms = value.GetDouble();
500 double sog_knot = sog_ms * 1.9438444924406;
502 temp_data.gSog = sog_knot;
505void CommDecoder::updateNavigationCourseOverGround(
506 const rapidjson::Value& value,
const wxString& sfixtime,
508 double cog_rad = value.GetDouble();
509 double cog_deg = GEODESIC_RAD2DEG(cog_rad);
511 temp_data.gCog = cog_deg;
514void CommDecoder::updateGnssSatellites(
const rapidjson::Value& value,
515 const wxString& sfixtime,
518 if (value.GetInt() > 0) {
519 temp_data.n_satellites = value.GetInt();
521 }
else if ((value.HasMember(
"count") && value[
"count"].IsInt())) {
522 temp_data.n_satellites = value[
"count"].GetInt();
525 if (GNSS_quality_map.find(src_string) != GNSS_quality_map.end()) {
526 if (GNSS_quality_map[src_string] == 0) {
527 temp_data.n_satellites = 0;
532void CommDecoder::updateHeadingTrue(
const rapidjson::Value& value,
533 const wxString& sfixtime,
535 temp_data.gHdt = GEODESIC_RAD2DEG(value.GetDouble());
538void CommDecoder::updateHeadingMagnetic(
const rapidjson::Value& value,
539 const wxString& sfixtime,
541 temp_data.gHdm = GEODESIC_RAD2DEG(value.GetDouble());
544void CommDecoder::updateMagneticVariance(
const rapidjson::Value& value,
545 const wxString& sfixtime,
547 temp_data.gVar = GEODESIC_RAD2DEG(value.GetDouble());