From: Jérôme Benoit Date: Mon, 31 Jul 2023 12:21:45 +0000 (+0200) Subject: refactor: factor out change availability helper X-Git-Tag: v1.2.20~62 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=366f75f699490d7e6a49714a0517b9ffe7176052;p=e-mobility-charging-stations-simulator.git refactor: factor out change availability helper Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index eb73f002..d3b53bd2 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -104,7 +104,7 @@ import { RegistrationStatusEnumType, RequestCommand, type Reservation, - type ReservationFilterKey, + type ReservationKey, ReservationTerminationReason, type Response, StandardParametersKey, @@ -988,7 +988,7 @@ export class ChargingStation { } public getReservationBy( - filterKey: ReservationFilterKey, + filterKey: ReservationKey, value: number | string, ): Reservation | undefined { if (this.hasEvses) { @@ -1076,10 +1076,10 @@ export class ChargingStation { } } - private restartReservationExpiryDateSetInterval(): void { - this.stopReservationExpirationSetInterval(); - this.startReservationExpirationSetInterval(); - } + // private restartReservationExpiryDateSetInterval(): void { + // this.stopReservationExpirationSetInterval(); + // this.startReservationExpirationSetInterval(); + // } private getNumberOfReservableConnectors(): number { let reservableConnectors = 0; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 356e453b..72d1457c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -19,13 +19,10 @@ import { } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { - type ChangeAvailabilityRequest, - type ChangeAvailabilityResponse, type ChangeConfigurationRequest, type ChangeConfigurationResponse, type ClearChargingProfileRequest, type ClearChargingProfileResponse, - type ConnectorStatus, ErrorType, type GenericResponse, GenericStatus, @@ -41,6 +38,8 @@ import { type OCPP16BootNotificationRequest, type OCPP16BootNotificationResponse, type OCPP16CancelReservationRequest, + type OCPP16ChangeAvailabilityRequest, + type OCPP16ChangeAvailabilityResponse, OCPP16ChargePointErrorCode, OCPP16ChargePointStatus, type OCPP16ChargingProfile, @@ -259,7 +258,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - OCPP16ServiceUtils.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json', moduleName, 'constructor', @@ -757,8 +756,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { private async handleRequestChangeAvailability( chargingStation: ChargingStation, - commandPayload: ChangeAvailabilityRequest, - ): Promise { + commandPayload: OCPP16ChangeAvailabilityRequest, + ): Promise { const connectorId: number = commandPayload.connectorId; if (chargingStation.hasConnector(connectorId) === false) { logger.error( @@ -772,33 +771,29 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ? OCPP16ChargePointStatus.Available : OCPP16ChargePointStatus.Unavailable; if (connectorId === 0) { - let response: ChangeAvailabilityResponse = - OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; - const changeAvailability = async (id: number, connectorStatus: ConnectorStatus) => { - if (connectorStatus?.transactionStarted === true) { - response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; - } - connectorStatus.availability = commandPayload.type; - if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - id, - chargePointStatus, - ); - } - }; + let response: OCPP16ChangeAvailabilityResponse; if (chargingStation.hasEvses) { for (const evseStatus of chargingStation.evses.values()) { - for (const [id, connectorStatus] of evseStatus.connectors) { - await changeAvailability(id, connectorStatus); + for (const id of evseStatus.connectors.keys()) { + response = await OCPP16ServiceUtils.changeAvailability( + chargingStation, + id, + chargePointStatus, + commandPayload.type, + ); } } } else { for (const id of chargingStation.connectors.keys()) { - await changeAvailability(id, chargingStation.getConnectorStatus(id)!); + response = await OCPP16ServiceUtils.changeAvailability( + chargingStation, + id, + chargePointStatus, + commandPayload.type, + ); } } - return response; + return response!; } else if ( connectorId > 0 && (chargingStation.isChargingStationAvailable() === true || diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 0f697f63..ae8b6287 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -14,7 +14,6 @@ import { } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { - type ChangeAvailabilityResponse, type ChangeConfigurationResponse, type ClearChargingProfileResponse, ErrorType, @@ -27,6 +26,7 @@ import { type OCPP16AuthorizeRequest, type OCPP16AuthorizeResponse, type OCPP16BootNotificationResponse, + type OCPP16ChangeAvailabilityResponse, OCPP16ChargePointStatus, type OCPP16DataTransferResponse, type OCPP16DiagnosticsStatusNotificationResponse, @@ -209,7 +209,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - OCPP16ServiceUtils.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json', moduleName, 'constructor', diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 00594e1c..cf055beb 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -2,6 +2,7 @@ import type { JSONSchemaType } from 'ajv'; +import { OCPP16Constants } from './OCPP16Constants'; import { type ChargingStation, hasFeatureProfile } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { @@ -14,6 +15,9 @@ import { MeterValueContext, MeterValueLocation, MeterValueUnit, + OCPP16AvailabilityType, + type OCPP16ChangeAvailabilityResponse, + OCPP16ChargePointStatus, type OCPP16ChargingProfile, type OCPP16IncomingRequestCommand, type OCPP16MeterValue, @@ -788,6 +792,29 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { return meterValues; } + public static changeAvailability = async ( + chargingStation: ChargingStation, + connectorId: number, + chargePointStatus: OCPP16ChargePointStatus, + availabilityType: OCPP16AvailabilityType, + ): Promise => { + let response: OCPP16ChangeAvailabilityResponse = + OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; + const connectorStatus = chargingStation.getConnectorStatus(connectorId)!; + if (connectorStatus?.transactionStarted === true) { + response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + } + connectorStatus.availability = availabilityType; + if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { + await OCPP16ServiceUtils.sendAndSetConnectorStatus( + chargingStation, + connectorId, + chargePointStatus, + ); + } + return response; + }; + public static setChargingProfile( chargingStation: ChargingStation, connectorId: number, diff --git a/src/types/index.ts b/src/types/index.ts index 11f1138c..57437830 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -80,7 +80,7 @@ export { type MessageEvent, } from './WorkerBroadcastChannel'; export { - type ChangeAvailabilityRequest, + type OCPP16ChangeAvailabilityRequest, type ChangeConfigurationRequest, type ClearChargingProfileRequest, type GetConfigurationRequest, @@ -110,7 +110,7 @@ export { type OCPP16CancelReservationRequest, } from './ocpp/1.6/Requests'; export { - type ChangeAvailabilityResponse, + type OCPP16ChangeAvailabilityResponse, type ChangeConfigurationResponse, type ClearChargingProfileResponse, type GetConfigurationResponse, @@ -257,6 +257,6 @@ export { } from './WebSocket'; export { type Reservation, - type ReservationFilterKey, + type ReservationKey, ReservationTerminationReason, } from './ocpp/Reservation'; diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts index 39e8a9bb..c6f041e5 100644 --- a/src/types/ocpp/1.6/Requests.ts +++ b/src/types/ocpp/1.6/Requests.ts @@ -119,7 +119,7 @@ export enum OCPP16AvailabilityType { Operative = 'Operative', } -export interface ChangeAvailabilityRequest extends JsonObject { +export interface OCPP16ChangeAvailabilityRequest extends JsonObject { connectorId: number; type: OCPP16AvailabilityType; } diff --git a/src/types/ocpp/1.6/Responses.ts b/src/types/ocpp/1.6/Responses.ts index 2e5e285b..c98c03a3 100644 --- a/src/types/ocpp/1.6/Responses.ts +++ b/src/types/ocpp/1.6/Responses.ts @@ -65,7 +65,7 @@ export enum OCPP16AvailabilityStatus { SCHEDULED = 'Scheduled', } -export interface ChangeAvailabilityResponse extends JsonObject { +export interface OCPP16ChangeAvailabilityResponse extends JsonObject { status: OCPP16AvailabilityStatus; } diff --git a/src/types/ocpp/Reservation.ts b/src/types/ocpp/Reservation.ts index 0d79375e..cddc3169 100644 --- a/src/types/ocpp/Reservation.ts +++ b/src/types/ocpp/Reservation.ts @@ -10,4 +10,4 @@ export enum ReservationTerminationReason { REPLACE_EXISTING = 'ReplaceExisting', } -export type ReservationFilterKey = keyof OCPP16ReserveNowRequest; +export type ReservationKey = keyof OCPP16ReserveNowRequest;