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"
35#include "model/ocpn_utils.h"
36
37#include "observable.h"
38#include "tty_scroll.h"
39
40#include "data_monitor_src.h"
41
42static void InitListener(ObsListener& ol, NavMsg& msg,
43 const std::function<void(ObservedEvt)>& on_message) {
44 ol.Init(msg, on_message);
45}
46
47DataMonitorSrc::DataMonitorSrc(const SinkFunc& sink_func)
48 : m_sink_func(sink_func) {
49 ObsListener listener;
50 auto messages = NavMsgBus::GetInstance().GetActiveMessages();
51 new_msg_lstnr.Init(NavMsgBus::GetInstance().new_msg_event,
52 [&](ObservedEvt&) { OnNewMessage(); });
53}
54
55void DataMonitorSrc::OnNewMessage() {
56 auto messages = NavMsgBus::GetInstance().GetActiveMessages();
57 for (const auto& msg : messages) {
58 auto found = m_listeners.find(msg);
59 if (found == m_listeners.end()) {
60 ObsListener listener;
61 std::string type(msg);
62 size_t pos;
63 NavAddr::Bus bus = NavAddr::Bus::Undef;
64 if ((pos = type.find("::")) != std::string::npos) {
65 auto bus_str = type.substr(0, pos);
66 bus = NavAddr::StringToBus(bus_str);
67 type = type.substr(pos + 2);
68 if ((pos = type.find('-')) != std::string::npos)
69 type = type.substr(pos + 1);
70 m_listeners[msg] = std::move(listener);
71 std::function<void(ObservedEvt)> listen_action = [&](ObservedEvt ev) {
72 OnMessage(ev);
73 };
74 switch (bus) {
75 case NavAddr::Bus::N0183: {
76 auto type_msg = Nmea0183Msg(type);
77 InitListener(m_listeners[msg], type_msg, listen_action);
78 } break;
79 case NavAddr::Bus::N2000: {
80 try {
81 auto type_msg = Nmea2000Msg(std::stoi(type));
82 InitListener(m_listeners[msg], type_msg, listen_action);
83 } catch (...) {
84 wxLogMessage("Bad Nmea2000 type: %s", type.c_str());
85 }
86 } break;
87 case NavAddr::Bus::Plugin: {
88 auto type_msg = PluginMsg(type, "");
89 InitListener(m_listeners[msg], type_msg, listen_action);
90 } break;
91 case NavAddr::Bus::Signalk: {
92 auto type_msg = SignalkMsg();
93 InitListener(m_listeners[msg], type_msg, listen_action);
94 } break;
95 default:
96 break; // Just ignore other types.
97 }
98 }
99 }
100 }
101}
102
103void DataMonitorSrc::OnMessage(ObservedEvt& ev) {
104 auto ptr = UnpackEvtPointer<NavMsg>(ev);
105 if (ptr && ptr->to_string() != m_last_payload) {
106 m_last_payload = ptr->to_string();
107 m_sink_func(ptr);
108 }
109}
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.
Scrolled TTY-like window for logging, related utilities.