Skip to content

Commit 9f93d41

Browse files
committed
claude switch + edits (wip: buf output)
1 parent e969bfc commit 9f93d41

File tree

13 files changed

+423
-0
lines changed

13 files changed

+423
-0
lines changed

src/viam/api/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
197197
${PROTO_GEN_DIR}/component/servo/v1/servo.grpc.pb.h
198198
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.cc
199199
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.h
200+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc
201+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.h
202+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc
203+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.h
200204
${PROTO_GEN_DIR}/google/api/annotations.pb.cc
201205
${PROTO_GEN_DIR}/google/api/annotations.pb.h
202206
${PROTO_GEN_DIR}/google/api/httpbody.pb.cc
@@ -322,6 +326,8 @@ target_sources(viamapi
322326
${PROTO_GEN_DIR}/component/sensor/v1/sensor.pb.cc
323327
${PROTO_GEN_DIR}/component/servo/v1/servo.grpc.pb.cc
324328
${PROTO_GEN_DIR}/component/servo/v1/servo.pb.cc
329+
${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc
330+
${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc
325331
${PROTO_GEN_DIR}/google/api/annotations.pb.cc
326332
${PROTO_GEN_DIR}/google/api/http.pb.cc
327333
${PROTO_GEN_DIR}/google/api/httpbody.pb.cc
@@ -382,6 +388,8 @@ target_sources(viamapi
382388
${PROTO_GEN_DIR}/../../viam/api/component/sensor/v1/sensor.pb.h
383389
${PROTO_GEN_DIR}/../../viam/api/component/servo/v1/servo.grpc.pb.h
384390
${PROTO_GEN_DIR}/../../viam/api/component/servo/v1/servo.pb.h
391+
${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.grpc.pb.h
392+
${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.pb.h
385393
${PROTO_GEN_DIR}/../../viam/api/google/api/annotations.pb.h
386394
${PROTO_GEN_DIR}/../../viam/api/google/api/http.pb.h
387395
${PROTO_GEN_DIR}/../../viam/api/google/api/httpbody.pb.h

src/viam/sdk/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ target_sources(viamsdk
100100
components/private/sensor_server.cpp
101101
components/private/servo_client.cpp
102102
components/private/servo_server.cpp
103+
components/private/switch_client.cpp
104+
components/private/switch_server.cpp
103105
components/sensor.cpp
104106
components/servo.cpp
107+
components/switch.cpp
105108
config/resource.cpp
106109
module/handler_map.cpp
107110
module/module.cpp
@@ -169,6 +172,7 @@ target_sources(viamsdk
169172
../../viam/sdk/components/power_sensor.hpp
170173
../../viam/sdk/components/sensor.hpp
171174
../../viam/sdk/components/servo.hpp
175+
../../viam/sdk/components/switch.hpp
172176
../../viam/sdk/config/resource.hpp
173177
../../viam/sdk/module/handler_map.hpp
174178
../../viam/sdk/module/module.hpp
Lines changed: 43 additions & 0 deletions
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
Lines changed: 43 additions & 0 deletions
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
Lines changed: 54 additions & 0 deletions
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
Lines changed: 50 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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

Lines changed: 77 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <viam/sdk/components/private/sensor_server.hpp>
4242
#include <viam/sdk/components/private/servo_client.hpp>
4343
#include <viam/sdk/components/private/servo_server.hpp>
44+
#include <viam/sdk/components/private/switch_client.hpp>
45+
#include <viam/sdk/components/private/switch_server.hpp>
4446
#include <viam/sdk/resource/resource.hpp>
4547
#include <viam/sdk/resource/resource_api.hpp>
4648
#include <viam/sdk/services/private/discovery_client.hpp>
@@ -211,6 +213,7 @@ void Registry::register_resources() {
211213
register_resource<impl::PowerSensorClient, impl::PowerSensorServer>();
212214
register_resource<impl::SensorClient, impl::SensorServer>();
213215
register_resource<impl::ServoClient, impl::ServoServer>();
216+
register_resource<impl::SwitchClient, impl::SwitchServer>();
214217

215218
// Register all services
216219
register_resource<impl::DiscoveryClient, impl::DiscoveryServer>();

src/viam/sdk/tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ target_sources(viamsdk_test
3636
mocks/mock_power_sensor.cpp
3737
mocks/mock_sensor.cpp
3838
mocks/mock_servo.cpp
39+
mocks/mock_switch.cpp
3940
mocks/mock_robot.cpp
4041
test_utils.cpp
4142
)
@@ -74,4 +75,5 @@ viamcppsdk_add_boost_test(test_proto_value_visit.cpp)
7475
viamcppsdk_add_boost_test(test_resource.cpp)
7576
viamcppsdk_add_boost_test(test_sensor.cpp)
7677
viamcppsdk_add_boost_test(test_servo.cpp)
78+
viamcppsdk_add_boost_test(test_switch.cpp)
7779
viamcppsdk_add_boost_test(test_robot.cpp)

0 commit comments

Comments
 (0)