X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=77b5cb586d90ed9752ef911c1b9aa25ba53502cd;hb=b2a0452d368cfc43bbacc4991de59afa64f662a4;hp=658664092d71451b5e088bd3e3390fba6deb69c8;hpb=4598878063d62259a6046a870dfa455b9d0027ca;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 65866409..77b5cb58 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -6,13 +6,15 @@ import { fileURLToPath } from 'url'; import type { JSONSchemaType } from 'ajv'; +import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; import type { JsonObject, JsonType } from '../../../types/JsonType'; import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; import { - type DiagnosticsStatusNotificationRequest, type OCPP16BootNotificationRequest, type OCPP16DataTransferRequest, + type OCPP16DiagnosticsStatusNotificationRequest, + type OCPP16FirmwareStatusNotificationRequest, type OCPP16HeartbeatRequest, OCPP16RequestCommand, type OCPP16StatusNotificationRequest, @@ -26,17 +28,15 @@ import { ErrorType } from '../../../types/ocpp/ErrorType'; import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; import type { RequestParams } from '../../../types/ocpp/Requests'; import Constants from '../../../utils/Constants'; -import logger from '../../../utils/Logger'; import Utils from '../../../utils/Utils'; import type ChargingStation from '../../ChargingStation'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; -import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; const moduleName = 'OCPP16RequestService'; export default class OCPP16RequestService extends OCPPRequestService { - private jsonSchemas: Map>; + protected jsonSchemas: Map>; public constructor(ocppResponseService: OCPPResponseService) { if (new.target?.name === moduleName) { @@ -46,111 +46,63 @@ export default class OCPP16RequestService extends OCPPRequestService { this.jsonSchemas = new Map>([ [ OCPP16RequestCommand.AUTHORIZE, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/Authorize.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/Authorize.json' + ), ], [ OCPP16RequestCommand.BOOT_NOTIFICATION, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/BootNotification.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/BootNotification.json' + ), ], [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json' + ), ], [ OCPP16RequestCommand.HEARTBEAT, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json' + ), ], [ OCPP16RequestCommand.METER_VALUES, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/MeterValues.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/MeterValues.json' + ), ], [ OCPP16RequestCommand.STATUS_NOTIFICATION, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json' + ), ], [ OCPP16RequestCommand.START_TRANSACTION, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json' + ), ], [ OCPP16RequestCommand.STOP_TRANSACTION, - JSON.parse( - fs.readFileSync( - path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json' - ), - 'utf8' - ) - ) as JSONSchemaType, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json' + ), ], [ OCPP16RequestCommand.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, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json' + ), + ], + [ + OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, + this.parseJsonSchemaFile( + '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json' + ), ], ]); this.buildRequestPayload.bind(this); @@ -185,19 +137,6 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } - protected getRequestPayloadValidationSchema( - chargingStation: ChargingStation, - commandName: OCPP16RequestCommand - ): JSONSchemaType | false { - if (this.jsonSchemas.has(commandName) === true) { - return this.jsonSchemas.get(commandName); - } - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.getPayloadValidationSchema: No JSON schema found for command ${commandName} PDU validation` - ); - return false; - } - private buildRequestPayload( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, @@ -236,8 +175,9 @@ export default class OCPP16RequestService extends OCPPRequestService { }), } as unknown as Request; case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: + case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION: return { - status: commandParams?.diagnosticsStatus, + status: commandParams?.status, } as unknown as Request; case OCPP16RequestCommand.HEARTBEAT: return {} as unknown as Request; @@ -262,7 +202,7 @@ export default class OCPP16RequestService extends OCPPRequestService { meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( commandParams?.connectorId as number ), - timestamp: new Date(), + timestamp: commandParams?.timestamp ?? new Date(), } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: connectorId = chargingStation.getConnectorIdByTransactionId( @@ -280,7 +220,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandParams?.idTag ?? chargingStation.getTransactionIdTag(commandParams?.transactionId as number), meterStop: commandParams?.meterStop ?? energyActiveImportRegister, - timestamp: new Date(), + timestamp: commandParams?.timestamp ?? new Date(), reason: commandParams?.reason, ...(chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( @@ -306,4 +246,13 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } } + + private parseJsonSchemaFile(relativePath: string): JSONSchemaType { + return JSON.parse( + fs.readFileSync( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + 'utf8' + ) + ) as JSONSchemaType; + } }