Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON RPC2 implementation #129

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The supported functionality contains:
## Packages

- `api`: global API interface definitions and eebus service configuration
- `com/jsonrpc2`: provides an JSON RPC2 implementation, to communicate with systems not written in Golang
- `features/client`: provides feature helpers with the local SPINE feature having the client role and the remote SPINE feature being the server for easy access to commonly used functions
- `features/server`: provides feature helpers with the local SPINE feature having the server role for easy access to commonly used functions
- `service`: central package which provides access to SHIP and SPINE. Use this to create the EEBUS service, its configuration and connect to remote EEBUS services
Expand All @@ -42,7 +43,7 @@ Services with implemented use cases will be implemented in different repositorie
#### First Run

```sh
go run cmd/hems/main.go 4715
go run examples/hems/main.go 4715
```

`4715` is the example server port that this process should use
Expand All @@ -52,7 +53,7 @@ The certificate and key and the local SKI will be generated and printed. You sho
#### General Usage

```sh
Usage: go run cmd/hems/main.go <serverport> <remoteski> <certfile> <keyfile>
Usage: go run examples/hems/main.go <serverport> <remoteski> <certfile> <keyfile>
```

- `remoteski` is the SKI of the remote device or service you want to connect to
Expand All @@ -64,7 +65,7 @@ Usage: go run cmd/hems/main.go <serverport> <remoteski> <certfile> <keyfile>
#### First Run

```sh
go run cmd/hems/main.go 4715
go run examples/hems/main.go 4715
```

`4715` is the example server port that this process should use
Expand All @@ -74,7 +75,7 @@ The certificate and key and the local SKI will be generated and printed. You sho
#### General Usage

```sh
Usage: go run cmd/evse/main.go <serverport> <remoteski> <certfile> <keyfile>
Usage: go run examples/evse/main.go <serverport> <remoteski> <certfile> <keyfile>
```

- `remoteski` is the SKI of the remote device or service you want to connect to
Expand Down
3 changes: 3 additions & 0 deletions com/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Overview

This folder contains multiple communication APIs for services that are not written in Golang and need to communicate with a Go service running this stack and use the public API for interaction.
1 change: 1 addition & 0 deletions com/jsonrpc2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is currently a placeholder file as this folder will host the jsonrpc2 API interface and implementation
22 changes: 20 additions & 2 deletions examples/remote/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ func isExportedOrBuiltinType(t reflect.Type) bool {
return token.IsExported(t.Name()) || t.PkgPath() == ""
}

// json.Marshall won't marshall error types, marshall as string
func errorAsJson(v reflect.Value) interface{} {
if v.IsNil() {
// passthrough nil as nil, otherwise nil.(error) will panic
return nil
} else {
return v.Interface().(error).Error()
}
}

func transformReturnValues(values []reflect.Value) []interface{} {
result := make([]interface{}, len(values))

for i, e := range values {
switch e.Type() {
valueType := e.Type()

switch valueType {
case reflect.TypeFor[spineapi.DeviceRemoteInterface]():
result[i] = e.Interface().(spineapi.DeviceRemoteInterface).Address()
case reflect.TypeFor[[]spineapi.DeviceRemoteInterface]():
Expand All @@ -51,12 +63,18 @@ func transformReturnValues(values []reflect.Value) []interface{} {
}
result[i] = transformedValues
default:
result[i] = e.Interface()
switch {
case valueType.Implements(reflect.TypeFor[error]()):
result[i] = errorAsJson(e)
default:
result[i] = e.Interface()
}
}
}

return result
}

func WriteKey(cert tls.Certificate, path string) error {
file, err := os.Create(path)
if err != nil {
Expand Down
Loading