This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from w3c/rl-integration
Rl integration
- Loading branch information
Showing
24 changed files
with
685 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
client/client-1.0/grpc_client/grpc_map_client/grpc_client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* (C) 2023 Ford Motor Company | ||
* (C) 2023 Volvo Cars | ||
* | ||
* All files and artifacts in the repository at https://github.com/w3c/automotive-viss2 | ||
* are licensed under the provisions of the license provided by the LICENSE file in this repository. | ||
* | ||
**/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"github.com/akamensky/argparse" | ||
"github.com/gorilla/mux" | ||
"github.com/w3c/automotive-viss2/client/client-1.0/grpc_client/grpc_map_client/mapserver" | ||
pb "github.com/w3c/automotive-viss2/grpc_pb" | ||
utils "github.com/w3c/automotive-viss2/utils" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
var clientCert tls.Certificate | ||
var caCertPool x509.CertPool | ||
|
||
const ( | ||
address = "127.0.0.1" | ||
name = "VISSv2-gRPC-client" | ||
) | ||
|
||
var grpcCompression utils.Compression | ||
|
||
var commandList []string | ||
|
||
func initCommandList() { | ||
commandList = make([]string, 4) | ||
// commandList[0] = `{"action":"get","path":"Vehicle/Cabin/Door/Row1/Right/IsOpen","requestId":"232"}` | ||
//commandList[0] = `{"action":"get","path":"Vehicle/Cabin/Door","filter":{"type":"paths","parameter":"*.*.IsOpen"},"requestId":"235"}` | ||
// commandList[1] = `{"action":"subscribe","path":"Vehicle/Cabin/Door/Row1/Right/IsOpen","filter":{"type":"timebased","parameter":{"period":"3000"}},"requestId":"246"}` | ||
commandList[1] = `{"action":"subscribe","path":"Vehicle","filter":[{"type":"paths","parameter":["Speed","CurrentLocation.Latitude", "CurrentLocation.Longitude"]}, {"type":"timebased","parameter":{"period":"100"}}],"requestId":"285"}` | ||
commandList[0] = `{"action":"get","path":"Vehicle/Speed","requestId":"232"}` | ||
//commandList[1] = `{"action":"subscribe","path":"Vehicle/Speed","filter":{"type":"timebased","parameter":{"period":"100"}},"requestId":"246"}` | ||
//commandList[1] = `{"action":"subscribe","path":"Vehicle","filter":[{"type":"paths","parameter":["Speed", "Chassis/Accelerator/PedalPosition"]},{"type":"timebased","parameter":{"period":"100"}}],"requestId":"246"}` | ||
//commandList[1] = `{"action":"subscribe","path":"Vehicle","filter":{"type":"paths","parameter":["Speed", "Chassis/Accelerator/PedalPosition"]},"requestId":"246"}` | ||
//commandList[1] = `{"action":"subscribe","path":"Vehicle/Speed","requestId":"258"}` | ||
commandList[2] = `{"action":"unsubscribe","subscriptionId":"1","requestId":"240"}` | ||
commandList[3] = `{"action":"set", "path":"Vehicle/Body/Lights/IsLeftIndicatorOn", "value":"999", "requestId":"245"}` | ||
} | ||
|
||
// {"action":"subscribe","path":"Vehicle","filter":{"type":"paths","parameter":["Speed", "Chassis.Accelerator.PedalPosition"]},"requestId":"246"}` | ||
|
||
func noStreamCall(commandIndex int) { | ||
var conn *grpc.ClientConn | ||
var err error | ||
if secConfig.TransportSec == "yes" { | ||
config := &tls.Config{ | ||
Certificates: []tls.Certificate{clientCert}, | ||
RootCAs: &caCertPool, | ||
} | ||
tlsCredentials := credentials.NewTLS(config) | ||
portNo := secConfig.GrpcSecPort | ||
conn, err = grpc.Dial(address+portNo, grpc.WithTransportCredentials(tlsCredentials), grpc.WithBlock()) | ||
} else { | ||
// grpc.Dial | ||
|
||
utils.Info.Printf("connecting to port = 8887") | ||
conn, err = grpc.Dial(address+":8887", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) | ||
} | ||
if err != nil { | ||
fmt.Printf("did not connect: %v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
client := pb.NewVISSv2Client(conn) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
vssRequest := commandList[commandIndex] | ||
var vssResponse string | ||
if commandIndex == 0 { | ||
pbRequest := utils.GetRequestJsonToPb(vssRequest, grpcCompression) | ||
pbResponse, err := client.GetRequest(ctx, pbRequest) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
vssResponse = utils.GetResponsePbToJson(pbResponse, grpcCompression) | ||
} else if commandIndex == 2 { | ||
pbRequest := utils.UnsubscribeRequestJsonToPb(vssRequest, grpcCompression) | ||
pbResponse, _ := client.UnsubscribeRequest(ctx, pbRequest) | ||
vssResponse = utils.UnsubscribeResponsePbToJson(pbResponse, grpcCompression) | ||
} else { | ||
pbRequest := utils.SetRequestJsonToPb(vssRequest, grpcCompression) | ||
pbResponse, _ := client.SetRequest(ctx, pbRequest) | ||
vssResponse = utils.SetResponsePbToJson(pbResponse, grpcCompression) | ||
} | ||
if err != nil { | ||
fmt.Printf("Error when issuing request=:%s", vssRequest) | ||
} else { | ||
fmt.Printf("Received response:%s", vssResponse) | ||
} | ||
} | ||
|
||
func streamCall(commandIndex int) { | ||
var conn *grpc.ClientConn | ||
var err error | ||
if secConfig.TransportSec == "yes" { | ||
config := &tls.Config{ | ||
Certificates: []tls.Certificate{clientCert}, | ||
RootCAs: &caCertPool, | ||
} | ||
tlsCredentials := credentials.NewTLS(config) | ||
portNo := secConfig.GrpcSecPort | ||
conn, err = grpc.Dial(address+portNo, grpc.WithTransportCredentials(tlsCredentials), grpc.WithBlock()) | ||
} else { | ||
conn, err = grpc.Dial(address+":8887", grpc.WithInsecure(), grpc.WithBlock()) | ||
} | ||
if err != nil { | ||
fmt.Printf("did not connect: %v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
client := pb.NewVISSv2Client(conn) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
vssRequest := commandList[commandIndex] | ||
pbRequest := utils.SubscribeRequestJsonToPb(vssRequest, grpcCompression) | ||
stream, err := client.SubscribeRequest(ctx, pbRequest) | ||
for { | ||
pbResponse, err := stream.Recv() | ||
vssResponse := utils.SubscribeStreamPbToJson(pbResponse, grpcCompression) | ||
if err != nil { | ||
fmt.Printf("Error=%v when issuing request=:%s", err, vssRequest) | ||
} else { | ||
fmt.Printf("Received response:%s\n", vssResponse) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
// Create new parser object | ||
go mapserver.ServeWebViewSite(mux.NewRouter()) | ||
|
||
parser := argparse.NewParser("print", "gRPC client") | ||
// Create string flag | ||
logFile := parser.Flag("", "logfile", &argparse.Options{Required: false, Help: "outputs to logfile in ./logs folder"}) | ||
logLevel := parser.Selector("", "loglevel", []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, &argparse.Options{ | ||
Required: false, | ||
Help: "changes log output level", | ||
Default: "info"}) | ||
|
||
// Parse input | ||
err := parser.Parse(os.Args) | ||
if err != nil { | ||
fmt.Print(parser.Usage(err)) | ||
} | ||
|
||
utils.InitLog("grpc_client-log.txt", "./logs", *logFile, *logLevel) | ||
grpcCompression = utils.PB_LEVEL1 | ||
readTransportSecConfig() | ||
utils.Info.Printf("secConfig.TransportSec=%s", secConfig.TransportSec) | ||
if secConfig.TransportSec == "yes" { | ||
caCertPool = *prepareTransportSecConfig() | ||
} | ||
initCommandList() | ||
|
||
fmt.Printf("Command indicies: 0=GET, 1=SUBSCRIBE, 2=UNSUBSCRIBE, 3=SET, any other value terminates.\n") | ||
var commandIndex int | ||
for { | ||
fmt.Printf("\nCommand index [0-3]:") | ||
fmt.Scanf("%d", &commandIndex) | ||
if commandIndex < 0 || commandIndex > 3 { | ||
break | ||
} | ||
fmt.Printf("Command:%s", commandList[commandIndex]) | ||
if commandIndex == 1 { // subscribe | ||
go streamCall(commandIndex) | ||
} else { | ||
go noStreamCall(commandIndex) | ||
} | ||
time.Sleep(1 * time.Second) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
client/client-1.0/grpc_client/grpc_map_client/mapserver/mapserver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/******** Peter Winzell (c), 10/30/23 *********************************************/ | ||
|
||
package mapserver | ||
|
||
import ( | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
ReleaseTag = "server 0.0.1" | ||
) | ||
|
||
var pingHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// log.Println("pingHandler called ...") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(" HTTP status(200) OK code returned running: " + ReleaseTag)) | ||
}) | ||
|
||
func ServeWebViewSite(r *mux.Router) error { | ||
|
||
r.Handle("/version", pingHandler).Methods("GET") | ||
r.Handle("/", http.FileServer(http.Dir("./static/"))) | ||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) | ||
|
||
return http.ListenAndServe(":8085", r) | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
client/client-1.0/grpc_client/grpc_map_client/static/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Volvo Cars(c) 2023 VISSv2 remotive labs example </title> | ||
<title>Accessible Map</title> | ||
<link rel="stylesheet" href="node_modules/ol/ol.css"> | ||
<style> | ||
.map { | ||
width: 100%; | ||
height: 400px; | ||
} | ||
a.skiplink { | ||
position: absolute; | ||
clip: rect(1px, 1px, 1px, 1px); | ||
padding: 0; | ||
border: 0; | ||
height: 1px; | ||
width: 1px; | ||
overflow: hidden; | ||
} | ||
a.skiplink:focus { | ||
clip: auto; | ||
height: auto; | ||
width: auto; | ||
background-color: #fff; | ||
padding: 0.3em; | ||
} | ||
#map:focus { | ||
outline: #4A74A8 solid 0.15em; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="map"></div> | ||
<script type="module" src="mappen.js"></script> | ||
</body> |
19 changes: 19 additions & 0 deletions
19
client/client-1.0/grpc_client/grpc_map_client/static/mappen.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Map from 'ol/Map'; | ||
import View from 'ol/View'; | ||
import TileLayer from 'ol/layer/Tile'; | ||
import XYZ from 'ol/source/XYZ'; | ||
|
||
new Map({ | ||
target: 'map', | ||
layers: [ | ||
new TileLayer({ | ||
source: new XYZ({ | ||
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' | ||
}) | ||
}) | ||
], | ||
view: new View({ | ||
center: [0, 0], | ||
zoom: 10 | ||
}) | ||
}); |
Oops, something went wrong.