X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F2.0%2FOCPP20ResponseService.ts;h=704d3a15fa305d4f9c6a07b1f4e0d8215e5113ed;hb=68220b423c52da387fdf41967dd8c738da0ff52e;hp=39baa0e579bb41a7eed3587c9a89c02a3306ca9f;hpb=edd134392e237a3242dc2093341df70244c51472;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 39baa0e5..704d3a15 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -1,56 +1,118 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import type { JSONSchemaType } from 'ajv'; +import type { JSONSchemaType } from 'ajv' -import OCPPError from '../../../exception/OCPPError'; -import type { JsonObject, JsonType } from '../../../types/JsonType'; -import type { OCPP20RequestCommand } from '../../../types/ocpp/2.0/Requests'; -import { ErrorType } from '../../../types/ocpp/ErrorType'; -import type { ResponseHandler } from '../../../types/ocpp/Responses'; -import logger from '../../../utils/Logger'; -import type ChargingStation from '../../ChargingStation'; -import OCPPResponseService from '../OCPPResponseService'; -import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; +import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' +import { type ChargingStation, addConfigurationKey } from '../../../charging-station/index.js' +import { OCPPError } from '../../../exception/index.js' +import { + ErrorType, + type JsonType, + type OCPP20BootNotificationResponse, + type OCPP20ClearCacheResponse, + type OCPP20HeartbeatResponse, + OCPP20IncomingRequestCommand, + OCPP20OptionalVariableName, + OCPP20RequestCommand, + type OCPP20StatusNotificationResponse, + OCPPVersion, + RegistrationStatusEnumType, + type ResponseHandler +} from '../../../types/index.js' +import { logger } from '../../../utils/index.js' +import { OCPPResponseService } from '../OCPPResponseService.js' -const moduleName = 'OCPP20ResponseService'; +const moduleName = 'OCPP20ResponseService' -export default class OCPP20ResponseService extends OCPPResponseService { - private responseHandlers: Map; - private jsonSchemas: Map>; +export class OCPP20ResponseService extends OCPPResponseService { + public jsonIncomingRequestResponseSchemas: Map< + OCPP20IncomingRequestCommand, + JSONSchemaType + > - public constructor() { - if (new.target?.name === moduleName) { - throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); - } - super(); - this.responseHandlers = new Map(); - this.jsonSchemas = new Map>(); - this.validatePayload.bind(this); + private readonly responseHandlers: Map + private readonly jsonSchemas: Map> + + public constructor () { + // if (new.target.name === moduleName) { + // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) + // } + super(OCPPVersion.VERSION_20) + this.responseHandlers = new Map([ + [ + OCPP20RequestCommand.BOOT_NOTIFICATION, + this.handleResponseBootNotification.bind(this) as ResponseHandler + ], + [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this) as ResponseHandler], + [ + OCPP20RequestCommand.STATUS_NOTIFICATION, + this.emptyResponseHandler.bind(this) as ResponseHandler + ] + ]) + this.jsonSchemas = new Map>([ + [ + OCPP20RequestCommand.BOOT_NOTIFICATION, + OCPP20ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/2.0/BootNotificationResponse.json', + moduleName, + 'constructor' + ) + ], + [ + OCPP20RequestCommand.HEARTBEAT, + OCPP20ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/2.0/HeartbeatResponse.json', + moduleName, + 'constructor' + ) + ], + [ + OCPP20RequestCommand.STATUS_NOTIFICATION, + OCPP20ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json', + moduleName, + 'constructor' + ) + ] + ]) + this.jsonIncomingRequestResponseSchemas = new Map([ + [ + OCPP20IncomingRequestCommand.CLEAR_CACHE, + OCPP20ServiceUtils.parseJsonSchemaFile( + 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', + moduleName, + 'constructor' + ) + ] + ]) + this.validatePayload = this.validatePayload.bind(this) as ( + chargingStation: ChargingStation, + commandName: OCPP20RequestCommand, + payload: JsonType + ) => boolean } - public async responseHandler( + public async responseHandler( chargingStation: ChargingStation, commandName: OCPP20RequestCommand, - payload: JsonType, - requestPayload: JsonType + payload: ResType, + requestPayload: ReqType ): Promise { - if ( - chargingStation.isRegistered() === true /* || - commandName === OCPP20RequestCommand.BOOT_NOTIFICATION */ - ) { + if (chargingStation.isRegistered() || commandName === OCPP20RequestCommand.BOOT_NOTIFICATION) { if ( - this.responseHandlers.has(commandName) === true && - OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true + this.responseHandlers.has(commandName) && + OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) ) { try { - this.validatePayload(chargingStation, commandName, payload); - await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); + this.validatePayload(chargingStation, commandName, payload) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload) } catch (error) { logger.error( `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, error - ); - throw error; + ) + throw error } } else { // Throw exception @@ -58,28 +120,28 @@ export default class OCPP20ResponseService extends OCPPResponseService { ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle response PDU ${JSON.stringify( payload, - null, + undefined, 2 )}`, commandName, payload - ); + ) } } else { throw new OCPPError( ErrorType.SECURITY_ERROR, `${commandName} cannot be issued to handle response PDU ${JSON.stringify( payload, - null, + 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 - ); + ) } } - private validatePayload( + private validatePayload ( chargingStation: ChargingStation, commandName: OCPP20RequestCommand, payload: JsonType @@ -88,13 +150,43 @@ export default class OCPP20ResponseService extends OCPPResponseService { return this.validateResponsePayload( chargingStation, commandName, - this.jsonSchemas.get(commandName), + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.jsonSchemas.get(commandName)!, payload - ); + ) } logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` - ); - return false; + `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation` + ) + return false + } + + private handleResponseBootNotification ( + 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)) { + const logMsg = `${chargingStation.logPrefix()} Charging station in '${ + payload.status + }' state on the central server` + payload.status === RegistrationStatusEnumType.REJECTED + ? logger.warn(logMsg) + : logger.info(logMsg) + } else { + logger.error( + `${chargingStation.logPrefix()} Charging station boot notification response received: %j with undefined registration status`, + payload + ) + } } }