From: Jérôme Benoit Date: Tue, 18 Jul 2023 17:52:10 +0000 (+0200) Subject: build(simulator): switch to strict type checking X-Git-Tag: v1.2.18~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a37fc6dc8267e22b2b2d35773525980b81f014e8;p=e-mobility-charging-stations-simulator.git build(simulator): switch to strict type checking Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d0c7b078..bca03a79 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -363,7 +363,10 @@ export class ChargingStation { public getMaximumPower(stationInfo?: ChargingStationInfo): number { const localStationInfo = stationInfo ?? this.stationInfo; // eslint-disable-next-line @typescript-eslint/dot-notation - return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower; + return ( + (localStationInfo['maxPower' as keyof ChargingStationInfo] as number) ?? + localStationInfo.maximumPower + ); } public getConnectorMaximumAvailablePower(connectorId: number): number { @@ -1006,14 +1009,14 @@ export class ChargingStation { if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey] === value) { + if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { return connectorStatus.reservation; } } } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey] === value) { + if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { return connectorStatus.reservation; } } diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 0fae41f7..383db847 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -623,7 +623,7 @@ const warnDeprecatedTemplateKey = ( templateFile: string, logMsgToAppend = '', ): void => { - if (!isUndefined(template[key])) { + if (!isUndefined(template[key as keyof ChargingStationTemplate])) { const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' }`; @@ -637,11 +637,12 @@ const convertDeprecatedTemplateKey = ( deprecatedKey: string, key?: string, ): void => { - if (!isUndefined(template[deprecatedKey])) { + if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) { if (!isUndefined(key)) { - template[key!] = template[deprecatedKey] as unknown; + (template as unknown as Record)[key!] = + template[deprecatedKey as keyof ChargingStationTemplate]; } - delete template[deprecatedKey]; + delete template[deprecatedKey as keyof ChargingStationTemplate]; } }; diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 70d96011..d506637d 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -17,8 +17,8 @@ const moduleName = 'ChargingStationWorker'; * * @param data - workerData */ -const startChargingStation = (data: ChargingStationWorkerData): void => { - new ChargingStation(data.index, data.templateFile).start(); +const startChargingStation = (data?: ChargingStationWorkerData): void => { + new ChargingStation(data!.index, data!.templateFile).start(); }; class ChargingStationWorker extends AsyncResource { @@ -28,7 +28,7 @@ class ChargingStationWorker extends AsyncResource { parentPort?.on('message', (message: WorkerMessage) => { if (message.id === WorkerMessageEvents.startWorkerElement) { this.runInAsyncScope( - startChargingStation.bind(this) as (data: ChargingStationWorkerData) => void, + startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, this, message.data, ); diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index c9c2d84a..84d6961e 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -239,8 +239,8 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne ], ]); 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 { diff --git a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts index d94dfe24..b385be7f 100644 --- a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts @@ -24,8 +24,8 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { constructor(uiService: AbstractUIService) { super(); this.uiService = uiService; - this.onmessage = this.responseHandler.bind(this) as (message: MessageEvent) => void; - this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; + this.onmessage = this.responseHandler.bind(this) as (message: unknown) => void; + this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void; this.responses = new Map(); } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index eaa84704..5fa160c3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -115,49 +115,73 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { // } super(OCPPVersion.VERSION_16); this.incomingRequestHandlers = new Map([ - [OCPP16IncomingRequestCommand.RESET, this.handleRequestReset.bind(this)], - [OCPP16IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], - [OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, this.handleRequestUnlockConnector.bind(this)], + [ + OCPP16IncomingRequestCommand.RESET, + this.handleRequestReset.bind(this) as unknown as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.CLEAR_CACHE, + this.handleRequestClearCache.bind(this) as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, + this.handleRequestUnlockConnector.bind(this) as unknown as IncomingRequestHandler, + ], [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, - this.handleRequestGetConfiguration.bind(this), + this.handleRequestGetConfiguration.bind(this) as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, - this.handleRequestChangeConfiguration.bind(this), + this.handleRequestChangeConfiguration.bind(this) as unknown as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, - this.handleRequestGetCompositeSchedule.bind(this), + this.handleRequestGetCompositeSchedule.bind(this) as unknown as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, - this.handleRequestSetChargingProfile.bind(this), + this.handleRequestSetChargingProfile.bind(this) as unknown as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, - this.handleRequestClearChargingProfile.bind(this), + this.handleRequestClearChargingProfile.bind(this) as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - this.handleRequestChangeAvailability.bind(this), + this.handleRequestChangeAvailability.bind(this) as unknown as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - this.handleRequestRemoteStartTransaction.bind(this), + this.handleRequestRemoteStartTransaction.bind(this) as unknown as IncomingRequestHandler, ], [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, - this.handleRequestRemoteStopTransaction.bind(this), + this.handleRequestRemoteStopTransaction.bind(this) as unknown as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, + this.handleRequestGetDiagnostics.bind(this) as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, + this.handleRequestTriggerMessage.bind(this) as unknown as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.DATA_TRANSFER, + this.handleRequestDataTransfer.bind(this) as unknown as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, + this.handleRequestUpdateFirmware.bind(this) as unknown as IncomingRequestHandler, + ], + [ + OCPP16IncomingRequestCommand.RESERVE_NOW, + this.handleRequestReserveNow.bind(this) as unknown as IncomingRequestHandler, ], - [OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, this.handleRequestGetDiagnostics.bind(this)], - [OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, this.handleRequestTriggerMessage.bind(this)], - [OCPP16IncomingRequestCommand.DATA_TRANSFER, this.handleRequestDataTransfer.bind(this)], - [OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, this.handleRequestUpdateFirmware.bind(this)], - [OCPP16IncomingRequestCommand.RESERVE_NOW, this.handleRequestReserveNow.bind(this)], [ OCPP16IncomingRequestCommand.CANCEL_RESERVATION, - this.handleRequestCancelReservation.bind(this), + this.handleRequestCancelReservation.bind(this) as unknown as IncomingRequestHandler, ], ]); this.jsonSchemas = new Map>([ diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 66f1f6b9..02c03140 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -76,16 +76,34 @@ export class OCPP16ResponseService extends OCPPResponseService { // } super(OCPPVersion.VERSION_16); this.responseHandlers = new Map([ - [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.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.emptyResponseHandler.bind(this)], - [OCPP16RequestCommand.METER_VALUES, this.emptyResponseHandler.bind(this)], - [OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], - [OCPP16RequestCommand.DATA_TRANSFER, this.emptyResponseHandler.bind(this)], - [OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], + [ + OCPP16RequestCommand.BOOT_NOTIFICATION, + this.handleResponseBootNotification.bind(this) as ResponseHandler, + ], + [OCPP16RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this) as ResponseHandler], + [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this) as ResponseHandler], + [ + OCPP16RequestCommand.START_TRANSACTION, + this.handleResponseStartTransaction.bind(this) as ResponseHandler, + ], + [ + OCPP16RequestCommand.STOP_TRANSACTION, + this.handleResponseStopTransaction.bind(this) as ResponseHandler, + ], + [ + OCPP16RequestCommand.STATUS_NOTIFICATION, + this.emptyResponseHandler.bind(this) as ResponseHandler, + ], + [OCPP16RequestCommand.METER_VALUES, this.emptyResponseHandler.bind(this) as ResponseHandler], + [ + OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, + this.emptyResponseHandler.bind(this) as ResponseHandler, + ], + [OCPP16RequestCommand.DATA_TRANSFER, this.emptyResponseHandler.bind(this) as ResponseHandler], + [ + OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, + this.emptyResponseHandler.bind(this) as ResponseHandler, + ], ]); this.jsonSchemas = new Map>([ [ diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index f65e0c6e..c3e708df 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -411,9 +411,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { const phaseValue = `L${phase}-N`; meterValue.sampledValue.push( OCPP16ServiceUtils.buildSampledValue( - (powerPerPhaseSampledValueTemplates[`L${phase}`] as SampledValueTemplate) ?? - powerSampledValueTemplate, - powerMeasurandValues[`L${phase}`] as number, + powerPerPhaseSampledValueTemplates[ + `L${phase}` as keyof MeasurandPerPhaseSampledValueTemplates + ]! ?? powerSampledValueTemplate, + powerMeasurandValues[`L${phase}` as keyof MeasurandPerPhaseSampledValueTemplates], undefined, phaseValue as OCPP16MeterValuePhase, ), @@ -632,9 +633,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { const phaseValue = `L${phase}`; meterValue.sampledValue.push( OCPP16ServiceUtils.buildSampledValue( - (currentPerPhaseSampledValueTemplates[phaseValue] as SampledValueTemplate) ?? - currentSampledValueTemplate, - currentMeasurandValues[phaseValue] as number, + currentPerPhaseSampledValueTemplates[ + phaseValue as keyof MeasurandPerPhaseSampledValueTemplates + ]! ?? currentSampledValueTemplate, + currentMeasurandValues[phaseValue as keyof MeasurandPerPhaseSampledValueTemplates], undefined, phaseValue as OCPP16MeterValuePhase, ), diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 4984b115..47271946 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -40,9 +40,15 @@ export class OCPP20ResponseService extends OCPPResponseService { // } 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)], + [ + OCPP20RequestCommand.BOOT_NOTIFICATION, + this.handleResponseBootNotification.bind(this) as ResponseHandler, + ], + [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this) as ResponseHandler], + [ + OCPP20RequestCommand.STATUS_NOTIFICATION, + this.emptyResponseHandler.bind(this) as ResponseHandler, + ], ]); this.jsonSchemas = new Map>([ [ diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index dcd74472..16d83ee9 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -15,7 +15,6 @@ import { ErrorType, FileType, IncomingRequestCommand, - type JsonObject, type JsonType, MessageTrigger, MessageType, @@ -144,10 +143,14 @@ export class OCPPServiceUtils { } public static convertDateToISOString(obj: T): void { - for (const key in obj as JsonObject) { + for (const key in obj) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion if (obj![key] instanceof Date) { - obj![key] = (obj![key] as Date).toISOString(); + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + (obj![key] as string) = (obj![key] as Date).toISOString(); + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion } else if (obj![key] !== null && typeof obj![key] === 'object') { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion OCPPServiceUtils.convertDateToISOString(obj![key] as T); } } diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index cb02d4a5..103a27ae 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -132,9 +132,9 @@ export class UIHttpServer extends AbstractUIServer { body ?? Constants.EMPTY_FREEZED_OBJECT, ), ) - .then((protocolResponse: ProtocolResponse) => { + .then((protocolResponse: ProtocolResponse | undefined) => { if (!isNullOrUndefined(protocolResponse)) { - this.sendResponse(protocolResponse); + this.sendResponse(protocolResponse!); } }) .catch(Constants.EMPTY_FUNCTION); diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index b28b6df7..befb9854 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -60,9 +60,9 @@ export class UIWebSocketServer extends AbstractUIServer { this.uiServices .get(version) ?.requestHandler(request) - .then((protocolResponse: ProtocolResponse) => { + .then((protocolResponse: ProtocolResponse | undefined) => { if (!isNullOrUndefined(protocolResponse)) { - this.sendResponse(protocolResponse); + this.sendResponse(protocolResponse!); } }) .catch(Constants.EMPTY_FUNCTION); diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 0caf003e..8e359b4d 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -19,34 +19,40 @@ import type { AbstractUIServer } from '../AbstractUIServer'; const moduleName = 'AbstractUIService'; export abstract class AbstractUIService { - protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping: Omit< - Record, - | ProcedureName.START_SIMULATOR - | ProcedureName.STOP_SIMULATOR - | ProcedureName.LIST_CHARGING_STATIONS - > = { - [ProcedureName.START_CHARGING_STATION]: BroadcastChannelProcedureName.START_CHARGING_STATION, - [ProcedureName.STOP_CHARGING_STATION]: BroadcastChannelProcedureName.STOP_CHARGING_STATION, - [ProcedureName.CLOSE_CONNECTION]: BroadcastChannelProcedureName.CLOSE_CONNECTION, - [ProcedureName.OPEN_CONNECTION]: BroadcastChannelProcedureName.OPEN_CONNECTION, - [ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR]: + protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping = new Map< + ProcedureName, + BroadcastChannelProcedureName + >([ + [ProcedureName.START_CHARGING_STATION, BroadcastChannelProcedureName.START_CHARGING_STATION], + [ProcedureName.STOP_CHARGING_STATION, BroadcastChannelProcedureName.STOP_CHARGING_STATION], + [ProcedureName.CLOSE_CONNECTION, BroadcastChannelProcedureName.CLOSE_CONNECTION], + [ProcedureName.OPEN_CONNECTION, BroadcastChannelProcedureName.OPEN_CONNECTION], + [ + ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, - [ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR]: + ], + [ + ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, - [ProcedureName.SET_SUPERVISION_URL]: BroadcastChannelProcedureName.SET_SUPERVISION_URL, - [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION, - [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION, - [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE, - [ProcedureName.BOOT_NOTIFICATION]: BroadcastChannelProcedureName.BOOT_NOTIFICATION, - [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION, - [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT, - [ProcedureName.METER_VALUES]: BroadcastChannelProcedureName.METER_VALUES, - [ProcedureName.DATA_TRANSFER]: BroadcastChannelProcedureName.DATA_TRANSFER, - [ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION]: + ], + [ProcedureName.SET_SUPERVISION_URL, BroadcastChannelProcedureName.SET_SUPERVISION_URL], + [ProcedureName.START_TRANSACTION, BroadcastChannelProcedureName.START_TRANSACTION], + [ProcedureName.STOP_TRANSACTION, BroadcastChannelProcedureName.STOP_TRANSACTION], + [ProcedureName.AUTHORIZE, BroadcastChannelProcedureName.AUTHORIZE], + [ProcedureName.BOOT_NOTIFICATION, BroadcastChannelProcedureName.BOOT_NOTIFICATION], + [ProcedureName.STATUS_NOTIFICATION, BroadcastChannelProcedureName.STATUS_NOTIFICATION], + [ProcedureName.HEARTBEAT, BroadcastChannelProcedureName.HEARTBEAT], + [ProcedureName.METER_VALUES, BroadcastChannelProcedureName.METER_VALUES], + [ProcedureName.DATA_TRANSFER, BroadcastChannelProcedureName.DATA_TRANSFER], + [ + ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION, BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION, - [ProcedureName.FIRMWARE_STATUS_NOTIFICATION]: + ], + [ + ProcedureName.FIRMWARE_STATUS_NOTIFICATION, BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION, - }; + ], + ]); protected readonly requestHandlers: Map; private readonly version: ProtocolVersion; @@ -144,9 +150,7 @@ export abstract class AbstractUIService { ): void { this.sendBroadcastChannelRequest( uuid, - AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping[ - procedureName - ] as BroadcastChannelProcedureName, + AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping.get(procedureName)!, payload, ); } diff --git a/src/charging-station/ui-server/ui-services/UIService001.ts b/src/charging-station/ui-server/ui-services/UIService001.ts index b8ac1c91..e7570c1a 100644 --- a/src/charging-station/ui-server/ui-services/UIService001.ts +++ b/src/charging-station/ui-server/ui-services/UIService001.ts @@ -1,13 +1,13 @@ import { AbstractUIService } from './AbstractUIService'; -import { type ProcedureName, type ProtocolRequestHandler, ProtocolVersion } from '../../../types'; +import { type ProtocolRequestHandler, ProtocolVersion } from '../../../types'; import type { AbstractUIServer } from '../AbstractUIServer'; export class UIService001 extends AbstractUIService { constructor(uiServer: AbstractUIServer) { super(uiServer, ProtocolVersion['0.0.1']); - for (const procedureName in AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping) { + for (const procedureName of AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping.keys()) { this.requestHandlers.set( - procedureName as ProcedureName, + procedureName, this.handleProtocolRequest.bind(this) as ProtocolRequestHandler, ); } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 4aaa2250..cbc9f20d 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -119,16 +119,18 @@ export class Configuration { "Use 'stationTemplateUrls' instead", ); // eslint-disable-next-line @typescript-eslint/dot-notation - !isUndefined(Configuration.getConfigurationData()!['stationTemplateURLs']) && + !isUndefined( + Configuration.getConfigurationData()!['stationTemplateURLs' as keyof ConfigurationData], + ) && (Configuration.getConfigurationData()!.stationTemplateUrls = Configuration.getConfigurationData()![ // eslint-disable-next-line @typescript-eslint/dot-notation - 'stationTemplateURLs' + 'stationTemplateURLs' as keyof ConfigurationData ] as StationTemplateUrl[]); Configuration.getConfigurationData()!.stationTemplateUrls.forEach( (stationTemplateUrl: StationTemplateUrl) => { // eslint-disable-next-line @typescript-eslint/dot-notation - if (!isUndefined(stationTemplateUrl['numberOfStation'])) { + if (!isUndefined(stationTemplateUrl['numberOfStation' as keyof StationTemplateUrl])) { console.error( `${chalk.green(Configuration.logPrefix())} ${chalk.red( `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead`, @@ -147,10 +149,14 @@ export class Configuration { "Use 'supervisionUrls' instead", ); // eslint-disable-next-line @typescript-eslint/dot-notation - if (!isUndefined(Configuration.getConfigurationData()!['supervisionURLs'])) { + if ( + !isUndefined( + Configuration.getConfigurationData()!['supervisionURLs' as keyof ConfigurationData], + ) + ) { Configuration.getConfigurationData()!.supervisionUrls = Configuration.getConfigurationData()![ // eslint-disable-next-line @typescript-eslint/dot-notation - 'supervisionURLs' + 'supervisionURLs' as keyof ConfigurationData ] as string | string[]; } return Configuration.getConfigurationData()?.supervisionUrls; @@ -445,9 +451,14 @@ export class Configuration { ) { if ( sectionName && - !isUndefined(Configuration.getConfigurationData()![sectionName]) && + !isUndefined(Configuration.getConfigurationData()![sectionName as keyof ConfigurationData]) && !isUndefined( - (Configuration.getConfigurationData()![sectionName] as Record)[key], + ( + Configuration.getConfigurationData()![sectionName as keyof ConfigurationData] as Record< + string, + unknown + > + )[key], ) ) { console.error( @@ -457,7 +468,9 @@ export class Configuration { }`, )}`, ); - } else if (!isUndefined(Configuration.getConfigurationData()![key])) { + } else if ( + !isUndefined(Configuration.getConfigurationData()![key as keyof ConfigurationData]) + ) { console.error( `${chalk.green(Configuration.logPrefix())} ${chalk.red( `Deprecated configuration key '${key}' usage${ diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index 6ea286b2..239ec918 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -46,7 +46,10 @@ if (logConfiguration.rotate === true) { export const logger = createLogger({ silent: !logConfiguration.enabled, level: logConfiguration.level, - format: format.combine(format.splat(), (format[logConfiguration.format!] as FormatWrap)()), + format: format.combine( + format.splat(), + (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)(), + ), transports, }); @@ -57,7 +60,10 @@ export const logger = createLogger({ if (logConfiguration.console) { logger.add( new TransportType.Console({ - format: format.combine(format.splat(), (format[logConfiguration.format!] as FormatWrap)()), + format: format.combine( + format.splat(), + (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)(), + ), }), ); } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 49a5fdf9..8d51349a 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -198,7 +198,7 @@ export const isCFEnvironment = (): boolean => { }; export const isIterable = (obj: T): boolean => { - return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; + return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator as keyof T] === 'function' : false; }; const isString = (value: unknown): boolean => { @@ -332,8 +332,12 @@ export const getWebSocketCloseEventStatusString = (code: number): string => { return '(For applications)'; } } - if (!isUndefined(WebSocketCloseEventStatusString[code])) { - return WebSocketCloseEventStatusString[code] as string; + if ( + !isUndefined( + WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString], + ) + ) { + return WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString]; } return '(Unknown)'; }; diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 75fc2f6b..41145f80 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,8 +1,7 @@ import type { EventEmitter } from 'node:events'; import { existsSync } from 'node:fs'; -import type { Worker } from 'node:worker_threads'; -import type { ErrorHandler, ExitHandler, PoolEmitter, PoolInfo } from 'poolifier'; +import type { PoolEmitter, PoolInfo } from 'poolifier'; import { WorkerConstants } from './WorkerConstants'; import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; @@ -44,14 +43,10 @@ export abstract class WorkerAbstract { } this.workerScript = workerScript; this.workerOptions = workerOptions; - this.workerOptions.poolOptions?.messageHandler?.bind(this); - this.workerOptions.poolOptions!.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler - ).bind(this) as ErrorHandler; - this.workerOptions.poolOptions?.onlineHandler?.bind(this); - this.workerOptions.poolOptions!.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler - ).bind(this) as ExitHandler; + this.workerOptions.poolOptions!.errorHandler = + this.workerOptions.poolOptions?.errorHandler ?? defaultErrorHandler; + this.workerOptions.poolOptions!.exitHandler = + this.workerOptions.poolOptions?.exitHandler ?? defaultExitHandler; } /** diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 03d52625..61118a0d 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -16,7 +16,7 @@ import { import { sleep } from './WorkerUtils'; export class WorkerSet extends WorkerAbstract { - public readonly emitter: EventEmitter; + public readonly emitter!: EventEmitter; private readonly workerSet: Set; /** @@ -35,7 +35,7 @@ export class WorkerSet extends WorkerAbstract { ...this.workerOptions.poolOptions, }; this.workerSet = new Set(); - if (this.workerOptions?.poolOptions?.enableEvents) { + if (this.workerOptions.poolOptions?.enableEvents) { this.emitter = new EventEmitter(); } } @@ -110,25 +110,25 @@ export class WorkerSet extends WorkerAbstract { }); worker.on( 'message', - this.workerOptions?.poolOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION, + this.workerOptions.poolOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION, ); worker.on( 'error', - this.workerOptions?.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION, + this.workerOptions.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION, ); worker.on('error', (error) => { this.emitter?.emit(WorkerSetEvents.error, error); - if (this.workerOptions?.poolOptions?.restartWorkerOnError) { + if (this.workerOptions.poolOptions?.restartWorkerOnError) { this.addWorkerSetElement(); } }); worker.on( 'online', - this.workerOptions?.poolOptions?.onlineHandler ?? WorkerConstants.EMPTY_FUNCTION, + this.workerOptions.poolOptions?.onlineHandler ?? WorkerConstants.EMPTY_FUNCTION, ); worker.on( 'exit', - this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION, + this.workerOptions.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION, ); worker.once('exit', () => this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)!), diff --git a/tsconfig-base.json b/tsconfig-base.json index e4810932..5e8e5f44 100644 --- a/tsconfig-base.json +++ b/tsconfig-base.json @@ -28,9 +28,9 @@ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - // "strict": true, /* Enable all strict type-checking options. */ + "strict": true, /* Enable all strict type-checking options. */ // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ - "strictNullChecks": true, /* Enable strict null checks. */ + // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */