X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F2.0%2FOCPP20ResponseService.ts;h=f9d8fde9feb7d39d944bb1f5d38cf27b1f474f42;hb=32e1ad3b16c388b9c6aea81b649fef40dc2d21e1;hp=db320f6136275eaae68942c613a4eb4e9c597d30;hpb=953d6b028e82d6997897802afefbb4ce0d225dee;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 db320f61..f9d8fde9 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -1,12 +1,26 @@ -// 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 { fileURLToPath } from 'url'; 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 { + OCPP20IncomingRequestCommand, + OCPP20RequestCommand, +} from '../../../types/ocpp/2.0/Requests'; +import type { + OCPP20BootNotificationResponse, + OCPP20ClearCacheResponse, + OCPP20HeartbeatResponse, + OCPP20StatusNotificationResponse, +} from '../../../types/ocpp/2.0/Responses'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import type { ResponseHandler } from '../../../types/ocpp/Responses'; +import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; +import { RegistrationStatusEnumType, ResponseHandler } from '../../../types/ocpp/Responses'; import logger from '../../../utils/Logger'; import type ChargingStation from '../../ChargingStation'; import OCPPResponseService from '../OCPPResponseService'; @@ -15,6 +29,11 @@ import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; const moduleName = 'OCPP20ResponseService'; export default class OCPP20ResponseService extends OCPPResponseService { + public jsonIncomingRequestResponseSchemas: Map< + OCPP20IncomingRequestCommand, + JSONSchemaType + >; + private responseHandlers: Map; private jsonSchemas: Map>; @@ -22,9 +41,40 @@ export default class OCPP20ResponseService extends OCPPResponseService { if (new.target?.name === moduleName) { throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); } - super(); - this.responseHandlers = new Map(); - this.jsonSchemas = new Map>(); + super(OCPPVersion.VERSION_20); + this.responseHandlers = new Map([ + [OCPP20RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], + [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)], + [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + ]); + this.jsonSchemas = new Map>([ + [ + OCPP20RequestCommand.BOOT_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json' + ), + ], + [ + OCPP20RequestCommand.HEARTBEAT, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json' + ), + ], + [ + OCPP20RequestCommand.STATUS_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json' + ), + ], + ]); + this.jsonIncomingRequestResponseSchemas = new Map([ + [ + OCPP20IncomingRequestCommand.CLEAR_CACHE, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json' + ), + ], + ]); this.validatePayload.bind(this); } @@ -35,8 +85,8 @@ export default class OCPP20ResponseService extends OCPPResponseService { requestPayload: JsonType ): Promise { if ( - chargingStation.isRegistered() === true /* || - commandName === OCPP20RequestCommand.BOOT_NOTIFICATION */ + chargingStation.isRegistered() === true || + commandName === OCPP20RequestCommand.BOOT_NOTIFICATION ) { if ( this.responseHandlers.has(commandName) === true && @@ -72,7 +122,7 @@ export default class OCPP20ResponseService extends OCPPResponseService { payload, null, 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 ); @@ -84,7 +134,7 @@ export default class OCPP20ResponseService extends OCPPResponseService { commandName: OCPP20RequestCommand, payload: JsonType ): boolean { - if (this.jsonSchemas.has(commandName)) { + if (this.jsonSchemas.has(commandName) === true) { return this.validateResponsePayload( chargingStation, commandName, @@ -93,8 +143,56 @@ export default class OCPP20ResponseService extends OCPPResponseService { ); } 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; } + + private handleResponseBootNotification( + chargingStation: ChargingStation, + payload: OCPP20BootNotificationResponse + ): void { + if (payload.status === RegistrationStatusEnumType.ACCEPTED) { + // ChargingStationConfigurationUtils.addConfigurationKey( + // chargingStation, + // OCPP16StandardParametersKey.HeartbeatInterval, + // payload.interval.toString(), + // {}, + // { overwrite: true, save: true } + // ); + // ChargingStationConfigurationUtils.addConfigurationKey( + // chargingStation, + // OCPP16StandardParametersKey.HeartBeatInterval, + // payload.interval.toString(), + // { visible: false }, + // { overwrite: true, save: true } + // ); + chargingStation.heartbeatSetInterval + ? chargingStation.restartHeartbeat() + : chargingStation.startHeartbeat(); + } + 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 + ); + } + } + + private parseJsonSchemaFile(relativePath: string): JSONSchemaType { + return JSON.parse( + fs.readFileSync( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + 'utf8' + ) + ) as JSONSchemaType; + } }