Skip to content

Commit 695f23c

Browse files
committed
Improved client.go GRPC example
1 parent d473179 commit 695f23c

File tree

1 file changed

+92
-54
lines changed

1 file changed

+92
-54
lines changed

daemon/client/client.go

Lines changed: 92 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ func main() {
3333
os.Exit(1)
3434
}
3535
datadir := os.Args[1]
36+
37+
fmt.Println("=== Connecting to GRPC server")
3638
conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
3739
if err != nil {
3840
fmt.Printf("Error connecting to server: %s\n", err)
3941
os.Exit(1)
4042
}
4143
client := rpc.NewArduinoCoreClient(conn)
44+
fmt.Println()
4245

43-
resp, err := client.Init(context.Background(), &rpc.InitReq{
46+
// INIT
47+
fmt.Println("=== calling Init")
48+
initResp, err := client.Init(context.Background(), &rpc.InitReq{
4449
Configuration: &rpc.Configuration{
4550
DataDir: datadir,
4651
},
@@ -49,10 +54,55 @@ func main() {
4954
fmt.Printf("Error creating server instance: %s\n", err)
5055
os.Exit(1)
5156
}
52-
instance := resp.GetInstance()
53-
fmt.Println("Created new server instance:", instance)
57+
instance := initResp.GetInstance()
58+
fmt.Printf("---> %+v\n", initResp)
59+
fmt.Println()
60+
61+
// UPDATE-INDEX
62+
fmt.Println("=== calling UpdateIndex")
63+
uiRespStream, err := client.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
64+
Instance: instance,
65+
})
66+
if err != nil {
67+
fmt.Printf("Error Upgrade platform: %s\n", err)
68+
os.Exit(1)
69+
}
70+
for {
71+
uiResp, err := uiRespStream.Recv()
72+
if err == io.EOF {
73+
fmt.Printf("---> %+v\n", uiResp)
74+
fmt.Println()
75+
break
76+
}
77+
if err != nil {
78+
fmt.Printf("Compile error: %s\n", err)
79+
os.Exit(1)
80+
}
81+
if uiResp.GetDownloadProgress() != nil {
82+
fmt.Printf(">> DOWNLOAD: %s\n", uiResp.GetDownloadProgress())
83+
}
84+
}
85+
86+
// PLATFORM SEARCH
87+
fmt.Println("=== calling PlatformSearch(uno)")
88+
searchResp, err := client.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
89+
Instance: instance,
90+
SearchArgs: "uno",
91+
})
92+
if err != nil {
93+
fmt.Printf("Search error: %s\n", err)
94+
os.Exit(1)
95+
}
96+
serchedOutput := searchResp.GetSearchOutput()
97+
for _, outsearch := range serchedOutput {
98+
fmt.Printf(">> SEARCH: %+v\n", outsearch)
99+
}
100+
fmt.Printf("---> %+v\n", searchResp)
101+
fmt.Println()
54102

103+
// PLATFORM INSTALL
55104
install := func() {
105+
fmt.Println("=== calling PlatformInstall(arduino:samd@1.6.19)")
56106
installRespStream, err := client.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
57107
Instance: instance,
58108
PlatformPackage: "arduino",
@@ -66,6 +116,8 @@ func main() {
66116
for {
67117
installResp, err := installRespStream.Recv()
68118
if err == io.EOF {
119+
fmt.Printf("---> %+v\n", installResp)
120+
fmt.Println()
69121
break
70122
}
71123
if err != nil {
@@ -79,12 +131,28 @@ func main() {
79131
fmt.Printf(">> TASK: %s\n", installResp.GetTaskProgress())
80132
}
81133
}
82-
fmt.Println("Installation completed!")
83134
}
84135

85136
install()
86137
install()
87138

139+
// PLATFORM LIST
140+
fmt.Println("=== calling PlatformList()")
141+
listResp, err := client.PlatformList(context.Background(), &rpc.PlatformListReq{
142+
Instance: instance,
143+
})
144+
if err != nil {
145+
fmt.Printf("List error: %s\n", err)
146+
os.Exit(1)
147+
}
148+
Installedplatforms := listResp.GetInstalledPlatform()
149+
for _, listpfm := range Installedplatforms {
150+
fmt.Printf("---> LIST: %+v\n", listpfm)
151+
}
152+
fmt.Println()
153+
154+
// PLATFORM UPGRADE
155+
fmt.Println("=== calling PlatformUpgrade(arduino:samd)")
88156
upgradeRespStream, err := client.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{
89157
Instance: instance,
90158
PlatformPackage: "arduino",
@@ -97,6 +165,8 @@ func main() {
97165
for {
98166
upgradeResp, err := upgradeRespStream.Recv()
99167
if err == io.EOF {
168+
fmt.Printf("---> %+v\n", upgradeResp)
169+
fmt.Println()
100170
break
101171
}
102172
if err != nil {
@@ -110,18 +180,21 @@ func main() {
110180
fmt.Printf(">> TASK: %s\n", upgradeResp.GetTaskProgress())
111181
}
112182
}
113-
fmt.Println("Upgrade completed!")
114183

184+
// BOARDS DETAILS
185+
fmt.Println("=== calling BoardDetails(arduino:samd:mkr1000)")
115186
details, err := client.BoardDetails(context.Background(), &rpc.BoardDetailsReq{
116187
Instance: instance,
117188
Fqbn: "arduino:samd:mkr1000",
118189
})
119190
if err != nil {
120191
fmt.Printf("Error getting board data: %s\n", err)
121-
} else {
122-
fmt.Printf("Board name: %s\n", details.GetName())
123192
}
193+
fmt.Printf("---> %+v\n", details)
194+
fmt.Println()
124195

196+
// COMPILE
197+
fmt.Println("=== calling Compile(arduino:samd:mkr1000, VERBOSE, " + os.Args[2] + ")")
125198
compRespStream, err := client.Compile(context.Background(), &rpc.CompileReq{
126199
Instance: instance,
127200
Fqbn: "arduino:samd:mkr1000",
@@ -135,6 +208,8 @@ func main() {
135208
for {
136209
compResp, err := compRespStream.Recv()
137210
if err == io.EOF {
211+
fmt.Printf("---> %+v\n", compResp)
212+
fmt.Println()
138213
break
139214
}
140215
if err != nil {
@@ -155,6 +230,8 @@ func main() {
155230
}
156231
}
157232

233+
// UPLOAD
234+
fmt.Println("=== calling Upload(arduino:samd:mkr1000, /dev/ttyACM0, VERBOSE, " + os.Args[2] + ")")
158235
uplRespStream, err := client.Upload(context.Background(), &rpc.UploadReq{
159236
Instance: instance,
160237
Fqbn: "arduino:samd:mkr1000",
@@ -169,6 +246,8 @@ func main() {
169246
for {
170247
uplResp, err := uplRespStream.Recv()
171248
if err == io.EOF {
249+
fmt.Printf("---> %+v\n", uplResp)
250+
fmt.Println()
172251
break
173252
}
174253
if err != nil {
@@ -183,52 +262,8 @@ func main() {
183262
}
184263
}
185264

186-
uiRespStream, err := client.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
187-
Instance: instance,
188-
})
189-
if err != nil {
190-
fmt.Printf("Error Upgrade platform: %s\n", err)
191-
os.Exit(1)
192-
}
193-
for {
194-
uiResp, err := uiRespStream.Recv()
195-
if err == io.EOF {
196-
break
197-
}
198-
if err != nil {
199-
fmt.Printf("Compile error: %s\n", err)
200-
os.Exit(1)
201-
}
202-
if uiResp.GetDownloadProgress() != nil {
203-
fmt.Printf(">> DOWNLOAD: %s\n", uiResp.GetDownloadProgress())
204-
}
205-
}
206-
207-
searchResp, err := client.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
208-
Instance: instance,
209-
SearchArgs: "uno",
210-
})
211-
if err != nil {
212-
fmt.Printf("Search error: %s\n", err)
213-
os.Exit(1)
214-
}
215-
serchedOutput := searchResp.GetSearchOutput()
216-
for _, outsearch := range serchedOutput {
217-
fmt.Printf(">> SEARCH: %+v\n", outsearch)
218-
}
219-
220-
listResp, err := client.PlatformList(context.Background(), &rpc.PlatformListReq{
221-
Instance: instance,
222-
})
223-
if err != nil {
224-
fmt.Printf("List error: %s\n", err)
225-
os.Exit(1)
226-
}
227-
Installedplatforms := listResp.GetInstalledPlatform()
228-
for _, listpfm := range Installedplatforms {
229-
fmt.Printf(">> LIST: %+v\n", listpfm)
230-
}
231-
265+
// PLATFORM UNINSTALL
266+
fmt.Println("=== calling PlatformUninstall(arduino:samd@1.6.21)")
232267
uninstallRespStream, err := client.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
233268
Instance: instance,
234269
PlatformPackage: "arduino",
@@ -242,6 +277,8 @@ func main() {
242277
for {
243278
uninstallResp, err := uninstallRespStream.Recv()
244279
if err == io.EOF {
280+
fmt.Printf("---> %+v\n", uninstallResp)
281+
fmt.Println()
245282
break
246283
}
247284
if err != nil {
@@ -253,6 +290,8 @@ func main() {
253290
}
254291
}
255292

293+
// DESTROY
294+
fmt.Println("=== calling Destroy()")
256295
_, err = client.Destroy(context.Background(), &rpc.DestroyReq{
257296
Instance: instance,
258297
})
@@ -261,5 +300,4 @@ func main() {
261300
} else {
262301
fmt.Println("Successfully closed server instance")
263302
}
264-
fmt.Println("Done")
265303
}

0 commit comments

Comments
 (0)