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