Add DataTransfer support for incoming request
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 16 Nov 2022 20:52:43 +0000 (21:52 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 16 Nov 2022 20:52:43 +0000 (21:52 +0100)
Ref #36

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/types/ocpp/1.6/Requests.ts
src/utils/Constants.ts

index dd1018a07d0bd49b3bae29547af8fc7aae12f0b6..ebec27d23544cd40f741b7429042464534fdaf9f 100644 (file)
@@ -31,6 +31,8 @@ import {
   OCPP16AvailabilityType,
   OCPP16BootNotificationRequest,
   OCPP16ClearCacheRequest,
+  OCPP16DataTransferRequest,
+  OCPP16DataTransferVendorId,
   OCPP16HeartbeatRequest,
   OCPP16IncomingRequestCommand,
   OCPP16MessageTrigger,
@@ -43,7 +45,7 @@ import {
   SetChargingProfileRequest,
   UnlockConnectorRequest,
 } from '../../../types/ocpp/1.6/Requests';
-import type {
+import {
   ChangeAvailabilityResponse,
   ChangeConfigurationResponse,
   ClearChargingProfileResponse,
@@ -51,6 +53,8 @@ import type {
   GetConfigurationResponse,
   GetDiagnosticsResponse,
   OCPP16BootNotificationResponse,
+  OCPP16DataTransferResponse,
+  OCPP16DataTransferStatus,
   OCPP16HeartbeatResponse,
   OCPP16StatusNotificationResponse,
   OCPP16TriggerMessageResponse,
@@ -123,6 +127,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       ],
       [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)],
       [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)],
+      [OCPP16IncomingRequestCommand.DATA_TRANSFER, this.handleRequestDataTransfer.bind(this)],
     ]);
     this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>([
       [
@@ -269,6 +274,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           )
         ) as JSONSchemaType<OCPP16TriggerMessageRequest>,
       ],
+      [
+        OCPP16IncomingRequestCommand.DATA_TRANSFER,
+        JSON.parse(
+          fs.readFileSync(
+            path.resolve(
+              path.dirname(fileURLToPath(import.meta.url)),
+              '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
+            ),
+            'utf8'
+          )
+        ) as JSONSchemaType<OCPP16DataTransferRequest>,
+      ],
     ]);
     this.validatePayload.bind(this);
   }
@@ -1226,4 +1243,27 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       );
     }
   }
+
+  private handleRequestDataTransfer(
+    chargingStation: ChargingStation,
+    commandPayload: OCPP16DataTransferRequest
+  ): OCPP16DataTransferResponse {
+    try {
+      if (Object.values(OCPP16DataTransferVendorId).includes(commandPayload.vendorId)) {
+        return {
+          status: OCPP16DataTransferStatus.ACCEPTED,
+        };
+      }
+      return {
+        status: OCPP16DataTransferStatus.UNKNOWN_VENDOR_ID,
+      };
+    } catch (error) {
+      return this.handleIncomingRequestError(
+        chargingStation,
+        OCPP16IncomingRequestCommand.DATA_TRANSFER,
+        error as Error,
+        { errorResponse: Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED }
+      );
+    }
+  }
 }
index cf810704edcc0436c90610b66b38316668d19793..4a90569d5e9a18fee51b7a46bc000dac260f4b95 100644 (file)
@@ -55,6 +55,7 @@ export enum OCPP16IncomingRequestCommand {
   REMOTE_STOP_TRANSACTION = 'RemoteStopTransaction',
   GET_DIAGNOSTICS = 'GetDiagnostics',
   TRIGGER_MESSAGE = 'TriggerMessage',
+  DATA_TRANSFER = 'DataTransfer',
 }
 
 export type OCPP16ClearCacheRequest = EmptyObject;
@@ -139,6 +140,8 @@ export interface OCPP16TriggerMessageRequest extends JsonObject {
   connectorId?: number;
 }
 
+export enum OCPP16DataTransferVendorId {}
+
 export interface OCPP16DataTransferRequest extends JsonObject {
   vendorId: string;
   messageId?: string;
index bfb4fbf38aaf34341dcbb03f6549126ae4e284a7..74b88834e1b943742320f8956d4fabf1c4dbbd97 100644 (file)
@@ -4,6 +4,7 @@ import {
   ChargingProfileStatus,
   ClearChargingProfileStatus,
   ConfigurationStatus,
+  DataTransferStatus,
   DefaultStatus,
   TriggerMessageStatus,
   UnlockStatus,
@@ -82,6 +83,10 @@ export default class Constants {
     status: TriggerMessageStatus.NOT_IMPLEMENTED,
   });
 
+  static readonly OCPP_DATA_TRANSFER_RESPONSE_REJECTED = Object.freeze({
+    status: DataTransferStatus.REJECTED,
+  });
+
   static readonly OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL = 60000; // Ms
   static readonly OCPP_WEBSOCKET_TIMEOUT = 60000; // Ms
   static readonly OCPP_TRIGGER_MESSAGE_DELAY = 500; // Ms