OpenCPN Partial API docs
ais_decoder.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Copyright (C) 2010 by David S. Register *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  ***************************************************************************
23  */
24 
25 #ifndef _AIS_DECODER_H__
26 #define _AIS_DECODER_H__
27 
28 #include <map>
29 #include <unordered_map>
30 #include <memory>
31 #include <vector>
32 
33 #include <wx/datetime.h>
34 #include <wx/event.h>
35 #include <wx/string.h>
36 
37 #include "rapidjson/fwd.h"
38 #include "model/ais_bitstring.h"
39 #include "model/ais_defs.h"
40 #include "model/ais_target_data.h"
41 #include "model/comm_navmsg.h"
42 #include "model/ocpn_types.h"
43 #include "model/select.h"
44 #include "model/track.h"
45 #include "observable_evtvar.h"
46 
47 class AisDecoder; // forward
48 class ArrayOfMmsiProperties; // forward
49 
50 // AISTargetAlertDialog in gui layer
51 extern wxEvtHandler* g_pais_alert_dialog_active;
52 
53 extern Select* pSelectAIS;
54 extern wxString AISTargetNameFileName;
55 extern AisDecoder *g_pAIS;
56 extern ArrayOfMmsiProperties g_MMSI_Props_Array;
57 
58 enum AISAudioSoundType {
59  AISAUDIO_NONE,
60  AISAUDIO_CPA,
61  AISAUDIO_SART,
62  AISAUDIO_DSC
63 };
64 
66 public:
67  MmsiProperties(){};
68  MmsiProperties(int mmsi) {
69  Init();
70  MMSI = mmsi;
71  }
72  MmsiProperties(wxString &spec);
73 
74  ~MmsiProperties();
75 
76  wxString Serialize();
77 
78  void Init(void);
79  int MMSI;
80  int TrackType;
81  bool m_bignore;
82  bool m_bMOB;
83  bool m_bVDM;
84  bool m_bFollower;
85  bool m_bPersistentTrack;
86  wxString m_ShipName;
87 };
88 
89 WX_DEFINE_ARRAY_PTR(MmsiProperties *, ArrayOfMmsiProperties);
90 
91 
93  std::function<bool()> confirm_stop_track;
94  std::function<int()> get_target_mmsi;
96  : confirm_stop_track([]() { return true; } ),
97  get_target_mmsi([]() { return 0; }) {}
98 };
99 
100 class AisDecoder : public wxEvtHandler {
101 public:
102  AisDecoder(AisDecoderCallbacks callbacks);
103 
104  ~AisDecoder(void);
105 
106  AisError DecodeN0183(const wxString &str);
107  std::unordered_map<int, std::shared_ptr <AisTargetData>> &GetTargetList(void) {
108  return AISTargetList;
109  }
110  std::unordered_map<int, std::shared_ptr <AisTargetData>> &GetAreaNoticeSourcesList(void) {
111  return AIS_AreaNotice_Sources;
112  }
113  std::shared_ptr<AisTargetData> Get_Target_Data_From_MMSI(int mmsi);
114  int GetNumTargets(void) { return m_n_targets; }
115  bool IsAISSuppressed(void) { return m_bSuppressed; }
116  bool IsAISAlertGeneral(void) { return m_bGeneralAlert; }
117  AisError DecodeSingleVDO(const wxString &str, GenericPosDatEx *pos,
118  wxString *acc);
119  void DeletePersistentTrack(Track *track);
120  std::map<int, Track *> m_persistent_tracks;
121  bool AIS_AlertPlaying(void) { return m_bAIS_AlertPlaying; };
122 
128 
131 
134 
137 
140 
143 
144 private:
145  void OnActivate(wxActivateEvent &event);
146  void OnTimerAIS(wxTimerEvent &event);
147  void OnTimerDSC(wxTimerEvent &event);
148 
149  bool NMEACheckSumOK(const wxString &str);
150  bool Parse_VDXBitstring(AisBitstring *bstr, std::shared_ptr<AisTargetData> ptd);
151  void UpdateAllCPA(void);
152  void UpdateOneCPA(AisTargetData *ptarget);
153  void UpdateAllAlarms(void);
154  void UpdateAllTracks(void);
155  void UpdateOneTrack(AisTargetData *ptarget);
156  void BuildERIShipTypeHash(void);
157  std::shared_ptr<AisTargetData> ProcessDSx(const wxString &str, bool b_take_dsc = false);
158 
159  wxString DecodeDSEExpansionCharacters(wxString dseData);
160  void getAISTarget(long mmsi, std::shared_ptr<AisTargetData> &pTargetData,
161  std::shared_ptr<AisTargetData> &pStaleTarget, bool &bnewtarget,
162  int &last_report_ticks, wxDateTime &now);
163  void getMmsiProperties(std::shared_ptr<AisTargetData> &pTargetData);
164  void handleUpdate(std::shared_ptr<AisTargetData> pTargetData, bool bnewtarget,
165  const rapidjson::Value &update);
166  void updateItem(std::shared_ptr<AisTargetData> pTargetData, bool bnewtarget,
167  const rapidjson::Value &item, wxString &sfixtime) const;
168  void CommitAISTarget( std::shared_ptr<AisTargetData> pTargetData,
169  const wxString &str, bool message_valid,
170  bool new_target);
171  void InitCommListeners(void);
172  bool HandleN0183_AIS( std::shared_ptr <const Nmea0183Msg> n0183_msg );
173  void HandleSignalK(std::shared_ptr<const SignalkMsg> sK_msg);
174 
175  bool HandleN2K_129038( std::shared_ptr<const Nmea2000Msg> n2k_msg );
176  bool HandleN2K_129039( std::shared_ptr<const Nmea2000Msg> n2k_msg );
177  bool HandleN2K_129041( std::shared_ptr<const Nmea2000Msg> n2k_msg );
178  bool HandleN2K_129794( std::shared_ptr<const Nmea2000Msg> n2k_msg );
179  bool HandleN2K_129809( std::shared_ptr<const Nmea2000Msg> n2k_msg );
180  bool HandleN2K_129810( std::shared_ptr<const Nmea2000Msg> n2k_msg );
181  bool HandleN2K_129793( std::shared_ptr<const Nmea2000Msg> n2k_msg );
182 
183  wxString m_signalk_selfid;
184  std::unordered_map<int, std::shared_ptr<AisTargetData>> AISTargetList;
185  std::unordered_map<int, std::shared_ptr<AisTargetData>> AIS_AreaNotice_Sources;
186  AIS_Target_Name_Hash *AISTargetNamesC;
187  AIS_Target_Name_Hash *AISTargetNamesNC;
188 
189  ObservableListener listener_N0183_VDM;
190  ObservableListener listener_N0183_FRPOS;
191  ObservableListener listener_N0183_CDDSC;
192  ObservableListener listener_N0183_CDDSE;
193  ObservableListener listener_N0183_TLL;
194  ObservableListener listener_N0183_TTM;
195  ObservableListener listener_N0183_OSD;
196  ObservableListener listener_N0183_WPL;
197  ObservableListener listener_SignalK;
198 
199  ObservableListener listener_N2K_129038;
200  ObservableListener listener_N2K_129039;
201  ObservableListener listener_N2K_129041;
202  ObservableListener listener_N2K_129794;
203  ObservableListener listener_N2K_129809;
204  ObservableListener listener_N2K_129810;
205  ObservableListener listener_N2K_129793;
206 
207  bool m_busy;
208  wxTimer TimerAIS;
209  wxFrame *m_parent_frame;
210  AisDecoderCallbacks m_callbacks;
211 
212  int nsentences;
213  int isentence;
214  wxString sentence_accumulator;
215  bool m_OK;
216 
217  std::shared_ptr<AisTargetData> m_pLatestTargetData;
218 
219  bool m_bAIS_Audio_Alert_On;
220  wxTimer m_AIS_Audio_Alert_Timer;
221  int m_n_targets;
222  bool m_bSuppressed;
223  bool m_bGeneralAlert;
224  std::shared_ptr<AisTargetData> m_ptentative_dsctarget;
225  wxTimer m_dsc_timer;
226  wxString m_dsc_last_string;
227  std::vector<int> m_MMSI_MismatchVec;
228 
229  bool m_bAIS_AlertPlaying;
230  DECLARE_EVENT_TABLE()
231 };
232 
233 #endif // _AIS_DECODER_H__
EventVar plugin_msg
A JSON message should be sent.
Definition: ais_decoder.h:142
EventVar new_track
Notified on new track creation.
Definition: ais_decoder.h:136
EventVar new_ais_wp
Notified when new AIS wp is created.
Definition: ais_decoder.h:133
EventVar info_update
Notified when AIS user dialogs should update.
Definition: ais_decoder.h:121
EventVar delete_track
Notified when about to delete track.
Definition: ais_decoder.h:139
EventVar touch_state
Notified when gFrame->TouchAISActive() should be invoked.
Definition: ais_decoder.h:130
Generic event handling between MVC Model and Controller based on a shared EventVar variable.
Keeps listening over it's lifespan, removes itself on destruction.
Definition: observable.h:133
Definition: select.h:54
Definition: track.h:78