X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fbroadcast-channel%2FChargingStationWorkerBroadcastChannel.ts;h=422c964bcbedd232d28f1a7005efc09c7e5e989b;hb=b85cef4cba7e4d2294940aef29a9f74edf800dac;hp=9eb69cd3ebdf5fde8f2cbebbd0fc35445352228b;hpb=9bf0ef23c51160abc6866ad8d07eea85e308edb8;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 9eb69cd3..422c964b 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -15,6 +15,7 @@ import { DataTransferStatus, type DiagnosticsStatusNotificationRequest, type DiagnosticsStatusNotificationResponse, + type EmptyObject, type FirmwareStatusNotificationRequest, type FirmwareStatusNotificationResponse, type HeartbeatRequest, @@ -36,25 +37,22 @@ import { } from '../../types'; import { Constants, convertToInt, isEmptyObject, isNullOrUndefined, logger } from '../../utils'; import type { ChargingStation } from '../ChargingStation'; -import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; +import { getConfigurationKey } from '../ChargingStationConfigurationUtils'; import { OCPP16ServiceUtils } from '../ocpp'; const moduleName = 'ChargingStationWorkerBroadcastChannel'; type CommandResponse = + | EmptyObject | StartTransactionResponse | StopTransactionResponse | AuthorizeResponse | BootNotificationResponse - | StatusNotificationResponse | HeartbeatResponse - | MeterValuesResponse - | DataTransferResponse - | DiagnosticsStatusNotificationResponse - | FirmwareStatusNotificationResponse; + | DataTransferResponse; type CommandHandler = ( - requestPayload?: BroadcastChannelRequestPayload + requestPayload?: BroadcastChannelRequestPayload, ) => Promise | void; export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { @@ -114,12 +112,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne RequestCommand.STOP_TRANSACTION, { meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( - requestPayload.transactionId, - true + requestPayload!.transactionId!, + true, ), ...requestPayload, }, - requestParams + requestParams, ), ], [ @@ -147,7 +145,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne { skipBufferingOnError: true, throwError: true, - } + }, ); return this.chargingStation.bootNotificationResponse; }, @@ -162,7 +160,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne this.chargingStation, RequestCommand.STATUS_NOTIFICATION, requestPayload, - requestParams + requestParams, ), ], [ @@ -176,11 +174,10 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne [ BroadcastChannelProcedureName.METER_VALUES, async (requestPayload?: BroadcastChannelRequestPayload) => { - const configuredMeterValueSampleInterval = - ChargingStationConfigurationUtils.getConfigurationKey( - chargingStation, - StandardParametersKey.MeterValueSampleInterval - ); + const configuredMeterValueSampleInterval = getConfigurationKey( + chargingStation, + StandardParametersKey.MeterValueSampleInterval, + ); return this.chargingStation.ocppRequestService.requestHandler< MeterValuesRequest, MeterValuesResponse @@ -192,17 +189,17 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne // FIXME: Implement OCPP version agnostic helpers OCPP16ServiceUtils.buildMeterValue( this.chargingStation, - requestPayload.connectorId, - this.chargingStation.getConnectorStatus(requestPayload.connectorId) - ?.transactionId, + requestPayload!.connectorId!, + this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)! + .transactionId!, configuredMeterValueSampleInterval ? convertToInt(configuredMeterValueSampleInterval.value) * 1000 - : Constants.DEFAULT_METER_VALUES_INTERVAL + : Constants.DEFAULT_METER_VALUES_INTERVAL, ), ], ...requestPayload, }, - requestParams + requestParams, ); }, ], @@ -224,7 +221,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne this.chargingStation, RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, requestPayload, - requestParams + requestParams, ), ], [ @@ -237,13 +234,13 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne this.chargingStation, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, requestPayload, - requestParams + requestParams, ), ], ]); this.chargingStation = chargingStation; - this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void; - this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; + this.onmessage = this.requestHandler.bind(this) as (message: unknown) => void; + this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void; } private async requestHandler(messageEvent: MessageEvent): Promise { @@ -256,19 +253,19 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne } const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest; if ( - !isNullOrUndefined(requestPayload?.hashIds) && - requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false + !isNullOrUndefined(requestPayload.hashIds) && + requestPayload.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false ) { return; } - if (!isNullOrUndefined(requestPayload?.hashId)) { + if (!isNullOrUndefined(requestPayload.hashId)) { logger.error( - `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead` + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`, ); return; } - let responsePayload: BroadcastChannelResponsePayload; - let commandResponse: CommandResponse | void; + let responsePayload: BroadcastChannelResponsePayload | undefined; + let commandResponse: CommandResponse | void | undefined; try { commandResponse = await this.commandHandler(command, requestPayload); if (isNullOrUndefined(commandResponse) || isEmptyObject(commandResponse as CommandResponse)) { @@ -280,13 +277,13 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne responsePayload = this.commandResponseToResponsePayload( command, requestPayload, - commandResponse as CommandResponse + commandResponse as CommandResponse, ); } } catch (error) { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, - error + error, ); responsePayload = { hashId: this.chargingStation.stationInfo.hashId, @@ -299,31 +296,31 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne errorDetails: (error as OCPPError).details, }; } finally { - this.sendResponse([uuid, responsePayload]); + this.sendResponse([uuid, responsePayload!]); } } private messageErrorHandler(messageEvent: MessageEvent): void { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, - messageEvent + messageEvent, ); } private async commandHandler( command: BroadcastChannelProcedureName, - requestPayload: BroadcastChannelRequestPayload + requestPayload: BroadcastChannelRequestPayload, ): Promise { if (this.commandHandlers.has(command) === true) { this.cleanRequestPayload(command, requestPayload); - return this.commandHandlers.get(command)(requestPayload); + return this.commandHandlers.get(command)!(requestPayload); } throw new BaseError(`Unknown worker broadcast channel command: ${command}`); } private cleanRequestPayload( command: BroadcastChannelProcedureName, - requestPayload: BroadcastChannelRequestPayload + requestPayload: BroadcastChannelRequestPayload, ): void { delete requestPayload.hashId; delete requestPayload.hashIds; @@ -336,7 +333,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne private commandResponseToResponsePayload( command: BroadcastChannelProcedureName, requestPayload: BroadcastChannelRequestPayload, - commandResponse: CommandResponse + commandResponse: CommandResponse, ): BroadcastChannelResponsePayload { const responseStatus = this.commandResponseToResponseStatus(command, commandResponse); if (responseStatus === ResponseStatus.SUCCESS) { @@ -356,7 +353,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne private commandResponseToResponseStatus( command: BroadcastChannelProcedureName, - commandResponse: CommandResponse + commandResponse: CommandResponse, ): ResponseStatus { switch (command) { case BroadcastChannelProcedureName.START_TRANSACTION: