X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16IncomingRequestService.ts;h=7b6fe220b7a00aaa21a3c612e02fc493dfef3cc6;hb=d270cc878c61c42098557a0e03cc1620f74112de;hp=c4b8d156ce2b00d9d32570b7cdb4ff09275e44fa;hpb=fd3c56d100bd07942aa29905b8efd1e47c58c494;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 c4b8d156..7b6fe220 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -1,11 +1,11 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. import fs from 'fs'; import path from 'path'; import { URL, fileURLToPath } from 'url'; import type { JSONSchemaType } from 'ajv'; -import { Client, FTPResponse } from 'basic-ftp'; +import { Client, type FTPResponse } from 'basic-ftp'; import tar from 'tar'; import OCPPError from '../../../exception/OCPPError'; @@ -14,7 +14,7 @@ import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointE import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { ChargingProfilePurposeType, - OCPP16ChargingProfile, + type OCPP16ChargingProfile, } from '../../../types/ocpp/1.6/ChargingProfile'; import { OCPP16StandardParametersKey, @@ -22,51 +22,58 @@ import { } from '../../../types/ocpp/1.6/Configuration'; import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; import { - ChangeAvailabilityRequest, - ChangeConfigurationRequest, - ClearChargingProfileRequest, - DiagnosticsStatusNotificationRequest, - GetConfigurationRequest, - GetDiagnosticsRequest, - MessageTrigger, + type ChangeAvailabilityRequest, + type ChangeConfigurationRequest, + type ClearChargingProfileRequest, + type DiagnosticsStatusNotificationRequest, + type GetConfigurationRequest, + type GetDiagnosticsRequest, OCPP16AvailabilityType, - OCPP16BootNotificationRequest, - OCPP16ClearCacheRequest, - OCPP16HeartbeatRequest, + type OCPP16BootNotificationRequest, + type OCPP16ClearCacheRequest, + type OCPP16DataTransferRequest, + OCPP16DataTransferVendorId, + type OCPP16HeartbeatRequest, OCPP16IncomingRequestCommand, + OCPP16MessageTrigger, OCPP16RequestCommand, - OCPP16StatusNotificationRequest, - OCPP16TriggerMessageRequest, - RemoteStartTransactionRequest, - RemoteStopTransactionRequest, - ResetRequest, - SetChargingProfileRequest, - UnlockConnectorRequest, + type OCPP16StatusNotificationRequest, + type OCPP16TriggerMessageRequest, + type OCPP16UpdateFirmwareRequest, + type RemoteStartTransactionRequest, + type RemoteStopTransactionRequest, + type ResetRequest, + type SetChargingProfileRequest, + type UnlockConnectorRequest, } from '../../../types/ocpp/1.6/Requests'; -import type { - ChangeAvailabilityResponse, - ChangeConfigurationResponse, - ClearChargingProfileResponse, - DiagnosticsStatusNotificationResponse, - GetConfigurationResponse, - GetDiagnosticsResponse, - OCPP16BootNotificationResponse, - OCPP16HeartbeatResponse, - OCPP16StatusNotificationResponse, - OCPP16TriggerMessageResponse, - SetChargingProfileResponse, - UnlockConnectorResponse, +import { + type ChangeAvailabilityResponse, + type ChangeConfigurationResponse, + type ClearChargingProfileResponse, + type DiagnosticsStatusNotificationResponse, + type GetConfigurationResponse, + type GetDiagnosticsResponse, + type OCPP16BootNotificationResponse, + type OCPP16DataTransferResponse, + OCPP16DataTransferStatus, + type OCPP16HeartbeatResponse, + type OCPP16StatusNotificationResponse, + type OCPP16TriggerMessageResponse, + type OCPP16UpdateFirmwareResponse, + type SetChargingProfileResponse, + type UnlockConnectorResponse, } from '../../../types/ocpp/1.6/Responses'; import { OCPP16AuthorizationStatus, - OCPP16AuthorizeRequest, - OCPP16AuthorizeResponse, - OCPP16StartTransactionRequest, - OCPP16StartTransactionResponse, + type OCPP16AuthorizeRequest, + type OCPP16AuthorizeResponse, + type OCPP16StartTransactionRequest, + type OCPP16StartTransactionResponse, OCPP16StopTransactionReason, } from '../../../types/ocpp/1.6/Transaction'; import type { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; import { ErrorType } from '../../../types/ocpp/ErrorType'; +import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; import type { IncomingRequestHandler } from '../../../types/ocpp/Requests'; import type { DefaultResponse } from '../../../types/ocpp/Responses'; import Constants from '../../../utils/Constants'; @@ -88,7 +95,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer if (new.target?.name === moduleName) { throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); } - super(); + super(OCPPVersion.VERSION_16); this.incomingRequestHandlers = new Map([ [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)], [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], @@ -123,6 +130,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ], [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)], [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)], + [OCPP16IncomingRequestCommand.DATA_TRANSFER, this.handleRequestDataTransfer.bind(this)], + // [OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, this.handleRequestUpdateFirmware.bind(this)], ]); this.jsonSchemas = new Map>([ [ @@ -269,6 +278,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ) ) as JSONSchemaType, ], + [ + OCPP16IncomingRequestCommand.DATA_TRANSFER, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], ]); this.validatePayload.bind(this); } @@ -281,8 +302,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ): Promise { let response: JsonType; if ( - chargingStation.getOcppStrictCompliance() && - chargingStation.isInPendingState() && + chargingStation.getOcppStrictCompliance() === true && + chargingStation.isInPendingState() === true && (commandName === OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION || commandName === OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION) ) { @@ -298,12 +319,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } if ( - chargingStation.isRegistered() || - (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState()) + chargingStation.isRegistered() === true || + (chargingStation.getOcppStrictCompliance() === false && + chargingStation.isInUnknownState() === true) ) { if ( - this.incomingRequestHandlers.has(commandName) && - OCPP16ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName) + this.incomingRequestHandlers.has(commandName) === true && + OCPP16ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName) === true ) { try { this.validatePayload(chargingStation, commandName, commandPayload); @@ -378,10 +400,18 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation: ChargingStation, commandPayload: ResetRequest ): DefaultResponse { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - setImmediate(async (): Promise => { - await chargingStation.reset((commandPayload.type + 'Reset') as OCPP16StopTransactionReason); - }); + this.asyncResource + .runInAsyncScope( + chargingStation.reset.bind(chargingStation) as ( + this: ChargingStation, + ...args: any[] + ) => Promise, + chargingStation, + (commandPayload.type + 'Reset') as OCPP16StopTransactionReason + ) + .catch(() => { + /* This is intentional */ + }); logger.info( `${chargingStation.logPrefix()} ${ commandPayload.type @@ -392,7 +422,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_RESPONSE_ACCEPTED; } - private handleRequestClearCache(): DefaultResponse { + private handleRequestClearCache(chargingStation: ChargingStation): DefaultResponse { + chargingStation.authorizedTagsCache.deleteAuthorizedTags( + ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) + ); return Constants.OCPP_RESPONSE_ACCEPTED; } @@ -401,9 +434,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: UnlockConnectorRequest ): Promise { const connectorId = commandPayload.connectorId; + if (chargingStation.connectors.has(connectorId) === false) { + logger.error( + `${chargingStation.logPrefix()} Trying to unlock a non existing connector Id ${connectorId.toString()}` + ); + return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; + } if (connectorId === 0) { logger.error( - chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString() + chargingStation.logPrefix() + ' Trying to unlock connector Id ' + connectorId.toString() ); return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; } @@ -537,15 +576,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: SetChargingProfileRequest ): SetChargingProfileResponse { if ( - !OCPP16ServiceUtils.checkFeatureProfile( + OCPP16ServiceUtils.checkFeatureProfile( chargingStation, OCPP16SupportedFeatureProfiles.SmartCharging, OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE - ) + ) === false ) { return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; } - if (!chargingStation.getConnectorStatus(commandPayload.connectorId)) { + if (chargingStation.connectors.has(commandPayload.connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${ commandPayload.connectorId @@ -567,6 +606,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted === false) ) { + logger.error( + `${chargingStation.logPrefix()} Trying to set transaction charging profile(s) on connector ${ + commandPayload.connectorId + } without a started transaction` + ); return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } OCPP16ServiceUtils.setChargingProfile( @@ -588,16 +632,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: ClearChargingProfileRequest ): ClearChargingProfileResponse { if ( - !OCPP16ServiceUtils.checkFeatureProfile( + OCPP16ServiceUtils.checkFeatureProfile( chargingStation, OCPP16SupportedFeatureProfiles.SmartCharging, OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE - ) + ) === false ) { return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } - const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId); - if (!connectorStatus) { + if (chargingStation.connectors.has(commandPayload.connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${ commandPayload.connectorId @@ -605,6 +648,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } + const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId); if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) { connectorStatus.chargingProfiles = []; logger.debug( @@ -669,7 +713,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: ChangeAvailabilityRequest ): Promise { const connectorId: number = commandPayload.connectorId; - if (!chargingStation.getConnectorStatus(connectorId)) { + if (chargingStation.connectors.has(connectorId) === false) { logger.error( `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}` ); @@ -701,9 +745,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return response; } else if ( connectorId > 0 && - (chargingStation.getConnectorStatus(0).availability === OCPP16AvailabilityType.OPERATIVE || - (chargingStation.getConnectorStatus(0).availability === - OCPP16AvailabilityType.INOPERATIVE && + (chargingStation.isChargingStationAvailable() === true || + (chargingStation.isChargingStationAvailable() === false && commandPayload.type === OCPP16AvailabilityType.INOPERATIVE)) ) { if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { @@ -730,8 +773,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: RemoteStartTransactionRequest ): Promise { const transactionConnectorId = commandPayload.connectorId; - const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId); - if (transactionConnectorId) { + if (chargingStation.connectors.has(transactionConnectorId) === true) { const remoteStartTransactionLogMsg = chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + @@ -749,24 +791,25 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer status: OCPP16ChargePointStatus.PREPARING, errorCode: OCPP16ChargePointErrorCode.NO_ERROR, }); + const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId); connectorStatus.status = OCPP16ChargePointStatus.PREPARING; - if (chargingStation.isChargingStationAvailable() && connectorStatus) { + if (chargingStation.isChargingStationAvailable() === true) { // Check if authorized - if (chargingStation.getAuthorizeRemoteTxRequests()) { + if (chargingStation.getAuthorizeRemoteTxRequests() === true) { let authorized = false; if ( - chargingStation.getLocalAuthListEnabled() && - chargingStation.hasAuthorizedTags() && + chargingStation.getLocalAuthListEnabled() === true && + chargingStation.hasAuthorizedTags() === true && chargingStation.authorizedTagsCache .getAuthorizedTags( ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) ) - .find((value) => value === commandPayload.idTag) + .find((idTag) => idTag === commandPayload.idTag) ) { connectorStatus.localAuthorizeIdTag = commandPayload.idTag; connectorStatus.idTagLocalAuthorized = true; authorized = true; - } else if (chargingStation.getMustAuthorizeAtRemoteStart()) { + } else if (chargingStation.getMustAuthorizeAtRemoteStart() === true) { connectorStatus.authorizeIdTag = commandPayload.idTag; const authorizeResponse: OCPP16AuthorizeResponse = await chargingStation.ocppRequestService.requestHandler< @@ -783,14 +826,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled` ); } - if (authorized) { + if (authorized === true) { // Authorization successful, start transaction if ( this.setRemoteStartTransactionChargingProfile( chargingStation, transactionConnectorId, commandPayload.chargingProfile - ) + ) === true ) { connectorStatus.transactionRemoteStarted = true; if ( @@ -831,7 +874,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation, transactionConnectorId, commandPayload.chargingProfile - ) + ) === true ) { connectorStatus.transactionRemoteStarted = true; if ( @@ -967,16 +1010,38 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_RESPONSE_REJECTED; } + private handleRequestUpdateFirmware( + chargingStation: ChargingStation, + commandPayload: OCPP16UpdateFirmwareRequest + ): OCPP16UpdateFirmwareResponse { + if ( + OCPP16ServiceUtils.checkFeatureProfile( + chargingStation, + OCPP16SupportedFeatureProfiles.FirmwareManagement, + OCPP16IncomingRequestCommand.UPDATE_FIRMWARE + ) === false + ) { + return Constants.OCPP_RESPONSE_EMPTY; + } + logger.debug( + chargingStation.logPrefix() + + ' ' + + OCPP16IncomingRequestCommand.UPDATE_FIRMWARE + + ' request received: %j', + commandPayload + ); + } + private async handleRequestGetDiagnostics( chargingStation: ChargingStation, commandPayload: GetDiagnosticsRequest ): Promise { if ( - !OCPP16ServiceUtils.checkFeatureProfile( + OCPP16ServiceUtils.checkFeatureProfile( chargingStation, OCPP16SupportedFeatureProfiles.FirmwareManagement, OCPP16IncomingRequestCommand.GET_DIAGNOSTICS - ) + ) === false ) { return Constants.OCPP_RESPONSE_EMPTY; } @@ -1096,22 +1161,26 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation, OCPP16SupportedFeatureProfiles.RemoteTrigger, OCPP16IncomingRequestCommand.TRIGGER_MESSAGE + ) || + !OCPP16ServiceUtils.isMessageTriggerSupported( + chargingStation, + commandPayload.requestedMessage ) ) { return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; } - // TODO: factor out the check on connector id - if (commandPayload?.connectorId < 0) { - logger.warn( - `${chargingStation.logPrefix()} ${ - OCPP16IncomingRequestCommand.TRIGGER_MESSAGE - } incoming request received with invalid connectorId ${commandPayload.connectorId}` - ); + if ( + !OCPP16ServiceUtils.isConnectorIdValid( + chargingStation, + OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, + commandPayload.connectorId + ) + ) { return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED; } try { switch (commandPayload.requestedMessage) { - case MessageTrigger.BootNotification: + case OCPP16MessageTrigger.BootNotification: setTimeout(() => { chargingStation.ocppRequestService .requestHandler( @@ -1128,7 +1197,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer }); }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; - case MessageTrigger.Heartbeat: + case OCPP16MessageTrigger.Heartbeat: setTimeout(() => { chargingStation.ocppRequestService .requestHandler( @@ -1144,7 +1213,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer }); }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; - case MessageTrigger.StatusNotification: + case OCPP16MessageTrigger.StatusNotification: setTimeout(() => { if (commandPayload?.connectorId) { chargingStation.ocppRequestService @@ -1200,4 +1269,27 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } } + + private handleRequestDataTransfer( + chargingStation: ChargingStation, + commandPayload: OCPP16DataTransferRequest + ): OCPP16DataTransferResponse { + try { + if (Object.values(OCPP16DataTransferVendorId).includes(commandPayload.vendorId)) { + return { + status: OCPP16DataTransferStatus.ACCEPTED, + }; + } + return { + status: OCPP16DataTransferStatus.UNKNOWN_VENDOR_ID, + }; + } catch (error) { + return this.handleIncomingRequestError( + chargingStation, + OCPP16IncomingRequestCommand.DATA_TRANSFER, + error as Error, + { errorResponse: Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED } + ); + } + } }