-
Notifications
You must be signed in to change notification settings - Fork 110
/
server.go
40 lines (34 loc) · 1.16 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
// Package generic contains a gRPC based generic service serviceServer.
package generic
import (
"context"
commonpb "go.viam.com/api/common/v1"
genericpb "go.viam.com/api/component/generic/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/rdk/resource"
)
// serviceServer implements the resource.Generic service.
type serviceServer struct {
genericpb.UnimplementedGenericServiceServer
coll resource.APIResourceCollection[resource.Resource]
}
// NewRPCServiceServer constructs an generic gRPC service serviceServer.
func NewRPCServiceServer(coll resource.APIResourceCollection[resource.Resource]) interface{} {
return &serviceServer{coll: coll}
}
// DoCommand returns an arbitrary command and returns arbitrary results.
func (s *serviceServer) DoCommand(ctx context.Context, req *commonpb.DoCommandRequest) (*commonpb.DoCommandResponse, error) {
genericDevice, err := s.coll.Resource(req.Name)
if err != nil {
return nil, err
}
result, err := genericDevice.DoCommand(ctx, req.Command.AsMap())
if err != nil {
return nil, err
}
res, err := protoutils.StructToStructPb(result)
if err != nil {
return nil, err
}
return &commonpb.DoCommandResponse{Result: res}, nil
}