-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
113 lines (102 loc) · 2.72 KB
/
client.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
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
102
103
104
105
106
107
108
109
110
111
112
113
package powersensor
import (
"context"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/powersensor/v1"
"go.viam.com/utils/rpc"
"google.golang.org/protobuf/types/known/structpb"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// client implements PowerSensorServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.PowerSensorServiceClient
logger logging.Logger
}
// NewClientFromConn constructs a new client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (PowerSensor, error) {
c := pb.NewPowerSensorServiceClient(conn)
return &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: c,
logger: logger,
}, nil
}
// Voltage returns the voltage reading in volts and a bool returning true if the voltage is AC.
func (c *client) Voltage(ctx context.Context, extra map[string]interface{}) (float64, bool, error) {
ext, err := structpb.NewStruct(extra)
if err != nil {
return 0, false, err
}
resp, err := c.client.GetVoltage(ctx, &pb.GetVoltageRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return 0, false, err
}
return resp.Volts,
resp.IsAc,
nil
}
// Current returns the current reading in amperes and a bool returning true if the current is AC.
func (c *client) Current(ctx context.Context, extra map[string]interface{}) (float64, bool, error) {
ext, err := structpb.NewStruct(extra)
if err != nil {
return 0, false, err
}
resp, err := c.client.GetCurrent(ctx, &pb.GetCurrentRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return 0, false, err
}
return resp.Amperes,
resp.IsAc,
nil
}
// Power returns the power reading in watts.
func (c *client) Power(ctx context.Context, extra map[string]interface{}) (float64, error) {
ext, err := structpb.NewStruct(extra)
if err != nil {
return 0, err
}
resp, err := c.client.GetPower(ctx, &pb.GetPowerRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return 0, err
}
return resp.Watts, nil
}
func (c *client) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
ext, err := structpb.NewStruct(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetReadings(ctx, &commonpb.GetReadingsRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return nil, err
}
return protoutils.ReadingProtoToGo(resp.Readings)
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return protoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}