Add template section to list OCPP commands supported (#129)
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 67967fd9718d7a4ac7bb38896b8537d8bf212013..172aa8dd4a55ec95b1ccf845bcc668f848841b01 100644 (file)
@@ -1,5 +1,29 @@
 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
 
+import fs from 'fs';
+import path from 'path';
+import { URL, fileURLToPath } from 'url';
+
+import { Client, FTPResponse } from 'basic-ftp';
+import tar from 'tar';
+
+import OCPPError from '../../../exception/OCPPError';
+import { JsonType } from '../../../types/JsonType';
+import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
+import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
+import {
+  ChargingProfilePurposeType,
+  OCPP16ChargingProfile,
+} from '../../../types/ocpp/1.6/ChargingProfile';
+import {
+  OCPP16StandardParametersKey,
+  OCPP16SupportedFeatureProfiles,
+} from '../../../types/ocpp/1.6/Configuration';
+import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
+import {
+  OCPP16MeterValuesRequest,
+  OCPP16MeterValuesResponse,
+} from '../../../types/ocpp/1.6/MeterValues';
 import {
   ChangeAvailabilityRequest,
   ChangeConfigurationRequest,
@@ -35,11 +59,6 @@ import {
   SetChargingProfileResponse,
   UnlockConnectorResponse,
 } from '../../../types/ocpp/1.6/Responses';
-import {
-  ChargingProfilePurposeType,
-  OCPP16ChargingProfile,
-} from '../../../types/ocpp/1.6/ChargingProfile';
-import { Client, FTPResponse } from 'basic-ftp';
 import {
   OCPP16AuthorizationStatus,
   OCPP16AuthorizeRequest,
@@ -50,35 +69,18 @@ import {
   OCPP16StopTransactionRequest,
   OCPP16StopTransactionResponse,
 } from '../../../types/ocpp/1.6/Transaction';
-import {
-  OCPP16MeterValuesRequest,
-  OCPP16MeterValuesResponse,
-} from '../../../types/ocpp/1.6/MeterValues';
-import {
-  OCPP16StandardParametersKey,
-  OCPP16SupportedFeatureProfiles,
-} from '../../../types/ocpp/1.6/Configuration';
-import { URL, fileURLToPath } from 'url';
-
-import type ChargingStation from '../../ChargingStation';
-import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
-import Constants from '../../../utils/Constants';
-import { DefaultResponse } from '../../../types/ocpp/Responses';
+import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
 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 { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
-import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
-import OCPPError from '../../../exception/OCPPError';
-import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
-import Utils from '../../../utils/Utils';
-import fs from 'fs';
+import { DefaultResponse } from '../../../types/ocpp/Responses';
+import Constants from '../../../utils/Constants';
 import logger from '../../../utils/Logger';
-import path from 'path';
-import tar from 'tar';
+import Utils from '../../../utils/Utils';
+import type ChargingStation from '../../ChargingStation';
+import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
+import { ChargingStationUtils } from '../../ChargingStationUtils';
+import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
+import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 
 const moduleName = 'OCPP16IncomingRequestService';
 
@@ -147,14 +149,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           null,
           2
         )} while the charging station is in pending state on the central server`,
-        commandName
+        commandName,
+        commandPayload
       );
     }
     if (
       chargingStation.isRegistered() ||
       (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState())
     ) {
-      if (this.incomingRequestHandlers.has(commandName)) {
+      if (
+        this.incomingRequestHandlers.has(commandName) &&
+        ChargingStationUtils.isCommandSupported(commandName, chargingStation.stationInfo)
+      ) {
         try {
           // Call the method to build the response
           response = await this.incomingRequestHandlers.get(commandName)(
@@ -175,7 +181,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             null,
             2
           )}`,
-          commandName
+          commandName,
+          commandPayload
         );
       }
     } else {
@@ -186,7 +193,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           null,
           2
         )} while the charging station is not registered on the central server.`,
-        commandName
+        commandName,
+        commandPayload
       );
     }
     // Send the built response
@@ -251,7 +259,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         >(chargingStation, OCPP16RequestCommand.METER_VALUES, {
           connectorId,
           transactionId,
-          meterValue: transactionEndMeterValue,
+          meterValue: [transactionEndMeterValue],
         });
       }
       const stopResponse = await chargingStation.ocppRequestService.requestHandler<
@@ -614,7 +622,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           if (
             chargingStation.getLocalAuthListEnabled() &&
             chargingStation.hasAuthorizedTags() &&
-            chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)
+            chargingStation.authorizedTagsCache
+              .getAuthorizedTags(
+                ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo)
+              )
+              .find((value) => value === commandPayload.idTag)
           ) {
             connectorStatus.localAuthorizeIdTag = commandPayload.idTag;
             connectorStatus.idTagLocalAuthorized = true;
@@ -834,7 +846,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           >(chargingStation, OCPP16RequestCommand.METER_VALUES, {
             connectorId,
             transactionId,
-            meterValue: transactionEndMeterValue,
+            meterValue: [transactionEndMeterValue],
           });
         }
         await chargingStation.ocppRequestService.requestHandler<
@@ -848,7 +860,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         return Constants.OCPP_RESPONSE_ACCEPTED;
       }
     }
-    logger.info(
+    logger.warn(
       chargingStation.logPrefix() +
         ' Trying to remote stop a non existing transaction ' +
         transactionId.toString()