X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16IncomingRequestService.ts;h=73943d4e7f527fa11daf6512a4b7441fdb59081a;hb=d972af76b6d7d1d2a099d254eacf45245b5316ac;hp=27045c89617c023da1b85d17ee6b8c4b9713e95f;hpb=6e3d9d04815abfe85d31735fc38bdf0253d85026;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 27045c89..73943d4e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -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'; @@ -24,7 +24,6 @@ import { type ClearChargingProfileRequest, type ClearChargingProfileResponse, type ConnectorStatus, - ConnectorStatusEnum, ErrorType, type GenericResponse, GenericStatus, @@ -40,7 +39,6 @@ import { type OCPP16BootNotificationRequest, type OCPP16BootNotificationResponse, type OCPP16CancelReservationRequest, - type OCPP16CancelReservationResponse, OCPP16ChargePointErrorCode, OCPP16ChargePointStatus, type OCPP16ChargingProfile, @@ -90,7 +88,6 @@ import { type UnlockConnectorResponse, } from '../../../types'; import { Constants, Utils, logger } from '../../../utils'; -import { OCPPConstants } from '../OCPPConstants'; import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService'; const moduleName = 'OCPP16IncomingRequestService'; @@ -880,7 +877,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { connectorStatus.transactionRemoteStarted = true; const startTransactionPayload: Partial = { connectorId: transactionConnectorId, - idTag: idTag, + idTag, }; if (reserved || reservedOnConnectorZero) { const reservation = chargingStation.getReservationBy( @@ -1087,7 +1084,14 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ).catch(Constants.EMPTY_FUNCTION); } else { setTimeout(() => { - this.updateFirmwareSimulation(chargingStation).catch(Constants.EMPTY_FUNCTION); + this.runInAsyncScope( + this.updateFirmwareSimulation.bind(this) as ( + this: OCPP16IncomingRequestService, + ...args: any[] + ) => Promise, + this, + chargingStation + ).catch(Constants.EMPTY_FUNCTION); }, retrieveDate?.getTime() - now); } return OCPP16Constants.OCPP_RESPONSE_EMPTY; @@ -1267,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, @@ -1304,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) { @@ -1535,56 +1535,60 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { OCPP16IncomingRequestCommand.RESERVE_NOW ) ) { - return OCPPConstants.OCPP_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; } const { reservationId, idTag, connectorId } = commandPayload; let response: OCPP16ReserveNowResponse; try { if (!chargingStation.isConnectorAvailable(connectorId) && connectorId > 0) { - return OCPPConstants.OCPP_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; } if (connectorId === 0 && !chargingStation.getReservationOnConnectorId0Enabled()) { - return OCPPConstants.OCPP_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; } if (!(await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, connectorId, idTag))) { - return OCPPConstants.OCPP_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_RESERVATION_RESPONSE_REJECTED; } switch (chargingStation.getConnectorStatus(connectorId).status) { - case ConnectorStatusEnum.Faulted: - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_FAULTED; + case OCPP16ChargePointStatus.Faulted: + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED; break; - case ConnectorStatusEnum.Occupied: - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_OCCUPIED; + case OCPP16ChargePointStatus.Preparing: + case OCPP16ChargePointStatus.Charging: + case OCPP16ChargePointStatus.SuspendedEV: + case OCPP16ChargePointStatus.SuspendedEVSE: + case OCPP16ChargePointStatus.Finishing: + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; break; - case ConnectorStatusEnum.Unavailable: - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_UNAVAILABLE; + case OCPP16ChargePointStatus.Unavailable: + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_UNAVAILABLE; break; - case ConnectorStatusEnum.Reserved: + case OCPP16ChargePointStatus.Reserved: if (!chargingStation.isConnectorReservable(reservationId, idTag, connectorId)) { - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_OCCUPIED; + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; break; } // eslint-disable-next-line no-fallthrough default: if (!chargingStation.isConnectorReservable(reservationId, idTag)) { - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_OCCUPIED; + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_OCCUPIED; break; } await chargingStation.addReservation({ id: commandPayload.reservationId, ...commandPayload, }); - response = OCPPConstants.OCPP_RESERVATION_RESPONSE_ACCEPTED; + response = OCPP16Constants.OCPP_RESERVATION_RESPONSE_ACCEPTED; break; } return response; } catch (error) { - chargingStation.getConnectorStatus(connectorId).status = ConnectorStatusEnum.Available; + chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.Available; return this.handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.RESERVE_NOW, error as Error, - { errorResponse: OCPPConstants.OCPP_RESERVATION_RESPONSE_FAULTED } + { errorResponse: OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED } ); } } @@ -1592,7 +1596,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { private async handleRequestCancelReservation( chargingStation: ChargingStation, commandPayload: OCPP16CancelReservationRequest - ): Promise { + ): Promise { if ( !OCPP16ServiceUtils.checkFeatureProfile( chargingStation, @@ -1600,7 +1604,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { OCPP16IncomingRequestCommand.CANCEL_RESERVATION ) ) { - return OCPPConstants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; } try { const { reservationId } = commandPayload; @@ -1610,16 +1614,19 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { `${chargingStation.logPrefix()} Reservation with ID ${reservationId} does not exist on charging station` ); - return OCPPConstants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; + return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED; } - await chargingStation.removeReservation(reservation); - return OCPPConstants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED; + await chargingStation.removeReservation( + reservation, + ReservationTerminationReason.RESERVATION_CANCELED + ); + return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED; } catch (error) { return this.handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.CANCEL_RESERVATION, error as Error, - { errorResponse: OCPPConstants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED } + { errorResponse: OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED } ); } }