OpenCPN Partial API docs
comm_drv_signalk_net.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: PlugIn Manager Object
5  * Author: David Register
6  *
7  ***************************************************************************
8  * Copyright (C) 2022 by David S. Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  **************************************************************************/
25 // Originally by balp on 2018-07-28.
26 
27 #ifndef _SIGNALK_NET_H
28 #define _SIGNALK_NET_H
29 
30 #include <atomic>
31 #include <string>
32 
33 #include <wx/wxprec.h>
34 
35 #ifndef WX_PRECOMP
36 #include <wx/wx.h>
37 #endif // precompiled header
38 
39 #include <wx/datetime.h>
40 #include <wx/socket.h>
41 
42 #include "rapidjson/fwd.h"
43 #include "model/conn_params.h"
44 #include "model/comm_drv_signalk.h"
45 
46 #define SIGNALK_SOCKET_ID 5011
47 #define N_DOG_TIMEOUT 5 // seconds
48 #define N_DOG_TIMEOUT_RECONNECT 10 // seconds
49 
50 static const double ms_to_knot_factor = 1.9438444924406;
51 
52 class WebSocketThread;
53 class OCPN_WebSocketMessageHandler;
55 
56 class CommDriverSignalKNet : public CommDriverSignalK, public wxEvtHandler {
57 public:
59  virtual ~CommDriverSignalKNet();
60 
61  void Open();
62  void Close();
63  static bool DiscoverSKServer(wxString &ip, int &port, int tSec);
64  static bool DiscoverSKServer(std::string serviceIdent, wxString &ip,
65  int &port, int tSec);
66 
67 
68  void SetThreadRunning(bool active) { m_threadActive = active; }
69  void SetThreadRunFlag(int run) { m_Thread_run_flag = run; }
70  void ResetWatchdog() { m_dog_value = N_DOG_TIMEOUT; }
71  void SetWatchdog(int n) { m_dog_value = n; }
72 
74  void Activate() override;
75 
76  void handle_SK_sentence(CommDriverSignalKNetEvent& event);
77  void handleUpdate(const rapidjson::Value &update);
78  void updateItem(const rapidjson::Value &item, wxString &sfixtime);
79 
80  void OpenWebSocket();
81  void CloseWebSocket();
82  bool IsThreadRunning() { return m_threadActive == 1; }
83 
84  std::string m_self;
85  std::string m_context;
86 
87  std::atomic_int m_Thread_run_flag;
88  std::atomic_int m_threadActive;
89 
90  ConnectionParams m_params;
91  DriverListener& m_listener;
92 
93  static void initIXNetSystem();
94 
95  static void uninitIXNetSystem();
96 
97 private:
98  wxIPV4address m_addr;
99  wxIPV4address GetAddr() const { return m_addr; }
100 
101  int m_dog_value;
102 
103  wxTimer m_socketread_watchdog_timer;
104  wxTimer *GetSocketThreadWatchdogTimer() {
105  return &m_socketread_watchdog_timer;
106  }
107 
108  OCPN_WebSocketMessageHandler *m_eventHandler;
109  bool m_useWebSocket;
110 
111  bool m_bGPSValid_SK;
112 
113  bool SetOutputSocketOptions(wxSocketBase *sock);
114 
115  std::string m_token;
116 
117  WebSocketThread *m_wsThread;
118 
119 };
120 
121 #endif // _SIGNALK_NET_H
void Activate() override
Register driver and possibly do other post-ctor steps.
Interface implemented by transport layer and possible other parties like test code which should handl...
Definition: comm_driver.h:47