refactor: cleanup imports
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 21da005239070a634ba3cb9b253c4eb1b8a769cf..73943d4e7f527fa11daf6512a4b7441fdb59081a 100644 (file)
@@ -1,12 +1,12 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'node:fs';
-import path from 'node:path';
+import { createWriteStream, readdirSync } from 'node:fs';
+import { dirname, join, resolve } from 'node:path';
 import { URL, fileURLToPath } from 'node:url';
 
 import type { JSONSchemaType } from 'ajv';
 import { Client, type FTPResponse } from 'basic-ftp';
-import tar from 'tar';
+import { create } from 'tar';
 
 import { OCPP16Constants } from './OCPP16Constants';
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
@@ -39,7 +39,6 @@ import {
   type OCPP16BootNotificationRequest,
   type OCPP16BootNotificationResponse,
   type OCPP16CancelReservationRequest,
-  type OCPP16CancelReservationResponse,
   OCPP16ChargePointErrorCode,
   OCPP16ChargePointStatus,
   type OCPP16ChargingProfile,
@@ -1272,12 +1271,11 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     if (uri.protocol.startsWith('ftp:')) {
       let ftpClient: Client;
       try {
-        const logFiles = fs
-          .readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'))
+        const logFiles = readdirSync(resolve(dirname(fileURLToPath(import.meta.url)), '../'))
           .filter((file) => file.endsWith('.log'))
-          .map((file) => path.join('./', file));
+          .map((file) => join('./', file));
         const diagnosticsArchive = `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`;
-        tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive));
+        create({ gzip: true }, logFiles).pipe(createWriteStream(diagnosticsArchive));
         ftpClient = new Client();
         const accessResponse = await ftpClient.access({
           host: uri.host,
@@ -1309,10 +1307,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
               });
           });
           uploadResponse = await ftpClient.uploadFrom(
-            path.join(
-              path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'),
-              diagnosticsArchive
-            ),
+            join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), diagnosticsArchive),
             `${uri.pathname}${diagnosticsArchive}`
           );
           if (uploadResponse.code === 226) {
@@ -1601,7 +1596,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
   private async handleRequestCancelReservation(
     chargingStation: ChargingStation,
     commandPayload: OCPP16CancelReservationRequest
-  ): Promise<OCPP16CancelReservationResponse> {
+  ): Promise<GenericResponse> {
     if (
       !OCPP16ServiceUtils.checkFeatureProfile(
         chargingStation,
@@ -1621,7 +1616,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         );
         return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED;
       }
-      await chargingStation.removeReservation(reservation);
+      await chargingStation.removeReservation(
+        reservation,
+        ReservationTerminationReason.RESERVATION_CANCELED
+      );
       return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED;
     } catch (error) {
       return this.handleIncomingRequestError(