Allow to specify timestamp in OCPP commands payload with the UI Server
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
index 6a7fe370cbf9103e5c98c7942299f248d601401e..18816f0ede247d613e4ce18079f5c1ffe0ee6394 100644 (file)
@@ -6,6 +6,7 @@ import { fileURLToPath } from 'url';
 
 import type { JSONSchemaType } from 'ajv';
 
+import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
 import OCPPError from '../../../exception/OCPPError';
 import type { JsonObject, JsonType } from '../../../types/JsonType';
 import {
@@ -21,7 +22,6 @@ import Utils from '../../../utils/Utils';
 import type ChargingStation from '../../ChargingStation';
 import OCPPRequestService from '../OCPPRequestService';
 import type OCPPResponseService from '../OCPPResponseService';
-import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
 
 const moduleName = 'OCPP20RequestService';
 
@@ -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);
@@ -140,7 +122,7 @@ export default class OCPP20RequestService extends OCPPRequestService {
         return {} as unknown as Request;
       case OCPP20RequestCommand.STATUS_NOTIFICATION:
         return {
-          timestamp: commandParams?.timestamp,
+          timestamp: commandParams?.timestamp ?? new Date(),
           connectorStatus: commandParams?.connectorStatus,
           evseId: commandParams?.evseId,
           connectorId: commandParams?.connectorId,
@@ -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>;
+  }
 }