### OCPP 1.6 CSMS Remote Start Transaction in Kotlin Source: https://github.com/izivia/ocpp-toolkit/blob/dev/README.md This Kotlin example illustrates how a charge point handles a remote start transaction request from the CSMS. It defines a callback to capture the RemoteStartTransactionReq16, establishes a connection, waits for the remote request, and then proceeds to update the charge point status to Preparing and starts the transaction based on the received remote request. ```kotlin var remoteStartTransactionReq: RemoteStartTransactionReq16? = null //As previously said, the idTag variable is used to make unique transaction, so, we can use it to stare at who started a transaction for example val idTag = "Tag2" //define the callback for the remoteTransactionRequest //It returns a RemoteTransactionResponse val ocppCSCallbacks = object : OcppCSCallbacks16() { override fun remoteStartTransaction(req: RemoteStartTransactionReq16): RemoteStartTransactionResp16 { remoteStartTransactionReq = req return RemoteStartTransactionResp16(status = RemoteStartStopStatus16.Accepted) } } //establish a connection to the CSMS val connection = Ocpp16ConnectionToCSMS( chargePointId = chargPointId, csmsUrl = csmsUrl, transportType = transport, ocppCSCallbacks = ocppCSCallbacks ) connection.connect() //defining the timeout delay for receiving a remoteTransactionRequest val waitUntil = now() + 1.toDuration(DurationUnit.MINUTES) //We are waiting for the remote start request from the CSMS while (remoteStartTransactionReq == null && now() < waitUntil) { sleep(1000) } //We are checking if there was a remoteTransactionRequest sent if (remoteStartTransactionReq != null) { println("${remoteStartTransactionReq?.idTag}") //As the Authorization has been accepted, we can start a transaction, but before, //we need to change the Status to Preparing connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq16( connectorId = 1, errorCode = ChargePointErrorCode16.NoError, status = ChargePointStatus16.Preparing ) ) //We can now start a transaction by sending a StartTransaction request to the CSMS. //We can identify this transaction thanks to the idTag parameter //This function returns a response generated by the CSMS val response: StartTransactionResp16 = connection.startTransaction( meta = RequestMetadata(chargPointId), StartTransactionReq16( connectorId = 1, idTag = idTag, meterStart = 0, timestamp = now() ) ``` -------------------------------- ### Initiate Charge Process (OCPP 2.0) Source: https://github.com/izivia/ocpp-toolkit/blob/dev/README.md This example demonstrates how to connect to a CSMS using OCPP 2.0, authorize a user, send initial status notifications, and then initiate a transaction event with a 'Started' type and 'Authorized' trigger reason. It concludes with another status notification and connection closure. ```kotlin val connection = Ocpp20ConnectionToCSMS( chargePointId = chargPointId, csmsUrl = csmsUrl, transportType = transport, ocppCSCallbacks = OcppCSCallbacks() ) connection.connect() val response: AuthorizeResp = connection.authorize(RequestMetadata(chargPointId), AuthorizeReq(IdTokenType( idToken = "2233223", type = IdTokenEnumType.Central, ))).response if (response.idTokenInfo.status == AuthorizationStatusEnumType.Accepted) { connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq( connectorId = 1, connectorStatus = ConnectorStatusEnumType.Occupied, evseId = 1, timestamp = now() ) ) val response: TransactionEventResp = connection.transactionEvent( meta = RequestMetadata(chargPointId), request = TransactionEventReq( eventType = TransactionEventEnumType.Started, timestamp = now(), triggerReason = TriggerReasonEnumType.Authorized, seqNo = 1, transactionInfo = TransactionType( "1", ChargingStateEnumType.Charging ) ) ).response connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq( connectorId = 1, connectorStatus = ConnectorStatusEnumType.Occupied, evseId = 1, timestamp = now() ) ) } connection.close() ``` -------------------------------- ### Handle Remote Start Transaction (OCPP 2.0) Source: https://github.com/izivia/ocpp-toolkit/blob/dev/README.md This snippet shows how to implement a callback to handle a remote start transaction request from the CSMS in OCPP 2.0. It connects to the CSMS, waits for a 'RequestStartTransaction' message, and then proceeds with status notifications and a transaction event if a request is received, finally closing the connection. ```kotlin var remoteStartTransactionReq: RequestStartTransactionReq? = null val ocppCSCallbacks = object : OcppCSCallbacks() { override fun requestStartTransaction(req: RequestStartTransactionReq): RequestStartTransactionResp { remoteStartTransactionReq = req return RequestStartTransactionResp(RequestStartStopStatusEnumType.Accepted) } } val connection = Ocpp20ConnectionToCSMS( chargePointId = chargPointId, csmsUrl = csmsUrl, transportType = transport, ocppCSCallbacks = ocppCSCallbacks ) connection.connect() val waitUntil = now() + 1.toDuration(DurationUnit.MINUTES) while (remoteStartTransactionReq == null && now() < waitUntil) { sleep(100) } if (remoteStartTransactionReq != null) { println("${remoteStartTransactionReq?.idToken?.idToken}") connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq( connectorId = 1, connectorStatus = ConnectorStatusEnumType.Occupied, evseId = 1, timestamp = now() ) ) val response: TransactionEventResp = connection.transactionEvent( meta = RequestMetadata(chargPointId), request = TransactionEventReq( eventType = TransactionEventEnumType.Started, timestamp = now(), triggerReason = TriggerReasonEnumType.Authorized, seqNo = 1, transactionInfo = TransactionType( "1", ChargingStateEnumType.Charging ) ) ).response connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq( connectorId = 1, connectorStatus = ConnectorStatusEnumType.Occupied, evseId = 1, timestamp = now() ) ) } connection.close() ``` -------------------------------- ### OCPP 1.6 Charge Point Initiated Transaction in Kotlin Source: https://github.com/izivia/ocpp-toolkit/blob/dev/README.md This Kotlin snippet demonstrates a complete OCPP 1.6 charging transaction initiated by the charge point. It covers establishing a connection, authorizing an idTag, updating the charge point status (Preparing, Charging), and starting a transaction with the CSMS. The idTag is used for unique transaction identification. ```kotlin //establish a connection to the CSMS val connection = Ocpp16ConnectionToCSMS( chargePointId = chargPointId, csmsUrl = csmsUrl, transportType = transport, ocppCSCallbacks = OcppCSCallbacks16() ) connection.connect() //the idTag variable is used to make unique transaction, so, we can use it to stare at who started a transaction for example val idTag = "321" //send an authorize request to the CSMS. We are retreiving the response from the CSMS throught the response variable. val response: AuthorizeResp16 = connection.authorize(RequestMetadata(chargPointId),AuthorizeReq16(idTag = idTag )).response //We're checking if the Authorization request has been accepted by the CSMS. if (response.idTagInfo.status == AuthorizationStatus16.Accepted) { println("Authorization Accepted") //As the Authorization has been accepted, we can start a transaction, but before, //we need to change the Status to Preparing connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq16( connectorId = 1, errorCode = ChargePointErrorCode16.NoError, status = ChargePointStatus16.Preparing ) ) //We can now start a transaction by sending a StartTransaction request to the CSMS. //We can identify this transaction thanks to the idTag parameter //This function returns a response generated by the CSMS val response: StartTransactionResp16 = connection.startTransaction( meta = RequestMetadata(chargPointId), request = StartTransactionReq16( connectorId = 4, idTag = idTag, meterStart = 0, now() ) ).response //we are retrieving the transactionId from the reponse of our previous transaction request. val transactionId = response.transactionId println("TransactionId : $transactionId") // As the authorization has been accepted and that we have sent and receive a response for our startTransaction, // we can now set the charge status to Charging. // In order to do this, we send a statusNotification to the CSMS notifying that the status has changed to Charging connection.statusNotification( meta = RequestMetadata(chargPointId), request = StatusNotificationReq16( connectorId = 1, errorCode = ChargePointErrorCode16.NoError, status = ChargePointStatus16.Charging ) ) } connection.close() ``` -------------------------------- ### Update Charge Status to Charging (OCPP 1.6) Source: https://github.com/izivia/ocpp-toolkit/blob/dev/README.md This snippet demonstrates how to send a StatusNotification to the CSMS to update the charge point status to 'Charging' for OCPP 1.6, typically after a transaction has started. It also shows how to close the connection. ```kotlin //we are retrieving the transactionId from the reponse of our previous transaction request. val transactionId = response.transactionId // As the authorization has been accepted and that we have sent and receive a response for our startTransaction, // we can now set the charge status to Charging. // In order to do this, we send a statusNotification to the CSMS notifying that the status has changed to Charging connection.statusNotification( meta = RequestMetadata(chargPointId), StatusNotificationReq16( connectorId = 1, errorCode = ChargePointErrorCode16.NoError, status = ChargePointStatus16.Charging ) ) } connection.close() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.