Convert sendStatusNotification to OCPP message sending handler
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 7 Mar 2022 21:58:20 +0000 (22:58 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 7 Mar 2022 21:58:20 +0000 (22:58 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 9297b3e838ada19afbe86760aca65e050a3942e0..539ab309e62276e5db999fcdd19988f505afded9 100644 (file)
@@ -28,6 +28,7 @@ import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
 import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws';
 
 import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
+import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode';
 import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
 import { ChargingProfile } from '../types/ocpp/ChargingProfile';
 import ChargingStationInfo from '../types/ChargingStationInfo';
@@ -513,10 +514,11 @@ export default class ChargingStation {
     await this.stopMessageSequence(reason);
     for (const connectorId of this.connectors.keys()) {
       if (connectorId > 0) {
-        await this.ocppRequestService.sendStatusNotification(
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
-          ChargePointStatus.UNAVAILABLE
-        );
+          status: ChargePointStatus.UNAVAILABLE,
+          errorCode: ChargePointErrorCode.NO_ERROR,
+        });
         this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE;
       }
     }
@@ -1266,10 +1268,11 @@ export default class ChargingStation {
         this.getConnectorStatus(connectorId)?.bootStatus
       ) {
         // Send status in template at startup
-        await this.ocppRequestService.sendStatusNotification(
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
-          this.getConnectorStatus(connectorId).bootStatus
-        );
+          status: this.getConnectorStatus(connectorId).bootStatus,
+          errorCode: ChargePointErrorCode.NO_ERROR,
+        });
         this.getConnectorStatus(connectorId).status =
           this.getConnectorStatus(connectorId).bootStatus;
       } else if (
@@ -1278,24 +1281,27 @@ export default class ChargingStation {
         this.getConnectorStatus(connectorId)?.bootStatus
       ) {
         // Send status in template after reset
-        await this.ocppRequestService.sendStatusNotification(
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
-          this.getConnectorStatus(connectorId).bootStatus
-        );
+          status: this.getConnectorStatus(connectorId).bootStatus,
+          errorCode: ChargePointErrorCode.NO_ERROR,
+        });
         this.getConnectorStatus(connectorId).status =
           this.getConnectorStatus(connectorId).bootStatus;
       } else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) {
         // Send previous status at template reload
-        await this.ocppRequestService.sendStatusNotification(
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
-          this.getConnectorStatus(connectorId).status
-        );
+          status: this.getConnectorStatus(connectorId).status,
+          errorCode: ChargePointErrorCode.NO_ERROR,
+        });
       } else {
         // Send default status
-        await this.ocppRequestService.sendStatusNotification(
+        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
-          ChargePointStatus.AVAILABLE
-        );
+          status: ChargePointStatus.AVAILABLE,
+          errorCode: ChargePointErrorCode.NO_ERROR,
+        });
         this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE;
       }
     }
index a7dae0c5b95eeed06843dad5f71afcdda0721f6b..ed91c87ad9eab6f6db14baf135b13f75fcc13b85 100644 (file)
@@ -43,6 +43,7 @@ import { DefaultResponse } from '../../../types/ocpp/Responses';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { IncomingRequestHandler } from '../../../types/ocpp/Requests';
 import { JsonType } from '../../../types/JsonType';
+import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
 import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
 import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
@@ -212,9 +213,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       }
       return Constants.OCPP_RESPONSE_UNLOCK_FAILED;
     }
-    await this.chargingStation.ocppRequestService.sendStatusNotification(
-      connectorId,
-      OCPP16ChargePointStatus.AVAILABLE
+    await this.chargingStation.ocppRequestService.sendMessageHandler(
+      OCPP16RequestCommand.STATUS_NOTIFICATION,
+      {
+        connectorId,
+        status: OCPP16ChargePointStatus.AVAILABLE,
+        errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+      }
     );
     this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
     return Constants.OCPP_RESPONSE_UNLOCKED;
@@ -462,9 +467,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         }
         this.chargingStation.getConnectorStatus(id).availability = commandPayload.type;
         if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
-          await this.chargingStation.ocppRequestService.sendStatusNotification(
-            id,
-            chargePointStatus
+          await this.chargingStation.ocppRequestService.sendMessageHandler(
+            OCPP16RequestCommand.STATUS_NOTIFICATION,
+            {
+              connectorId: id,
+              status: chargePointStatus,
+              errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+            }
           );
           this.chargingStation.getConnectorStatus(id).status = chargePointStatus;
         }
@@ -483,9 +492,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
       }
       this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
-      await this.chargingStation.ocppRequestService.sendStatusNotification(
-        connectorId,
-        chargePointStatus
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STATUS_NOTIFICATION,
+        { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR }
       );
       this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus;
       return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
@@ -498,9 +507,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
   ): Promise<DefaultResponse> {
     const transactionConnectorId: number = commandPayload.connectorId;
     if (transactionConnectorId) {
-      await this.chargingStation.ocppRequestService.sendStatusNotification(
-        transactionConnectorId,
-        OCPP16ChargePointStatus.PREPARING
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STATUS_NOTIFICATION,
+        {
+          connectorId: transactionConnectorId,
+          status: OCPP16ChargePointStatus.PREPARING,
+          errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+        }
       );
       this.chargingStation.getConnectorStatus(transactionConnectorId).status =
         OCPP16ChargePointStatus.PREPARING;
@@ -633,9 +646,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       this.chargingStation.getConnectorStatus(connectorId).status !==
       OCPP16ChargePointStatus.AVAILABLE
     ) {
-      await this.chargingStation.ocppRequestService.sendStatusNotification(
-        connectorId,
-        OCPP16ChargePointStatus.AVAILABLE
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STATUS_NOTIFICATION,
+        {
+          connectorId,
+          status: OCPP16ChargePointStatus.AVAILABLE,
+          errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+        }
       );
       this.chargingStation.getConnectorStatus(connectorId).status =
         OCPP16ChargePointStatus.AVAILABLE;
@@ -686,9 +703,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         connectorId > 0 &&
         this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
       ) {
-        await this.chargingStation.ocppRequestService.sendStatusNotification(
-          connectorId,
-          OCPP16ChargePointStatus.FINISHING
+        await this.chargingStation.ocppRequestService.sendMessageHandler(
+          OCPP16RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            status: OCPP16ChargePointStatus.FINISHING,
+            errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+          }
         );
         this.chargingStation.getConnectorStatus(connectorId).status =
           OCPP16ChargePointStatus.FINISHING;
index e7fa7751eb88f39e2e0533c5a6b5a8427932ca3d..295447b03fbb68039e8a1ba2399aa22ac1ccb81e 100644 (file)
@@ -63,19 +63,6 @@ export default class OCPP16RequestService extends OCPPRequestService {
     );
   }
 
-  public async sendStatusNotification(
-    connectorId: number,
-    status: OCPP16ChargePointStatus,
-    errorCode: OCPP16ChargePointErrorCode = OCPP16ChargePointErrorCode.NO_ERROR
-  ): Promise<void> {
-    const payload: StatusNotificationRequest = {
-      connectorId,
-      errorCode,
-      status,
-    };
-    await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.STATUS_NOTIFICATION);
-  }
-
   public async sendAuthorize(
     connectorId: number,
     idTag?: string
@@ -263,8 +250,8 @@ export default class OCPP16RequestService extends OCPPRequestService {
       case OCPP16RequestCommand.STATUS_NOTIFICATION:
         return {
           connectorId: commandParams?.connectorId,
-          errorCode: commandParams?.errorCode,
           status: commandParams?.status,
+          errorCode: commandParams?.errorCode,
         } as StatusNotificationRequest;
       case OCPP16RequestCommand.START_TRANSACTION:
         return {
index 79064f1c48d9deec97e583da6679fd4b2a600d86..759f3705700d0765af848e7596f71ed05251f255 100644 (file)
@@ -25,6 +25,7 @@ import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6
 import type ChargingStation from '../../ChargingStation';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { JsonType } from '../../../types/JsonType';
+import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
 import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
@@ -314,9 +315,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
           payload.transactionId,
           this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue
         ));
-      await this.chargingStation.ocppRequestService.sendStatusNotification(
-        connectorId,
-        OCPP16ChargePointStatus.CHARGING
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STATUS_NOTIFICATION,
+        {
+          connectorId,
+          status: OCPP16ChargePointStatus.CHARGING,
+          errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+        }
       );
       this.chargingStation.getConnectorStatus(connectorId).status =
         OCPP16ChargePointStatus.CHARGING;
@@ -363,9 +368,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       this.chargingStation.getConnectorStatus(connectorId).status !==
       OCPP16ChargePointStatus.AVAILABLE
     ) {
-      await this.chargingStation.ocppRequestService.sendStatusNotification(
-        connectorId,
-        OCPP16ChargePointStatus.AVAILABLE
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STATUS_NOTIFICATION,
+        {
+          connectorId,
+          status: OCPP16ChargePointStatus.AVAILABLE,
+          errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+        }
       );
       this.chargingStation.getConnectorStatus(connectorId).status =
         OCPP16ChargePointStatus.AVAILABLE;
@@ -412,16 +421,24 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         !this.chargingStation.isChargingStationAvailable() ||
         !this.chargingStation.isConnectorAvailable(transactionConnectorId)
       ) {
-        await this.chargingStation.ocppRequestService.sendStatusNotification(
-          transactionConnectorId,
-          OCPP16ChargePointStatus.UNAVAILABLE
+        await this.chargingStation.ocppRequestService.sendMessageHandler(
+          OCPP16RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId: transactionConnectorId,
+            status: OCPP16ChargePointStatus.UNAVAILABLE,
+            errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+          }
         );
         this.chargingStation.getConnectorStatus(transactionConnectorId).status =
           OCPP16ChargePointStatus.UNAVAILABLE;
       } else {
-        await this.chargingStation.ocppRequestService.sendStatusNotification(
-          transactionConnectorId,
-          OCPP16ChargePointStatus.AVAILABLE
+        await this.chargingStation.ocppRequestService.sendMessageHandler(
+          OCPP16RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId: transactionConnectorId,
+            status: OCPP16ChargePointStatus.AVAILABLE,
+            errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+          }
         );
         this.chargingStation.getConnectorStatus(transactionConnectorId).status =
           OCPP16ChargePointStatus.AVAILABLE;
index 73fc4bf15a6de2935974f15bc3994a64752bcaa7..41e7a6eaa5c6f9d4581c6b2f915ab207ccb86570 100644 (file)
@@ -333,12 +333,6 @@ export default abstract class OCPPRequestService {
     params?: SendParams
   ): Promise<ResponseType>;
 
-  public abstract sendStatusNotification(
-    connectorId: number,
-    status: ChargePointStatus,
-    errorCode?: ChargePointErrorCode
-  ): Promise<void>;
-
   public abstract sendAuthorize(connectorId: number, idTag?: string): Promise<AuthorizeResponse>;
   public abstract sendStartTransaction(
     connectorId: number,