Add template section to list OCPP commands supported (#129)
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 45f3723c5a3b7db22aa6fbc4cff2f1db4853d417..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,34 +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 type ChargingStation from '../../ChargingStation';
-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 { URL } from 'url';
-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';
 
@@ -146,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)(
@@ -174,7 +181,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             null,
             2
           )}`,
-          commandName
+          commandName,
+          commandPayload
         );
       }
     } else {
@@ -185,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
@@ -250,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<
@@ -301,7 +310,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       }
     } else {
       for (const key of commandPayload.key) {
-        const keyFound = chargingStation.getConfigurationKey(key);
+        const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
+          chargingStation,
+          key
+        );
         if (keyFound) {
           if (Utils.isUndefined(keyFound.visible)) {
             keyFound.visible = true;
@@ -346,7 +358,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         commandPayload
       );
     }
-    const keyToChange = chargingStation.getConfigurationKey(commandPayload.key, true);
+    const keyToChange = ChargingStationConfigurationUtils.getConfigurationKey(
+      chargingStation,
+      commandPayload.key,
+      true
+    );
     if (!keyToChange) {
       return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED;
     } else if (keyToChange && keyToChange.readonly) {
@@ -354,19 +370,26 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     } else if (keyToChange && !keyToChange.readonly) {
       let valueChanged = false;
       if (keyToChange.value !== commandPayload.value) {
-        chargingStation.setConfigurationKeyValue(commandPayload.key, commandPayload.value, true);
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
+          commandPayload.key,
+          commandPayload.value,
+          true
+        );
         valueChanged = true;
       }
       let triggerHeartbeatRestart = false;
       if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) {
-        chargingStation.setConfigurationKeyValue(
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
           OCPP16StandardParametersKey.HeartbeatInterval,
           commandPayload.value
         );
         triggerHeartbeatRestart = true;
       }
       if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) {
-        chargingStation.setConfigurationKeyValue(
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
           OCPP16StandardParametersKey.HeartBeatInterval,
           commandPayload.value
         );
@@ -496,7 +519,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 clearCurrentCP = true;
               }
               if (clearCurrentCP) {
-                connectorStatus.chargingProfiles[index] = {} as OCPP16ChargingProfile;
+                connectorStatus.chargingProfiles.splice(index, 1);
                 logger.debug(
                   `${chargingStation.logPrefix()} Matching charging profile(s) cleared on connector id ${
                     commandPayload.connectorId
@@ -599,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;
@@ -819,7 +846,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           >(chargingStation, OCPP16RequestCommand.METER_VALUES, {
             connectorId,
             transactionId,
-            meterValue: transactionEndMeterValue,
+            meterValue: [transactionEndMeterValue],
           });
         }
         await chargingStation.ocppRequestService.requestHandler<
@@ -833,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()
@@ -866,7 +893,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       let ftpClient: Client;
       try {
         const logFiles = fs
-          .readdirSync(path.resolve(__dirname, '../../../../'))
+          .readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../'))
           .filter((file) => file.endsWith('.log'))
           .map((file) => path.join('./', file));
         const diagnosticsArchive = chargingStation.stationInfo.chargingStationId + '_logs.tar.gz';
@@ -895,7 +922,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             });
           });
           uploadResponse = await ftpClient.uploadFrom(
-            path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive),
+            path.join(
+              path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../'),
+              diagnosticsArchive
+            ),
             uri.pathname + diagnosticsArchive
           );
           if (uploadResponse.code === 226) {