Apollo 11.0
自动驾驶开放平台
parameter_client.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include "cyber/node/node.h"
21
22namespace apollo {
23namespace cyber {
24
25ParameterClient::ParameterClient(const std::shared_ptr<Node>& node,
26 const std::string& service_node_name)
27 : node_(node) {
28 get_parameter_client_ = node_->CreateClient<ParamName, Param>(
29 FixParameterServiceName(service_node_name, GET_PARAMETER_SERVICE_NAME));
30
31 set_parameter_client_ = node_->CreateClient<Param, BoolResult>(
32 FixParameterServiceName(service_node_name, SET_PARAMETER_SERVICE_NAME));
33
34 list_parameters_client_ = node_->CreateClient<NodeName, Params>(
35 FixParameterServiceName(service_node_name, LIST_PARAMETERS_SERVICE_NAME));
36}
37
38bool ParameterClient::GetParameter(const std::string& param_name,
39 Parameter* parameter) {
40 auto request = std::make_shared<ParamName>();
41 request->set_value(param_name);
42 auto response = get_parameter_client_->SendRequest(request);
43 if (response == nullptr) {
44 AERROR << "Call " << get_parameter_client_->ServiceName() << " failed";
45 return false;
46 }
47 if (response->type() == ParamType::NOT_SET) {
48 AWARN << "Parameter " << param_name << " not exists yet.";
49 return false;
50 }
51 parameter->FromProtoParam(*response);
52 return true;
53}
54
56 auto request = std::make_shared<Param>(parameter.ToProtoParam());
57 auto response = set_parameter_client_->SendRequest(request);
58 if (response == nullptr) {
59 AERROR << "Call " << set_parameter_client_->ServiceName() << " failed";
60 return false;
61 }
62 return response->value();
63}
64
65bool ParameterClient::ListParameters(std::vector<Parameter>* parameters) {
66 auto request = std::make_shared<NodeName>();
67 request->set_value(node_->Name());
68 auto response = list_parameters_client_->SendRequest(request);
69 if (response == nullptr) {
70 AERROR << "Call " << list_parameters_client_->ServiceName() << " failed";
71 return false;
72 }
73 for (auto& param : response->param()) {
74 Parameter parameter;
75 parameter.FromProtoParam(param);
76 parameters->emplace_back(parameter);
77 }
78 return true;
79}
80
81} // namespace cyber
82} // namespace apollo
ParameterClient(const std::shared_ptr< Node > &node, const std::string &service_node_name)
Construct a new ParameterClient object
bool ListParameters(std::vector< Parameter > *parameters)
Get all the Parameter objects
bool SetParameter(const Parameter &parameter)
Set the Parameter object
bool GetParameter(const std::string &param_name, Parameter *parameter)
Get the Parameter object
apollo::cyber::proto::ParamName ParamName
apollo::cyber::proto::NodeName NodeName
apollo::cyber::proto::Param Param
A Parameter holds an apollo::cyber::proto::Param, It's more human-readable, you can use basic-value t...
Definition parameter.h:42
void FromProtoParam(const Param &param)
Parse a cyber::proto::Param object to cyber::parameter::Parameter object
Definition parameter.cc:112
Param ToProtoParam() const
Parse a cyber::parameter::Parameter object to cyber::proto::Param object
Definition parameter.cc:114
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
constexpr auto SET_PARAMETER_SERVICE_NAME
constexpr auto LIST_PARAMETERS_SERVICE_NAME
constexpr auto GET_PARAMETER_SERVICE_NAME
class register implement
Definition arena_queue.h:37