X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16IncomingRequestService.ts;h=9e0849e987080be00f57f931e62fcfc9f3b8a3d9;hb=22e0d48e12052aaa7ed3f76fc59cac779ae67a5c;hp=aecc08f786ccfe0d119260cfd2bb498688008230;hpb=3a13fc92f2bc5236dcde66fe5e79e0cea89f1c3d;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 aecc08f7..9e0849e9 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,61 @@ 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 GetConfigurationRequest, + type GetDiagnosticsRequest, OCPP16AvailabilityType, - OCPP16BootNotificationRequest, - OCPP16ClearCacheRequest, - OCPP16HeartbeatRequest, + type OCPP16BootNotificationRequest, + type OCPP16ClearCacheRequest, + type OCPP16DataTransferRequest, + OCPP16DataTransferVendorId, + type OCPP16DiagnosticsStatusNotificationRequest, + OCPP16FirmwareStatus, + type OCPP16FirmwareStatusNotificationRequest, + 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 GetConfigurationResponse, + type GetDiagnosticsResponse, + type OCPP16BootNotificationResponse, + type OCPP16DataTransferResponse, + OCPP16DataTransferStatus, + type OCPP16DiagnosticsStatusNotificationResponse, + type OCPP16FirmwareStatusNotificationResponse, + 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'; @@ -75,20 +85,21 @@ import Utils from '../../../utils/Utils'; import type ChargingStation from '../../ChargingStation'; import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; import { ChargingStationUtils } from '../../ChargingStationUtils'; +import OCPPConstants from '../OCPPConstants'; import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; const moduleName = 'OCPP16IncomingRequestService'; export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { + protected jsonSchemas: Map>; private incomingRequestHandlers: Map; - private jsonSchemas: Map>; public constructor() { 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 +134,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 +282,30 @@ 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, + ], + [ + OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], ]); this.validatePayload.bind(this); } @@ -360,7 +397,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandName: OCPP16IncomingRequestCommand, commandPayload: JsonType ): boolean { - if (this.jsonSchemas.has(commandName)) { + if (this.jsonSchemas.has(commandName) === true) { return this.validateIncomingRequestPayload( chargingStation, commandName, @@ -369,7 +406,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` + `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation` ); return false; } @@ -398,14 +435,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation.stationInfo.resetTime )}` ); - return Constants.OCPP_RESPONSE_ACCEPTED; - } - - private handleRequestClearCache(chargingStation: ChargingStation): DefaultResponse { - chargingStation.authorizedTagsCache.deleteAuthorizedTags( - ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) - ); - return Constants.OCPP_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; } private async handleRequestUnlockConnector( @@ -413,11 +443,17 @@ 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 OCPPConstants.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; + return OCPPConstants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; } if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { const stopResponse = await chargingStation.stopTransactionOnConnector( @@ -425,9 +461,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16StopTransactionReason.UNLOCK_COMMAND ); if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { - return Constants.OCPP_RESPONSE_UNLOCKED; + return OCPPConstants.OCPP_RESPONSE_UNLOCKED; } - return Constants.OCPP_RESPONSE_UNLOCK_FAILED; + return OCPPConstants.OCPP_RESPONSE_UNLOCK_FAILED; } await chargingStation.ocppRequestService.requestHandler< OCPP16StatusNotificationRequest, @@ -438,7 +474,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer errorCode: OCPP16ChargePointErrorCode.NO_ERROR, }); chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; - return Constants.OCPP_RESPONSE_UNLOCKED; + return OCPPConstants.OCPP_RESPONSE_UNLOCKED; } private handleRequestGetConfiguration( @@ -500,9 +536,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer true ); if (!keyToChange) { - return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; + return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; } else if (keyToChange && keyToChange.readonly) { - return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED; + return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_REJECTED; } else if (keyToChange && !keyToChange.readonly) { let valueChanged = false; if (keyToChange.value !== commandPayload.value) { @@ -538,9 +574,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation.restartWebSocketPing(); } if (keyToChange.reboot) { - return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; + return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; } - return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; } } @@ -555,7 +591,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE ) === false ) { - return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; + return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; } if (chargingStation.connectors.has(commandPayload.connectorId) === false) { logger.error( @@ -563,14 +599,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload.connectorId }` ); - return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; + return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } if ( commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && commandPayload.connectorId !== 0 ) { - return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; + return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } if ( commandPayload.csChargingProfiles.chargingProfilePurpose === @@ -584,7 +620,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload.connectorId } without a started transaction` ); - return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; + return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; } OCPP16ServiceUtils.setChargingProfile( chargingStation, @@ -597,7 +633,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer }, dump their stack: %j`, chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles ); - return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; } private handleRequestClearChargingProfile( @@ -611,7 +647,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE ) === false ) { - return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; + return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } if (chargingStation.connectors.has(commandPayload.connectorId) === false) { logger.error( @@ -619,7 +655,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload.connectorId }` ); - return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; + return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId); if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) { @@ -630,7 +666,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer }, dump their stack: %j`, connectorStatus.chargingProfiles ); - return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; } if (!commandPayload.connectorId) { let clearedCP = false; @@ -675,10 +711,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } } if (clearedCP) { - return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; } } - return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; + return OCPPConstants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } private async handleRequestChangeAvailability( @@ -686,24 +722,24 @@ 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()}` ); - return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; + return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_REJECTED; } const chargePointStatus: OCPP16ChargePointStatus = commandPayload.type === OCPP16AvailabilityType.OPERATIVE ? OCPP16ChargePointStatus.AVAILABLE : OCPP16ChargePointStatus.UNAVAILABLE; if (connectorId === 0) { - let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; + let response: ChangeAvailabilityResponse = OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; for (const id of chargingStation.connectors.keys()) { if (chargingStation.getConnectorStatus(id)?.transactionStarted === true) { - response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + response = OCPPConstants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } chargingStation.getConnectorStatus(id).availability = commandPayload.type; - if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { + if (response === OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { await chargingStation.ocppRequestService.requestHandler< OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse @@ -724,7 +760,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ) { if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; - return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; await chargingStation.ocppRequestService.requestHandler< @@ -736,9 +772,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer errorCode: OCPP16ChargePointErrorCode.NO_ERROR, }); chargingStation.getConnectorStatus(connectorId).status = chargePointStatus; - return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; } - return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; + return OCPPConstants.OCPP_AVAILABILITY_RESPONSE_REJECTED; } private async handleRequestRemoteStartTransaction( @@ -821,7 +857,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug(remoteStartTransactionLogMsg); - return Constants.OCPP_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; } return this.notifyRemoteStartTransactionRejected( chargingStation, @@ -862,7 +898,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug(remoteStartTransactionLogMsg); - return Constants.OCPP_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; } return this.notifyRemoteStartTransactionRejected( chargingStation, @@ -919,7 +955,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation.getConnectorStatus(connectorId).status + "'" ); - return Constants.OCPP_RESPONSE_REJECTED; + return OCPPConstants.OCPP_RESPONSE_REJECTED; } private setRemoteStartTransactionChargingProfile( @@ -970,9 +1006,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16StopTransactionReason.REMOTE ); if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { - return Constants.OCPP_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_RESPONSE_ACCEPTED; } - return Constants.OCPP_RESPONSE_REJECTED; + return OCPPConstants.OCPP_RESPONSE_REJECTED; } } logger.warn( @@ -980,7 +1016,94 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ' Trying to remote stop a non existing transaction ' + transactionId.toString() ); - return Constants.OCPP_RESPONSE_REJECTED; + return OCPPConstants.OCPP_RESPONSE_REJECTED; + } + + private handleRequestUpdateFirmware( + chargingStation: ChargingStation, + commandPayload: OCPP16UpdateFirmwareRequest + ): OCPP16UpdateFirmwareResponse { + if ( + OCPP16ServiceUtils.checkFeatureProfile( + chargingStation, + OCPP16SupportedFeatureProfiles.FirmwareManagement, + OCPP16IncomingRequestCommand.UPDATE_FIRMWARE + ) === false + ) { + return OCPPConstants.OCPP_RESPONSE_EMPTY; + } + const retrieveDate = Utils.convertToDate(commandPayload.retrieveDate); + if (retrieveDate.getTime() <= Date.now()) { + this.asyncResource + .runInAsyncScope( + this.updateFirmware.bind(this) as ( + this: OCPP16IncomingRequestService, + ...args: any[] + ) => Promise, + this, + chargingStation + ) + .catch(() => { + /* This is intentional */ + }); + } else { + setTimeout(() => { + this.updateFirmware(chargingStation).catch(() => { + /* Intentional */ + }); + }, retrieveDate.getTime() - Date.now()); + } + return OCPPConstants.OCPP_RESPONSE_EMPTY; + } + + private async updateFirmware( + chargingStation: ChargingStation, + minDelay = 15, + maxDelay = 30 + ): Promise { + chargingStation.stopAutomaticTransactionGenerator(); + for (const connectorId of chargingStation.connectors.keys()) { + if ( + connectorId > 0 && + chargingStation.getConnectorStatus(connectorId).transactionStarted === false + ) { + await chargingStation.ocppRequestService.requestHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.UNAVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); + chargingStation.getConnectorStatus(connectorId).status = + OCPP16ChargePointStatus.UNAVAILABLE; + } + } + await chargingStation.ocppRequestService.requestHandler< + OCPP16FirmwareStatusNotificationRequest, + OCPP16FirmwareStatusNotificationResponse + >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { + status: OCPP16FirmwareStatus.Downloading, + }); + chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Downloading; + await Utils.sleep(Utils.getRandomInteger(minDelay, maxDelay) * 1000); + await chargingStation.ocppRequestService.requestHandler< + OCPP16FirmwareStatusNotificationRequest, + OCPP16FirmwareStatusNotificationResponse + >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { + status: OCPP16FirmwareStatus.Downloaded, + }); + chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Downloaded; + await Utils.sleep(Utils.getRandomInteger(minDelay, maxDelay) * 1000); + await chargingStation.ocppRequestService.requestHandler< + OCPP16FirmwareStatusNotificationRequest, + OCPP16FirmwareStatusNotificationResponse + >(chargingStation, OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { + status: OCPP16FirmwareStatus.Installing, + }); + chargingStation.stationInfo.firmwareStatus = OCPP16FirmwareStatus.Installing; + await Utils.sleep(Utils.getRandomInteger(minDelay, maxDelay) * 1000); + await chargingStation.reset(OCPP16StopTransactionReason.REBOOT); } private async handleRequestGetDiagnostics( @@ -994,15 +1117,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16IncomingRequestCommand.GET_DIAGNOSTICS ) === false ) { - return Constants.OCPP_RESPONSE_EMPTY; + return OCPPConstants.OCPP_RESPONSE_EMPTY; } - logger.debug( - chargingStation.logPrefix() + - ' ' + - OCPP16IncomingRequestCommand.GET_DIAGNOSTICS + - ' request received: %j', - commandPayload - ); const uri = new URL(commandPayload.location); if (uri.protocol.startsWith('ftp:')) { let ftpClient: Client; @@ -1030,8 +1146,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } bytes transferred from diagnostics archive ${info.name}` ); await chargingStation.ocppRequestService.requestHandler< - DiagnosticsStatusNotificationRequest, - DiagnosticsStatusNotificationResponse + OCPP16DiagnosticsStatusNotificationRequest, + OCPP16DiagnosticsStatusNotificationResponse >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.Uploading, }); @@ -1045,8 +1161,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); if (uploadResponse.code === 226) { await chargingStation.ocppRequestService.requestHandler< - DiagnosticsStatusNotificationRequest, - DiagnosticsStatusNotificationResponse + OCPP16DiagnosticsStatusNotificationRequest, + OCPP16DiagnosticsStatusNotificationResponse >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.Uploaded, }); @@ -1072,8 +1188,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } catch (error) { await chargingStation.ocppRequestService.requestHandler< - DiagnosticsStatusNotificationRequest, - DiagnosticsStatusNotificationResponse + OCPP16DiagnosticsStatusNotificationRequest, + OCPP16DiagnosticsStatusNotificationResponse >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.UploadFailed, }); @@ -1084,7 +1200,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation, OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, error as Error, - { errorResponse: Constants.OCPP_RESPONSE_EMPTY } + { errorResponse: OCPPConstants.OCPP_RESPONSE_EMPTY } ); } } else { @@ -1094,12 +1210,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } to transfer the diagnostic logs archive` ); await chargingStation.ocppRequestService.requestHandler< - DiagnosticsStatusNotificationRequest, - DiagnosticsStatusNotificationResponse + OCPP16DiagnosticsStatusNotificationRequest, + OCPP16DiagnosticsStatusNotificationResponse >(chargingStation, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.UploadFailed, }); - return Constants.OCPP_RESPONSE_EMPTY; + return OCPPConstants.OCPP_RESPONSE_EMPTY; } } @@ -1112,22 +1228,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; + return OCPPConstants.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}` - ); - return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED; + if ( + !OCPP16ServiceUtils.isConnectorIdValid( + chargingStation, + OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, + commandPayload.connectorId + ) + ) { + return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED; } try { switch (commandPayload.requestedMessage) { - case MessageTrigger.BootNotification: + case OCPP16MessageTrigger.BootNotification: setTimeout(() => { chargingStation.ocppRequestService .requestHandler( @@ -1143,8 +1263,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer /* This is intentional */ }); }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); - return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; - case MessageTrigger.Heartbeat: + return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; + case OCPP16MessageTrigger.Heartbeat: setTimeout(() => { chargingStation.ocppRequestService .requestHandler( @@ -1159,8 +1279,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer /* This is intentional */ }); }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); - return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; - case MessageTrigger.StatusNotification: + return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; + case OCPP16MessageTrigger.StatusNotification: setTimeout(() => { if (commandPayload?.connectorId) { chargingStation.ocppRequestService @@ -1203,16 +1323,39 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } } }, Constants.OCPP_TRIGGER_MESSAGE_DELAY); - return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; + return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED; default: - return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; + return OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; } } catch (error) { return this.handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, error as Error, - { errorResponse: Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED } + { errorResponse: OCPPConstants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED } + ); + } + } + + 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: OCPPConstants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED } ); } }