Skip to content

Commit

Permalink
Improved wrapper messages. Added more examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregszalay committed Sep 23, 2022
1 parent 9767775 commit 8b1166b
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 72 deletions.
72 changes: 11 additions & 61 deletions example.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,23 @@
package main

import (
"encoding/json"
"fmt"

"github.com/google/uuid"
"github.com/gregszalay/ocpp-messages-go/types/BootNotificationRequest"
"github.com/gregszalay/ocpp-messages-go/wrappers"
"github.com/gregszalay/ocpp-messages-go/examples"
"github.com/sanity-io/litter"
)

func createExampleJSONString() []byte {
// Create example OCPP message object (e.g. BootNotificationRequest)
boot_req := BootNotificationRequest.BootNotificationRequestJson{
Reason: "PowerUp",
ChargingStation: BootNotificationRequest.ChargingStationType{
Model: "super-charger-6000",
VendorName: "WattsUp",
},
}

// All OCPP messages need to be wrapped into a CALL, CALLRESULT or CALLERROR type
call_wrapper := wrappers.CALL{
MessageTypeId: wrappers.CALL_TYPE,
MessageId: uuid.NewString(),
Action: "BootNotification",
Payload: boot_req,
}

return call_wrapper.Marshal()
}

func main() {
litter.Config.StripPackageNames = true

example_message := createExampleJSONString()
fmt.Println("*******************************")
fmt.Println("Example OCPP message json:")
fmt.Println("*******************************")
fmt.Printf("%s\n", example_message)
fmt.Println("*******************************")

// Unmarshal CALL message
var call wrappers.CALL
call_unmarshal_err := call.UnmarshalJSON(example_message)
if call_unmarshal_err != nil {
fmt.Printf("Failed OCPP message json unmarshal. Error: %s", call_unmarshal_err)
} else {
fmt.Println("Example CALL message unmarshalled using the types:")
fmt.Println("*******************************")
litter.Dump(call)
}
fmt.Println("*******************************")
// CALL example
call_json_string := examples.Create_call_json_string()
examples.Run_call_unmarshal_example(call_json_string)

// Re-marshal payload only
re_marshalled_payload, re_marshall_err := json.MarshalIndent(call.Payload, "", " ")
if re_marshall_err != nil {
fmt.Println(re_marshall_err)
}
// CALLRESULT example
callresult_json_string := examples.Create_callresult_json_string()
examples.Run_callresult_unmarshal_example(callresult_json_string)

// Unmarshal payload
var req BootNotificationRequest.BootNotificationRequestJson
payload_unmarshal_err := req.UnmarshalJSON(re_marshalled_payload)
if payload_unmarshal_err != nil {
fmt.Printf("Failed OCPP message json unmarshal. Error: %s", payload_unmarshal_err)
} else {
fmt.Println("Example payload unmarshalled using the types:")
fmt.Println("*******************************")
litter.Dump(req)
}
fmt.Println("*******************************")
// CALLERROR
callerror_json_string := examples.Create_callerror_json_string()
examples.Run_callerror_unmarshal_example(callerror_json_string)

}
44 changes: 44 additions & 0 deletions examples/callerrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package examples

import (
"fmt"

"github.com/google/uuid"
"github.com/gregszalay/ocpp-messages-go/wrappers"
"github.com/sanity-io/litter"
)

func Run_callerror_unmarshal_example(json_string []byte) {
// Create CALL JSON string
fmt.Println("\n*******************************")
fmt.Println("OCPP CALLERROR message as JSON:")
fmt.Println("*******************************")
fmt.Printf("%s\n", json_string)

// Unmarshal CALLERROR JSON string
fmt.Println("\n*******************************")
var callerror wrappers.CALLERROR
callerror_result_unmarshal_err := callerror.UnmarshalJSON(json_string)
if callerror_result_unmarshal_err != nil {
fmt.Printf("Failed to unmarshal OCPP CALLERROR message. Error: %s", callerror_result_unmarshal_err)
} else {
fmt.Println("OCPP CALLERROR message as an OBJECT:")
fmt.Println("*******************************")
litter.Dump(callerror)
}

}

func Create_callerror_json_string() []byte {

// All OCPP messages need to be wrapped into a CALL, callerror or CALLERROR type
call_result_wrapper := wrappers.CALLERROR{
MessageTypeId: wrappers.CALLERROR_TYPE,
MessageId: uuid.NewString(),
ErrorCode: string(wrappers.FormatViolation),
ErrorDescription: "Some really descriptive words about this error",
ErrorDetails: "Don't even bother debugging, just go on stackoverflow :D",
}

return call_result_wrapper.MarshalPretty()
}
62 changes: 62 additions & 0 deletions examples/callresults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package examples

import (
"fmt"

"github.com/google/uuid"
"github.com/gregszalay/ocpp-messages-go/types/AuthorizeResponse"
"github.com/gregszalay/ocpp-messages-go/wrappers"
"github.com/sanity-io/litter"
)

func Run_callresult_unmarshal_example(json_string []byte) {
// Create CALL JSON string
fmt.Println("\n*******************************")
fmt.Println("OCPP CALLRESULT message as JSON:")
fmt.Println("*******************************")
fmt.Printf("%s\n", json_string)

// Unmarshal CALLRESULT JSON string
fmt.Println("\n*******************************")
var callresult wrappers.CALLRESULT
call_result_unmarshal_err := callresult.UnmarshalJSON(json_string)
if call_result_unmarshal_err != nil {
fmt.Printf("Failed to unmarshal OCPP CALLRESULT message. Error: %s", call_result_unmarshal_err)
} else {
fmt.Println("OCPP CALLRESULT message as an OBJECT:")
fmt.Println("*******************************")
litter.Dump(callresult)
}

// Unmarshal payload
fmt.Println("\n*******************************")
var req AuthorizeResponse.AuthorizeResponseJson
payload_unmarshal_err := req.UnmarshalJSON(callresult.GetPayloadAsJSON())
if payload_unmarshal_err != nil {
fmt.Printf("Failed to unmarshal CALLRESULT message payload. Error: %s", payload_unmarshal_err)
} else {
fmt.Println("Payload as an OBJECT:")
fmt.Println("*******************************")
litter.Dump(req)
}
fmt.Println("*******************************")
}

func Create_callresult_json_string() []byte {
// Create example OCPP message object (e.g. BootNotificationRequest)
auth_resp := AuthorizeResponse.AuthorizeResponseJson{
IdTokenInfo: AuthorizeResponse.IdTokenInfoType{
Status: AuthorizeResponse.AuthorizationStatusEnumType_1_Accepted,
EvseId: []int{1},
},
}

// All OCPP messages need to be wrapped into a CALL, CALLRESULT or CALLERROR type
call_result_wrapper := wrappers.CALLRESULT{
MessageTypeId: wrappers.CALLRESULT_TYPE,
MessageId: uuid.NewString(),
Payload: auth_resp,
}

return call_result_wrapper.MarshalPretty()
}
64 changes: 64 additions & 0 deletions examples/calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package examples

import (
"fmt"

"github.com/google/uuid"
"github.com/gregszalay/ocpp-messages-go/types/BootNotificationRequest"
"github.com/gregszalay/ocpp-messages-go/wrappers"
"github.com/sanity-io/litter"
)

func Run_call_unmarshal_example(json_string []byte) {
// Create CALL JSON string
fmt.Println("\n*******************************")
fmt.Println("OCPP CALL message as JSON:")
fmt.Println("*******************************")
fmt.Printf("%s\n", json_string)

// Unmarshal CALL JSON string
fmt.Println("\n*******************************")
var call wrappers.CALL
call_unmarshal_err := call.UnmarshalJSON(json_string)
if call_unmarshal_err != nil {
fmt.Printf("Failed to unmarshal OCPP CALL message. Error: %s", call_unmarshal_err)
} else {
fmt.Println("OCPP CALL message as an OBJECT:")
fmt.Println("*******************************")
litter.Dump(call)
}

// Unmarshal payload
fmt.Println("\n*******************************")
var req BootNotificationRequest.BootNotificationRequestJson
payload_unmarshal_err := req.UnmarshalJSON(call.GetPayloadAsJSON())
if payload_unmarshal_err != nil {
fmt.Printf("Failed to unmarshal CALL message payload. Error: %s", payload_unmarshal_err)
} else {
fmt.Println("Payload as an OBJECT:")
fmt.Println("*******************************")
litter.Dump(req)
}
fmt.Println("*******************************")
}

func Create_call_json_string() []byte {
// Create example OCPP message object (e.g. BootNotificationRequest)
boot_req := BootNotificationRequest.BootNotificationRequestJson{
Reason: "PowerUp",
ChargingStation: BootNotificationRequest.ChargingStationType{
Model: "super-charger-6000",
VendorName: "WattsUp",
},
}

// All OCPP messages need to be wrapped into a CALL, CALLRESULT or CALLERROR type
call_wrapper := wrappers.CALL{
MessageTypeId: wrappers.CALL_TYPE,
MessageId: uuid.NewString(),
Action: "BootNotification",
Payload: boot_req,
}

return call_wrapper.MarshalPretty()
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/gregszalay/ocpp-messages-go
go 1.18

require (
github.com/google/uuid v1.3.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/google/uuid v1.3.0
github.com/sanity-io/litter v1.5.5
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b h1:XxMZvQZtTXpWMNWK82vdjCLCe7uGMFXdTsJH0v3Hkvw=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0BqZYGxvYaXvFGUbCmPGy8DM7qWJJiIQ=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
29 changes: 27 additions & 2 deletions wrappers/CALL.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wrappers

import (
"bytes"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -60,10 +61,34 @@ func (j *CALL) UnmarshalJSON(b []byte) error {

func (c *CALL) Marshal() []byte {
message_array := [...]interface{}{CALL_TYPE, c.MessageId, c.Action, c.Payload}
message_array_json, err := json.Marshal(message_array)
result, err := json.Marshal(message_array)
if err != nil {
fmt.Printf("Could not marshal CALL message: %s\n", err)
return []byte("")
}
return message_array_json
return result
}

func (c *CALL) MarshalPretty() []byte {
uglyJSON := c.Marshal()
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(uglyJSON), "", " "); err != nil {
return []byte("")
}
return prettyJSON.Bytes()
}

func (c *CALL) GetPayloadAsJSON() []byte {
if c == nil {
fmt.Println("CALL object is empty")
return []byte("")
}
// Re-marshal payload only
re_marshalled_payload, re_marshall_err := json.MarshalIndent(c.Payload, "", " ")
if re_marshall_err != nil {
fmt.Println("Failed to remarshall CALL pazload to json")
return []byte("")
}

return re_marshalled_payload
}
14 changes: 12 additions & 2 deletions wrappers/CALLERROR.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package wrappers

import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

type CALLERROR struct {
// This is a Message Type Number which is used to identify the type of the message.
MessageTypeId float64
MessageTypeId uint32
// This must be the exact same id that is in the call request so that the recipient can match request and result.
MessageId string
// This field must contain a string from the RPC Framework Error Codes table.
Expand Down Expand Up @@ -56,7 +57,7 @@ func (j *CALLERROR) UnmarshalJSON(b []byte) error {
return fmt.Errorf("CALLERROR data[4] is not a string")
}
*j = CALLERROR{
MessageTypeId: message_type_id,
MessageTypeId: uint32(message_type_id),
MessageId: message_id,
ErrorCode: error_code,
ErrorDescription: error_description,
Expand All @@ -75,6 +76,15 @@ func (c *CALLERROR) Marshal() []byte {
return message_array_json
}

func (c *CALLERROR) MarshalPretty() []byte {
uglyJSON := c.Marshal()
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(uglyJSON), "", " "); err != nil {
return []byte("")
}
return prettyJSON.Bytes()
}

type rpc_framework_error_code string

// Payload for Action is syntactically incorrect
Expand Down
Loading

0 comments on commit 8b1166b

Please sign in to comment.