Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Enforce singleton design pattern for the logger
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Feb 17, 2022
1 parent 1629a15 commit bc464bb
Show file tree
Hide file tree
Showing 17 changed files with 489 additions and 478 deletions.
631 changes: 318 additions & 313 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"tar": "^6.1.11",
"tslib": "^2.3.1",
"uuid": "^8.3.2",
"winston": "^3.5.1",
"winston-daily-rotate-file": "^4.6.0",
"winston": "^3.6.0",
"winston-daily-rotate-file": "^4.6.1",
"ws": "^8.5.0"
},
"optionalDependencies": {
Expand All @@ -94,19 +94,19 @@
"@rollup/plugin-json": "^4.1.0",
"@types/mocha": "^9.1.0",
"@types/mochawesome": "^6.2.1",
"@types/node": "^16.11.22",
"@types/node": "^16.11.25",
"@types/proper-lockfile": "^4.1.2",
"@types/tar": "^6.1.1",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.2.2",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"auto-changelog": "^2.4.0",
"clinic": "^11.0.0",
"clinic": "^11.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.8.0",
"eslint": "^8.9.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsdoc": "^37.8.0",
"eslint-plugin-jsdoc": "^37.9.2",
"eslint-plugin-node": "^11.1.0",
"expect": "^27.5.1",
"mocha": "^9.2.0",
Expand All @@ -115,7 +115,7 @@
"nyc": "^15.1.0",
"release-it": "^14.12.4",
"robohydra": "^0.6.9",
"rollup": "^2.67.1",
"rollup": "^2.67.2",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-delete": "^2.0.0",
Expand Down
38 changes: 19 additions & 19 deletions src/charging-station/AutomaticTransactionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Constants from '../utils/Constants';
import PerformanceStatistics from '../performance/PerformanceStatistics';
import { Status } from '../types/AutomaticTransactionGenerator';
import Utils from '../utils/Utils';
import logger from '../utils/Logger';
import getLogger from '../utils/Logger';

export default class AutomaticTransactionGenerator {
public started: boolean;
Expand All @@ -23,7 +23,7 @@ export default class AutomaticTransactionGenerator {

public start(): void {
if (this.started) {
logger.error(`${this.logPrefix()} trying to start while already started`);
getLogger().error(`${this.logPrefix()} trying to start while already started`);
return;
}
this.startConnectors();
Expand All @@ -32,7 +32,7 @@ export default class AutomaticTransactionGenerator {

public stop(): void {
if (!this.started) {
logger.error(`${this.logPrefix()} trying to stop while not started`);
getLogger().error(`${this.logPrefix()} trying to stop while not started`);
return;
}
this.stopConnectors();
Expand Down Expand Up @@ -60,36 +60,36 @@ export default class AutomaticTransactionGenerator {

private async internalStartConnector(connectorId: number): Promise<void> {
this.initStartConnectorStatus(connectorId);
logger.info(this.logPrefix(connectorId) + ' started on connector and will run for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stopDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
getLogger().info(this.logPrefix(connectorId) + ' started on connector and will run for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stopDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
while (this.connectorsStatus.get(connectorId).start) {
if ((new Date()) > this.connectorsStatus.get(connectorId).stopDate) {
this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isInAcceptedState()) {
logger.error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not in accepted state');
getLogger().error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not in accepted state');
this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isChargingStationAvailable()) {
logger.info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable');
getLogger().info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable');
this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isConnectorAvailable(connectorId)) {
logger.info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`);
getLogger().info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`);
this.stopConnector(connectorId);
break;
}
if (!this.chargingStation?.ocppRequestService) {
logger.info(`${this.logPrefix(connectorId)} transaction loop waiting for charging station service to be initialized`);
getLogger().info(`${this.logPrefix(connectorId)} transaction loop waiting for charging station service to be initialized`);
do {
await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
} while (!this.chargingStation?.ocppRequestService);
}
const wait = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait));
getLogger().info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait));
await Utils.sleep(wait);
const start = Utils.secureRandom();
if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
Expand All @@ -98,30 +98,30 @@ export default class AutomaticTransactionGenerator {
const startResponse = await this.startTransaction(connectorId);
this.connectorsStatus.get(connectorId).startTransactionRequests++;
if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
getLogger().warn(this.logPrefix(connectorId) + ' start transaction rejected');
this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
} else {
// Wait until end of transaction
const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration,
this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd));
getLogger().info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd));
this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
await Utils.sleep(waitTrxEnd);
// Stop transaction
logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString());
getLogger().info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString());
await this.stopTransaction(connectorId);
}
} else {
this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++;
this.connectorsStatus.get(connectorId).skippedTransactions++;
logger.info(this.logPrefix(connectorId) + ' skipped consecutively ' + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + '/' + this.connectorsStatus.get(connectorId).skippedTransactions.toString() + ' transaction(s)');
getLogger().info(this.logPrefix(connectorId) + ' skipped consecutively ' + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + '/' + this.connectorsStatus.get(connectorId).skippedTransactions.toString() + ' transaction(s)');
}
this.connectorsStatus.get(connectorId).lastRunDate = new Date();
}
await this.stopTransaction(connectorId);
this.connectorsStatus.get(connectorId).stoppedDate = new Date();
logger.info(this.logPrefix(connectorId) + ' stopped on connector and lasted for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stoppedDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId));
getLogger().info(this.logPrefix(connectorId) + ' stopped on connector and lasted for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stoppedDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
getLogger().debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId));
}

private startConnector(connectorId: number): void {
Expand Down Expand Up @@ -167,7 +167,7 @@ export default class AutomaticTransactionGenerator {
this.connectorsStatus.get(connectorId).authorizeRequests++;
if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++;
logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
getLogger().info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
// Start transaction
startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag);
PerformanceStatistics.endMeasure(measureId, beginId);
Expand All @@ -177,13 +177,13 @@ export default class AutomaticTransactionGenerator {
PerformanceStatistics.endMeasure(measureId, beginId);
return authorizeResponse;
}
logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
getLogger().info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag);
// Start transaction
startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag);
PerformanceStatistics.endMeasure(measureId, beginId);
return startResponse;
}
logger.info(this.logPrefix(connectorId) + ' start transaction without an idTag');
getLogger().info(this.logPrefix(connectorId) + ' start transaction without an idTag');
startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId);
PerformanceStatistics.endMeasure(measureId, beginId);
return startResponse;
Expand All @@ -202,7 +202,7 @@ export default class AutomaticTransactionGenerator {
reason);
this.connectorsStatus.get(connectorId).stopTransactionRequests++;
} else {
logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`);
getLogger().warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`);
}
PerformanceStatistics.endMeasure(measureId, beginId);
return stopResponse;
Expand Down
Loading

0 comments on commit bc464bb

Please sign in to comment.