OpenCPN Partial API docs
Loading...
Searching...
No Matches
local_api.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 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, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
25#include <iostream>
26#include <string>
27
28#include "model/local_api.h"
29#include "model/ocpn_utils.h"
30#include "model/logger.h"
31
32#ifdef __ANDROID__
33
34CmdlineAction LocalClientApi::ParseArgs(const wxCmdLineParser& parser,
35 std::string& arg) {
36 return CmdlineAction::Skip;
37}
38
39#else
40CmdlineAction LocalClientApi::ParseArgs(const wxCmdLineParser& parser,
41 std::string& arg) {
42 CmdlineAction result = CmdlineAction::Fail;
43 arg = "";
44 if (parser.GetParamCount() == 0) {
45 result = CmdlineAction::Raise;
46 } else if (parser.GetParamCount() == 1) {
47 if (parser.GetParam(0) == "raise") {
48 result = CmdlineAction::Raise;
49 } else if (parser.GetParam(0) == "quit") {
50 result = CmdlineAction::Quit;
51 } else if (parser.GetParam(0) == "get_rest_endpoint") {
52 result = CmdlineAction::GetRestEndpoint;
53 } else if (ocpn::exists(parser.GetParam(0).ToStdString().c_str())) {
54 result = CmdlineAction::Open;
55 arg = parser.GetParam(0).ToStdString();
56 }
57 } else if (parser.GetParamCount() == 2) {
58 if (parser.GetParam(0) == "open") {
59 result = CmdlineAction::Open;
60 arg = parser.GetParam(1).ToStdString();
61 }
62 }
63 return result;
64}
65#endif // __ANDROID__
66
67LocalApiResult LocalClientApi::HandleCmdline(const wxCmdLineParser& parser) {
68 std::string arg;
69 auto action = ParseArgs(parser, arg);
70 return HandleCmdline(action, arg);
71}
72
73LocalApiResult LocalClientApi::HandleCmdline(CmdlineAction action,
74 const std::string& arg) {
75 switch (action) {
76 case CmdlineAction::Fail:
77 MESSAGE_LOG << "IpcClient: Cannot parse command line (ignored)";
78 return LocalApiResult(false, "Cannot parse command line");
79 case CmdlineAction::Quit: {
80 auto result = SendQuit();
81 if (!result.first) {
82 MESSAGE_LOG << "Error running remote quit cmd: " << result.second;
83 }
84 return result;
85 } break;
86 case CmdlineAction::Raise: {
87 auto result = SendRaise();
88 if (!result.first) {
89 MESSAGE_LOG << "Error running remote raise cmd: " << result.second;
90 }
91 return result;
92 } break;
93 case CmdlineAction::Open: {
94 auto result = SendOpen(arg.c_str());
95 if (!result.first) {
96 MESSAGE_LOG << "Error running remote open of file \"" << arg
97 << "\": " << result.second;
98 }
99 return result;
100 } break;
101 case CmdlineAction::GetRestEndpoint: {
102 auto result = GetRestEndpoint();
103 if (result.first)
104 std::cout << result.second << "\n" << std::flush;
105 else
106 std::cout << "Error getting remote endpoint: " << result.second << "\n"
107 << std::flush;
108 return result;
109 }
110 case CmdlineAction::Skip:
111 return LocalApiResult(true, "Unknown command CmdlineAction::Skip");
112 }
113 wxLogMessage("Strange code path!");
114 return LocalApiResult(false, "Internal error");
115}
The local API has a server side handling commands and a client part issuing commands.
Enhanced logging interface on top of wx/log.h.
bool exists(const std::string &name)
Miscellaneous utilities, many of which string related.