Skip to content

Commit 9c65e45

Browse files
authored
Draft: RSDK-10311: Switch component (#391)
1 parent 62d40eb commit 9c65e45

14 files changed

+446
-0
lines changed

src/viam/api/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
113113
)
114114
endif()
115115

116+
# List of names of non-compilable generated code
117+
# The Switch component generates protobuf which uses `switch` as a namespace.
118+
# This needs to be processed below.
119+
set(VIAMCPPSDK_SWITCH_REPLACE_PATHS
120+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc
121+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.h
122+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc
123+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.h
124+
)
125+
116126
add_custom_command(
117127
OUTPUT
118128
# Unfortunately, there isn't a good way to know in advance what
@@ -201,6 +211,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
201211
${PROTO_GEN_DIR}/component/servo/v1/servo.grpc.pb.h
202212
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.cc
203213
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.h
214+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc
215+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.h
216+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc
217+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.h
204218
${PROTO_GEN_DIR}/google/api/annotations.pb.cc
205219
${PROTO_GEN_DIR}/google/api/annotations.pb.h
206220
${PROTO_GEN_DIR}/google/api/httpbody.pb.cc
@@ -245,6 +259,9 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
245259
COMMAND ${BUF_COMMAND} generate ${BUF_GOOGLE_API_SOURCE} --template buf.gen.yaml --path google/rpc --path google/api
246260
COMMAND ${BUF_COMMAND} generate ${BUF_VIAM_GOUTILS_SOURCE} --template buf.gen.yaml
247261
COMMAND ${BUF_COMMAND} generate ${BUF_VIAM_API_SOURCE} --template buf.gen.yaml --path ${BUF_PROTO_COMPONENTS_JOINED}
262+
263+
# After generating the protos, include a step to invoke a search-and-replace for switch -> switch_ in the Switch component files
264+
COMMAND ${CMAKE_COMMAND} "-DSWITCH_REPLACE_PATHS=\"${VIAMCPPSDK_SWITCH_REPLACE_PATHS}\"" -P ${CMAKE_CURRENT_SOURCE_DIR}/viamcppsdk_replace_switch.cmake
248265
MAIN_DEPENDENCY buf.gen.yaml
249266
)
250267

@@ -328,6 +345,8 @@ target_sources(viamapi
328345
${PROTO_GEN_DIR}/component/sensor/v1/sensor.pb.cc
329346
${PROTO_GEN_DIR}/component/servo/v1/servo.grpc.pb.cc
330347
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.cc
348+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc
349+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc
331350
${PROTO_GEN_DIR}/google/api/annotations.pb.cc
332351
${PROTO_GEN_DIR}/google/api/http.pb.cc
333352
${PROTO_GEN_DIR}/google/api/httpbody.pb.cc
@@ -390,6 +409,8 @@ target_sources(viamapi
390409
${PROTO_GEN_DIR}/../../viam/api/component/sensor/v1/sensor.pb.h
391410
${PROTO_GEN_DIR}/../../viam/api/component/servo/v1/servo.grpc.pb.h
392411
${PROTO_GEN_DIR}/../../viam/api/component/servo/v1/servo.pb.h
412+
${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.grpc.pb.h
413+
${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.pb.h
393414
${PROTO_GEN_DIR}/../../viam/api/google/api/annotations.pb.h
394415
${PROTO_GEN_DIR}/../../viam/api/google/api/http.pb.h
395416
${PROTO_GEN_DIR}/../../viam/api/google/api/httpbody.pb.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if (NOT SWITCH_REPLACE_PATHS)
2+
message(FATAL_ERROR "Please provide SWITCH_REPLACE_PATHS argument to switch replace script")
3+
endif()
4+
5+
foreach(SOURCE ${SWITCH_REPLACE_PATHS})
6+
file(READ "${SOURCE}" _src_text)
7+
string(REPLACE "namespace switch " "namespace switch_ " _src_text "${_src_text}")
8+
string(REPLACE "::switch::" "::switch_::" _src_text "${_src_text}")
9+
file(WRITE "${SOURCE}" "${_src_text}")
10+
endforeach()

src/viam/sdk/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ target_sources(viamsdk
113113
components/private/sensor_server.cpp
114114
components/private/servo_client.cpp
115115
components/private/servo_server.cpp
116+
components/private/switch_client.cpp
117+
components/private/switch_server.cpp
116118
components/sensor.cpp
117119
components/servo.cpp
120+
components/switch.cpp
118121
config/resource.cpp
119122
module/handler_map.cpp
120123
module/module.cpp
@@ -183,6 +186,7 @@ target_sources(viamsdk
183186
../../viam/sdk/components/power_sensor.hpp
184187
../../viam/sdk/components/sensor.hpp
185188
../../viam/sdk/components/servo.hpp
189+
../../viam/sdk/components/switch.hpp
186190
../../viam/sdk/config/resource.hpp
187191
../../viam/sdk/module/handler_map.hpp
188192
../../viam/sdk/module/module.hpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <viam/sdk/components/private/switch_client.hpp>
2+
3+
#include <viam/api/component/switch/v1/switch.grpc.pb.h>
4+
#include <viam/api/component/switch/v1/switch.pb.h>
5+
6+
#include <viam/sdk/common/client_helper.hpp>
7+
8+
namespace viam {
9+
namespace sdk {
10+
namespace impl {
11+
12+
SwitchClient::SwitchClient(std::string name, std::shared_ptr<grpc::Channel> channel)
13+
: Switch(std::move(name)),
14+
stub_(viam::component::switch_::v1::SwitchService::NewStub(channel)),
15+
channel_(std::move(channel)) {}
16+
17+
void SwitchClient::set_position(uint32_t position, const ProtoStruct& extra) {
18+
make_client_helper(this, *stub_, &StubType::SetPosition)
19+
.with(extra, [&](auto& request) { request.set_position(position); })
20+
.invoke();
21+
}
22+
23+
uint32_t SwitchClient::get_position(const ProtoStruct& extra) {
24+
return make_client_helper(this, *stub_, &StubType::GetPosition)
25+
.with(extra)
26+
.invoke([](auto& response) { return response.position(); });
27+
}
28+
29+
uint32_t SwitchClient::get_number_of_positions(const ProtoStruct& extra) {
30+
return make_client_helper(this, *stub_, &StubType::GetNumberOfPositions)
31+
.with(extra)
32+
.invoke([](auto& response) { return response.number_of_positions(); });
33+
}
34+
35+
ProtoStruct SwitchClient::do_command(const ProtoStruct& command) {
36+
return make_client_helper(this, *stub_, &StubType::DoCommand)
37+
.with([&](auto& request) { *request.mutable_command() = to_proto(command); })
38+
.invoke([](auto& response) { return from_proto(response.result()); });
39+
}
40+
41+
} // namespace impl
42+
} // namespace sdk
43+
} // namespace viam
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// @file components/private/switch_client.hpp
2+
///
3+
/// @brief Implements a gRPC client for the `Switch` component
4+
#pragma once
5+
6+
#include <grpcpp/channel.h>
7+
8+
#include <viam/api/component/switch/v1/switch.grpc.pb.h>
9+
10+
#include <viam/sdk/components/switch.hpp>
11+
12+
namespace viam {
13+
namespace sdk {
14+
namespace impl {
15+
16+
/// @class SwitchClient
17+
/// @brief gRPC client implementation of a `Switch` component.
18+
/// @ingroup Switch
19+
class SwitchClient : public Switch {
20+
public:
21+
using interface_type = Switch;
22+
SwitchClient(std::string name, std::shared_ptr<grpc::Channel> channel);
23+
24+
void set_position(uint32_t position, const ProtoStruct& extra) override;
25+
uint32_t get_position(const ProtoStruct& extra) override;
26+
uint32_t get_number_of_positions(const ProtoStruct& extra) override;
27+
ProtoStruct do_command(const ProtoStruct& command) override;
28+
29+
// Using declarations to introduce convenience overloads of interface which do not need to be
30+
// passed the ProtoStruct parameter.
31+
using Switch::get_number_of_positions;
32+
using Switch::get_position;
33+
using Switch::set_position;
34+
35+
private:
36+
using StubType = viam::component::switch_::v1::SwitchService::StubInterface;
37+
std::unique_ptr<StubType> stub_;
38+
std::shared_ptr<grpc::Channel> channel_;
39+
};
40+
41+
} // namespace impl
42+
} // namespace sdk
43+
} // namespace viam
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <viam/sdk/components/private/switch_server.hpp>
2+
3+
#include <viam/sdk/common/private/service_helper.hpp>
4+
5+
namespace viam {
6+
namespace sdk {
7+
namespace impl {
8+
9+
SwitchServer::SwitchServer(std::shared_ptr<ResourceManager> manager)
10+
: ResourceServer(std::move(manager)) {}
11+
12+
::grpc::Status SwitchServer::SetPosition(
13+
::grpc::ServerContext*,
14+
const ::viam::component::switch_::v1::SetPositionRequest* request,
15+
::viam::component::switch_::v1::SetPositionResponse*) noexcept {
16+
return make_service_helper<Switch>(
17+
"SwitchServer::SetPosition", this, request)([&](auto& helper, auto& switch_) {
18+
switch_->set_position(request->position(), helper.getExtra());
19+
});
20+
}
21+
22+
::grpc::Status SwitchServer::GetPosition(
23+
::grpc::ServerContext*,
24+
const ::viam::component::switch_::v1::GetPositionRequest* request,
25+
::viam::component::switch_::v1::GetPositionResponse* response) noexcept {
26+
return make_service_helper<Switch>(
27+
"SwitchServer::GetPosition", this, request)([&](auto& helper, auto& switch_) {
28+
response->set_position(switch_->get_position(helper.getExtra()));
29+
});
30+
}
31+
32+
::grpc::Status SwitchServer::GetNumberOfPositions(
33+
::grpc::ServerContext*,
34+
const ::viam::component::switch_::v1::GetNumberOfPositionsRequest* request,
35+
::viam::component::switch_::v1::GetNumberOfPositionsResponse* response) noexcept {
36+
return make_service_helper<Switch>(
37+
"SwitchServer::GetNumberOfPositions", this, request)([&](auto& helper, auto& switch_) {
38+
response->set_number_of_positions(switch_->get_number_of_positions(helper.getExtra()));
39+
});
40+
}
41+
42+
::grpc::Status SwitchServer::DoCommand(::grpc::ServerContext*,
43+
const ::viam::common::v1::DoCommandRequest* request,
44+
::viam::common::v1::DoCommandResponse* response) noexcept {
45+
return make_service_helper<Switch>(
46+
"SwitchServer::DoCommand", this, request)([&](auto&, auto& switch_) {
47+
const ProtoStruct result = switch_->do_command(from_proto(request->command()));
48+
*response->mutable_result() = to_proto(result);
49+
});
50+
}
51+
52+
} // namespace impl
53+
} // namespace sdk
54+
} // namespace viam
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// @file components/private/switch_server.hpp
2+
///
3+
/// @brief Implements a gRPC server for the `Switch` component
4+
#pragma once
5+
6+
#include <viam/api/component/switch/v1/switch.grpc.pb.h>
7+
#include <viam/api/component/switch/v1/switch.pb.h>
8+
9+
#include <viam/sdk/components/switch.hpp>
10+
#include <viam/sdk/resource/resource_manager.hpp>
11+
#include <viam/sdk/resource/resource_server_base.hpp>
12+
13+
namespace viam {
14+
namespace sdk {
15+
namespace impl {
16+
17+
/// @class SwitchServer
18+
/// @brief gRPC server implementation of a `Switch` component.
19+
/// @ingroup Switch
20+
class SwitchServer : public ResourceServer,
21+
public viam::component::switch_::v1::SwitchService::Service {
22+
public:
23+
using interface_type = Switch;
24+
using service_type = component::switch_::v1::SwitchService;
25+
26+
explicit SwitchServer(std::shared_ptr<ResourceManager> manager);
27+
28+
::grpc::Status SetPosition(
29+
::grpc::ServerContext* context,
30+
const ::viam::component::switch_::v1::SetPositionRequest* request,
31+
::viam::component::switch_::v1::SetPositionResponse* response) noexcept override;
32+
33+
::grpc::Status GetPosition(
34+
::grpc::ServerContext* context,
35+
const ::viam::component::switch_::v1::GetPositionRequest* request,
36+
::viam::component::switch_::v1::GetPositionResponse* response) noexcept override;
37+
38+
::grpc::Status GetNumberOfPositions(
39+
::grpc::ServerContext* context,
40+
const ::viam::component::switch_::v1::GetNumberOfPositionsRequest* request,
41+
::viam::component::switch_::v1::GetNumberOfPositionsResponse* response) noexcept override;
42+
43+
::grpc::Status DoCommand(::grpc::ServerContext* context,
44+
const ::viam::common::v1::DoCommandRequest* request,
45+
::viam::common::v1::DoCommandResponse* response) noexcept override;
46+
};
47+
48+
} // namespace impl
49+
} // namespace sdk
50+
} // namespace viam

src/viam/sdk/components/switch.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <viam/sdk/components/switch.hpp>
2+
3+
#include <viam/sdk/common/utils.hpp>
4+
5+
namespace viam {
6+
namespace sdk {
7+
8+
API Switch::api() const {
9+
return API::get<Switch>();
10+
}
11+
12+
API API::traits<Switch>::api() {
13+
return {kRDK, kComponent, "switch"};
14+
}
15+
16+
Switch::Switch(std::string name) : Component(std::move(name)) {}
17+
18+
} // namespace sdk
19+
} // namespace viam

src/viam/sdk/components/switch.hpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/// @file components/switch.hpp
2+
///
3+
/// @brief Defines a `Switch` component
4+
#pragma once
5+
6+
#include <cstdint>
7+
8+
#include <string>
9+
10+
#include <viam/sdk/common/proto_value.hpp>
11+
#include <viam/sdk/components/component.hpp>
12+
#include <viam/sdk/resource/resource_api.hpp>
13+
14+
namespace viam {
15+
namespace sdk {
16+
17+
/// @defgroup Switch Classes related to the Switch component.
18+
19+
/// @class Switch switch.hpp "components/switch.hpp"
20+
/// @brief A `Switch` represents a physical switch with multiple positions.
21+
/// @ingroup Switch
22+
///
23+
/// This acts as an abstract parent class to be inherited from by any drivers representing
24+
/// specific switch implementations. This class cannot be used on its own.
25+
class Switch : public Component {
26+
public:
27+
/// @brief Set the position of the switch.
28+
/// @param position The position to set the switch to.
29+
inline void set_position(uint32_t position) {
30+
set_position(position, {});
31+
}
32+
33+
/// @brief Set the position of the switch.
34+
/// @param position The position to set the switch to.
35+
/// @param extra Any additional arguments to the method.
36+
virtual void set_position(uint32_t position, const ProtoStruct& extra) = 0;
37+
38+
/// @brief Get the current position of the switch.
39+
/// @return The current position of the switch.
40+
inline uint32_t get_position() {
41+
return get_position({});
42+
}
43+
44+
/// @brief Get the current position of the switch.
45+
/// @param extra Any additional arguments to the method.
46+
/// @return The current position of the switch.
47+
virtual uint32_t get_position(const ProtoStruct& extra) = 0;
48+
49+
/// @brief Get the number of positions that the switch supports.
50+
/// @return The number of positions that the switch supports.
51+
inline uint32_t get_number_of_positions() {
52+
return get_number_of_positions({});
53+
}
54+
55+
/// @brief Get the number of positions that the switch supports.
56+
/// @param extra Any additional arguments to the method.
57+
/// @return The number of positions that the switch supports.
58+
virtual uint32_t get_number_of_positions(const ProtoStruct& extra) = 0;
59+
60+
/// @brief Send/receive arbitrary commands to the resource.
61+
/// @param command The command to execute.
62+
/// @return The result of the executed command.
63+
virtual ProtoStruct do_command(const ProtoStruct& command) = 0;
64+
65+
API api() const override;
66+
67+
protected:
68+
explicit Switch(std::string name);
69+
};
70+
71+
template <>
72+
struct API::traits<Switch> {
73+
static API api();
74+
};
75+
76+
} // namespace sdk
77+
} // namespace viam

src/viam/sdk/registry/registry.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include <viam/sdk/components/private/sensor_server.hpp>
4444
#include <viam/sdk/components/private/servo_client.hpp>
4545
#include <viam/sdk/components/private/servo_server.hpp>
46+
#include <viam/sdk/components/private/switch_client.hpp>
47+
#include <viam/sdk/components/private/switch_server.hpp>
4648
#include <viam/sdk/resource/resource.hpp>
4749
#include <viam/sdk/resource/resource_api.hpp>
4850
#include <viam/sdk/services/private/discovery_client.hpp>
@@ -214,6 +216,7 @@ void Registry::register_resources() {
214216
register_resource<impl::PowerSensorClient, impl::PowerSensorServer>();
215217
register_resource<impl::SensorClient, impl::SensorServer>();
216218
register_resource<impl::ServoClient, impl::ServoServer>();
219+
register_resource<impl::SwitchClient, impl::SwitchServer>();
217220

218221
// Register all services
219222
register_resource<impl::DiscoveryClient, impl::DiscoveryServer>();

0 commit comments

Comments
 (0)