forked from viamrobotics/viam-cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
101 lines (87 loc) · 3.76 KB
/
client.cpp
File metadata and controls
101 lines (87 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cstddef>
#include <iostream>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/support/status.h>
#include <viam/sdk/common/instance.hpp>
#include <viam/sdk/components/motor.hpp>
#include <viam/sdk/robot/client.hpp>
#include <viam/sdk/rpc/dial.hpp>
#include "gizmo/api.hpp"
#include "summation/api.hpp"
using namespace viam::sdk;
int main() {
// Every Viam C++ SDK program must have one and only one Instance object which is created before
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
Instance inst;
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
DialOptions dial_options;
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely
// Uncomment and fill out your credentials details if connecting securely
// std::string type = "<your authentication type>";
// std::string payload = "<your authentication payload>";
// Credentials credentials(type, payload);
// dial_options.set_credentials(credentials);
boost::optional<DialOptions> opts(dial_options);
std::string address(uri);
Options options(1, opts);
// Register custom gizmo and summation clients so robot client can access resources
// of that type from the server.
Registry::get().register_resource_client<GizmoClient>();
Registry::get().register_resource_client<SummationClient>();
// Connect to robot.
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
// Print resources.
std::cout << "Resources" << std::endl;
std::vector<Name> resource_names = robot->resource_names();
for (const Name& resource : resource_names) {
std::cout << "\t" << resource << "\n";
}
// Exercise Gizmo methods.
auto gc = robot->resource_by_name<Gizmo>("gizmo1");
if (!gc) {
std::cerr << "could not get 'gizmo1' resource from robot" << std::endl;
return EXIT_FAILURE;
}
bool do_one_ret = gc->do_one("arg1");
std::cout << "gizmo1 do_one returned: " << do_one_ret << std::endl;
bool do_one_client_stream_ret = gc->do_one_client_stream({"arg1", "arg1", "arg1"});
std::cout << "gizmo1 do_one_client_stream returned: " << do_one_client_stream_ret << std::endl;
std::string do_two_ret = gc->do_two(false);
std::cout << "gizmo1 do_two returned: " << do_two_ret << std::endl;
std::vector<bool> do_one_server_stream_ret = gc->do_one_server_stream("arg1");
std::cout << "gizmo1 do_one_server_stream returned: " << std::endl;
for (bool ret : do_one_server_stream_ret) {
std::cout << '\t' << ret << std::endl;
}
std::vector<bool> do_one_bidi_stream_ret = gc->do_one_bidi_stream({"arg1", "arg2", "arg3"});
std::cout << "gizmo1 do_one_bidi_stream returned: " << std::endl;
for (bool ret : do_one_bidi_stream_ret) {
std::cout << '\t' << ret << std::endl;
}
// Exercise Summation methods.
auto sc = robot->resource_by_name<Summation>("mysum1");
if (!sc) {
std::cerr << "could not get 'mysum1' resource from robot" << std::endl;
return EXIT_FAILURE;
}
double sum = sc->sum({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
std::cout << "mysum1 sum of numbers [0, 10) is: " << sum << std::endl;
// Exercise a Base method.
auto mc = robot->resource_by_name<Motor>("motor1");
if (!mc) {
std::cerr << "could not get 'motor1' resource from robot" << std::endl;
return EXIT_FAILURE;
}
if (mc->is_moving()) {
std::cout << "motor1 is moving" << std::endl;
} else {
std::cout << "motor1 is not moving" << std::endl;
}
return EXIT_SUCCESS;
}