Cleanups: performance statistics, URI handling.
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
1 import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction';
2
3 import ChargingStation from './ChargingStation';
4 import Constants from '../utils/Constants';
5 import { PerformanceObserver } from 'perf_hooks';
6 import PerformanceStatistics from '../utils/PerformanceStatistics';
7 import Utils from '../utils/Utils';
8 import logger from '../utils/Logger';
9
10 export default class AutomaticTransactionGenerator {
11 public timeToStop: boolean;
12 private chargingStation: ChargingStation;
13 private performanceObserver!: PerformanceObserver;
14
15 constructor(chargingStation: ChargingStation) {
16 this.chargingStation = chargingStation;
17 this.timeToStop = true;
18 if (this.chargingStation.getEnableStatistics()) {
19 this.performanceObserver = PerformanceStatistics.initPerformanceObserver(Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR, this.chargingStation.performanceStatistics,
20 this.performanceObserver);
21 }
22 }
23
24 public async start(): Promise<void> {
25 this.timeToStop = false;
26 if (this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours &&
27 this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) {
28 // eslint-disable-next-line @typescript-eslint/no-misused-promises
29 setTimeout(async (): Promise<void> => {
30 await this.stop();
31 }, this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
32 }
33 for (const connector in this.chargingStation.connectors) {
34 if (Utils.convertToInt(connector) > 0) {
35 await this.startConnector(Utils.convertToInt(connector));
36 }
37 }
38 logger.info(this.logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
39 }
40
41 public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
42 logger.info(this.logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
43 for (const connector in this.chargingStation.connectors) {
44 const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId;
45 if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) {
46 logger.info(this.logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + transactionId.toString());
47 await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
48 this.chargingStation.getTransactionIdTag(transactionId), reason);
49 }
50 }
51 this.timeToStop = true;
52 }
53
54 private async startConnector(connectorId: number): Promise<void> {
55 do {
56 if (this.timeToStop) {
57 logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
58 break;
59 }
60 if (!this.chargingStation.isRegistered()) {
61 logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered');
62 break;
63 }
64 if (!this.chargingStation.isChargingStationAvailable()) {
65 logger.info(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable');
66 await this.stop();
67 break;
68 }
69 if (!this.chargingStation.isConnectorAvailable(connectorId)) {
70 logger.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
71 break;
72 }
73 if (!this.chargingStation?.ocppRequestService) {
74 logger.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`);
75 do {
76 await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
77 } while (!this.chargingStation?.ocppRequestService);
78 }
79 const wait = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
80 this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
81 logger.info(this.logPrefix(connectorId) + ' wait for ' + Utils.milliSecondsToHHMMSS(wait));
82 await Utils.sleep(wait);
83 const start = Math.random();
84 let skip = 0;
85 if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
86 // Start transaction
87 let startTransaction: (connectorId: number, self: AutomaticTransactionGenerator) => Promise<StartTransactionResponse | AuthorizeResponse>;
88 if (this.chargingStation.getEnableStatistics()) {
89 startTransaction = PerformanceStatistics.timedFunction(this.startTransaction.bind(this), this.performanceObserver);
90 } else {
91 startTransaction = this.startTransaction.bind(this);
92 }
93 const startResponse = await startTransaction(connectorId, this);
94 if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
95 logger.warn(this.logPrefix(connectorId) + ' transaction rejected');
96 await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
97 } else {
98 // Wait until end of transaction
99 const waitTrxEnd = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration,
100 this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
101 logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.milliSecondsToHHMMSS(waitTrxEnd));
102 await Utils.sleep(waitTrxEnd);
103 // Stop transaction
104 if (this.chargingStation.getConnector(connectorId)?.transactionStarted) {
105 logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString());
106 let stopTransaction: (connectorId: number, self: AutomaticTransactionGenerator) => Promise<StopTransactionResponse>;
107 if (this.chargingStation.getEnableStatistics()) {
108 stopTransaction = PerformanceStatistics.timedFunction(this.stopTransaction.bind(this), this.performanceObserver);
109 } else {
110 stopTransaction = this.stopTransaction.bind(this);
111 }
112 await stopTransaction(connectorId, this);
113 }
114 }
115 } else {
116 skip++;
117 logger.info(this.logPrefix(connectorId) + ' transaction skipped ' + skip.toString());
118 }
119 } while (!this.timeToStop);
120 logger.info(this.logPrefix(connectorId) + ' ATG STOPPED on the connector');
121 }
122
123 // eslint-disable-next-line consistent-this
124 private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse | AuthorizeResponse> {
125 if (self.chargingStation.hasAuthorizedTags()) {
126 const tagId = self.chargingStation.getRandomTagId();
127 if (self.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) {
128 // Authorize tagId
129 const authorizeResponse = await self.chargingStation.ocppRequestService.sendAuthorize(connectorId, tagId);
130 if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
131 logger.info(self.logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
132 // Start transaction
133 return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
134 }
135 return authorizeResponse;
136 }
137 logger.info(self.logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
138 // Start transaction
139 return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
140 }
141 logger.info(self.logPrefix(connectorId) + ' start transaction without a tagID');
142 return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId);
143 }
144
145 // eslint-disable-next-line consistent-this
146 private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StopTransactionResponse> {
147 const transactionId = self.chargingStation.getConnector(connectorId).transactionId;
148 return self.chargingStation.ocppRequestService.sendStopTransaction(transactionId, self.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
149 self.chargingStation.getTransactionIdTag(transactionId));
150 }
151
152 private logPrefix(connectorId?: number): string {
153 if (connectorId) {
154 return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':');
155 }
156 return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:');
157 }
158 }