From 7671fa0be211e944f027ebd83f3a0ad64c2ef2d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 20 May 2023 21:15:19 +0200 Subject: [PATCH] refactor(simulator): factor out common helpers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .vscode/settings.json | 1 + src/charging-station/Bootstrap.ts | 18 ++-------- src/charging-station/ChargingStation.ts | 4 +-- .../ChargingStationWorkerBroadcastChannel.ts | 12 +++---- .../UIServiceWorkerBroadcastChannel.ts | 6 ++-- .../WorkerBroadcastChannel.ts | 4 +-- src/charging-station/index.ts | 1 - .../ocpp/1.6/OCPP16ResponseService.ts | 3 +- .../ocpp/OCPPRequestService.ts | 22 +++---------- .../ui-services/AbstractUIService.ts | 2 +- src/performance/PerformanceStatistics.ts | 10 ++++-- src/utils/ErrorUtils.ts | 33 ++++++++++++++++++- .../MessageChannelUtils.ts | 2 +- src/utils/index.ts | 1 + 14 files changed, 65 insertions(+), 54 deletions(-) rename src/charging-station/{ => broadcast-channel}/ChargingStationWorkerBroadcastChannel.ts (97%) rename src/charging-station/{ => broadcast-channel}/UIServiceWorkerBroadcastChannel.ts (95%) rename src/charging-station/{ => broadcast-channel}/WorkerBroadcastChannel.ts (96%) rename src/{charging-station => utils}/MessageChannelUtils.ts (97%) diff --git a/.vscode/settings.json b/.vscode/settings.json index b619a9aa..f417510f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,7 @@ "robohydra", "sonarlint", "tsdoc", + "VCAP", "webui" ], "sonarlint.connectedMode.project": { diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 0fd80dde..c94a9204 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -21,7 +21,7 @@ import { type StationTemplateUrl, type Statistics, } from '../types'; -import { Configuration, Utils, logger } from '../utils'; +import { Configuration, ErrorUtils, Utils, logger } from '../utils'; import { type MessageHandler, type WorkerAbstract, WorkerFactory } from '../worker'; const moduleName = 'Bootstrap'; @@ -46,8 +46,8 @@ export class Bootstrap { private constructor() { // Enable unconditionally for now - this.logUnhandledRejection(); - this.logUncaughtException(); + ErrorUtils.handleUnhandledRejection(); + ErrorUtils.handleUncaughtException(); this.initializedCounters = false; this.started = false; this.initializeCounters(); @@ -257,18 +257,6 @@ export class Bootstrap { } } - private logUncaughtException(): void { - process.on('uncaughtException', (error: Error) => { - console.error(chalk.red('Uncaught exception: '), error); - }); - } - - private logUnhandledRejection(): void { - process.on('unhandledRejection', (reason: unknown) => { - console.error(chalk.red('Unhandled rejection: '), reason); - }); - } - private async startChargingStation( index: number, stationTemplateUrl: StationTemplateUrl diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index bf6a183a..5ce061e8 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -10,11 +10,10 @@ import merge from 'just-merge'; import WebSocket, { type RawData } from 'ws'; import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator'; +import { ChargingStationWorkerBroadcastChannel } from './broadcast-channel/ChargingStationWorkerBroadcastChannel'; import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; import { ChargingStationUtils } from './ChargingStationUtils'; -import { ChargingStationWorkerBroadcastChannel } from './ChargingStationWorkerBroadcastChannel'; import { IdTagsCache } from './IdTagsCache'; -import { MessageChannelUtils } from './MessageChannelUtils'; import { OCPP16IncomingRequestService, OCPP16RequestService, @@ -92,6 +91,7 @@ import { DCElectricUtils, ErrorUtils, FileUtils, + MessageChannelUtils, Utils, logger, } from '../utils'; diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts similarity index 97% rename from src/charging-station/ChargingStationWorkerBroadcastChannel.ts rename to src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 30cef34e..b2828305 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -1,8 +1,5 @@ -import type { ChargingStation } from './ChargingStation'; -import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; -import { OCPP16ServiceUtils } from './ocpp'; import { WorkerBroadcastChannel } from './WorkerBroadcastChannel'; -import { BaseError, type OCPPError } from '../exception'; +import { BaseError, type OCPPError } from '../../exception'; import { AuthorizationStatus, type AuthorizeRequest, @@ -36,8 +33,11 @@ import { type StatusNotificationResponse, type StopTransactionRequest, type StopTransactionResponse, -} from '../types'; -import { Constants, Utils, logger } from '../utils'; +} from '../../types'; +import { Constants, Utils, logger } from '../../utils'; +import type { ChargingStation } from '../ChargingStation'; +import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; +import { OCPP16ServiceUtils } from '../ocpp'; const moduleName = 'ChargingStationWorkerBroadcastChannel'; diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts similarity index 95% rename from src/charging-station/UIServiceWorkerBroadcastChannel.ts rename to src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts index 075a92c4..25d62962 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts @@ -1,4 +1,3 @@ -import type { AbstractUIService } from './ui-server/ui-services/AbstractUIService'; import { WorkerBroadcastChannel } from './WorkerBroadcastChannel'; import { type BroadcastChannelResponse, @@ -6,8 +5,9 @@ import { type MessageEvent, type ResponsePayload, ResponseStatus, -} from '../types'; -import { Utils, logger } from '../utils'; +} from '../../types'; +import { Utils, logger } from '../../utils'; +import type { AbstractUIService } from '../ui-server/ui-services/AbstractUIService'; const moduleName = 'UIServiceWorkerBroadcastChannel'; diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts similarity index 96% rename from src/charging-station/WorkerBroadcastChannel.ts rename to src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts index f3c168bd..fd73b1bb 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/WorkerBroadcastChannel.ts @@ -5,8 +5,8 @@ import type { BroadcastChannelResponse, JsonType, MessageEvent, -} from '../types'; -import { Utils, logger } from '../utils'; +} from '../../types'; +import { Utils, logger } from '../../utils'; const moduleName = 'WorkerBroadcastChannel'; diff --git a/src/charging-station/index.ts b/src/charging-station/index.ts index db87a0d9..e31bba09 100644 --- a/src/charging-station/index.ts +++ b/src/charging-station/index.ts @@ -2,4 +2,3 @@ export { Bootstrap } from './Bootstrap'; export type { ChargingStation } from './ChargingStation'; export { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; export { ChargingStationUtils } from './ChargingStationUtils'; -export { MessageChannelUtils } from './MessageChannelUtils'; diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 2aaeb23d..80ed03b6 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -9,7 +9,6 @@ import { type ChargingStation, ChargingStationConfigurationUtils, ChargingStationUtils, - MessageChannelUtils, } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { @@ -50,7 +49,7 @@ import { type SetChargingProfileResponse, type UnlockConnectorResponse, } from '../../../types'; -import { Constants, Utils, logger } from '../../../utils'; +import { Constants, MessageChannelUtils, Utils, logger } from '../../../utils'; import { OCPPResponseService } from '../OCPPResponseService'; const moduleName = 'OCPP16ResponseService'; diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index f6aa567f..06258fe4 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -8,11 +8,9 @@ import type { ChargingStation } from '../../charging-station'; import { OCPPError } from '../../exception'; import { PerformanceStatistics } from '../../performance'; import { - type EmptyObject, type ErrorCallback, type ErrorResponse, ErrorType, - type HandleErrorParams, type IncomingRequestCommand, type JsonObject, type JsonType, @@ -25,7 +23,7 @@ import { type ResponseCallback, type ResponseType, } from '../../types'; -import { Constants, Utils, logger } from '../../utils'; +import { Constants, ErrorUtils, Utils, logger } from '../../utils'; const moduleName = 'OCPPRequestService'; @@ -129,7 +127,7 @@ export abstract class OCPPRequestService { commandName ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error, { + ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, { throwError: true, }); } @@ -151,7 +149,7 @@ export abstract class OCPPRequestService { commandName ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error); + ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error); } } @@ -176,7 +174,7 @@ export abstract class OCPPRequestService { params ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error, { + ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, { throwError: params.throwError, }); } @@ -477,18 +475,6 @@ export abstract class OCPPRequestService { return messageToSend; } - private handleSendMessageError( - chargingStation: ChargingStation, - commandName: RequestCommand | IncomingRequestCommand, - error: Error, - params: HandleErrorParams = { throwError: false } - ): void { - logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); - if (params?.throwError === true) { - throw error; - } - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars public abstract requestHandler( chargingStation: ChargingStation, diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 0aaeb3aa..71b057ab 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -12,7 +12,7 @@ import { } from '../../../types'; import { Utils, logger } from '../../../utils'; import { Bootstrap } from '../../Bootstrap'; -import { UIServiceWorkerBroadcastChannel } from '../../UIServiceWorkerBroadcastChannel'; +import { UIServiceWorkerBroadcastChannel } from '../../broadcast-channel/UIServiceWorkerBroadcastChannel'; import type { AbstractUIServer } from '../AbstractUIServer'; const moduleName = 'AbstractUIService'; diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index c25cde0d..587532b7 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -4,7 +4,6 @@ import { type PerformanceEntry, PerformanceObserver, performance } from 'node:pe import type { URL } from 'node:url'; import { parentPort } from 'node:worker_threads'; -import { MessageChannelUtils } from '../charging-station'; import { type IncomingRequestCommand, MessageType, @@ -12,7 +11,14 @@ import { type Statistics, type TimeSeries, } from '../types'; -import { CircularArray, Configuration, Constants, Utils, logger } from '../utils'; +import { + CircularArray, + Configuration, + Constants, + MessageChannelUtils, + Utils, + logger, +} from '../utils'; export class PerformanceStatistics { private static readonly instances: Map = new Map< diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts index 0589fa19..7efbe514 100644 --- a/src/utils/ErrorUtils.ts +++ b/src/utils/ErrorUtils.ts @@ -2,13 +2,32 @@ import chalk from 'chalk'; import { logger } from './Logger'; import { Utils } from './Utils'; -import type { EmptyObject, FileType, HandleErrorParams } from '../types'; +import type { ChargingStation } from '../charging-station'; +import type { + EmptyObject, + FileType, + HandleErrorParams, + IncomingRequestCommand, + RequestCommand, +} from '../types'; export class ErrorUtils { private constructor() { // This is intentional } + public static handleUncaughtException(): void { + process.on('uncaughtException', (error: Error) => { + console.error(chalk.red('Uncaught exception: '), error); + }); + } + + public static handleUnhandledRejection(): void { + process.on('unhandledRejection', (reason: unknown) => { + console.error(chalk.red('Unhandled rejection: '), reason); + }); + } + public static handleFileException( file: string, fileType: FileType, @@ -40,4 +59,16 @@ export class ErrorUtils { throw error; } } + + public static handleSendMessageError( + chargingStation: ChargingStation, + commandName: RequestCommand | IncomingRequestCommand, + error: Error, + params: HandleErrorParams = { throwError: false } + ): void { + logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); + if (params?.throwError === true) { + throw error; + } + } } diff --git a/src/charging-station/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts similarity index 97% rename from src/charging-station/MessageChannelUtils.ts rename to src/utils/MessageChannelUtils.ts index 5c1d1cac..4e353f4c 100644 --- a/src/charging-station/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -1,4 +1,4 @@ -import type { ChargingStation } from './ChargingStation'; +import type { ChargingStation } from '../charging-station'; import { type ChargingStationData, type ChargingStationWorkerMessage, diff --git a/src/utils/index.ts b/src/utils/index.ts index 32d351bc..03c88c57 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,5 +5,6 @@ export { Configuration } from './Configuration'; export { Constants } from './Constants'; export { ErrorUtils } from './ErrorUtils'; export { FileUtils } from './FileUtils'; +export { MessageChannelUtils } from './MessageChannelUtils'; export { Utils } from './Utils'; export { logger } from './Logger'; -- 2.34.1