Properly handle charging profiles set a remote start transaction.
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 28 Jun 2021 13:56:59 +0000 (15:56 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 28 Jun 2021 13:56:59 +0000 (15:56 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 14a8b97d928df3e29f98ea884a1909517f85347d..e806051a449da65b5a4b989492d0050f50d760aa 100644 (file)
@@ -261,9 +261,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.PREPARING);
       this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING;
       if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorId)) {
+        // Check if authorized
         if (this.chargingStation.getAuthorizeRemoteTxRequests()) {
           let authorized = false;
-          // Check if authorized
           if (this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags()
               && this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)) {
             authorized = true;
@@ -278,20 +278,26 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           }
           if (authorized) {
             // Authorization successful, start transaction
-            await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag);
-            logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag);
-            return await this.setRemoteStartChargingProfile(transactionConnectorId, commandPayload.chargingProfile)
-              ? Constants.OCPP_RESPONSE_ACCEPTED
-              : await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
+            if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) {
+              if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) {
+                logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag);
+                return Constants.OCPP_RESPONSE_ACCEPTED;
+              }
+              return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
+            }
+            return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
           }
           return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
         }
         // No authorization check required, start transaction
-        await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag);
-        logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag);
-        return await this.setRemoteStartChargingProfile(transactionConnectorId, commandPayload.chargingProfile)
-          ? Constants.OCPP_RESPONSE_ACCEPTED
-          : await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
+        if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) {
+          if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) {
+            logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag);
+            return Constants.OCPP_RESPONSE_ACCEPTED;
+          }
+          return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
+        }
+        return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
       }
       return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag);
     }
@@ -299,22 +305,24 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
   }
 
   private async notifyRemoteStartTransactionRejected(connectorId: number, idTag: string): Promise<DefaultResponse> {
-    await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
-    this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
-    logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', idTag ' + idTag);
+    if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) {
+      await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
+      this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
+    }
+    logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', status ' + this.chargingStation.getConnector(connectorId).status);
     return Constants.OCPP_RESPONSE_REJECTED;
   }
 
-  private async setRemoteStartChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): Promise<boolean> {
+  private setRemoteStartTransactionChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): boolean {
     if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) {
       this.chargingStation.setChargingProfile(connectorId, cp);
       logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnector(connectorId).chargingProfiles);
       return true;
     } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) {
-      await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
-      this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
       logger.warn(`${this.chargingStation.logPrefix()} Not allowed to set ${cp.chargingProfilePurpose} charging profile(s) at remote start transaction`);
       return false;
+    } else if (!cp) {
+      return true;
     }
   }
 
index edfb99035211b157a5603c6952a3282cdb7665a2..0bfef77e4a09b616efede9fd1054ef404cf00a87 100644 (file)
@@ -75,8 +75,10 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     } else {
       logger.warn(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag);
       this.chargingStation.resetTransactionOnConnector(connectorId);
-      await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
-      this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
+      if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) {
+        await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
+        this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
+      }
     }
   }
 
index b50ea9f7303adba864042f060038212fd5fb72d4..85c3f831d4a7eeb67fedf976c0fa3cd01f3e2024 100644 (file)
@@ -23,7 +23,7 @@ export class OCPP16ServiceUtils {
     const sampledValueContext = context ?? (sampledValueTemplate.context ?? null);
     const sampledValueLocation = sampledValueTemplate.location
       ? sampledValueTemplate.location
-      : (OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate.measurand ?? null));
+      : OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate.measurand ?? null);
     const sampledValuePhase = phase ?? (sampledValueTemplate.phase ?? null);
     return {
       ...!Utils.isNullOrUndefined(sampledValueTemplate.unit) && { unit: sampledValueTemplate.unit },