OpenCPN Partial API docs
Loading...
Searching...
No Matches
data_monitor_src.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2025 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
25#include <functional>
26#include <memory>
27#include <string>
28#include <unordered_map>
29
30#include <wx/event.h>
31#include <wx/log.h>
32
33#include "model/comm_navmsg.h"
36#include "model/ocpn_utils.h"
37
38#include "observable.h"
39
40static void InitListener(ObsListener& ol, NavMsg& msg,
41 const std::function<void(ObservedEvt)>& on_message) {
42 ol.Init(msg, on_message);
43}
44
45DataMonitorSrc::DataMonitorSrc(const SinkFunc& sink_func)
46 : m_sink_func(sink_func) {
47 ObsListener listener;
48 auto messages = NavMsgBus::GetInstance().GetActiveMessages();
49 new_msg_lstnr.Init(NavMsgBus::GetInstance().new_msg_event,
50 [&](ObservedEvt&) { OnNewMessage(); });
51}
52
53void DataMonitorSrc::OnNewMessage() {
54 auto messages = NavMsgBus::GetInstance().GetActiveMessages();
55 for (const auto& msg : messages) {
56 auto found = m_listeners.find(msg);
57 if (found == m_listeners.end()) {
58 ObsListener listener;
59 std::string type(msg);
60 size_t pos;
61 NavAddr::Bus bus = NavAddr::Bus::Undef;
62 if ((pos = type.find("::")) != std::string::npos) {
63 auto bus_str = type.substr(0, pos);
64 bus = NavAddr::StringToBus(bus_str);
65 type = type.substr(pos + 2);
66 if ((pos = type.find('-')) != std::string::npos)
67 type = type.substr(pos + 1);
68 m_listeners[msg] = std::move(listener);
69 std::function<void(ObservedEvt)> listen_action = [&](ObservedEvt ev) {
70 OnMessage(ev);
71 };
72 switch (bus) {
73 case NavAddr::Bus::N0183: {
74 auto type_msg = Nmea0183Msg(type);
75 InitListener(m_listeners[msg], type_msg, listen_action);
76 } break;
77 case NavAddr::Bus::N2000: {
78 try {
79 auto type_msg = Nmea2000Msg(std::stoi(type));
80 InitListener(m_listeners[msg], type_msg, listen_action);
81 } catch (...) {
82 wxLogMessage("Bad Nmea2000 type: %s", type.c_str());
83 }
84 } break;
85 case NavAddr::Bus::Plugin: {
86 auto type_msg = PluginMsg(type, "");
87 InitListener(m_listeners[msg], type_msg, listen_action);
88 } break;
89 case NavAddr::Bus::Signalk: {
90 auto type_msg = SignalkMsg();
91 InitListener(m_listeners[msg], type_msg, listen_action);
92 } break;
93 default:
94 break; // Just ignore other types.
95 }
96 }
97 }
98 }
99}
100
101void DataMonitorSrc::OnMessage(ObservedEvt& ev) {
102 auto ptr = UnpackEvtPointer<NavMsg>(ev);
103 if (ptr && ptr->to_string() != m_last_payload) {
104 m_last_payload = ptr->to_string();
105 m_sink_func(ptr);
106 }
107}
DataMonitorSrc(const SinkFunc &sink_func)
Create instance which listens and forwards messages using the sink_func callback function.
const std::set< std::string > & GetActiveMessages()
Return list of message types sent or received.
Actual data sent between application and transport layer.
A regular Nmea0183 message.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
Define an action to be performed when a KeyProvider is notified.
Definition observable.h:228
void Init(const KeyProvider &kp, std::function< void(ObservedEvt &ev)> action)
Initiate an object yet not listening.
Definition observable.h:255
Custom event class for OpenCPN's notification system.
A plugin to plugin json message over the REST interface.
A parsed SignalK message over ipv4.
Raw, undecoded messages definitions.
Raw messages layer, supports sending and recieving navmsg messages.
Provide a data stream of input messages for the Data Monitor.
Miscellaneous utilities, many of which string related.