Add missing OCPP 1.6 command payload OCA JSON schemas
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index dc14d0064dceaa24e4ad658eb08a4f6912f5913a..93c59ba29cfcedaa63f334270f4f2ca37766372f 100644 (file)
@@ -1,4 +1,4 @@
-// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
 import fs from 'fs';
 import path from 'path';
@@ -73,6 +73,7 @@ import {
 } from '../../../types/ocpp/1.6/Transaction';
 import type { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
+import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
 import type { IncomingRequestHandler } from '../../../types/ocpp/Requests';
 import type { DefaultResponse } from '../../../types/ocpp/Responses';
 import Constants from '../../../utils/Constants';
@@ -81,6 +82,7 @@ import Utils from '../../../utils/Utils';
 import type ChargingStation from '../../ChargingStation';
 import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
 import { ChargingStationUtils } from '../../ChargingStationUtils';
+import OCPPConstants from '../OCPPConstants';
 import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 
@@ -94,7 +96,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     if (new.target?.name === moduleName) {
       throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
     }
-    super();
+    super(OCPPVersion.VERSION_16);
     this.incomingRequestHandlers = new Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>([
       [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)],
       [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
@@ -289,6 +291,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           )
         ) as JSONSchemaType<OCPP16DataTransferRequest>,
       ],
+      [
+        OCPP16IncomingRequestCommand.UPDATE_FIRMWARE,
+        JSON.parse(
+          fs.readFileSync(
+            path.resolve(
+              path.dirname(fileURLToPath(import.meta.url)),
+              '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json'
+            ),
+            'utf8'
+          )
+        ) as JSONSchemaType<OCPP16UpdateFirmwareRequest>,
+      ],
     ]);
     this.validatePayload.bind(this);
   }
@@ -380,7 +394,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandName: OCPP16IncomingRequestCommand,
     commandPayload: JsonType
   ): boolean {
-    if (this.jsonSchemas.has(commandName)) {
+    if (this.jsonSchemas.has(commandName) === true) {
       return this.validateIncomingRequestPayload(
         chargingStation,
         commandName,
@@ -418,14 +432,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         chargingStation.stationInfo.resetTime
       )}`
     );
-    return Constants.OCPP_RESPONSE_ACCEPTED;
+    return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
   }
 
   private handleRequestClearCache(chargingStation: ChargingStation): DefaultResponse {
     chargingStation.authorizedTagsCache.deleteAuthorizedTags(
       ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo)
     );
-    return Constants.OCPP_RESPONSE_ACCEPTED;
+    return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
   }
 
   private async handleRequestUnlockConnector(
@@ -437,13 +451,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       logger.error(
         `${chargingStation.logPrefix()} Trying to unlock a non existing connector Id ${connectorId.toString()}`
       );
-      return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED;
+      return OCPPConstants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED;
     }
     if (connectorId === 0) {
       logger.error(
         chargingStation.logPrefix() + ' Trying to unlock connector Id ' + connectorId.toString()
       );
-      return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED;
+      return OCPPConstants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED;
     }
     if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
       const stopResponse = await chargingStation.stopTransactionOnConnector(
@@ -451,9 +465,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         OCPP16StopTransactionReason.UNLOCK_COMMAND
       );
       if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
-        return Constants.OCPP_RESPONSE_UNLOCKED;
+        return OCPPConstants.OCPP_RESPONSE_UNLOCKED;
       }
-      return Constants.OCPP_RESPONSE_UNLOCK_FAILED;
+      return OCPPConstants.OCPP_RESPONSE_UNLOCK_FAILED;
     }
     await chargingStation.ocppRequestService.requestHandler<
       OCPP16StatusNotificationRequest,
@@ -464,7 +478,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
     });
     chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
-    return Constants.OCPP_RESPONSE_UNLOCKED;
+    return OCPPConstants.OCPP_RESPONSE_UNLOCKED;
   }
 
   private handleRequestGetConfiguration(
@@ -526,9 +540,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       true
     );
     if (!keyToChange) {
-      return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED;
+      return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED;
     } else if (keyToChange && keyToChange.readonly) {
-      return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_REJECTED;
     } else if (keyToChange && !keyToChange.readonly) {
       let valueChanged = false;
       if (keyToChange.value !== commandPayload.value) {
@@ -564,9 +578,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         chargingStation.restartWebSocketPing();
       }
       if (keyToChange.reboot) {
-        return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED;
+        return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED;
       }
-      return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED;
+      return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED;
     }
   }
 
@@ -581,7 +595,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE
       ) === false
     ) {
-      return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED;
+      return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED;
     }
     if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
       logger.error(
@@ -589,14 +603,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           commandPayload.connectorId
         }`
       );
-      return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
     }
     if (
       commandPayload.csChargingProfiles.chargingProfilePurpose ===
         ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE &&
       commandPayload.connectorId !== 0
     ) {
-      return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
     }
     if (
       commandPayload.csChargingProfiles.chargingProfilePurpose ===
@@ -610,7 +624,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           commandPayload.connectorId
         } without a started transaction`
       );
-      return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
     }
     OCPP16ServiceUtils.setChargingProfile(
       chargingStation,
@@ -623,7 +637,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       }, dump their stack: %j`,
       chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
     );
-    return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED;
+    return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED;
   }
 
   private handleRequestClearChargingProfile(
@@ -637,7 +651,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE
       ) === false
     ) {
-      return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
+      return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
     if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
       logger.error(
@@ -645,7 +659,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           commandPayload.connectorId
         }`
       );
-      return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
+      return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
     const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId);
     if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) {
@@ -656,7 +670,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         }, dump their stack: %j`,
         connectorStatus.chargingProfiles
       );
-      return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
+      return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
     }
     if (!commandPayload.connectorId) {
       let clearedCP = false;
@@ -701,10 +715,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         }
       }
       if (clearedCP) {
-        return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
+        return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
       }
     }
-    return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
+    return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
   }
 
   private async handleRequestChangeAvailability(
@@ -716,20 +730,20 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       logger.error(
         `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`
       );
-      return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
     }
     const chargePointStatus: OCPP16ChargePointStatus =
       commandPayload.type === OCPP16AvailabilityType.OPERATIVE
         ? OCPP16ChargePointStatus.AVAILABLE
         : OCPP16ChargePointStatus.UNAVAILABLE;
     if (connectorId === 0) {
-      let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
+      let response: ChangeAvailabilityResponse = OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
       for (const id of chargingStation.connectors.keys()) {
         if (chargingStation.getConnectorStatus(id)?.transactionStarted === true) {
-          response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
+          response = OCPPConstants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
         }
         chargingStation.getConnectorStatus(id).availability = commandPayload.type;
-        if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
+        if (response === OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
           await chargingStation.ocppRequestService.requestHandler<
             OCPP16StatusNotificationRequest,
             OCPP16StatusNotificationResponse
@@ -750,7 +764,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     ) {
       if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
         chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
-        return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
+        return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
       }
       chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
       await chargingStation.ocppRequestService.requestHandler<
@@ -762,9 +776,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
       chargingStation.getConnectorStatus(connectorId).status = chargePointStatus;
-      return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
+      return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
     }
-    return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
+    return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_REJECTED;
   }
 
   private async handleRequestRemoteStartTransaction(
@@ -847,7 +861,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
               ) {
                 logger.debug(remoteStartTransactionLogMsg);
-                return Constants.OCPP_RESPONSE_ACCEPTED;
+                return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
               }
               return this.notifyRemoteStartTransactionRejected(
                 chargingStation,
@@ -888,7 +902,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
           ) {
             logger.debug(remoteStartTransactionLogMsg);
-            return Constants.OCPP_RESPONSE_ACCEPTED;
+            return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
           }
           return this.notifyRemoteStartTransactionRejected(
             chargingStation,
@@ -945,7 +959,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         chargingStation.getConnectorStatus(connectorId).status +
         "'"
     );
-    return Constants.OCPP_RESPONSE_REJECTED;
+    return OCPPConstants.OCPP_RESPONSE_REJECTED;
   }
 
   private setRemoteStartTransactionChargingProfile(
@@ -996,9 +1010,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           OCPP16StopTransactionReason.REMOTE
         );
         if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
-          return Constants.OCPP_RESPONSE_ACCEPTED;
+          return OCPPConstants.OCPP_RESPONSE_ACCEPTED;
         }
-        return Constants.OCPP_RESPONSE_REJECTED;
+        return OCPPConstants.OCPP_RESPONSE_REJECTED;
       }
     }
     logger.warn(
@@ -1006,7 +1020,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         ' Trying to remote stop a non existing transaction ' +
         transactionId.toString()
     );
-    return Constants.OCPP_RESPONSE_REJECTED;
+    return OCPPConstants.OCPP_RESPONSE_REJECTED;
   }
 
   private handleRequestUpdateFirmware(
@@ -1020,7 +1034,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         OCPP16IncomingRequestCommand.UPDATE_FIRMWARE
       ) === false
     ) {
-      return Constants.OCPP_RESPONSE_EMPTY;
+      return OCPPConstants.OCPP_RESPONSE_EMPTY;
     }
     logger.debug(
       chargingStation.logPrefix() +
@@ -1042,7 +1056,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
       ) === false
     ) {
-      return Constants.OCPP_RESPONSE_EMPTY;
+      return OCPPConstants.OCPP_RESPONSE_EMPTY;
     }
     logger.debug(
       chargingStation.logPrefix() +
@@ -1132,7 +1146,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           chargingStation,
           OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
           error as Error,
-          { errorResponse: Constants.OCPP_RESPONSE_EMPTY }
+          { errorResponse: OCPPConstants.OCPP_RESPONSE_EMPTY }
         );
       }
     } else {
@@ -1147,7 +1161,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, {
         status: OCPP16DiagnosticsStatus.UploadFailed,
       });
-      return Constants.OCPP_RESPONSE_EMPTY;
+      return OCPPConstants.OCPP_RESPONSE_EMPTY;
     }
   }
 
@@ -1166,7 +1180,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         commandPayload.requestedMessage
       )
     ) {
-      return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
+      return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
     }
     if (
       !OCPP16ServiceUtils.isConnectorIdValid(
@@ -1175,7 +1189,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         commandPayload.connectorId
       )
     ) {
-      return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED;
+      return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED;
     }
     try {
       switch (commandPayload.requestedMessage) {
@@ -1195,7 +1209,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 /* This is intentional */
               });
           }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
-          return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
+          return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
         case OCPP16MessageTrigger.Heartbeat:
           setTimeout(() => {
             chargingStation.ocppRequestService
@@ -1211,7 +1225,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 /* This is intentional */
               });
           }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
-          return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
+          return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
         case OCPP16MessageTrigger.StatusNotification:
           setTimeout(() => {
             if (commandPayload?.connectorId) {
@@ -1255,16 +1269,16 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
               }
             }
           }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
-          return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
+          return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
         default:
-          return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
+          return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
       }
     } catch (error) {
       return this.handleIncomingRequestError(
         chargingStation,
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
         error as Error,
-        { errorResponse: Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }
+        { errorResponse: OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }
       );
     }
   }
@@ -1287,7 +1301,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         chargingStation,
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
         error as Error,
-        { errorResponse: Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED }
+        { errorResponse: OCPPConstants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED }
       );
     }
   }