-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathserver.go
44 lines (37 loc) · 1.22 KB
/
server.go
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
// Package button contains a gRPC based button service server.
package button
import (
"context"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/button/v1"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// serviceServer implements the ButtonService from button.proto.
type serviceServer struct {
pb.UnimplementedButtonServiceServer
coll resource.APIResourceCollection[Button]
}
// NewRPCServiceServer constructs an gripper gRPC service server.
// It is intentionally untyped to prevent use outside of tests.
func NewRPCServiceServer(coll resource.APIResourceCollection[Button]) interface{} {
return &serviceServer{coll: coll}
}
// Pushes a button.
func (s *serviceServer) Push(ctx context.Context, req *pb.PushRequest) (*pb.PushResponse, error) {
button, err := s.coll.Resource(req.Name)
if err != nil {
return nil, err
}
return &pb.PushResponse{}, button.Push(ctx, req.Extra.AsMap())
}
// DoCommand receives arbitrary commands.
func (s *serviceServer) DoCommand(ctx context.Context,
req *commonpb.DoCommandRequest,
) (*commonpb.DoCommandResponse, error) {
button, err := s.coll.Resource(req.GetName())
if err != nil {
return nil, err
}
return protoutils.DoFromResourceServer(ctx, button, req)
}