-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved wrapper messages. Added more examples.
- Loading branch information
1 parent
9767775
commit 8b1166b
Showing
9 changed files
with
255 additions
and
72 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
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) | ||
|
||
} |
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,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() | ||
} |
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,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() | ||
} |
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,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() | ||
} |
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
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= |
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
Oops, something went wrong.