lorenzodonini / ocpp-go

Open Charge Point Protocol implementation in Go
MIT License
262 stars 125 forks source link

How to send optional ...props for BootNotification? #139

Closed bhatanku1 closed 2 years ago

bhatanku1 commented 2 years ago

Hi,

Please pardon it might be a little basic Go doubt. I am trying to send a bootNotification to a backendProvider. The chargingStation.BootNotification method expects chargePointModel, chargePointVendor. But how can I set the other optional props like Iccid,Imsi, etc while calling this API.

utsavanand2 commented 2 years ago

Hi @bhatanku1 ! You can send such optional values using this syntax:

bootConf, err := chargePoint.BootNotification("<model>", "<vendor>", func(request *core.BootNotificationRequest) {
        request.Iccid = "<value>"
})

Please refer to a similar issue at #137

bhatanku1 commented 2 years ago

Thank you so much for a quick reply @utsavanand2 . It solved my problem. :)

lorenzodonini commented 2 years ago

What @utsavanand2 said. It's the same syntax for every request/response.

You can also do something like:

request := core.NewBootNotificationRequest("model", "vendor") // constructor only includes required values
request.Iccid = "value"
// set other optional values
confirmation, err := chargePoint.SendRequest(request)

or even literally:

request := core.BootNotificationRequest{
    // set all props
}

I discourage the latter approach btw, since you'll have less information about which fields are required, while the regular constructor API forces you to set at least the required values.

Also note that if you use these approaches, you will have to perform type assertion at the end:

bootConf := confirmation.(*core.BootNotificationConfirmation)

which works nicely but is more verbose.

The approach mentioned in the comment above is the recommended one though 😉