OpenCPN Partial API docs
comm_appmsg.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: Decoded messages definitions. These messages are handled by the
5  * ApgMsgBus defined in comm_appmsg_bus.h.
6  * Author: David Register, Alec Leamas
7  *
8  ***************************************************************************
9  * Copyright (C) 2022 by David Register, Alec Leamas *
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  * This program is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19  * GNU General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU General Public License *
22  * along with this program; if not, write to the *
23  * Free Software Foundation, Inc., *
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
25  **************************************************************************/
26 
27 #ifndef _APP_MSG_H
28 #define _APP_MSG_H
29 
30 #include <memory>
31 #include <iomanip>
32 
33 #include <wx/event.h>
34 
35 #include "model/comm_driver.h"
36 #include "observable.h"
37 
38 double PosPartsToDegrees(float degrees, float minutes, float percent_of_minute);
39 
40 std::string DegreesToString(double degrees);
41 
42 std::string TimeToString(const time_t time);
43 
44 class Position {
45 public:
46  enum class Type { NE, NW, SE, SW, Undef };
47 
49  Position(double _lat, double _lon, Type t);
50 
52  Position(double _lat, double _lon);
53 
55  Position();
56 
57 
58  bool IsValid() const { return type != Type::Undef; }
59 
61  std::string to_string() const;
62 
63  const double lat; // signed value
64  const double lon; // signed value
65  const Type type;
66 
72  static Position ParseGGA(const std::string gga);
73 
74 private:
75  std::string TypeToStr(const Type t) const;
76 
78  Type LatLongToType(double lat, double lon);
79 
81  double TypeToLat(Type t, double lat);
82 
84  double TypeToLong(Type t, double lon);
85 
86 };
87 
88 
89 class AppMsg : public KeyProvider {
90 public:
91  enum class Type;
92  AppMsg(AppMsg::Type t)
93  : type(t), name(TypeToString(t)), source(NavAddr()), prio(0){};
94 
95  virtual std::string key() const { return std::string("@!appmsg-") + name; }
96 
97  std::string GetKey() const { return key(); }
98 
99  std::string TypeToString(const Type t) const;
100 
101  const Type type;
102  const std::string name; // Must be unique, probably using TypeToString().
103  NavAddr source;
104  unsigned short prio; // Initially 0, modified using set_priority
105 
106 protected:
107  AppMsg(AppMsg::Type tp, const std::string& nm, NavAddr src)
108  : type(tp), name(nm), source(src), prio(0){};
109 };
110 
111 enum class AppMsg::Type {
112  BasicNavData,
113  GPSWatchdog,
114  GnssFix,
115  AisData,
117  CustomMsg,
118  Undef
119 };
120 
125 class DataPrioNeeded : public AppMsg {
126 public:
127  AppMsg::Type what;
128  std::vector<NavAddr> sources;
129 };
130 
132 class GnssFix : public AppMsg {
133 public:
134  enum class Quality { none, gnss, differential };
135 
136  GnssFix(Position p, time_t t, Quality q = Quality::none, int s_used = -1)
137  : AppMsg(AppMsg::Type::GnssFix, "gnss-fix", NavAddr()),
138  pos(p),
139  time(t),
140  quality(q),
141  satellites_used(s_used){};
142  virtual ~GnssFix() = default;
143 
144  std::string to_string() const {
145  std::stringstream buf;
146  buf << pos.to_string() << " " << TimeToString(time);
147  return buf.str();
148  }
149 
150  Position pos;
151  const time_t time;
152  Quality quality;
153  int satellites_used;
154 };
155 
156 // bitmask defining update validity of BasicNavDataMsg members
157 #define POS_UPDATE (int)(1)
158 #define COG_UPDATE (int)(1 << 1)
159 #define SOG_UPDATE (int)(1 << 2)
160 #define VAR_UPDATE (int)(1 << 3)
161 #define HDT_UPDATE (int)(1 << 4)
162 #define POS_VALID (int)(1 << 5)
163 
164 class BasicNavDataMsg : public AppMsg {
165 public:
166  BasicNavDataMsg(double lat, double lon, double SOG, double COG, double VAR,
167  double HDT, int valid_flag, time_t t)
168  : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
169  pos(lat, lon),
170  sog(SOG),
171  cog(COG),
172  var(VAR),
173  hdt(HDT),
174  vflag(valid_flag),
175  time(t){};
176 
178  : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
179  sog(0),
180  cog(0),
181  var(0),
182  hdt(0),
183  vflag(0),
184  time(0){};
185 
186  virtual ~BasicNavDataMsg() = default;
187 
188  const Position pos;
189  const double sog;
190  const double cog;
191  const double var;
192  const double hdt;
193  const int vflag;
194  const time_t time;
195 };
196 
197 class GPSWatchdogMsg : public AppMsg {
198 public:
199  enum class WDSource { position, velocity, heading, var, sats };
200 
201  GPSWatchdogMsg(WDSource _source, int value)
202  : AppMsg(AppMsg::Type::GPSWatchdog, "gps-watchdog", NavAddr()),
203  gps_watchdog(value),
204  wd_source(_source){};
205 
206  virtual ~GPSWatchdogMsg() = default;
207 
208  const int gps_watchdog;
209  const WDSource wd_source;
210 };
211 
213 class AisData : public AppMsg {
214 public:
215  time_t time;
216  Position pos;
217  float sog; // Speed over ground, knots.
218  float cog; // Course over ground, 0..360 degrees.
219  float heading; // Magnetic sensor, 0..360 degrees.
220  float rate_of_turn; // Degrees per minute, "-" means bow turns to port.
221  uint8_t type; // https://api.vtexplorer.com/docs/ref-aistypes.html
222  std::string name;
223  std::string callsign;
224  std::string dest; // Destination port
225  int length;
226  int beam;
227  int draft;
228  uint8_t status; // https://api.vtexplorer.com/docs/ref-navstat.html
229 };
230 
235 class CustomMsg : public AppMsg {
236  CustomMsg(const std::string s, std::shared_ptr<const void> ptr)
237  : AppMsg(Type::CustomMsg, "custom", NavAddr()), id(s), payload(ptr) {}
238 
239  std::string key() const override {
240  return std::string("@##_appmsg-custom-") + id;
241  }
242 
243  const std::string id; // Must be unique.
244  std::shared_ptr<const void> payload;
245 };
246 
247 #endif // APP_MSG_H
AIS data point for a vessel.
Definition: comm_appmsg.h:213
A generic message containing a const pointer to basically anything, the pointer neds to be casted to ...
Definition: comm_appmsg.h:235
Issued when there are multiple sources providing 'what' with priority == 0.
Definition: comm_appmsg.h:125
GPS, Galileo, etc.
Definition: comm_appmsg.h:132
Interface implemented by classes which listens.
Definition: observable.h:55
Where messages are sent to or received from.
Definition: comm_navmsg.h:133
Position()
Construct a (0,0) position, type == Undef.
Definition: comm_appmsg.cpp:77
static Position ParseGGA(const std::string gga)
Parse a GGA string like "5800.588,N,01145.776,E" as present in GGA and other n0183 messages.
std::string to_string() const
Return utf string like 65°25,11N 21°12,01E.
Definition: comm_appmsg.cpp:79