-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
226 lines (200 loc) · 8.62 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2023 Robert Bosch GmbH
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License 2.0 which is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
//go:generate protoc --go_out=proto --go-grpc_out=proto -I proto kuksa/val/v1/types.proto
//go:generate protoc --go_out=proto --go-grpc_out=proto -I proto kuksa/val/v1/val.proto
package main
import (
"flag"
"log"
"github.com/eclipse-kuksa/kuksa-incubation/kuksa_go_client/kuksa_client"
v1 "github.com/eclipse-kuksa/kuksa-incubation/kuksa_go_client/proto/kuksa/val/v1"
)
func main() {
log.Println("Starting Kuksa Client!!")
// Load the Kuksa Configuration
var configKuksaClient kuksa_client.KuksaClientConfig
kuksa_client.ReadConfig(&configKuksaClient)
protocol := flag.String("protocol", "undefined", "Could be grpc or ws. Per default the protocol variable is undefined. Then the value from the config file kuksa_client.json will be used.")
flag.Parse()
if *protocol == "undefined" {
protocol = &configKuksaClient.TransportProtocol
}
var backend kuksa_client.KuksaBackend
var token string
if *protocol == "ws" {
// example token from kuksa.val/kuksa_certificates/jwt/all-read-write.json.token
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJrdWtzYS52YWwiLCJpc3MiOiJFY2xpcHNlIEtVS1NBIERldiIsImFkbWluIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTc2NzIyNTU5OSwia3Vrc2EtdnNzIjp7IioiOiJydyJ9fQ.QQcVR0RuRJIoasPXYsMGZhdvhLjUalk4GcRaxhh3-0_j3CtVSZ0lTbv_Z3As5BfIYzaMlwUzFGvCVOq2MXVjRK81XOAZ6wIsyKOxva16zjbZryr2V_m3yZ4twI3CPEzJch11_qnhInirHltej-tGg6ySfLaTYeAkw4xYGwENMBBhN5t9odANpScZP_xx5bNfwdW1so6FkV1WhpKlCywoxk_vYZxo187d89bbiu-xOZUa5D-ycFkd1-1rjPXLGE_g5bc4jcQBvNBc-5FDbvt4aJlTQqjpdeppxhxn_gjkPGIAacYDI7szOLC-WYajTStbksUju1iQCyli11kPx0E66me_ZVwOX07f1lRF6D2brWm1LcMAHM3bQUK0LuyVwWPxld64uSAEsvSKsRyJERc7nZUgLf7COnUrrkxgIUNjukbdT2JVN_I-3l3b4YXg6JVD7Y5g0QYBKgXEFpZrDbBVhzo7PXPAhJD6-c3DcUQyRZExbrnFV56RwWuExphw8lYnbMvxPWImiVmB9nRVgFKD0TYaw1sidPSSlZt8Uw34VZzHWIZQAQY0BMjR33fefg42XQ1YzIwPmDx4GYXLl7HNIIVbsRsibKaJnf49mz2qnLC1K272zXSPljO11Ke1MNnsnKyUH7mcwEs9nhTsnMgEOx_TyMLRYo-VEHBDLuEOiBo"
backend = &kuksa_client.KuksaClientCommWs{Config: &configKuksaClient}
} else if *protocol == "grpc" {
// example token from kuksa.val/jwt/provide-all.token
// and https://github.com/eclipse-kuksa/kuksa-common/blob/main/jwt/provide-all.token
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJsb2NhbCBkZXYiLCJpc3MiOiJjcmVhdGVUb2tlbi5weSIsImF1ZCI6WyJrdWtzYS52YWwiXSwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3NjcyMjU1OTksInNjb3BlIjoicHJvdmlkZSJ9.OJWzTvDjcmeWyg3vmBR5TEtqYaHq8HrpFLlTKZAfDBAQBUHpyUEboJ97jfWuWgBnTpnfboyfAbwvLqo6bEVZ6tXzF8n9LtW6HmPbIWoDqXuobM2grUCVaGKuOcnCpMCQYChziqHbYwRJYP9nkYgbQU1kE4dN7880Io4xzq0GEbWksB2CVpOoExQUmCZpCohPs-XEkdmXhcUKnWnOeiSsRGKusx987vpY_WOXh6WE7DfJgzAgpPDo33qI7zQuTzUILORQsiHmsrQO0-zcvokNjaQUzlt5ETZ7MQLCtiUQaN0NMbDMCWkmSfNvZ5hKCNbfr2FaiMzrGBOQdvQiFo-DqZKGNweaGpufYXuaKfn3SXKoDr8u1xDE5oKgWMjxDR9pQYGzIF5bDXITSywCm4kN5DIn7e2_Ga28h3rBl0t0ZT0cwlszftQRueDTFcMns1u9PEDOqf7fRrhjq3zqpxuMAoRANVd2z237eBsS0AvdSIxL52N4xO8P_h93NN8Vaum28fTPxzm8p9WlQh4mgUelggtT415hLcxizx15ARIRG0RiW91Pglzt4WRtXHnsg93Ixd3yXXzZ2i4Y0hqhj_L12SsXunK2VxKup2sFCQz6wM-t_7ADmNYcs80idzsadY8rYKDV8N1WqOOd4ANG_nzWa86Tyu6wAwhDVag5nbFmLZQ"
backend = &kuksa_client.KuksaClientCommGrpc{Config: &configKuksaClient}
} else {
log.Println("Specify -protocol=ws or -protocol=grpc")
}
err := backend.ConnectToKuksaVal()
if err != nil {
log.Fatalf("Connection Error: %v", err)
}
defer backend.Close()
// Authorize the connection
err = backend.AuthorizeKuksaValConn(token)
if err != nil {
log.Fatalf("Authorization Error: %v", err)
}
err = backend.SetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "true", "value")
if err != nil {
log.Printf("Set Value Error: %v", err)
} else {
log.Printf("Vehicle.ADAS.ABS.IsEnabled Set: true")
}
values, err := backend.GetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "value")
if err != nil {
log.Printf("Get Value Error: %v", err)
} else {
for _, value := range values {
if *protocol == "grpc" {
log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(*v1.DataEntry).String())
} else {
log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(string))
}
}
}
// Go client does not support setting of array values for Websocket
// Reason is SetValueFromKuksaVal where we set the JSON array we get as onput as string,
// so it gets quoted and considered as a string on server side and cause error
if *protocol == "grpc" {
err = backend.SetValueFromKuksaVal("Vehicle.OBD.DTCList", "[dtc1, dtc2, dtc3]", "value")
if err != nil {
log.Printf("Set Value Error: %v", err)
} else {
log.Printf("Vehicle.OBD.DTCList Set: [dtc1, dtc2, dtc3]")
}
values, err = backend.GetValueFromKuksaVal("Vehicle.OBD.DTCList", "value")
if err != nil {
log.Printf("Get Value Error: %v", err)
} else {
for _, value := range values {
if *protocol == "grpc" {
log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String())
} else {
log.Println("Vehicle.OBD.DTCList: " + value.(string))
}
}
}
// set string with "" and \"
// Expected result is 4 items in the list
// dtc1, dtc2
// dtc2
// dtc3, dtc3
// dtc4
var valstr = "['dtc1, dtc2', dtc2, \"dtc3, dtc3\", dtc4]"
err = backend.SetValueFromKuksaVal("Vehicle.OBD.DTCList", valstr, "value")
if err != nil {
log.Printf("Set Value Error: %v", err)
} else {
log.Printf("Vehicle.OBD.DTCList Set: %s", valstr)
}
values, err = backend.GetValueFromKuksaVal("Vehicle.OBD.DTCList", "value")
if err != nil {
log.Printf("Get Value Error: %v", err)
} else {
for _, value := range values {
if *protocol == "grpc" {
log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String())
} else {
log.Println("Vehicle.OBD.DTCList: " + value.(string))
}
}
}
}
err = backend.SetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "true", "targetValue")
if err != nil {
log.Printf("Set Value Error: %v", err)
} else {
log.Printf("Vehicle.ADAS.ABS.IsEnabled Set: true")
}
tValues, err := backend.GetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "targetValue")
if err != nil {
log.Printf("Get Value Error: %v", err)
} else {
for _, value := range tValues {
if *protocol == "grpc" {
log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(*v1.DataEntry).String())
} else {
log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(string))
}
}
}
// Get MetaData of Vehicle.Speed
value, err := backend.GetMetadataFromKuksaVal("Vehicle.Speed")
if err == nil {
for _, val := range value {
if *protocol == "grpc" {
log.Println("Vehicle.Speed Metadata: " + val.(*v1.DataEntry).String())
} else {
log.Println("Vehicle.Speed Metadata: " + val.(string))
}
}
} else {
log.Printf("Error while getting metadata: %s", err)
}
//Subscribe to Vehicle.Speed
id, err := backend.SubscribeFromKuksaVal("Vehicle.Speed", "value")
if err == nil {
log.Printf("Vehicle.Speed Subscription Id: %s", id)
} else {
log.Printf("Subscription Error %s", err)
}
err = backend.PrintSubscriptionMessages(id)
if err != nil {
log.Printf("Printing the subscription messages failed with: %s", err)
}
}
// More subscribing examples
// idT, err := backend.SubscribeFromKuksaVal("Vehicle.Speed", "targetValue")
// if err == nil {
// log.Printf("Vehicle.Speed Subscription Id: %s", idT)
// } else {
// log.Printf("Subscription Error %s", err)
// }
// err = backend.PrintSubscriptionMessages(idT)
// if err != nil {
// log.Printf("Printing the subscription messages failed with: %s", err)
// }
// err = backend.UnsubscribeFromKuksaVal(idT)
// if err != nil {
// log.Printf("Unsubscribing failed with: %s", err)
// }
// err = backend.UnsubscribeFromKuksaVal(id)
// if err != nil {
// log.Printf("Unsubscribing failed with: %s", err)
// }
// idM, err := backend.SubscribeFromKuksaVal("Vehicle.Speed", "metadata")
// if err == nil {
// log.Printf("Vehicle.Speed Subscription Id: %s", idM)
// } else {
// log.Printf("Subscription Error %s", err)
// }
// err = backend.PrintSubscriptionMessages(idM)
// if err != nil {
// log.Printf("Printing the subscription messages failed with: %s", err)
// }
// err = backend.UnsubscribeFromKuksaVal(idM)
// if err != nil {
// log.Printf("Unsubscribing failed with: %s", err)
// }
// }
// }