X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16ResponseService.ts;h=e3e87d4c59d31180e1b0e1a3cbda1c1482c17d68;hb=06ad945f66c591d91a4c0062e9f39e007b05ba83;hp=1bcce7055b6814d4de4c7344f6b35d2757b9d2ea;hpb=17ac262c08a637a9aef23c350176bf476ad212ef;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 1bcce705..e3e87d4c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -1,47 +1,56 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import { JSONSchemaType } from 'ajv'; + +import OCPPError from '../../../exception/OCPPError'; +import { JsonObject, JsonType } from '../../../types/JsonType'; +import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; +import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; +import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; import { - OCPP16AuthorizationStatus, - OCPP16AuthorizeRequest, - OCPP16AuthorizeResponse, - OCPP16StartTransactionRequest, - OCPP16StartTransactionResponse, - OCPP16StopTransactionRequest, - OCPP16StopTransactionResponse, -} from '../../../types/ocpp/1.6/Transaction'; + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse, +} from '../../../types/ocpp/1.6/MeterValues'; import { OCPP16BootNotificationRequest, OCPP16RequestCommand, OCPP16StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; import { + DiagnosticsStatusNotificationResponse, OCPP16BootNotificationResponse, + OCPP16HeartbeatResponse, OCPP16RegistrationStatus, OCPP16StatusNotificationResponse, } from '../../../types/ocpp/1.6/Responses'; import { - OCPP16MeterValuesRequest, - OCPP16MeterValuesResponse, -} from '../../../types/ocpp/1.6/MeterValues'; - -import type ChargingStation from '../../ChargingStation'; -import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; + OCPP16AuthorizationStatus, + OCPP16AuthorizeRequest, + OCPP16AuthorizeResponse, + OCPP16StartTransactionRequest, + OCPP16StartTransactionResponse, + OCPP16StopTransactionRequest, + OCPP16StopTransactionResponse, +} from '../../../types/ocpp/1.6/Transaction'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { JsonType } from '../../../types/JsonType'; -import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; -import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; -import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; -import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; -import OCPPError from '../../../exception/OCPPError'; -import OCPPResponseService from '../OCPPResponseService'; import { ResponseHandler } from '../../../types/ocpp/Responses'; -import Utils from '../../../utils/Utils'; import logger from '../../../utils/Logger'; +import Utils from '../../../utils/Utils'; +import type ChargingStation from '../../ChargingStation'; +import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; +import { ChargingStationUtils } from '../../ChargingStationUtils'; +import OCPPResponseService from '../OCPPResponseService'; +import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; const moduleName = 'OCPP16ResponseService'; export default class OCPP16ResponseService extends OCPPResponseService { private responseHandlers: Map; + private jsonSchemas: Map>; public constructor() { if (new.target?.name === moduleName) { @@ -50,12 +59,111 @@ export default class OCPP16ResponseService extends OCPPResponseService { super(); this.responseHandlers = new Map([ [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], - [OCPP16RequestCommand.HEARTBEAT, this.handleResponseHeartbeat.bind(this)], + [OCPP16RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)], [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)], [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)], [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)], - [OCPP16RequestCommand.STATUS_NOTIFICATION, this.handleResponseStatusNotification.bind(this)], - [OCPP16RequestCommand.METER_VALUES, this.handleResponseMeterValues.bind(this)], + [OCPP16RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + [OCPP16RequestCommand.METER_VALUES, this.emptyResponseHandler.bind(this)], + [OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + ]); + this.jsonSchemas = new Map>([ + [ + OCPP16RequestCommand.BOOT_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.HEARTBEAT, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.AUTHORIZE, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.START_TRANSACTION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.STOP_TRANSACTION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.STATUS_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.METER_VALUES, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], + [ + OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, + JSON.parse( + fs.readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json' + ), + 'utf8' + ) + ) as JSONSchemaType, + ], ]); } @@ -66,8 +174,23 @@ export default class OCPP16ResponseService extends OCPPResponseService { requestPayload: JsonType ): Promise { if (chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) { - if (this.responseHandlers.has(commandName)) { + if ( + this.responseHandlers.has(commandName) && + ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation) + ) { try { + if (this.jsonSchemas.has(commandName)) { + this.validateResponsePayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + payload + ); + } else { + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.responseHandler: No JSON schema found for command ${commandName} PDU validation` + ); + } await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); } catch (error) { logger.error(chargingStation.logPrefix() + ' Handle request response error: %j', error); @@ -77,23 +200,25 @@ export default class OCPP16ResponseService extends OCPPResponseService { // Throw exception throw new OCPPError( ErrorType.NOT_IMPLEMENTED, - `${commandName} is not implemented to handle request response payload ${JSON.stringify( + `${commandName} is not implemented to handle request response PDU ${JSON.stringify( payload, null, 2 )}`, - commandName + commandName, + payload ); } } else { throw new OCPPError( ErrorType.SECURITY_ERROR, - `${commandName} cannot be issued to handle request response payload ${JSON.stringify( + `${commandName} cannot be issued to handle request response PDU ${JSON.stringify( payload, null, 2 )} while the charging station is not registered on the central server. `, - commandName + commandName, + payload ); } } @@ -137,9 +262,6 @@ export default class OCPP16ResponseService extends OCPPResponseService { } } - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseHeartbeat(): void {} - private handleResponseAuthorize( chargingStation: ChargingStation, payload: OCPP16AuthorizeResponse, @@ -312,7 +434,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId, transactionId: payload.transactionId, - meterValue: chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + meterValue: [chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue], })); await chargingStation.ocppRequestService.requestHandler< OCPP16StatusNotificationRequest, @@ -335,7 +457,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { requestPayload.idTag ); if (chargingStation.stationInfo.powerSharedByConnectors) { - chargingStation.stationInfo.powerDivider++; + chargingStation.powerDivider++; } const configuredMeterValueSampleInterval = ChargingStationConfigurationUtils.getConfigurationKey( @@ -408,11 +530,13 @@ export default class OCPP16ResponseService extends OCPPResponseService { >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId: transactionConnectorId, transactionId: requestPayload.transactionId, - meterValue: OCPP16ServiceUtils.buildTransactionEndMeterValue( - chargingStation, - transactionConnectorId, - requestPayload.meterStop - ), + meterValue: [ + OCPP16ServiceUtils.buildTransactionEndMeterValue( + chargingStation, + transactionConnectorId, + requestPayload.meterStop + ), + ], })); if ( !chargingStation.isChargingStationAvailable() || @@ -441,7 +565,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { OCPP16ChargePointStatus.AVAILABLE; } if (chargingStation.stationInfo.powerSharedByConnectors) { - chargingStation.stationInfo.powerDivider--; + chargingStation.powerDivider--; } logger.info( chargingStation.logPrefix() + @@ -464,10 +588,4 @@ export default class OCPP16ResponseService extends OCPPResponseService { ); } } - - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseStatusNotification(): void {} - - // eslint-disable-next-line @typescript-eslint/no-empty-function - private handleResponseMeterValues(): void {} }