OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_appmsg.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
26#ifndef _APP_MSG_H
27#define _APP_MSG_H
28
29#include <memory>
30#include <iomanip>
31
32#include <wx/event.h>
33
34#include "model/comm_driver.h"
35#include "observable.h"
36
37double PosPartsToDegrees(float degrees, float minutes, float percent_of_minute);
38
39std::string DegreesToString(double degrees);
40
41std::string TimeToString(const time_t time);
42
43class Position {
44public:
45 enum class Type { NE, NW, SE, SW, Undef };
46
48 Position(double _lat, double _lon, Type t);
49
51 Position(double _lat, double _lon);
52
54 Position();
55
56 bool IsValid() const { return type != Type::Undef; }
57
59 std::string to_string() const;
60
61 const double lat; // signed value
62 const double lon; // signed value
63 const Type type;
64
70 static Position ParseGGA(const std::string gga);
71
72private:
73 std::string TypeToStr(const Type t) const;
74
76 Type LatLongToType(double lat, double lon);
77
79 double TypeToLat(Type t, double lat);
80
82 double TypeToLong(Type t, double lon);
83};
84
85class AppMsg : public KeyProvider {
86public:
87 enum class Type;
88
89 AppMsg(AppMsg::Type t)
90 : type(t), name(TypeToString(t)), source(NavAddr()), prio(0) {};
91
92 virtual ~AppMsg() = default;
93
95 virtual std::string key() const { return "appmsg::" + name; }
96
98 std::string GetKey() const { return key(); }
99
100 std::string TypeToString(const Type t) const;
101
102 virtual std::string to_string() const { return "appmsg::" + name; }
103
104 const Type type;
105 const std::string name; // Must be unique, probably using TypeToString().
106 NavAddr source;
107 unsigned short prio; // Initially 0, modified using set_priority
108
109protected:
110 AppMsg(AppMsg::Type tp, const std::string& nm, NavAddr src)
111 : type(tp), name(nm), source(src), prio(0) {};
112};
113
114enum class AppMsg::Type {
115 BasicNavData,
116 GPSWatchdog,
117 GnssFix,
118 AisData,
120 CustomMsg,
121 Undef
122};
123
128class DataPrioNeeded : public AppMsg {
129public:
130 AppMsg::Type what;
131 std::vector<NavAddr> sources;
132};
133
135class GnssFix : public AppMsg {
136public:
137 enum class Quality { none, gnss, differential };
138
139 GnssFix(Position p, time_t t, Quality q = Quality::none, int s_used = -1)
140 : AppMsg(AppMsg::Type::GnssFix, "gnss-fix", NavAddr()),
141 pos(p),
142 time(t),
143 quality(q),
144 satellites_used(s_used) {};
145
146 std::string to_string() const override {
147 std::stringstream buf;
148 buf << pos.to_string() << " " << TimeToString(time);
149 return buf.str();
150 }
151
152 Position pos;
153 const time_t time;
154 Quality quality;
155 int satellites_used;
156};
157
158// bitmask defining update validity of BasicNavDataMsg members
159#define POS_UPDATE (int)(1)
160#define COG_UPDATE (int)(1 << 1)
161#define SOG_UPDATE (int)(1 << 2)
162#define VAR_UPDATE (int)(1 << 3)
163#define HDT_UPDATE (int)(1 << 4)
164#define POS_VALID (int)(1 << 5)
165
166class BasicNavDataMsg : public AppMsg {
167public:
168 BasicNavDataMsg(double lat, double lon, double SOG, double COG, double VAR,
169 double HDT, int valid_flag, time_t t)
170 : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
171 pos(lat, lon),
172 sog(SOG),
173 cog(COG),
174 var(VAR),
175 hdt(HDT),
176 vflag(valid_flag),
177 time(t) {};
178
180 : AppMsg(AppMsg::Type::BasicNavData, "basic-nav-data", NavAddr()),
181 sog(0),
182 cog(0),
183 var(0),
184 hdt(0),
185 vflag(0),
186 time(0),
187 set_time() {};
188
189 virtual ~BasicNavDataMsg() = default;
190
191 virtual std::string to_string() const;
192
193 const Position pos;
194 const double sog;
195 const double cog;
196 const double var;
197 const double hdt;
198 const int vflag;
199 const time_t time;
200 struct timespec set_time;
201};
202
203class GPSWatchdogMsg : public AppMsg {
204public:
205 enum class WDSource { position, velocity, heading, var, sats };
206
207 GPSWatchdogMsg(WDSource _source, int value)
208 : AppMsg(AppMsg::Type::GPSWatchdog, "gps-watchdog", NavAddr()),
209 gps_watchdog(value),
210 wd_source(_source) {};
211
212 virtual ~GPSWatchdogMsg() = default;
213
214 const int gps_watchdog;
215 const WDSource wd_source;
216};
217
219class AisData : public AppMsg {
220public:
221 std::string key() const override { return "appmsg::aisdata"; }
222 std::string to_string() const override;
223 time_t time;
224 Position pos;
225 float sog; // Speed over ground, knots.
226 float cog; // Course over ground, 0..360 degrees.
227 float heading; // Magnetic sensor, 0..360 degrees.
228 float rate_of_turn; // Degrees per minute, "-" means bow turns to port.
229 uint8_t type; // https://api.vtexplorer.com/docs/ref-aistypes.html
230 std::string name;
231 std::string callsign;
232 std::string dest; // Destination port
233 int length;
234 int beam;
235 int draft;
236 uint8_t status; // https://api.vtexplorer.com/docs/ref-navstat.html
237};
238
243class CustomMsg : public AppMsg {
244 CustomMsg(const std::string s, std::shared_ptr<const void> ptr)
245 : AppMsg(Type::CustomMsg, "custom", NavAddr()), id(s), payload(ptr) {}
246
247 std::string key() const override { return "appmsg::custom-" + id; }
248
249 const std::string id; // Must be unique.
250 std::shared_ptr<const void> payload;
251};
252
253#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:95
std::string GetKey() const
Alias for key().
Definition comm_appmsg.h:98
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:64
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.
General observable implementation with several specializations.