OpenCPN Partial API docs
Loading...
Searching...
No Matches
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
38double PosPartsToDegrees(float degrees, float minutes, float percent_of_minute);
39
40std::string DegreesToString(double degrees);
41
42std::string TimeToString(const time_t time);
43
44class Position {
45public:
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 bool IsValid() const { return type != Type::Undef; }
58
60 std::string to_string() const;
61
62 const double lat; // signed value
63 const double lon; // signed value
64 const Type type;
65
71 static Position ParseGGA(const std::string gga);
72
73private:
74 std::string TypeToStr(const Type t) const;
75
77 Type LatLongToType(double lat, double lon);
78
80 double TypeToLat(Type t, double lat);
81
83 double TypeToLong(Type t, double lon);
84};
85
86class AppMsg : public KeyProvider {
87public:
88 enum class Type;
89
90 AppMsg(AppMsg::Type t)
91 : type(t), name(TypeToString(t)), source(NavAddr()), prio(0) {};
92
93 virtual ~AppMsg() = default;
94
96 virtual std::string key() const { return "appmsg::" + name; }
97
99 std::string GetKey() const { return key(); }
100
101 std::string TypeToString(const Type t) const;
102
103 virtual std::string to_string() const { return "appmsg::" + name; }
104
105 const Type type;
106 const std::string name; // Must be unique, probably using TypeToString().
107 NavAddr source;
108 unsigned short prio; // Initially 0, modified using set_priority
109
110protected:
111 AppMsg(AppMsg::Type tp, const std::string& nm, NavAddr src)
112 : type(tp), name(nm), source(src), prio(0) {};
113};
114
115enum class AppMsg::Type {
116 BasicNavData,
117 GPSWatchdog,
118 GnssFix,
119 AisData,
121 CustomMsg,
122 Undef
123};
124
129class DataPrioNeeded : public AppMsg {
130public:
131 AppMsg::Type what;
132 std::vector<NavAddr> sources;
133};
134
136class GnssFix : public AppMsg {
137public:
138 enum class Quality { none, gnss, differential };
139
140 GnssFix(Position p, time_t t, Quality q = Quality::none, int s_used = -1)
141 : AppMsg(AppMsg::Type::GnssFix, "gnss-fix", NavAddr()),
142 pos(p),
143 time(t),
144 quality(q),
145 satellites_used(s_used) {};
146
147 std::string to_string() const override {
148 std::stringstream buf;
149 buf << pos.to_string() << " " << TimeToString(time);
150 return buf.str();
151 }
152
153 Position pos;
154 const time_t time;
155 Quality quality;
156 int satellites_used;
157};
158
159// bitmask defining update validity of BasicNavDataMsg members
160#define POS_UPDATE (int)(1)
161#define COG_UPDATE (int)(1 << 1)
162#define SOG_UPDATE (int)(1 << 2)
163#define VAR_UPDATE (int)(1 << 3)
164#define HDT_UPDATE (int)(1 << 4)
165#define POS_VALID (int)(1 << 5)
166
167class BasicNavDataMsg : public AppMsg {
168public:
169 BasicNavDataMsg(double lat, double lon, double SOG, double COG, double VAR,
170 double HDT, int valid_flag, time_t t)
171 : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
172 pos(lat, lon),
173 sog(SOG),
174 cog(COG),
175 var(VAR),
176 hdt(HDT),
177 vflag(valid_flag),
178 time(t) {};
179
181 : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
182 sog(0),
183 cog(0),
184 var(0),
185 hdt(0),
186 vflag(0),
187 time(0),
188 set_time() {};
189
190 virtual ~BasicNavDataMsg() = default;
191
192 virtual std::string to_string() const;
193
194 const Position pos;
195 const double sog;
196 const double cog;
197 const double var;
198 const double hdt;
199 const int vflag;
200 const time_t time;
201 struct timespec set_time;
202};
203
204class GPSWatchdogMsg : public AppMsg {
205public:
206 enum class WDSource { position, velocity, heading, var, sats };
207
208 GPSWatchdogMsg(WDSource _source, int value)
209 : AppMsg(AppMsg::Type::GPSWatchdog, "gps-watchdog", NavAddr()),
210 gps_watchdog(value),
211 wd_source(_source) {};
212
213 virtual ~GPSWatchdogMsg() = default;
214
215 const int gps_watchdog;
216 const WDSource wd_source;
217};
218
220class AisData : public AppMsg {
221public:
222 std::string key() const override { return "appmsg::aisdata"; }
223 std::string to_string() const override;
224 time_t time;
225 Position pos;
226 float sog; // Speed over ground, knots.
227 float cog; // Course over ground, 0..360 degrees.
228 float heading; // Magnetic sensor, 0..360 degrees.
229 float rate_of_turn; // Degrees per minute, "-" means bow turns to port.
230 uint8_t type; // https://api.vtexplorer.com/docs/ref-aistypes.html
231 std::string name;
232 std::string callsign;
233 std::string dest; // Destination port
234 int length;
235 int beam;
236 int draft;
237 uint8_t status; // https://api.vtexplorer.com/docs/ref-navstat.html
238};
239
244class CustomMsg : public AppMsg {
245 CustomMsg(const std::string s, std::shared_ptr<const void> ptr)
246 : AppMsg(Type::CustomMsg, "custom", NavAddr()), id(s), payload(ptr) {}
247
248 std::string key() const override { return "appmsg::custom-" + id; }
249
250 const std::string id; // Must be unique.
251 std::shared_ptr<const void> payload;
252};
253
254#endif // APP_MSG_H
AIS data point for a vessel.
std::string key() const override
Return unique key used by Observable.
virtual std::string key() const
Return unique key used by Observable.
Definition comm_appmsg.h:96
std::string GetKey() const
Alias for key().
Definition comm_appmsg.h:99
A generic message containing a const pointer to basically anything, the pointer neds to be casted to ...
Issued when there are multiple sources providing 'what' with priority == 0.
GPS, Galileo, etc.
Interface implemented by classes which listens.
Definition observable.h:55
Where messages are sent to or received from.
Position()
Construct a (0,0) position, type == Undef.
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.
Communication driver layer.