Strong type sendMessageHandler response with generics
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Mar 2022 13:29:29 +0000 (14:29 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Mar 2022 13:29:29 +0000 (14:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
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
src/types/ocpp/1.6/MeterValues.ts
src/types/ocpp/1.6/Responses.ts
src/types/ocpp/Responses.ts

index 985e8e4779ff2633e9c089b6f569c3d00fe89b42..32d9897380b261ce1338bfe84c1e82e96cd156e7 100644 (file)
@@ -10,6 +10,7 @@ import {
 
 import type ChargingStation from './ChargingStation';
 import Constants from '../utils/Constants';
+import { MeterValuesResponse } from '../types/ocpp/Responses';
 import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import { RequestCommand } from '../types/ocpp/Requests';
@@ -274,24 +275,25 @@ export default class AutomaticTransactionGenerator {
         this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
         // Authorize idTag
         const authorizeResponse: AuthorizeResponse =
-          (await this.chargingStation.ocppRequestService.sendMessageHandler(
+          await this.chargingStation.ocppRequestService.sendMessageHandler<AuthorizeResponse>(
             RequestCommand.AUTHORIZE,
             {
               idTag,
             }
-          )) as AuthorizeResponse;
+          );
         this.connectorsStatus.get(connectorId).authorizeRequests++;
         if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
           this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++;
           logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
           // Start transaction
-          startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
-            RequestCommand.START_TRANSACTION,
-            {
-              connectorId,
-              idTag,
-            }
-          )) as StartTransactionResponse;
+          startResponse =
+            await this.chargingStation.ocppRequestService.sendMessageHandler<StartTransactionResponse>(
+              RequestCommand.START_TRANSACTION,
+              {
+                connectorId,
+                idTag,
+              }
+            );
           PerformanceStatistics.endMeasure(measureId, beginId);
           return startResponse;
         }
@@ -301,21 +303,23 @@ export default class AutomaticTransactionGenerator {
       }
       logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
       // Start transaction
-      startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
-        RequestCommand.START_TRANSACTION,
-        {
-          connectorId,
-          idTag,
-        }
-      )) as StartTransactionResponse;
+      startResponse =
+        await this.chargingStation.ocppRequestService.sendMessageHandler<StartTransactionResponse>(
+          RequestCommand.START_TRANSACTION,
+          {
+            connectorId,
+            idTag,
+          }
+        );
       PerformanceStatistics.endMeasure(measureId, beginId);
       return startResponse;
     }
     logger.info(this.logPrefix(connectorId) + ' start transaction without an idTag');
-    startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
-      RequestCommand.START_TRANSACTION,
-      { connectorId }
-    )) as StartTransactionResponse;
+    startResponse =
+      await this.chargingStation.ocppRequestService.sendMessageHandler<StartTransactionResponse>(
+        RequestCommand.START_TRANSACTION,
+        { connectorId }
+      );
     PerformanceStatistics.endMeasure(measureId, beginId);
     return startResponse;
   }
@@ -341,7 +345,7 @@ export default class AutomaticTransactionGenerator {
           connectorId,
           this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
         );
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<MeterValuesResponse>(
           RequestCommand.METER_VALUES,
           {
             connectorId,
@@ -350,16 +354,17 @@ export default class AutomaticTransactionGenerator {
           }
         );
       }
-      stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
-        RequestCommand.STOP_TRANSACTION,
-        {
-          transactionId,
-          meterStop:
-            this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
-          idTag: this.chargingStation.getTransactionIdTag(transactionId),
-          reason,
-        }
-      )) as StopTransactionResponse;
+      stopResponse =
+        await this.chargingStation.ocppRequestService.sendMessageHandler<StopTransactionResponse>(
+          RequestCommand.STOP_TRANSACTION,
+          {
+            transactionId,
+            meterStop:
+              this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+            idTag: this.chargingStation.getTransactionIdTag(transactionId),
+            reason,
+          }
+        );
       this.connectorsStatus.get(connectorId).stopTransactionRequests++;
     } else {
       logger.warn(
index f917580339feae3e6a6dd3222698d8eaad65f77c..bd3557717e273e7fa75acd65a0c562452cb8ec53 100644 (file)
@@ -8,7 +8,13 @@ import {
   IncomingRequestCommand,
   RequestCommand,
 } from '../types/ocpp/Requests';
-import { BootNotificationResponse, RegistrationStatus } from '../types/ocpp/Responses';
+import {
+  BootNotificationResponse,
+  HeartbeatResponse,
+  MeterValuesResponse,
+  RegistrationStatus,
+  StatusNotificationResponse,
+} from '../types/ocpp/Responses';
 import ChargingStationConfiguration, {
   ConfigurationKey,
 } from '../types/ChargingStationConfiguration';
@@ -24,6 +30,7 @@ import {
   VendorDefaultParametersKey,
 } from '../types/ocpp/Configuration';
 import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues';
+import { StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction';
 import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
 import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws';
 
@@ -52,7 +59,6 @@ import OCPPRequestService from './ocpp/OCPPRequestService';
 import { OCPPVersion } from '../types/ocpp/OCPPVersion';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemplates';
-import { StopTransactionReason } from '../types/ocpp/Transaction';
 import { SupervisionUrlDistribution } from '../types/ConfigurationData';
 import { URL } from 'url';
 import Utils from '../utils/Utils';
@@ -395,7 +401,9 @@ export default class ChargingStation {
     ) {
       // eslint-disable-next-line @typescript-eslint/no-misused-promises
       this.heartbeatSetInterval = setInterval(async (): Promise<void> => {
-        await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT);
+        await this.ocppRequestService.sendMessageHandler<HeartbeatResponse>(
+          RequestCommand.HEARTBEAT
+        );
       }, this.getHeartbeatInterval());
       logger.info(
         this.logPrefix() +
@@ -465,11 +473,14 @@ export default class ChargingStation {
             this.getConnectorStatus(connectorId).transactionId,
             interval
           );
-          await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, {
-            connectorId,
-            transactionId: this.getConnectorStatus(connectorId).transactionId,
-            meterValue: [meterValue],
-          });
+          await this.ocppRequestService.sendMessageHandler<MeterValuesResponse>(
+            RequestCommand.METER_VALUES,
+            {
+              connectorId,
+              transactionId: this.getConnectorStatus(connectorId).transactionId,
+              meterValue: [meterValue],
+            }
+          );
         },
         interval
       );
@@ -567,11 +578,14 @@ export default class ChargingStation {
     await this.stopMessageSequence(reason);
     for (const connectorId of this.connectors.keys()) {
       if (connectorId > 0) {
-        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
-          connectorId,
-          status: ChargePointStatus.UNAVAILABLE,
-          errorCode: ChargePointErrorCode.NO_ERROR,
-        });
+        await this.ocppRequestService.sendMessageHandler<StatusNotificationResponse>(
+          RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            status: ChargePointStatus.UNAVAILABLE,
+            errorCode: ChargePointErrorCode.NO_ERROR,
+          }
+        );
         this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE;
       }
     }
@@ -1116,21 +1130,22 @@ export default class ChargingStation {
       // Send BootNotification
       let registrationRetryCount = 0;
       do {
-        this.bootNotificationResponse = (await this.ocppRequestService.sendMessageHandler(
-          RequestCommand.BOOT_NOTIFICATION,
-          {
-            chargePointModel: this.bootNotificationRequest.chargePointModel,
-            chargePointVendor: this.bootNotificationRequest.chargePointVendor,
-            chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber,
-            firmwareVersion: this.bootNotificationRequest.firmwareVersion,
-            chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber,
-            iccid: this.bootNotificationRequest.iccid,
-            imsi: this.bootNotificationRequest.imsi,
-            meterSerialNumber: this.bootNotificationRequest.meterSerialNumber,
-            meterType: this.bootNotificationRequest.meterType,
-          },
-          { skipBufferingOnError: true }
-        )) as BootNotificationResponse;
+        this.bootNotificationResponse =
+          await this.ocppRequestService.sendMessageHandler<BootNotificationResponse>(
+            RequestCommand.BOOT_NOTIFICATION,
+            {
+              chargePointModel: this.bootNotificationRequest.chargePointModel,
+              chargePointVendor: this.bootNotificationRequest.chargePointVendor,
+              chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber,
+              firmwareVersion: this.bootNotificationRequest.firmwareVersion,
+              chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber,
+              iccid: this.bootNotificationRequest.iccid,
+              imsi: this.bootNotificationRequest.imsi,
+              meterSerialNumber: this.bootNotificationRequest.meterSerialNumber,
+              meterType: this.bootNotificationRequest.meterType,
+            },
+            { skipBufferingOnError: true }
+          );
         if (!this.isInAcceptedState()) {
           this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
           await Utils.sleep(
@@ -1419,7 +1434,7 @@ export default class ChargingStation {
 
   private async startMessageSequence(): Promise<void> {
     if (this.stationInfo.autoRegister) {
-      await this.ocppRequestService.sendMessageHandler(
+      await this.ocppRequestService.sendMessageHandler<BootNotificationResponse>(
         RequestCommand.BOOT_NOTIFICATION,
         {
           chargePointModel: this.bootNotificationRequest.chargePointModel,
@@ -1449,11 +1464,14 @@ export default class ChargingStation {
         this.getConnectorStatus(connectorId)?.bootStatus
       ) {
         // Send status in template at startup
-        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
-          connectorId,
-          status: this.getConnectorStatus(connectorId).bootStatus,
-          errorCode: ChargePointErrorCode.NO_ERROR,
-        });
+        await this.ocppRequestService.sendMessageHandler<StatusNotificationResponse>(
+          RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            status: this.getConnectorStatus(connectorId).bootStatus,
+            errorCode: ChargePointErrorCode.NO_ERROR,
+          }
+        );
         this.getConnectorStatus(connectorId).status =
           this.getConnectorStatus(connectorId).bootStatus;
       } else if (
@@ -1462,27 +1480,36 @@ export default class ChargingStation {
         this.getConnectorStatus(connectorId)?.bootStatus
       ) {
         // Send status in template after reset
-        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
-          connectorId,
-          status: this.getConnectorStatus(connectorId).bootStatus,
-          errorCode: ChargePointErrorCode.NO_ERROR,
-        });
+        await this.ocppRequestService.sendMessageHandler<StatusNotificationResponse>(
+          RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            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.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
-          connectorId,
-          status: this.getConnectorStatus(connectorId).status,
-          errorCode: ChargePointErrorCode.NO_ERROR,
-        });
+        await this.ocppRequestService.sendMessageHandler<StatusNotificationResponse>(
+          RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            status: this.getConnectorStatus(connectorId).status,
+            errorCode: ChargePointErrorCode.NO_ERROR,
+          }
+        );
       } else {
         // Send default status
-        await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
-          connectorId,
-          status: ChargePointStatus.AVAILABLE,
-          errorCode: ChargePointErrorCode.NO_ERROR,
-        });
+        await this.ocppRequestService.sendMessageHandler<StatusNotificationResponse>(
+          RequestCommand.STATUS_NOTIFICATION,
+          {
+            connectorId,
+            status: ChargePointStatus.AVAILABLE,
+            errorCode: ChargePointErrorCode.NO_ERROR,
+          }
+        );
         this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE;
       }
     }
@@ -1529,18 +1556,24 @@ export default class ChargingStation {
               connectorId,
               this.getEnergyActiveImportRegisterByTransactionId(transactionId)
             );
-            await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, {
-              connectorId,
-              transactionId,
-              meterValue: transactionEndMeterValue,
-            });
+            await this.ocppRequestService.sendMessageHandler<MeterValuesResponse>(
+              RequestCommand.METER_VALUES,
+              {
+                connectorId,
+                transactionId,
+                meterValue: transactionEndMeterValue,
+              }
+            );
           }
-          await this.ocppRequestService.sendMessageHandler(RequestCommand.STOP_TRANSACTION, {
-            transactionId,
-            meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId),
-            idTag: this.getTransactionIdTag(transactionId),
-            reason,
-          });
+          await this.ocppRequestService.sendMessageHandler<StopTransactionResponse>(
+            RequestCommand.STOP_TRANSACTION,
+            {
+              transactionId,
+              meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId),
+              idTag: this.getTransactionIdTag(transactionId),
+              reason,
+            }
+          );
         }
       }
     }
index bfb229be4d08045e53deb2eb760750351165ef03..6e66807df9409c1c331354b8939d62f650bc1ce5 100644 (file)
@@ -21,8 +21,12 @@ import {
   ChangeAvailabilityResponse,
   ChangeConfigurationResponse,
   ClearChargingProfileResponse,
+  DiagnosticsStatusNotificationResponse,
   GetConfigurationResponse,
   GetDiagnosticsResponse,
+  OCPP16BootNotificationResponse,
+  OCPP16HeartbeatResponse,
+  OCPP16StatusNotificationResponse,
   OCPP16TriggerMessageResponse,
   SetChargingProfileResponse,
   UnlockConnectorResponse,
@@ -49,6 +53,7 @@ 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 { OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues';
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
 import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
@@ -217,7 +222,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           connectorId,
           this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
         );
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>(
           OCPP16RequestCommand.METER_VALUES,
           {
             connectorId,
@@ -226,22 +231,23 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           }
         );
       }
-      const stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
-        OCPP16RequestCommand.STOP_TRANSACTION,
-        {
-          transactionId,
-          meterStop:
-            this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
-          idTag: this.chargingStation.getTransactionIdTag(transactionId),
-          reason: OCPP16StopTransactionReason.UNLOCK_COMMAND,
-        }
-      )) as OCPP16StopTransactionResponse;
+      const stopResponse =
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StopTransactionResponse>(
+          OCPP16RequestCommand.STOP_TRANSACTION,
+          {
+            transactionId,
+            meterStop:
+              this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+            idTag: this.chargingStation.getTransactionIdTag(transactionId),
+            reason: OCPP16StopTransactionReason.UNLOCK_COMMAND,
+          }
+        );
       if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
         return Constants.OCPP_RESPONSE_UNLOCKED;
       }
       return Constants.OCPP_RESPONSE_UNLOCK_FAILED;
     }
-    await this.chargingStation.ocppRequestService.sendMessageHandler(
+    await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
       OCPP16RequestCommand.STATUS_NOTIFICATION,
       {
         connectorId,
@@ -489,7 +495,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         }
         this.chargingStation.getConnectorStatus(id).availability = commandPayload.type;
         if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
-          await this.chargingStation.ocppRequestService.sendMessageHandler(
+          await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
             OCPP16RequestCommand.STATUS_NOTIFICATION,
             {
               connectorId: id,
@@ -514,7 +520,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
       }
       this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
         { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR }
       );
@@ -530,7 +536,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     const transactionConnectorId = commandPayload.connectorId;
     const connectorStatus = this.chargingStation.getConnectorStatus(transactionConnectorId);
     if (transactionConnectorId) {
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
         {
           connectorId: transactionConnectorId,
@@ -554,12 +560,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) {
             connectorStatus.authorizeIdTag = commandPayload.idTag;
             const authorizeResponse: OCPP16AuthorizeResponse =
-              (await this.chargingStation.ocppRequestService.sendMessageHandler(
+              await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16AuthorizeResponse>(
                 OCPP16RequestCommand.AUTHORIZE,
                 {
                   idTag: commandPayload.idTag,
                 }
-              )) as OCPP16AuthorizeResponse;
+              );
             if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
               authorized = true;
             }
@@ -579,13 +585,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
               connectorStatus.transactionRemoteStarted = true;
               if (
                 (
-                  (await this.chargingStation.ocppRequestService.sendMessageHandler(
+                  await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StartTransactionResponse>(
                     OCPP16RequestCommand.START_TRANSACTION,
                     {
                       connectorId: transactionConnectorId,
                       idTag: commandPayload.idTag,
                     }
-                  )) as OCPP16StartTransactionResponse
+                  )
                 ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
               ) {
                 logger.debug(
@@ -624,13 +630,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           connectorStatus.transactionRemoteStarted = true;
           if (
             (
-              (await this.chargingStation.ocppRequestService.sendMessageHandler(
+              await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StartTransactionResponse>(
                 OCPP16RequestCommand.START_TRANSACTION,
                 {
                   connectorId: transactionConnectorId,
                   idTag: commandPayload.idTag,
                 }
-              )) as OCPP16StartTransactionResponse
+              )
             ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
           ) {
             logger.debug(
@@ -670,7 +676,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       this.chargingStation.getConnectorStatus(connectorId).status !==
       OCPP16ChargePointStatus.AVAILABLE
     ) {
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
         {
           connectorId,
@@ -727,7 +733,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         connectorId > 0 &&
         this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
       ) {
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
           OCPP16RequestCommand.STATUS_NOTIFICATION,
           {
             connectorId,
@@ -748,7 +754,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             connectorId,
             this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
           );
-          await this.chargingStation.ocppRequestService.sendMessageHandler(
+          await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>(
             OCPP16RequestCommand.METER_VALUES,
             {
               connectorId,
@@ -757,7 +763,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             }
           );
         }
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StopTransactionResponse>(
           OCPP16RequestCommand.STOP_TRANSACTION,
           {
             transactionId,
@@ -814,7 +820,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 info.bytes / 1024
               } bytes transferred from diagnostics archive ${info.name}`
             );
-            await this.chargingStation.ocppRequestService.sendMessageHandler(
+            await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>(
               OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
               {
                 status: OCPP16DiagnosticsStatus.Uploading,
@@ -826,7 +832,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             uri.pathname + diagnosticsArchive
           );
           if (uploadResponse.code === 226) {
-            await this.chargingStation.ocppRequestService.sendMessageHandler(
+            await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>(
               OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
               {
                 status: OCPP16DiagnosticsStatus.Uploaded,
@@ -853,7 +859,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
         );
       } catch (error) {
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>(
           OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
           {
             status: OCPP16DiagnosticsStatus.UploadFailed,
@@ -874,7 +880,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           uri.protocol
         } to transfer the diagnostic logs archive`
       );
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<DiagnosticsStatusNotificationResponse>(
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
         {
           status: OCPP16DiagnosticsStatus.UploadFailed,
@@ -892,7 +898,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         case MessageTrigger.BootNotification:
           setTimeout(() => {
             this.chargingStation.ocppRequestService
-              .sendMessageHandler(
+              .sendMessageHandler<OCPP16BootNotificationResponse>(
                 OCPP16RequestCommand.BOOT_NOTIFICATION,
                 {
                   chargePointModel:
@@ -921,7 +927,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         case MessageTrigger.Heartbeat:
           setTimeout(() => {
             this.chargingStation.ocppRequestService
-              .sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { triggerMessage: true })
+              .sendMessageHandler<OCPP16HeartbeatResponse>(OCPP16RequestCommand.HEARTBEAT, null, {
+                triggerMessage: true,
+              })
               .catch(() => {
                 /* This is intentional */
               });
index 571ac066fc5710159c7bc5f968b9554689eb26de..3d91576bb5b85b41e667e5354174ae2d815ac119 100644 (file)
@@ -1,28 +1,15 @@
 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
 
-import {
-  AuthorizeRequest,
-  StartTransactionRequest,
-  StopTransactionRequest,
-} from '../../../types/ocpp/1.6/Transaction';
-import {
-  DiagnosticsStatusNotificationRequest,
-  HeartbeatRequest,
-  OCPP16BootNotificationRequest,
-  OCPP16RequestCommand,
-  StatusNotificationRequest,
-} from '../../../types/ocpp/1.6/Requests';
-import { ResponseType, SendParams } from '../../../types/ocpp/Requests';
-
 import type ChargingStation from '../../ChargingStation';
 import Constants from '../../../utils/Constants';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { JsonType } from '../../../types/JsonType';
-import { MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues';
+import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests';
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import OCPPError from '../../../exception/OCPPError';
 import OCPPRequestService from '../OCPPRequestService';
 import type OCPPResponseService from '../OCPPResponseService';
+import { SendParams } from '../../../types/ocpp/Requests';
 import Utils from '../../../utils/Utils';
 
 const moduleName = 'OCPP16RequestService';
@@ -35,18 +22,18 @@ export default class OCPP16RequestService extends OCPPRequestService {
     super(chargingStation, ocppResponseService);
   }
 
-  public async sendMessageHandler(
+  public async sendMessageHandler<Response extends JsonType>(
     commandName: OCPP16RequestCommand,
     commandParams?: JsonType,
     params?: SendParams
-  ): Promise<ResponseType> {
+  ): Promise<Response> {
     if (Object.values(OCPP16RequestCommand).includes(commandName)) {
-      return this.sendMessage(
+      return (await this.sendMessage(
         Utils.generateUUID(),
         this.buildCommandPayload(commandName, commandParams),
         commandName,
         params
-      );
+      )) as unknown as Response;
     }
     throw new OCPPError(
       ErrorType.NOT_SUPPORTED,
@@ -56,10 +43,10 @@ export default class OCPP16RequestService extends OCPPRequestService {
     );
   }
 
-  private buildCommandPayload(
+  private buildCommandPayload<Request extends JsonType>(
     commandName: OCPP16RequestCommand,
     commandParams?: JsonType
-  ): JsonType {
+  ): Request {
     let connectorId: number;
     switch (commandName) {
       case OCPP16RequestCommand.AUTHORIZE:
@@ -67,7 +54,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
           ...(!Utils.isUndefined(commandParams?.idTag)
             ? { idTag: commandParams.idTag }
             : { idTag: Constants.DEFAULT_IDTAG }),
-        } as AuthorizeRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.BOOT_NOTIFICATION:
         return {
           chargePointModel: commandParams?.chargePointModel,
@@ -89,13 +76,13 @@ export default class OCPP16RequestService extends OCPPRequestService {
           ...(!Utils.isUndefined(commandParams?.meterType) && {
             meterType: commandParams.meterType,
           }),
-        } as OCPP16BootNotificationRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION:
         return {
           status: commandParams?.diagnosticsStatus,
-        } as DiagnosticsStatusNotificationRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.HEARTBEAT:
-        return {} as HeartbeatRequest;
+        return {} as unknown as Request;
       case OCPP16RequestCommand.METER_VALUES:
         return {
           connectorId: commandParams?.connectorId,
@@ -103,13 +90,13 @@ export default class OCPP16RequestService extends OCPPRequestService {
           meterValue: Array.isArray(commandParams?.meterValue)
             ? commandParams?.meterValue
             : [commandParams?.meterValue],
-        } as MeterValuesRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.STATUS_NOTIFICATION:
         return {
           connectorId: commandParams?.connectorId,
           status: commandParams?.status,
           errorCode: commandParams?.errorCode,
-        } as StatusNotificationRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.START_TRANSACTION:
         return {
           connectorId: commandParams?.connectorId,
@@ -120,7 +107,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
             commandParams?.connectorId as number
           ),
           timestamp: new Date().toISOString(),
-        } as StartTransactionRequest;
+        } as unknown as Request;
       case OCPP16RequestCommand.STOP_TRANSACTION:
         connectorId = this.chargingStation.getConnectorIdByTransactionId(
           commandParams?.transactionId as number
@@ -141,7 +128,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
               )
             ),
           }),
-        } as StopTransactionRequest;
+        } as unknown as Request;
       default:
         throw new OCPPError(
           ErrorType.NOT_SUPPORTED,
index 9eb82d2aca41d920eac7261553b975e2fe919dd5..92fee6609eba8ac431a2ef01d18e48ca935e9a98 100644 (file)
@@ -14,13 +14,13 @@ import {
   OCPP16RequestCommand,
   StatusNotificationRequest,
 } from '../../../types/ocpp/1.6/Requests';
+import { MeterValuesRequest, OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues';
 import {
-  HeartbeatResponse,
   OCPP16BootNotificationResponse,
+  OCPP16HeartbeatResponse,
   OCPP16RegistrationStatus,
-  StatusNotificationResponse,
+  OCPP16StatusNotificationResponse,
 } from '../../../types/ocpp/1.6/Responses';
-import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues';
 
 import type ChargingStation from '../../ChargingStation';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
@@ -135,7 +135,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
   }
 
   private handleResponseHeartbeat(
-    payload: HeartbeatResponse,
+    payload: OCPP16HeartbeatResponse,
     requestPayload: HeartbeatRequest
   ): void {
     logger.debug(
@@ -313,7 +313,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
           requestPayload.meterStart
         );
       this.chargingStation.getBeginEndMeterValues() &&
-        (await this.chargingStation.ocppRequestService.sendMessageHandler(
+        (await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>(
           OCPP16RequestCommand.METER_VALUES,
           {
             connectorId,
@@ -322,7 +322,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
               this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
           }
         ));
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
         {
           connectorId,
@@ -375,7 +375,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       this.chargingStation.getConnectorStatus(connectorId).status !==
       OCPP16ChargePointStatus.AVAILABLE
     ) {
-      await this.chargingStation.ocppRequestService.sendMessageHandler(
+      await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
         {
           connectorId,
@@ -407,7 +407,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       this.chargingStation.getBeginEndMeterValues() &&
         !this.chargingStation.getOcppStrictCompliance() &&
         this.chargingStation.getOutOfOrderEndMeterValues() &&
-        (await this.chargingStation.ocppRequestService.sendMessageHandler(
+        (await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16MeterValuesResponse>(
           OCPP16RequestCommand.METER_VALUES,
           {
             connectorId: transactionConnectorId,
@@ -423,7 +423,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         !this.chargingStation.isChargingStationAvailable() ||
         !this.chargingStation.isConnectorAvailable(transactionConnectorId)
       ) {
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16StatusNotificationResponse>(
           OCPP16RequestCommand.STATUS_NOTIFICATION,
           {
             connectorId: transactionConnectorId,
@@ -434,7 +434,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         this.chargingStation.getConnectorStatus(transactionConnectorId).status =
           OCPP16ChargePointStatus.UNAVAILABLE;
       } else {
-        await this.chargingStation.ocppRequestService.sendMessageHandler(
+        await this.chargingStation.ocppRequestService.sendMessageHandler<OCPP16BootNotificationResponse>(
           OCPP16RequestCommand.STATUS_NOTIFICATION,
           {
             connectorId: transactionConnectorId,
@@ -472,7 +472,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
 
   private handleResponseStatusNotification(
     payload: StatusNotificationRequest,
-    requestPayload: StatusNotificationResponse
+    requestPayload: OCPP16StatusNotificationResponse
   ): void {
     logger.debug(
       this.chargingStation.logPrefix() +
@@ -484,7 +484,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
 
   private handleResponseMeterValues(
     payload: MeterValuesRequest,
-    requestPayload: MeterValuesResponse
+    requestPayload: OCPP16MeterValuesResponse
   ): void {
     logger.debug(
       this.chargingStation.logPrefix() +
index 4ab79d9d2bdb400cbbad9dca3f2befb7d93505b5..7b50b62a3de321a1212b99fbf935e6e0fe461128 100644 (file)
@@ -315,9 +315,9 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendMessageHandler(
+  public abstract sendMessageHandler<Response extends JsonType>(
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: SendParams
-  ): Promise<ResponseType>;
+  ): Promise<Response>;
 }
index 282df11a9ea5d827d8ef40c360f08358d5156220..70967dde3175851c23777fcbaf3274414669ac28 100644 (file)
@@ -103,4 +103,4 @@ export interface MeterValuesRequest extends JsonType {
   meterValue: OCPP16MeterValue[];
 }
 
-export type MeterValuesResponse = EmptyObject;
+export type OCPP16MeterValuesResponse = EmptyObject;
index d6a5d3470dcd9d07c7385f4ae09cb66f6a3b2291..ac59c291a544f790e0892135002562c7c166d840 100644 (file)
@@ -2,7 +2,7 @@ import { EmptyObject } from '../../EmptyObject';
 import { JsonType } from '../../JsonType';
 import { OCPPConfigurationKey } from '../Configuration';
 
-export interface HeartbeatResponse extends JsonType {
+export interface OCPP16HeartbeatResponse extends JsonType {
   currentTime: string;
 }
 
@@ -39,7 +39,7 @@ export interface OCPP16BootNotificationResponse extends JsonType {
   interval: number;
 }
 
-export type StatusNotificationResponse = EmptyObject;
+export type OCPP16StatusNotificationResponse = EmptyObject;
 
 export interface GetConfigurationResponse extends JsonType {
   configurationKey: OCPPConfigurationKey[];
index 50f1d2b134f9b71867ac7eb02b92ae4b11fac827..d7e6db554f68456e79aac01709f4366b6bbaeb64 100644 (file)
@@ -4,12 +4,15 @@ import {
   OCPP16ChargingProfileStatus,
   OCPP16ClearChargingProfileStatus,
   OCPP16ConfigurationStatus,
+  OCPP16HeartbeatResponse,
   OCPP16RegistrationStatus,
+  OCPP16StatusNotificationResponse,
   OCPP16TriggerMessageStatus,
   OCPP16UnlockStatus,
 } from './1.6/Responses';
 
 import { JsonType } from '../JsonType';
+import { OCPP16MeterValuesResponse } from './1.6/MeterValues';
 
 export type ResponseHandler = (
   payload: JsonType | string,
@@ -18,6 +21,12 @@ export type ResponseHandler = (
 
 export type BootNotificationResponse = OCPP16BootNotificationResponse;
 
+export type HeartbeatResponse = OCPP16HeartbeatResponse;
+
+export type StatusNotificationResponse = OCPP16StatusNotificationResponse;
+
+export type MeterValuesResponse = OCPP16MeterValuesResponse;
+
 export enum DefaultStatus {
   ACCEPTED = 'Accepted',
   REJECTED = 'Rejected',