feat: add initial support get composite schedule OCPP 1.6 command
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ResponseService.ts
index aae1d0655a39fd52f1d38d61c3043975974aee61..68e9f7c66cae6febf94318a5a5523ef5ee4d6e3d 100644 (file)
@@ -1,70 +1,56 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
-
 import type { JSONSchemaType } from 'ajv';
 
-import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
-import OCPPError from '../../../exception/OCPPError';
-import type { JsonObject, JsonType } from '../../../types/JsonType';
-import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
-import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
-import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
-import type {
-  OCPP16MeterValuesRequest,
-  OCPP16MeterValuesResponse,
-} from '../../../types/ocpp/1.6/MeterValues';
-import {
-  type OCPP16BootNotificationRequest,
-  OCPP16IncomingRequestCommand,
-  OCPP16RequestCommand,
-  type OCPP16StatusNotificationRequest,
-} from '../../../types/ocpp/1.6/Requests';
-import type {
-  ChangeAvailabilityResponse,
-  ChangeConfigurationResponse,
-  ClearChargingProfileResponse,
-  GetConfigurationResponse,
-  GetDiagnosticsResponse,
-  OCPP16BootNotificationResponse,
-  OCPP16DataTransferResponse,
-  OCPP16DiagnosticsStatusNotificationResponse,
-  OCPP16FirmwareStatusNotificationResponse,
-  OCPP16HeartbeatResponse,
-  OCPP16StatusNotificationResponse,
-  OCPP16TriggerMessageResponse,
-  OCPP16UpdateFirmwareResponse,
-  SetChargingProfileResponse,
-  UnlockConnectorResponse,
-} from '../../../types/ocpp/1.6/Responses';
+import { type ChargingStation, ChargingStationConfigurationUtils } from '../../../charging-station';
+import { OCPPError } from '../../../exception';
 import {
+  type ChangeAvailabilityResponse,
+  type ChangeConfigurationResponse,
+  type ClearChargingProfileResponse,
+  ErrorType,
+  type GenericResponse,
+  type GetConfigurationResponse,
+  type GetDiagnosticsResponse,
+  type JsonObject,
+  type JsonType,
   OCPP16AuthorizationStatus,
   type OCPP16AuthorizeRequest,
   type OCPP16AuthorizeResponse,
+  type OCPP16BootNotificationRequest,
+  type OCPP16BootNotificationResponse,
+  OCPP16ChargePointErrorCode,
+  OCPP16ChargePointStatus,
+  type OCPP16DataTransferResponse,
+  type OCPP16DiagnosticsStatusNotificationResponse,
+  type OCPP16FirmwareStatusNotificationResponse,
+  type OCPP16GetCompositeScheduleResponse,
+  type OCPP16HeartbeatResponse,
+  OCPP16IncomingRequestCommand,
+  type OCPP16MeterValuesRequest,
+  type OCPP16MeterValuesResponse,
+  OCPP16RequestCommand,
+  OCPP16StandardParametersKey,
   type OCPP16StartTransactionRequest,
   type OCPP16StartTransactionResponse,
+  type OCPP16StatusNotificationRequest,
+  type OCPP16StatusNotificationResponse,
   type OCPP16StopTransactionRequest,
   type OCPP16StopTransactionResponse,
-} from '../../../types/ocpp/1.6/Transaction';
-import { ErrorType } from '../../../types/ocpp/ErrorType';
-import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
-import {
-  type GenericResponse,
+  type OCPP16TriggerMessageResponse,
+  type OCPP16UpdateFirmwareResponse,
+  OCPPVersion,
   RegistrationStatusEnumType,
   type ResponseHandler,
-} from '../../../types/ocpp/Responses';
-import Constants from '../../../utils/Constants';
-import logger from '../../../utils/Logger';
-import Utils from '../../../utils/Utils';
-import type ChargingStation from '../../ChargingStation';
-import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
-import OCPPResponseService from '../OCPPResponseService';
+  type SetChargingProfileResponse,
+  type UnlockConnectorResponse,
+} from '../../../types';
+import { Constants, Utils, logger } from '../../../utils';
+import { OCPP16ServiceUtils, OCPPResponseService } from '../internal';
 
 const moduleName = 'OCPP16ResponseService';
 
-export default class OCPP16ResponseService extends OCPPResponseService {
+export class OCPP16ResponseService extends OCPPResponseService {
   public jsonIncomingRequestResponseSchemas: Map<
     OCPP16IncomingRequestCommand,
     JSONSchemaType<JsonObject>
@@ -74,9 +60,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
   private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>;
 
   public constructor() {
-    if (new.target?.name === moduleName) {
-      throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
-    }
+    // if (new.target?.name === moduleName) {
+    //   throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
+    // }
     super(OCPPVersion.VERSION_16);
     this.responseHandlers = new Map<OCPP16RequestCommand, ResponseHandler>([
       [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)],
@@ -93,148 +79,204 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP16HeartbeatResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16HeartbeatResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.AUTHORIZE,
-        this.parseJsonSchemaFile<OCPP16AuthorizeResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StartTransactionResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StartTransactionResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StopTransactionResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StopTransactionResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16StatusNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        this.parseJsonSchemaFile<OCPP16MeterValuesResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16MeterValuesResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
     ]);
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP16IncomingRequestCommand.RESET,
-        this.parseJsonSchemaFile<GenericResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<GenericResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        this.parseJsonSchemaFile<ChangeAvailabilityResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR,
-        this.parseJsonSchemaFile<UnlockConnectorResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<UnlockConnectorResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_CONFIGURATION,
-        this.parseJsonSchemaFile<GetConfigurationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetConfigurationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
-        this.parseJsonSchemaFile<ChangeConfigurationResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeConfigurationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json',
+          moduleName,
+          'constructor'
+        ),
+      ],
+      [
+        OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE,
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16GetCompositeScheduleResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/GetCompositeScheduleResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<SetChargingProfileResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<SetChargingProfileResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
-        this.parseJsonSchemaFile<ClearChargingProfileResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<ClearChargingProfileResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-        this.parseJsonSchemaFile<GenericResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
-        this.parseJsonSchemaFile<GenericResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
-        this.parseJsonSchemaFile<GetDiagnosticsResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<GetDiagnosticsResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        this.parseJsonSchemaFile<OCPP16TriggerMessageResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16TriggerMessageResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16IncomingRequestCommand.UPDATE_FIRMWARE,
-        this.parseJsonSchemaFile<OCPP16UpdateFirmwareResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16UpdateFirmwareResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
     ]);
@@ -330,9 +372,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         { visible: false },
         { overwrite: true, save: true }
       );
-      chargingStation.heartbeatSetInterval
-        ? chargingStation.restartHeartbeat()
-        : chargingStation.startHeartbeat();
+      OCPP16ServiceUtils.startHeartbeatInterval(chargingStation, payload.interval);
     }
     if (Object.values(RegistrationStatusEnumType).includes(payload.status)) {
       const logMsg = `${chargingStation.logPrefix()} Charging station in '${
@@ -410,7 +450,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       chargingStation.getConnectorStatus(connectorId)?.transactionRemoteStarted === true &&
       chargingStation.getAuthorizeRemoteTxRequests() === true &&
       chargingStation.getLocalAuthListEnabled() === true &&
-      chargingStation.hasAuthorizedTags() &&
+      chargingStation.hasIdTags() &&
       chargingStation.getConnectorStatus(connectorId)?.idTagLocalAuthorized === false
     ) {
       logger.error(
@@ -473,8 +513,8 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     }
     if (
       chargingStation.getConnectorStatus(connectorId)?.status !==
-        OCPP16ChargePointStatus.AVAILABLE &&
-      chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING
+        OCPP16ChargePointStatus.Available &&
+      chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.Preparing
     ) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${
@@ -519,10 +559,10 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         OCPP16StatusNotificationResponse
       >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, {
         connectorId,
-        status: OCPP16ChargePointStatus.CHARGING,
+        status: OCPP16ChargePointStatus.Charging,
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
-      chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING;
+      chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.Charging;
       logger.info(
         `${chargingStation.logPrefix()} Transaction ${payload.transactionId.toString()} STARTED on ${
           chargingStation.stationInfo.chargingStationId
@@ -558,17 +598,17 @@ export default class OCPP16ResponseService extends OCPPResponseService {
   ): Promise<void> {
     chargingStation.resetConnectorStatus(connectorId);
     if (
-      chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.AVAILABLE
+      chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.Available
     ) {
       await chargingStation.ocppRequestService.requestHandler<
         OCPP16StatusNotificationRequest,
         OCPP16StatusNotificationResponse
       >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, {
         connectorId,
-        status: OCPP16ChargePointStatus.AVAILABLE,
+        status: OCPP16ChargePointStatus.Available,
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
-      chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
+      chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.Available;
     }
   }
 
@@ -612,22 +652,22 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         OCPP16StatusNotificationResponse
       >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, {
         connectorId: transactionConnectorId,
-        status: OCPP16ChargePointStatus.UNAVAILABLE,
+        status: OCPP16ChargePointStatus.Unavailable,
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
       chargingStation.getConnectorStatus(transactionConnectorId).status =
-        OCPP16ChargePointStatus.UNAVAILABLE;
+        OCPP16ChargePointStatus.Unavailable;
     } else {
       await chargingStation.ocppRequestService.requestHandler<
         OCPP16BootNotificationRequest,
         OCPP16BootNotificationResponse
       >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, {
         connectorId: transactionConnectorId,
-        status: OCPP16ChargePointStatus.AVAILABLE,
+        status: OCPP16ChargePointStatus.Available,
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
       chargingStation.getConnectorStatus(transactionConnectorId).status =
-        OCPP16ChargePointStatus.AVAILABLE;
+        OCPP16ChargePointStatus.Available;
     }
     if (chargingStation.stationInfo.powerSharedByConnectors) {
       chargingStation.powerDivider--;
@@ -647,13 +687,4 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       logger.warn(logMsg);
     }
   }
-
-  private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
-    return JSON.parse(
-      fs.readFileSync(
-        path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
-        'utf8'
-      )
-    ) as JSONSchemaType<T>;
-  }
 }