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