// 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';
type ClearChargingProfileRequest,
type ClearChargingProfileResponse,
type ConnectorStatus,
- ConnectorStatusEnum,
ErrorType,
type GenericResponse,
GenericStatus,
type OCPP16BootNotificationRequest,
type OCPP16BootNotificationResponse,
type OCPP16CancelReservationRequest,
- type OCPP16CancelReservationResponse,
OCPP16ChargePointErrorCode,
OCPP16ChargePointStatus,
type OCPP16ChargingProfile,
type UnlockConnectorResponse,
} from '../../../types';
import { Constants, Utils, logger } from '../../../utils';
-import { OCPPConstants } from '../OCPPConstants';
import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService';
const moduleName = 'OCPP16IncomingRequestService';
connectorStatus.transactionRemoteStarted = true;
const startTransactionPayload: Partial<StartTransactionRequest> = {
connectorId: transactionConnectorId,
- idTag: idTag,
+ idTag,
};
if (reserved || reservedOnConnectorZero) {
const reservation = chargingStation.getReservationBy(
).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<void>,
+ this,
+ chargingStation
+ ).catch(Constants.EMPTY_FUNCTION);
}, retrieveDate?.getTime() - now);
}
return OCPP16Constants.OCPP_RESPONSE_EMPTY;
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,
});
});
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) {
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 }
);
}
}
private async handleRequestCancelReservation(
chargingStation: ChargingStation,
commandPayload: OCPP16CancelReservationRequest
- ): Promise<OCPP16CancelReservationResponse> {
+ ): Promise<GenericResponse> {
if (
!OCPP16ServiceUtils.checkFeatureProfile(
chargingStation,
OCPP16IncomingRequestCommand.CANCEL_RESERVATION
)
) {
- return OCPPConstants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED;
+ return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED;
}
try {
const { reservationId } = commandPayload;
`${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 }
);
}
}