Initial cleanup at loading JSON schemas
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 10 Jan 2023 22:33:53 +0000 (23:33 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 10 Jan 2023 22:33:53 +0000 (23:33 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
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/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts

index 9e0849e987080be00f57f931e62fcfc9f3b8a3d9..36869b6d3c551996f73ddf99a8303098d27c7a1c 100644 (file)
@@ -140,171 +140,85 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16IncomingRequestCommand.RESET,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/Reset.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ResetRequest>,
+        this.parseJsonSchemaFile<ResetRequest>('../../../assets/json-schemas/ocpp/1.6/Reset.json'),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CACHE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16ClearCacheRequest>,
+        this.parseJsonSchemaFile<OCPP16ClearCacheRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearCache.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<UnlockConnectorRequest>,
+        this.parseJsonSchemaFile<UnlockConnectorRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_CONFIGURATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<GetConfigurationRequest>,
+        this.parseJsonSchemaFile<GetConfigurationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ChangeConfigurationRequest>,
+        this.parseJsonSchemaFile<ChangeConfigurationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<GetDiagnosticsRequest>,
+        this.parseJsonSchemaFile<GetDiagnosticsRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<SetChargingProfileRequest>,
+        this.parseJsonSchemaFile<SetChargingProfileRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ClearChargingProfileRequest>,
+        this.parseJsonSchemaFile<ClearChargingProfileRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ChangeAvailabilityRequest>,
+        this.parseJsonSchemaFile<ChangeAvailabilityRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<RemoteStartTransactionRequest>,
+        this.parseJsonSchemaFile<RemoteStartTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<RemoteStopTransactionRequest>,
+        this.parseJsonSchemaFile<RemoteStopTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16TriggerMessageRequest>,
+        this.parseJsonSchemaFile<OCPP16TriggerMessageRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DataTransferRequest>,
+        this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
+        ),
       ],
       [
         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.parseJsonSchemaFile<OCPP16UpdateFirmwareRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json'
+        ),
       ],
     ]);
     this.validatePayload.bind(this);
@@ -1359,4 +1273,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       );
     }
   }
+
+  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>;
+  }
 }
index 3a38f02aa1879f38f03cfedabb6e9929c71eae79..a24e0a07039a10aa568dca2fc0275e9c18a16706 100644 (file)
@@ -13,10 +13,10 @@ import {
   type OCPP16BootNotificationRequest,
   type OCPP16DataTransferRequest,
   type OCPP16DiagnosticsStatusNotificationRequest,
+  type OCPP16FirmwareStatusNotificationRequest,
   type OCPP16HeartbeatRequest,
   OCPP16RequestCommand,
   type OCPP16StatusNotificationRequest,
-  type OCPP16UpdateFirmwareRequest,
 } from '../../../types/ocpp/1.6/Requests';
 import type {
   OCPP16AuthorizeRequest,
@@ -46,123 +46,63 @@ export default class OCPP16RequestService extends OCPPRequestService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.AUTHORIZE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16AuthorizeRequest>,
+        this.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
+        ),
       ],
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16BootNotificationRequest>,
+        this.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
+        ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DiagnosticsStatusNotificationRequest>,
+        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
+        ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16HeartbeatRequest>,
+        this.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
+        ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16MeterValuesRequest>,
+        this.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
+        ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StatusNotificationRequest>,
+        this.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
+        ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StartTransactionRequest>,
+        this.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
+        ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StopTransactionRequest>,
+        this.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
+        ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DataTransferRequest>,
+        this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
+        ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16UpdateFirmwareRequest>,
+        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
+        ),
       ],
     ]);
     this.buildRequestPayload.bind(this);
@@ -306,4 +246,13 @@ export default class OCPP16RequestService extends OCPPRequestService {
         );
     }
   }
+
+  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>;
+  }
 }
index ac183aecc4e9898d82c3e0c2defa2d775095859a..163ecbcf7ce185c4b32b1d3c51968fed2b1f1f70 100644 (file)
@@ -93,293 +93,149 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16BootNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16HeartbeatResponse>,
+        this.parseJsonSchemaFile<OCPP16HeartbeatResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.AUTHORIZE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16AuthorizeResponse>,
+        this.parseJsonSchemaFile<OCPP16AuthorizeResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StartTransactionResponse>,
+        this.parseJsonSchemaFile<OCPP16StartTransactionResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StopTransactionResponse>,
+        this.parseJsonSchemaFile<OCPP16StopTransactionResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16StatusNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP16StatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16MeterValuesResponse>,
+        this.parseJsonSchemaFile<OCPP16MeterValuesResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DiagnosticsStatusNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DataTransferResponse>,
+        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
+        ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16FirmwareStatusNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json'
+        ),
       ],
     ]);
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP16IncomingRequestCommand.RESET,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<DefaultResponse>,
+        this.parseJsonSchemaFile<DefaultResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CACHE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<DefaultResponse>,
+        this.parseJsonSchemaFile<DefaultResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ChangeAvailabilityResponse>,
+        this.parseJsonSchemaFile<ChangeAvailabilityResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<UnlockConnectorResponse>,
+        this.parseJsonSchemaFile<UnlockConnectorResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_CONFIGURATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<GetConfigurationResponse>,
+        this.parseJsonSchemaFile<GetConfigurationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ChangeConfigurationResponse>,
+        this.parseJsonSchemaFile<ChangeConfigurationResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<SetChargingProfileResponse>,
+        this.parseJsonSchemaFile<SetChargingProfileResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<ClearChargingProfileResponse>,
+        this.parseJsonSchemaFile<ClearChargingProfileResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<DefaultResponse>,
+        this.parseJsonSchemaFile<DefaultResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<DefaultResponse>,
+        this.parseJsonSchemaFile<DefaultResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<GetDiagnosticsResponse>,
+        this.parseJsonSchemaFile<GetDiagnosticsResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16TriggerMessageResponse>,
+        this.parseJsonSchemaFile<OCPP16TriggerMessageResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16DataTransferResponse>,
+        this.parseJsonSchemaFile<OCPP16DataTransferResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json'
+        ),
       ],
       [
         OCPP16IncomingRequestCommand.UPDATE_FIRMWARE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP16UpdateFirmwareResponse>,
+        this.parseJsonSchemaFile<OCPP16UpdateFirmwareResponse>(
+          '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json'
+        ),
       ],
     ]);
     this.validatePayload.bind(this);
@@ -826,4 +682,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       );
     }
   }
+
+  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>;
+  }
 }
index 08fa5d2ebb4381755d8b12fe7221c1d650bd7ee4..7a0b1e6fc202eb5b7e22579aa69bf55a1e2ed396 100644 (file)
@@ -37,15 +37,9 @@ export default class OCPP20IncomingRequestService extends OCPPIncomingRequestSer
     this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20ClearCacheRequest>,
+        this.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
+          '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json'
+        ),
       ],
     ]);
     this.validatePayload.bind(this);
@@ -151,4 +145,13 @@ export default class OCPP20IncomingRequestService extends OCPPIncomingRequestSer
     );
     return false;
   }
+
+  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>;
+  }
 }
index 6a7fe370cbf9103e5c98c7942299f248d601401e..d550e4d1bd3c2339397a03d4b42a8b98f9e06b6d 100644 (file)
@@ -36,39 +36,21 @@ export default class OCPP20RequestService extends OCPPRequestService {
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20BootNotificationRequest>,
+        this.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
+        ),
       ],
       [
         OCPP20RequestCommand.HEARTBEAT,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20HeartbeatRequest>,
+        this.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
+          '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
+        ),
       ],
       [
         OCPP20RequestCommand.STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20StatusNotificationRequest>,
+        this.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
+        ),
       ],
     ]);
     this.buildRequestPayload.bind(this);
@@ -156,4 +138,13 @@ export default class OCPP20RequestService extends OCPPRequestService {
         );
     }
   }
+
+  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>;
+  }
 }
index add126e408933e9576c90ea59988057bbe73d2be..f9d8fde9feb7d39d944bb1f5d38cf27b1f474f42 100644 (file)
@@ -50,53 +50,29 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20BootNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
+        ),
       ],
       [
         OCPP20RequestCommand.HEARTBEAT,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20HeartbeatResponse>,
+        this.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
+        ),
       ],
       [
         OCPP20RequestCommand.STATUS_NOTIFICATION,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20StatusNotificationResponse>,
+        this.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
+        ),
       ],
     ]);
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        JSON.parse(
-          fs.readFileSync(
-            path.resolve(
-              path.dirname(fileURLToPath(import.meta.url)),
-              '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
-            ),
-            'utf8'
-          )
-        ) as JSONSchemaType<OCPP20ClearCacheResponse>,
+        this.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
+          '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
+        ),
       ],
     ]);
     this.validatePayload.bind(this);
@@ -210,4 +186,13 @@ export default class OCPP20ResponseService extends OCPPResponseService {
       );
     }
   }
+
+  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>;
+  }
 }