From ba7965c4b933e479b36fc81f934a643336efcbea Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 11 Sep 2022 15:11:40 +0200 Subject: [PATCH] Improve and unify log messages MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 28 ++++++++++++------- src/charging-station/Bootstrap.ts | 4 +-- src/charging-station/ChargingStation.ts | 17 +++++------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index a09ee7a1..03297e4a 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -60,7 +60,7 @@ export default class AutomaticTransactionGenerator { public start(): void { if (this.started === true) { - logger.warn(`${this.logPrefix()} trying to start while already started`); + logger.warn(`${this.logPrefix()} is already started`); return; } this.startConnectors(); @@ -69,7 +69,7 @@ export default class AutomaticTransactionGenerator { public stop(): void { if (this.started === false) { - logger.warn(`${this.logPrefix()} trying to stop while not started`); + logger.warn(`${this.logPrefix()} is already stopped`); return; } this.stopConnectors(); @@ -78,7 +78,7 @@ export default class AutomaticTransactionGenerator { public startConnector(connectorId: number): void { if (this.chargingStation.connectors.has(connectorId) === false) { - logger.warn(`${this.logPrefix(connectorId)} trying to start on non existing connector`); + logger.warn(`${this.logPrefix(connectorId)} starting on non existing connector`); return; } if (this.connectorsStatus.get(connectorId)?.start === false) { @@ -89,15 +89,23 @@ export default class AutomaticTransactionGenerator { }); }); } else if (this.connectorsStatus.get(connectorId)?.start === true) { - logger.warn(`${this.logPrefix(connectorId)} already started on connector`); + logger.warn(`${this.logPrefix(connectorId)} is already started on connector`); } } public stopConnector(connectorId: number): void { - this.connectorsStatus.set(connectorId, { - ...this.connectorsStatus.get(connectorId), - start: false, - }); + if (this.chargingStation.connectors.has(connectorId) === false) { + logger.warn(`${this.logPrefix(connectorId)} stopping on non existing connector`); + return; + } + if (this.connectorsStatus.get(connectorId)?.start === true) { + this.connectorsStatus.set(connectorId, { + ...this.connectorsStatus.get(connectorId), + start: false, + }); + } else if (this.connectorsStatus.get(connectorId)?.start === false) { + logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`); + } } private startConnectors(): void { @@ -286,7 +294,7 @@ export default class AutomaticTransactionGenerator { const idTag = this.chargingStation.getRandomIdTag(); const startTransactionLogMsg = `${this.logPrefix( connectorId - )} start transaction for idTag '${idTag}'`; + )} start transaction with an idTag '${idTag}'`; if (this.getRequireAuthorize()) { this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag; // Authorize idTag @@ -358,7 +366,7 @@ export default class AutomaticTransactionGenerator { } else { const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; logger.warn( - `${this.logPrefix(connectorId)} trying to stop a not started transaction${ + `${this.logPrefix(connectorId)} stopping a not started transaction${ transactionId ? ' ' + transactionId.toString() : '' }` ); diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index d0c40162..eb437ab0 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -147,10 +147,10 @@ export class Bootstrap { this.workerImplementation = null; this.uiServer?.stop(); await this.storage?.close(); + this.started = false; } else { - console.error(chalk.red('Trying to stop the charging stations simulator while not started')); + console.error(chalk.red('Cannot stop a not started charging stations simulator')); } - this.started = false; } public async restart(): Promise { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9d7bdae7..32cbc671 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1471,7 +1471,7 @@ export default class ChargingStation { // Outcome Message case MessageType.CALL_RESULT_MESSAGE: [, , commandPayload] = request as Response; - if (!this.requests.has(messageId)) { + if (this.requests.has(messageId) === false) { // Error throw new OCPPError( ErrorType.INTERNAL_ERROR, @@ -1502,7 +1502,7 @@ export default class ChargingStation { // Error Message case MessageType.CALL_ERROR_MESSAGE: [, , errorType, errorMessage, errorDetails] = request as ErrorResponse; - if (!this.requests.has(messageId)) { + if (this.requests.has(messageId) === false) { // Error throw new OCPPError( ErrorType.INTERNAL_ERROR, @@ -1539,7 +1539,7 @@ export default class ChargingStation { parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this)); } else { throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming message is not an array', null, { - payload: request, + request, }); } } catch (error) { @@ -1552,7 +1552,7 @@ export default class ChargingStation { )}' processing error:`, error ); - if (!(error instanceof OCPPError)) { + if (error instanceof OCPPError === false) { logger.warn( `${this.logPrefix()} Error thrown at incoming OCPP command '${ commandName ?? requestCommandName ?? null @@ -1560,14 +1560,15 @@ export default class ChargingStation { error ); } - // Send error - messageType === MessageType.CALL_MESSAGE && - (await this.ocppRequestService.sendError( + if (messageType === MessageType.CALL_MESSAGE) { + // Send error + await this.ocppRequestService.sendError( this, messageId, error as OCPPError, commandName ?? requestCommandName ?? null - )); + ); + } } } -- 2.34.1