X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=bd3e9c9cf998b26acbf0fef91b9e2cdd6f751ea9;hb=84e13ca9efa04e9516d65c99a54ba9b81457bef8;hp=e8122d18688243acb8f876946e96e41cb4ad027d;hpb=25f5a959c137b7d302485e8ef6ca9d4a2f1cb6f6;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index e8122d18..bd3e9c9c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -8,7 +8,13 @@ import { IncomingRequestCommand, RequestCommand, } from '../types/ocpp/Requests'; -import { BootNotificationResponse, RegistrationStatus } from '../types/ocpp/Responses'; +import { + BootNotificationResponse, + HeartbeatResponse, + MeterValuesResponse, + RegistrationStatus, + StatusNotificationResponse, +} from '../types/ocpp/Responses'; import ChargingStationConfiguration, { ConfigurationKey, } from '../types/ChargingStationConfiguration'; @@ -24,6 +30,7 @@ import { VendorDefaultParametersKey, } from '../types/ocpp/Configuration'; import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; +import { StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws'; @@ -52,7 +59,6 @@ import OCPPRequestService from './ocpp/OCPPRequestService'; import { OCPPVersion } from '../types/ocpp/OCPPVersion'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemplates'; -import { StopTransactionReason } from '../types/ocpp/Transaction'; import { SupervisionUrlDistribution } from '../types/ConfigurationData'; import { URL } from 'url'; import Utils from '../utils/Utils'; @@ -63,7 +69,7 @@ import { parentPort } from 'worker_threads'; import path from 'path'; export default class ChargingStation { - public readonly id: string; + public hashId!: string; public readonly stationTemplateFile: string; public authorizedTags: string[]; public stationInfo!: ChargingStationInfo; @@ -74,10 +80,10 @@ export default class ChargingStation { public performanceStatistics!: PerformanceStatistics; public heartbeatSetInterval!: NodeJS.Timeout; public ocppRequestService!: OCPPRequestService; + public bootNotificationResponse!: BootNotificationResponse | null; private readonly index: number; private configurationFile!: string; private bootNotificationRequest!: BootNotificationRequest; - private bootNotificationResponse!: BootNotificationResponse | null; private connectorsConfigurationHash!: string; private ocppIncomingRequestService!: OCPPIncomingRequestService; private readonly messageBuffer: Set; @@ -89,7 +95,6 @@ export default class ChargingStation { private webSocketPingSetInterval!: NodeJS.Timeout; constructor(index: number, stationTemplateFile: string) { - this.id = Utils.generateUUID(); this.index = index; this.stationTemplateFile = stationTemplateFile; this.stopped = false; @@ -396,7 +401,9 @@ export default class ChargingStation { ) { // eslint-disable-next-line @typescript-eslint/no-misused-promises this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.HEARTBEAT + ); }, this.getHeartbeatInterval()); logger.info( this.logPrefix() + @@ -466,11 +473,14 @@ export default class ChargingStation { this.getConnectorStatus(connectorId).transactionId, interval ); - await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, { - connectorId, - transactionId: this.getConnectorStatus(connectorId).transactionId, - meterValue: [meterValue], - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.METER_VALUES, + { + connectorId, + transactionId: this.getConnectorStatus(connectorId).transactionId, + meterValue: [meterValue], + } + ); }, interval ); @@ -568,11 +578,14 @@ export default class ChargingStation { await this.stopMessageSequence(reason); for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: ChargePointStatus.UNAVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: ChargePointStatus.UNAVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE; } } @@ -612,16 +625,10 @@ export default class ChargingStation { }, params: { overwrite?: boolean; save?: boolean } = { overwrite: false, save: false } ): void { - if (!options || Utils.isEmptyObject(options)) { - options = { - readonly: false, - visible: true, - reboot: false, - }; - } - const readonly = options.readonly; - const visible = options.visible; - const reboot = options.reboot; + options = options ?? ({} as { readonly?: boolean; visible?: boolean; reboot?: boolean }); + options.readonly = options?.readonly ?? false; + options.visible = options?.visible ?? true; + options.reboot = options?.reboot ?? false; let keyFound = this.getConfigurationKey(key); if (keyFound && params?.overwrite) { this.deleteConfigurationKey(keyFound.key, { save: false }); @@ -630,10 +637,10 @@ export default class ChargingStation { if (!keyFound) { this.configuration.configurationKey.push({ key, - readonly, + readonly: options.readonly, value, - visible, - reboot, + visible: options.visible, + reboot: options.reboot, }); params?.save && this.saveConfiguration(); } else { @@ -746,6 +753,19 @@ export default class ChargingStation { idSuffix; } + private getRandomSerialNumberSuffix(params?: { + randomBytesLength?: number; + upperCase?: boolean; + }): string { + const randomSerialNumberSuffix = crypto + .randomBytes(params?.randomBytesLength ?? 16) + .toString('hex'); + if (params?.upperCase) { + return randomSerialNumberSuffix.toUpperCase(); + } + return randomSerialNumberSuffix; + } + private buildStationInfo(): ChargingStationInfo { let stationTemplateFromFile: ChargingStationTemplate; try { @@ -771,6 +791,14 @@ export default class ChargingStation { ); this.convertDeprecatedTemplateKey(stationTemplateFromFile, 'supervisionUrl', 'supervisionUrls'); const stationInfo: ChargingStationInfo = stationTemplateFromFile ?? ({} as ChargingStationInfo); + stationInfo.chargePointSerialNumber = + stationTemplateFromFile?.chargePointSerialNumberPrefix && + stationTemplateFromFile.chargePointSerialNumberPrefix; + delete stationInfo.chargePointSerialNumberPrefix; + stationInfo.chargeBoxSerialNumber = + stationTemplateFromFile?.chargeBoxSerialNumberPrefix && + stationTemplateFromFile.chargeBoxSerialNumberPrefix; + delete stationInfo.chargeBoxSerialNumberPrefix; stationInfo.wsOptions = stationTemplateFromFile?.wsOptions ?? {}; if (!Utils.isEmptyArray(stationTemplateFromFile.power)) { stationTemplateFromFile.power = stationTemplateFromFile.power as number[]; @@ -815,23 +843,40 @@ export default class ChargingStation { private initialize(): void { this.stationInfo = this.buildStationInfo(); - this.configurationFile = path.join( - path.resolve(__dirname, '../'), - 'assets/configurations', - this.stationInfo.chargingStationId + '.json' - ); - this.configuration = this.getConfiguration(); - delete this.stationInfo.Configuration; this.bootNotificationRequest = { chargePointModel: this.stationInfo.chargePointModel, chargePointVendor: this.stationInfo.chargePointVendor, - ...(!Utils.isUndefined(this.stationInfo.chargeBoxSerialNumberPrefix) && { - chargeBoxSerialNumber: this.stationInfo.chargeBoxSerialNumberPrefix, + ...(!Utils.isUndefined(this.stationInfo.chargeBoxSerialNumber) && { + chargeBoxSerialNumber: this.stationInfo.chargeBoxSerialNumber, + }), + ...(!Utils.isUndefined(this.stationInfo.chargePointSerialNumber) && { + chargePointSerialNumber: this.stationInfo.chargePointSerialNumber, }), ...(!Utils.isUndefined(this.stationInfo.firmwareVersion) && { firmwareVersion: this.stationInfo.firmwareVersion, }), + ...(!Utils.isUndefined(this.stationInfo.iccid) && { iccid: this.stationInfo.iccid }), + ...(!Utils.isUndefined(this.stationInfo.imsi) && { imsi: this.stationInfo.imsi }), + ...(!Utils.isUndefined(this.stationInfo.meterSerialNumber) && { + meterSerialNumber: this.stationInfo.meterSerialNumber, + }), + ...(!Utils.isUndefined(this.stationInfo.meterType) && { + meterType: this.stationInfo.meterType, + }), }; + this.hashId = crypto + .createHash(Constants.DEFAULT_HASH_ALGORITHM) + .update(JSON.stringify(this.bootNotificationRequest) + this.stationInfo.chargingStationId) + .digest('hex'); + logger.info(`${this.logPrefix()} Charging station hashId '${this.hashId}'`); + this.configurationFile = path.join( + path.resolve(__dirname, '../'), + 'assets', + 'configurations', + this.hashId + '.json' + ); + this.configuration = this.getConfiguration(); + delete this.stationInfo.Configuration; // Build connectors if needed const maxConnectors = this.getMaxNumberOfConnectors(); if (maxConnectors <= 0) { @@ -870,7 +915,7 @@ export default class ChargingStation { this.stationInfo.randomConnectors = true; } const connectorsConfigHash = crypto - .createHash('sha256') + .createHash(Constants.DEFAULT_HASH_ALGORITHM) .update(JSON.stringify(this.stationInfo.Connectors) + maxConnectors.toString()) .digest('hex'); const connectorsConfigChanged = @@ -927,6 +972,8 @@ export default class ChargingStation { this.wsConfiguredConnectionUrl = new URL( this.getConfiguredSupervisionUrl().href + '/' + this.stationInfo.chargingStationId ); + // OCPP parameters + this.initOcppParameters(); switch (this.getOcppVersion()) { case OCPPVersion.VERSION_16: this.ocppIncomingRequestService = @@ -940,8 +987,6 @@ export default class ChargingStation { this.handleUnsupportedVersion(this.getOcppVersion()); break; } - // OCPP parameters - this.initOcppParameters(); if (this.stationInfo.autoRegister) { this.bootNotificationResponse = { currentTime: new Date().toISOString(), @@ -952,7 +997,7 @@ export default class ChargingStation { this.stationInfo.powerDivider = this.getPowerDivider(); if (this.getEnableStatistics()) { this.performanceStatistics = PerformanceStatistics.getInstance( - this.id, + this.hashId, this.stationInfo.chargingStationId, this.wsConnectionUrl ); @@ -1102,21 +1147,22 @@ export default class ChargingStation { // Send BootNotification let registrationRetryCount = 0; do { - this.bootNotificationResponse = (await this.ocppRequestService.sendMessageHandler( - RequestCommand.BOOT_NOTIFICATION, - { - chargePointModel: this.bootNotificationRequest.chargePointModel, - chargePointVendor: this.bootNotificationRequest.chargePointVendor, - chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, - firmwareVersion: this.bootNotificationRequest.firmwareVersion, - chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, - iccid: this.bootNotificationRequest.iccid, - imsi: this.bootNotificationRequest.imsi, - meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, - meterType: this.bootNotificationRequest.meterType, - }, - { skipBufferingOnError: true } - )) as BootNotificationResponse; + this.bootNotificationResponse = + await this.ocppRequestService.sendMessageHandler( + RequestCommand.BOOT_NOTIFICATION, + { + chargePointModel: this.bootNotificationRequest.chargePointModel, + chargePointVendor: this.bootNotificationRequest.chargePointVendor, + chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, + firmwareVersion: this.bootNotificationRequest.firmwareVersion, + chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, + iccid: this.bootNotificationRequest.iccid, + imsi: this.bootNotificationRequest.imsi, + meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, + meterType: this.bootNotificationRequest.meterType, + }, + { skipBufferingOnError: true } + ); if (!this.isInAcceptedState()) { this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++; await Utils.sleep( @@ -1405,7 +1451,7 @@ export default class ChargingStation { private async startMessageSequence(): Promise { if (this.stationInfo.autoRegister) { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.BOOT_NOTIFICATION, { chargePointModel: this.bootNotificationRequest.chargePointModel, @@ -1435,11 +1481,14 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template at startup - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if ( @@ -1448,27 +1497,36 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template after reset - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) { // Send previous status at template reload - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).status, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).status, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); } else { // Send default status - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: ChargePointStatus.AVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: ChargePointStatus.AVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE; } } @@ -1515,18 +1573,24 @@ export default class ChargingStation { connectorId, this.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.METER_VALUES, + { + connectorId, + transactionId, + meterValue: transactionEndMeterValue, + } + ); } - await this.ocppRequestService.sendMessageHandler(RequestCommand.STOP_TRANSACTION, { - transactionId, - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.getTransactionIdTag(transactionId), - reason, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.getTransactionIdTag(transactionId), + reason, + } + ); } } }