33 #include <wx/jsonval.h>
34 #include <wx/jsonreader.h>
36 #include "model/base_platform.h"
37 #include "model/comm_appmsg.h"
38 #include "model/comm_drv_n0183_net.h"
40 #include "model/comm_drv_n2k.h"
41 #include "model/comm_drv_registry.h"
42 #include "model/comm_navmsg_bus.h"
48 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
53 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
54 return msg->source->to_string();
58 auto msg = UnpackEvtPointer<Nmea0183Msg>(ev);
63 auto msg = UnpackEvtPointer<PluginMsg>(ev);
69 auto msg = UnpackEvtPointer<SignalkMsg>(ev);
72 reader.Parse(wxString(msg->raw_message), &data);
74 wxJSONValue root(wxJSONTYPE_OBJECT);
76 root[
"ErrorCount"] = reader.GetErrorCount();
77 root[
"WarningCount"] = reader.GetWarningCount();
79 root[
"Errors"] = wxJSONValue(wxJSONTYPE_ARRAY);
80 for (
size_t i = 0; i < reader.GetErrors().GetCount(); i++)
81 root[
"Errors"].Append(reader.GetErrors().Item(i));
83 root[
"Warnings"] = wxJSONValue(wxJSONTYPE_ARRAY);
84 for (
size_t i = 0; i < reader.GetWarnings().GetCount(); i++)
85 root[
"Warnings"].Append(reader.GetWarnings().Item(i));
87 root[
"Context"] = msg->context;
88 root[
"ContextSelf"] = msg->context_self;
90 return static_pointer_cast<void>(std::make_shared<wxJSONValue>(root));
93 shared_ptr<ObservableListener> GetListener(
NMEA2000Id id, wxEventType et,
95 return make_shared<ObservableListener>(
Nmea2000Msg(
id.
id), eh,
99 std::shared_ptr<ObservableListener> GetListener(
NMEA0183Id id, wxEventType et,
101 return make_shared<ObservableListener>(
Nmea0183Msg(
id.
id), eh, et);
104 shared_ptr<ObservableListener> GetListener(
SignalkId id, wxEventType et,
106 return make_shared<ObservableListener>(
SignalkMsg(), eh, et);
109 shared_ptr<ObservableListener> GetListener(
NavDataId id, wxEventType et,
114 std::shared_ptr<ObservableListener> GetListener(
PluginMsgId id, wxEventType et,
116 return make_shared<ObservableListener>(
PluginMsg(
id.
id,
""), eh, et);
121 auto msg = UnpackEvtPointer<BasicNavDataMsg>(ev);
124 data.lat = msg->pos.lat;
125 data.lon = msg->pos.lon;
130 data.time = msg->time;
137 std::vector<DriverHandle> result;
139 auto& registry = CommDriverRegistry::GetInstance();
140 const std::vector<std::shared_ptr<AbstractCommDriver>>& drivers =
141 registry.GetDrivers();
143 for (
auto& driver : drivers) result.push_back(driver->Key());
150 auto& registry = CommDriverRegistry::GetInstance();
151 auto drivers = registry.GetDrivers();
152 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
153 auto found = std::find_if(drivers.begin(), drivers.end(), func);
155 std::unordered_map<std::string, std::string> rv;
156 if (found == drivers.end()) {
159 return found->get()->GetAttributes();
163 DriverHandle handle,
const std::shared_ptr<std::vector<uint8_t>>& payload) {
165 auto& registry = CommDriverRegistry::GetInstance();
166 auto drivers = registry.GetDrivers();
167 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
168 auto found = std::find_if(drivers.begin(), drivers.end(), func);
170 if (found == drivers.end()) {
171 return RESULT_COMM_INVALID_HANDLE;
173 auto driver = *found;
176 std::unordered_map<std::string, std::string> attributes =
178 auto protocol_it = attributes.find(
"protocol");
179 if (protocol_it == attributes.end())
return RESULT_COMM_INVALID_PARMS;
180 std::string protocol = protocol_it->second;
182 if (protocol ==
"nmea0183") {
183 auto d0183 = std::dynamic_pointer_cast<CommDriverN0183>(driver);
185 std::string msg(payload->begin(), payload->end());
186 std::string
id = msg.substr(1, 5);
187 auto address = std::make_shared<NavAddr0183>(d0183->iface);
188 auto msg_out = std::make_shared<Nmea0183Msg>(
id, msg, address);
189 bool xmit_ok = d0183->SendMessage(msg_out, address);
190 return xmit_ok ? RESULT_COMM_NO_ERROR : RESULT_COMM_TX_ERROR;
192 return RESULT_COMM_INVALID_PARMS;
196 DriverHandle handle,
int PGN,
int destinationCANAddress,
int priority,
197 const std::shared_ptr<std::vector<uint8_t>>& payload) {
202 auto& registry = CommDriverRegistry::GetInstance();
203 auto drivers = registry.GetDrivers();
204 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
205 auto found = std::find_if(drivers.begin(), drivers.end(), func);
207 if (found == drivers.end()) {
208 return RESULT_COMM_INVALID_HANDLE;
210 auto driver = *found;
211 auto dest_addr = std::make_shared<const NavAddr2000>(driver->iface,
212 destinationCANAddress);
213 auto msg = std::make_shared<const Nmea2000Msg>(_PGN, *payload, dest_addr,
215 bool result = driver->SendMessage(msg, dest_addr);
217 return RESULT_COMM_NO_ERROR;
221 std::vector<int>& pgn_list) {
222 if (!pgn_list.size())
return RESULT_COMM_INVALID_PARMS;
225 auto& registry = CommDriverRegistry::GetInstance();
226 auto drivers = registry.GetDrivers();
227 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
228 auto driver = std::find_if(drivers.begin(), drivers.end(), func);
230 if (driver == drivers.end()) {
231 return RESULT_COMM_INVALID_HANDLE;
233 auto dn2k = std::dynamic_pointer_cast<CommDriverN2K>(*driver);
236 for (
size_t i = 0; i < pgn_list.size(); i++) {
240 while (nTry && iresult < 0) {
241 iresult = dn2k->SetTXPGN(pgn_list[i]);
246 return RESULT_COMM_REGISTER_PGN_ERROR;
249 return RESULT_COMM_NO_ERROR;
252 wxString* GetpPrivateApplicationDataLocation(
void) {
A regular Nmea0183 message.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
Adds a std::shared<void> element to wxCommandEvent.
A plugin to plugin json message over the REST interface.
A parsed SignalK message over ipv4.
CommDriverResult
Error return values
std::string DriverHandle
Plugin API supporting direct access to comm drivers for output purposes.
CommDriverResult WriteCommDriverN2K(DriverHandle handle, int PGN, int destinationCANAddress, int priority, const std::shared_ptr< std::vector< uint8_t >> &payload)
Send a PGN message to an NMEA2000 address.
const std::unordered_map< std::string, std::string > GetAttributes(DriverHandle handle)
Query a specific driver for attributes
std::string GetN0183Payload(NMEA0183Id id, ObservedEvt ev)
Return payload in a received n0183 message of type id in ev.
std::shared_ptr< void > GetSignalkPayload(ObservedEvt ev)
Get SignalK status payload after receiving a message.
PluginNavdata GetEventNavdata(ObservedEvt ev)
Return BasicNavDataMsg decoded data available in ev.
vector< uint8_t > GetN2000Payload(NMEA2000Id id, ObservedEvt ev)
Return N2K payload for a received n2000 message of type id in ev.
std::string GetN2000Source(NMEA2000Id id, ObservedEvt ev)
Return source identifier (iface) of a received n2000 message of type id in ev.
std::vector< DriverHandle > GetActiveDrivers()
Comm port plugin TX support methods
CommDriverResult RegisterTXPGNs(DriverHandle handle, std::vector< int > &pgn_list)
Special NMEA2000 requirements NMEA2000 bus protocol device management requires that devices writing o...
Facade for BasicNavDataMsg.
Facade for NavAddrPluginMsg.
Available decoded data for plugins.
Facade for NavAddrSignalK.