forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovisioning_client.go
139 lines (123 loc) · 3.86 KB
/
provisioning_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package app
import (
"context"
pb "go.viam.com/api/provisioning/v1"
"go.viam.com/utils/rpc"
)
// ProvisioningInfo holds provisioning info.
type ProvisioningInfo struct {
FragmentID string
Model string
Manufacturer string
}
// NetworkInfo holds network information.
type NetworkInfo struct {
Type string
SSID string
Security string
Signal int
Connected bool
LastError string
}
// GetSmartMachineStatusResponse contains smart machine status information.
type GetSmartMachineStatusResponse struct {
ProvisioningInfo *ProvisioningInfo
HasSmartMachineCredentials bool
IsOnline bool
LastestConnectionAttempt *NetworkInfo
Errors []string
}
// CloudConfig is the minimal config to create a /etc/viam.json, containing the smart machine's part ID and secret.
type CloudConfig struct {
ID string
Secret string
AppAddress string
}
// ProvisioningClient is a gRPC client for method calls to the Provisioning API.
type ProvisioningClient struct {
client pb.ProvisioningServiceClient
}
func newProvisioningClient(conn rpc.ClientConn) *ProvisioningClient {
return &ProvisioningClient{client: pb.NewProvisioningServiceClient(conn)}
}
// GetSmartMachineStatus gets the status of the smart machine including networking.
func (c *ProvisioningClient) GetSmartMachineStatus(ctx context.Context) (*GetSmartMachineStatusResponse, error) {
resp, err := c.client.GetSmartMachineStatus(ctx, &pb.GetSmartMachineStatusRequest{})
if err != nil {
return nil, err
}
return getSmartMachineStatusResponseFromProto(resp), nil
}
// SetNetworkCredentials sets the wifi credentials.
func (c *ProvisioningClient) SetNetworkCredentials(ctx context.Context, credentialsType, ssid, psk string) error {
_, err := c.client.SetNetworkCredentials(ctx, &pb.SetNetworkCredentialsRequest{
Type: credentialsType,
Ssid: ssid,
Psk: psk,
})
return err
}
// SetSmartMachineCredentials sets the smart machine credentials.
func (c *ProvisioningClient) SetSmartMachineCredentials(ctx context.Context, cloud *CloudConfig) error {
_, err := c.client.SetSmartMachineCredentials(ctx, &pb.SetSmartMachineCredentialsRequest{
Cloud: cloudConfigToProto(cloud),
})
return err
}
// GetNetworkList gets the list of networks that are visible to the smart machine.
func (c *ProvisioningClient) GetNetworkList(ctx context.Context) ([]*NetworkInfo, error) {
resp, err := c.client.GetNetworkList(ctx, &pb.GetNetworkListRequest{})
if err != nil {
return nil, err
}
var networks []*NetworkInfo
for _, network := range resp.Networks {
networks = append(networks, networkInfoFromProto(network))
}
return networks, nil
}
func provisioningInfoFromProto(info *pb.ProvisioningInfo) *ProvisioningInfo {
if info == nil {
return nil
}
return &ProvisioningInfo{
FragmentID: info.FragmentId,
Model: info.Model,
Manufacturer: info.Manufacturer,
}
}
func networkInfoFromProto(info *pb.NetworkInfo) *NetworkInfo {
if info == nil {
return nil
}
return &NetworkInfo{
Type: info.Type,
SSID: info.Ssid,
Security: info.Security,
Signal: int(info.Signal),
Connected: info.Connected,
LastError: info.LastError,
}
}
func getSmartMachineStatusResponseFromProto(resp *pb.GetSmartMachineStatusResponse) *GetSmartMachineStatusResponse {
if resp == nil {
return nil
}
return &GetSmartMachineStatusResponse{
ProvisioningInfo: provisioningInfoFromProto(resp.ProvisioningInfo),
HasSmartMachineCredentials: resp.HasSmartMachineCredentials,
IsOnline: resp.IsOnline,
LastestConnectionAttempt: networkInfoFromProto(resp.LatestConnectionAttempt),
Errors: resp.Errors,
}
}
func cloudConfigToProto(config *CloudConfig) *pb.CloudConfig {
if config == nil {
return nil
}
return &pb.CloudConfig{
Id: config.ID,
Secret: config.Secret,
AppAddress: config.AppAddress,
}
}