X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCCP16IncomingRequestService.ts;h=e806051a449da65b5a4b989492d0050f50d760aa;hb=e060fe587a245ba900b8895f9cf5b8f926bdecb6;hp=ba1f2f7ef274bc034d7a8311b47e11ecd86cd170;hpb=6ed92bc13120d78f84f2182ddbafd3cbcaa37359;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts index ba1f2f7e..e806051a 100644 --- a/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts @@ -1,9 +1,10 @@ import { ChangeAvailabilityRequest, ChangeConfigurationRequest, ClearChargingProfileRequest, GetConfigurationRequest, OCPP16AvailabilityType, OCPP16IncomingRequestCommand, RemoteStartTransactionRequest, RemoteStopTransactionRequest, ResetRequest, SetChargingProfileRequest, UnlockConnectorRequest } from '../../../types/ocpp/1.6/Requests'; -import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, DefaultResponse, GetConfigurationResponse, SetChargingProfileResponse, UnlockConnectorResponse } from '../../../types/ocpp/1.6/Responses'; +import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, GetConfigurationResponse, SetChargingProfileResponse, UnlockConnectorResponse } from '../../../types/ocpp/1.6/Responses'; import { ChargingProfilePurposeType, OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; import { OCPP16AuthorizationStatus, OCPP16StopTransactionReason } from '../../../types/ocpp/1.6/Transaction'; import Constants from '../../../utils/Constants'; +import { DefaultResponse } from '../../../types/ocpp/Responses'; import { ErrorType } from '../../../types/ocpp/ErrorType'; import { MessageType } from '../../../types/ocpp/MessageType'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; @@ -255,44 +256,76 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } private async handleRequestRemoteStartTransaction(commandPayload: RemoteStartTransactionRequest): Promise { - const transactionConnectorID: number = commandPayload.connectorId ? commandPayload.connectorId : 1; - if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorID)) { - if (this.chargingStation.getAuthorizeRemoteTxRequests() && this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags()) { + const transactionConnectorId: number = commandPayload.connectorId; + if (transactionConnectorId) { + await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.PREPARING); + this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING; + if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorId)) { // Check if authorized - if (this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)) { - await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorID, OCPP16ChargePointStatus.PREPARING); - this.chargingStation.getConnector(transactionConnectorID).status = OCPP16ChargePointStatus.PREPARING; - if (commandPayload.chargingProfile && commandPayload.chargingProfile.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { - this.chargingStation.setChargingProfile(transactionConnectorID, commandPayload.chargingProfile); - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at start transaction, dump their stack: %j`, this.chargingStation.getConnector(transactionConnectorID).chargingProfiles); - } else if (commandPayload.chargingProfile && commandPayload.chargingProfile.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { - return Constants.OCPP_RESPONSE_REJECTED; + if (this.chargingStation.getAuthorizeRemoteTxRequests()) { + let authorized = false; + if (this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags() + && this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)) { + authorized = true; } - // Authorization successful start transaction - await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorID, commandPayload.idTag); - logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorID.toString() + ' for idTag ' + commandPayload.idTag); - return Constants.OCPP_RESPONSE_ACCEPTED; + if (!authorized || (authorized && this.chargingStation.getMayAuthorizeAtRemoteStart())) { + const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(transactionConnectorId, commandPayload.idTag); + if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { + authorized = true; + } else { + authorized = false; + } + } + if (authorized) { + // Authorization successful, start transaction + if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { + if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { + logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); + return Constants.OCPP_RESPONSE_ACCEPTED; + } + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); + } + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); + } + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); } - logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag); - return Constants.OCPP_RESPONSE_REJECTED; - } - await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorID, OCPP16ChargePointStatus.PREPARING); - this.chargingStation.getConnector(transactionConnectorID).status = OCPP16ChargePointStatus.PREPARING; - if (commandPayload.chargingProfile && commandPayload.chargingProfile.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { - this.chargingStation.setChargingProfile(transactionConnectorID, commandPayload.chargingProfile); - logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at start transaction, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); - } else if (commandPayload.chargingProfile && commandPayload.chargingProfile.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { - return Constants.OCPP_RESPONSE_REJECTED; + // No authorization check required, start transaction + if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { + if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { + logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); + return Constants.OCPP_RESPONSE_ACCEPTED; + } + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); + } + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); } - // No local authorization check required => start transaction - await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorID, commandPayload.idTag); - logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorID.toString() + ' for idTag ' + commandPayload.idTag); - return Constants.OCPP_RESPONSE_ACCEPTED; + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); } - logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on unavailable connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag); + return await this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); + } + + private async notifyRemoteStartTransactionRejected(connectorId: number, idTag: string): Promise { + if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { + await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); + this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; + } + logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', status ' + this.chargingStation.getConnector(connectorId).status); return Constants.OCPP_RESPONSE_REJECTED; } + private setRemoteStartTransactionChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): boolean { + if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { + this.chargingStation.setChargingProfile(connectorId, cp); + logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnector(connectorId).chargingProfiles); + return true; + } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { + logger.warn(`${this.chargingStation.logPrefix()} Not allowed to set ${cp.chargingProfilePurpose} charging profile(s) at remote start transaction`); + return false; + } else if (!cp) { + return true; + } + } + private async handleRequestRemoteStopTransaction(commandPayload: RemoteStopTransactionRequest): Promise { const transactionId = commandPayload.transactionId; for (const connector in this.chargingStation.connectors) {