|
| 1 | +## OCPP 2.1 Usage |
| 2 | + |
| 3 | +OCPP 2.1 is now supported (experimental) in this library. |
| 4 | + |
| 5 | +Requests and responses in OCPP 2.1 are handled the same way they were in v1.6 and 2.0.1. |
| 6 | +There are additional message types on the websocket layer, which are handled automatically by the library. |
| 7 | + |
| 8 | +The notable change is that there are now significantly more supported messages and profiles (feature sets), |
| 9 | +which also require their own handlers to be implemented. |
| 10 | + |
| 11 | +Below are very minimal setup code snippets, to get you started. |
| 12 | +CSMS is now the equivalent of the Central System, |
| 13 | +while the Charging Station is the new equivalent of a Charge Point. |
| 14 | + |
| 15 | +Refer to the [examples folder](../example/2.1) for a full working example. |
| 16 | +More in-depth documentation for v2.1 will follow. |
| 17 | + |
| 18 | +### CSMS |
| 19 | + |
| 20 | +To start a CSMS instance, run the following: |
| 21 | + |
| 22 | +```go |
| 23 | +import "github.com/lorenzodonini/ocpp-go/ocpp2.1" |
| 24 | + |
| 25 | +csms := ocpp2.NewCSMS(nil, nil) |
| 26 | + |
| 27 | +// Set callback handlers for connect/disconnect |
| 28 | +csms.SetNewChargingStationHandler(func (chargingStation ocpp2.ChargingStationConnection) { |
| 29 | +log.Printf("new charging station %v connected", chargingStation.ID()) |
| 30 | +}) |
| 31 | +csms.SetChargingStationDisconnectedHandler(func (chargingStation ocpp2.ChargingStationConnection) { |
| 32 | +log.Printf("charging station %v disconnected", chargingStation.ID()) |
| 33 | +}) |
| 34 | + |
| 35 | +// Set handler for profile callbacks |
| 36 | +handler := &CSMSHandler{} |
| 37 | +csms.SetAuthorizationHandler(handler) |
| 38 | +csms.SetAvailabilityHandler(handler) |
| 39 | +csms.SetDiagnosticsHandler(handler) |
| 40 | +csms.SetFirmwareHandler(handler) |
| 41 | +csms.SetLocalAuthListHandler(handler) |
| 42 | +csms.SetMeterHandler(handler) |
| 43 | +csms.SetProvisioningHandler(handler) |
| 44 | +csms.SetRemoteControlHandler(handler) |
| 45 | +csms.SetReservationHandler(handler) |
| 46 | +csms.SetTariffCostHandler(handler) |
| 47 | +csms.SetTransactionsHandler(handler) |
| 48 | + |
| 49 | + |
| 50 | +// Start central system |
| 51 | +listenPort := 8887 |
| 52 | +log.Printf("starting CSMS") |
| 53 | +csms.Start(listenPort, "/{ws}") // This call starts server in daemon mode and is blocking |
| 54 | +log.Println("stopped CSMS") |
| 55 | +``` |
| 56 | + |
| 57 | +#### Sending requests |
| 58 | + |
| 59 | +Similarly to v2.0.1, you may send requests using the simplified API, e.g. |
| 60 | + |
| 61 | +```go |
| 62 | +err := csms.GetLocalListVersion(chargingStationID, myCallback) |
| 63 | +if err != nil { |
| 64 | +log.Printf("error sending message: %v", err) |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Or you may build requests manually and send them using the asynchronous API. |
| 69 | + |
| 70 | +#### Docker image |
| 71 | + |
| 72 | +There is a Dockerfile and a docker image available upstream. |
| 73 | + |
| 74 | +### Charging Station |
| 75 | + |
| 76 | +To start a charging station instance, simply run the following: |
| 77 | + |
| 78 | +```go |
| 79 | +chargingStationID := "cs0001" |
| 80 | +csmsUrl = "ws://localhost:8887" |
| 81 | +chargingStation := ocpp2.NewChargingStation(chargingStationID, nil, nil) |
| 82 | + |
| 83 | +// Set a handler for all callback functions |
| 84 | +handler := &ChargingStationHandler{} |
| 85 | +chargingStation.SetAvailabilityHandler(handler) |
| 86 | +chargingStation.SetAuthorizationHandler(handler) |
| 87 | +chargingStation.SetDataHandler(handler) |
| 88 | +chargingStation.SetDiagnosticsHandler(handler) |
| 89 | +chargingStation.SetDisplayHandler(handler) |
| 90 | +chargingStation.SetFirmwareHandler(handler) |
| 91 | +chargingStation.SetISO15118Handler(handler) |
| 92 | +chargingStation.SetLocalAuthListHandler(handler) |
| 93 | +chargingStation.SetProvisioningHandler(handler) |
| 94 | +chargingStation.SetRemoteControlHandler(handler) |
| 95 | +chargingStation.SetReservationHandler(handler) |
| 96 | +chargingStation.SetSmartChargingHandler(handler) |
| 97 | +chargingStation.SetTariffCostHandler(handler) |
| 98 | +chargingStation.SetTransactionsHandler(handler) |
| 99 | + |
| 100 | +// Connects to CSMS |
| 101 | +err := chargingStation.Start(csmsUrl) |
| 102 | +if err != nil { |
| 103 | +log.Println(err) |
| 104 | +} else { |
| 105 | +log.Printf("connected to CSMS at %v", csmsUrl) |
| 106 | +mainRoutine() // ... your program logic goes here |
| 107 | +} |
| 108 | + |
| 109 | +// Disconnect |
| 110 | +chargingStation.Stop() |
| 111 | +log.Println("disconnected from CSMS") |
| 112 | +``` |
| 113 | + |
| 114 | +#### Sending requests |
| 115 | + |
| 116 | +Similarly to v2.0.1 you may send requests using the simplified API (recommended), e.g. |
| 117 | + |
| 118 | +```go |
| 119 | +bootResp, err := chargingStation.BootNotification(provisioning.BootReasonPowerUp, "model1", "vendor1") |
| 120 | +if err != nil { |
| 121 | +log.Printf("error sending message: %v", err) |
| 122 | +} else { |
| 123 | +log.Printf("status: %v, interval: %v, current time: %v", bootResp.Status, bootResp.Interval, bootResp.CurrentTime.String()) |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Or you may build requests manually and send them using either the synchronous or asynchronous API. |
0 commit comments