X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F2.0%2FOCPP20ResponseService.ts;h=9851d3339371d8fe3e47cb7a5dcaa835d7bcace0;hb=9388120364d5a2f3b9103f05802e1756d2dad04f;hp=99ca656614c452073bca6732fa3c4ba17fc509e1;hpb=24d15716245469249b57e5aa790147d703d60a4e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 99ca6566..9851d333 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -2,10 +2,10 @@ import type { ValidateFunction } from 'ajv' -import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' -import { type ChargingStation, addConfigurationKey } from '../../../charging-station/index.js' +import { addConfigurationKey, type ChargingStation } from '../../../charging-station/index.js' import { OCPPError } from '../../../exception/index.js' import { + ChargingStationEvents, ErrorType, type JsonType, type OCPP20BootNotificationResponse, @@ -19,25 +19,26 @@ import { RegistrationStatusEnumType, type ResponseHandler } from '../../../types/index.js' -import { logger } from '../../../utils/index.js' +import { isAsyncFunction, logger } from '../../../utils/index.js' import { OCPPResponseService } from '../OCPPResponseService.js' +import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' const moduleName = 'OCPP20ResponseService' export class OCPP20ResponseService extends OCPPResponseService { - public jsonSchemasIncomingRequestResponseValidateFunction: Map< + public incomingRequestResponsePayloadValidateFunctions: Map< OCPP20IncomingRequestCommand, ValidateFunction > - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> private readonly responseHandlers: Map public constructor () { // if (new.target.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) // } - super(OCPPVersion.VERSION_20) + super(OCPPVersion.VERSION_201) this.responseHandlers = new Map([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, @@ -46,7 +47,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler], [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler] ]) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, this.ajv @@ -84,13 +85,13 @@ export class OCPP20ResponseService extends OCPPResponseService { .bind(this) ] ]) - this.jsonSchemasIncomingRequestResponseValidateFunction = new Map< + this.incomingRequestResponsePayloadValidateFunctions = new Map< OCPP20IncomingRequestCommand, ValidateFunction >([ [ OCPP20IncomingRequestCommand.CLEAR_CACHE, - this.ajv + this.ajvIncomingRequest .compile( OCPP20ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', @@ -118,7 +119,18 @@ export class OCPP20ResponseService extends OCPPResponseService { try { this.validatePayload(chargingStation, commandName, payload) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload) + const responseHandler = this.responseHandlers.get(commandName)! + if (isAsyncFunction(responseHandler)) { + await responseHandler(chargingStation, payload, requestPayload) + } else { + ( + responseHandler as ( + chargingStation: ChargingStation, + payload: JsonType, + requestPayload?: JsonType + ) => void + )(chargingStation, payload, requestPayload) + } } catch (error) { logger.error( `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, @@ -146,7 +158,7 @@ export class OCPP20ResponseService extends OCPPResponseService { payload, undefined, 2 - )} while the charging station is not registered on the central server.`, + )} while the charging station is not registered on the central server`, commandName, payload ) @@ -158,7 +170,7 @@ export class OCPP20ResponseService extends OCPPResponseService { commandName: OCPP20RequestCommand, payload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateResponsePayload(chargingStation, commandName, payload) } logger.warn( @@ -171,17 +183,23 @@ export class OCPP20ResponseService extends OCPPResponseService { chargingStation: ChargingStation, payload: OCPP20BootNotificationResponse ): void { - if (payload.status === RegistrationStatusEnumType.ACCEPTED) { - addConfigurationKey( - chargingStation, - OCPP20OptionalVariableName.HeartbeatInterval, - payload.interval.toString(), - {}, - { overwrite: true, save: true } - ) - OCPP20ServiceUtils.startHeartbeatInterval(chargingStation, payload.interval) - } if (Object.values(RegistrationStatusEnumType).includes(payload.status)) { + chargingStation.bootNotificationResponse = payload + if (chargingStation.isRegistered()) { + chargingStation.emit(ChargingStationEvents.registered) + if (chargingStation.inAcceptedState()) { + addConfigurationKey( + chargingStation, + OCPP20OptionalVariableName.HeartbeatInterval, + payload.interval.toString(), + {}, + { overwrite: true, save: true } + ) + chargingStation.emit(ChargingStationEvents.accepted) + } + } else if (chargingStation.inRejectedState()) { + chargingStation.emit(ChargingStationEvents.rejected) + } const logMsg = `${chargingStation.logPrefix()} Charging station in '${ payload.status }' state on the central server` @@ -189,6 +207,7 @@ export class OCPP20ResponseService extends OCPPResponseService { ? logger.warn(logMsg) : logger.info(logMsg) } else { + delete chargingStation.bootNotificationResponse logger.error( `${chargingStation.logPrefix()} Charging station boot notification response received: %j with undefined registration status`, payload