OpenCPN Partial API docs
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 
28 CmdlineAction LocalClientApi::ParseArgs(const wxCmdLineParser& parser,
29  std::string& arg) {
30  return CmdlineAction::Skip;
31 }
32 
33 #else
34 CmdlineAction 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 
61 LocalApiResult LocalClientApi::HandleCmdline(const wxCmdLineParser& parser) {
62  std::string arg;
63  auto action = ParseArgs(parser, arg);
64  return HandleCmdline(action, arg);
65 }
66 
67 LocalApiResult 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  }
80  break;
81  case CmdlineAction::Raise: {
82  auto result = SendRaise();
83  if (!result.first) {
84  MESSAGE_LOG << "Error running remote raise cmd: " << result.second;
85  }
86  return result;
87  }
88  break;
89  case CmdlineAction::Open: {
90  auto result = SendOpen(arg.c_str());
91  if (!result.first) {
92  MESSAGE_LOG << "Error running remote open of file \"" << arg
93  << "\": " << result.second;
94  }
95  return result;
96  }
97  break;
98  case CmdlineAction::GetRestEndpoint: {
99  auto result = GetRestEndpoint();
100  if (result.first)
101  std::cout << result.second << "\n" << std::flush;
102  else
103  std::cout << "Error getting remote endpoint: " << result.second
104  << "\n" << std::flush;
105  return result;
106  }
107  case CmdlineAction::Skip:
108  return LocalApiResult(true, "Unknown command CmdlineAction::Skip");
109  }
110  wxLogMessage("Strange code path!");
111  return LocalApiResult(false, "Internal error");
112 }
The local API has a server side handling commands and a client part issuing commands.