Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
c0560973 | 3 | import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; |
6af9012e JB |
4 | |
5 | import ChargingStation from './ChargingStation'; | |
6 | import Constants from '../utils/Constants'; | |
a6b3c6c3 | 7 | import PerformanceStatistics from '../performance/PerformanceStatistics'; |
6af9012e JB |
8 | import Utils from '../utils/Utils'; |
9 | import logger from '../utils/Logger'; | |
10 | ||
11 | export default class AutomaticTransactionGenerator { | |
265e4266 | 12 | public started: boolean; |
7d75bee1 | 13 | private startDate!: Date; |
265e4266 | 14 | private lastRunDate!: Date; |
7d75bee1 | 15 | private stopDate!: Date; |
ad2f27c3 | 16 | private chargingStation: ChargingStation; |
6af9012e JB |
17 | |
18 | constructor(chargingStation: ChargingStation) { | |
ad2f27c3 | 19 | this.chargingStation = chargingStation; |
265e4266 | 20 | this.started = false; |
6af9012e JB |
21 | } |
22 | ||
7d75bee1 JB |
23 | public start(): void { |
24 | this.startDate = new Date(); | |
265e4266 | 25 | this.lastRunDate = this?.lastRunDate ?? this.startDate; |
58fad749 JB |
26 | this.stopDate = new Date(this.startDate.getTime() |
27 | + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 | |
265e4266 JB |
28 | - (this.lastRunDate.getTime() - this.startDate.getTime())); |
29 | this.started = true; | |
ad2f27c3 | 30 | for (const connector in this.chargingStation.connectors) { |
6af9012e | 31 | if (Utils.convertToInt(connector) > 0) { |
7d75bee1 JB |
32 | // Avoid hogging the event loop with a busy loop |
33 | setImmediate(() => { | |
34 | this.startOnConnector(Utils.convertToInt(connector)).catch(() => { /* This is intentional */ }); | |
35 | }); | |
6af9012e JB |
36 | } |
37 | } | |
d7d1db72 | 38 | logger.info(this.logPrefix() + ' started and will run for ' + Utils.formatDurationMilliSeconds(this.stopDate.getTime() - this.startDate.getTime())); |
6af9012e JB |
39 | } |
40 | ||
c0560973 | 41 | public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> { |
265e4266 JB |
42 | if (!this.started) { |
43 | logger.error(`${this.logPrefix()} trying to stop while not started`); | |
44 | return; | |
45 | } | |
46 | logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`); | |
ad2f27c3 | 47 | for (const connector in this.chargingStation.connectors) { |
c0560973 | 48 | const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId; |
ad2f27c3 | 49 | if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { |
7d75bee1 | 50 | logger.info(this.logPrefix(Utils.convertToInt(connector)) + ' over. Stop transaction ' + transactionId.toString()); |
6ed92bc1 | 51 | await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), |
035742f7 | 52 | this.chargingStation.getTransactionIdTag(transactionId), reason); |
6af9012e JB |
53 | } |
54 | } | |
265e4266 | 55 | this.started = false; |
6af9012e JB |
56 | } |
57 | ||
7d75bee1 JB |
58 | private async startOnConnector(connectorId: number): Promise<void> { |
59 | logger.info(this.logPrefix(connectorId) + ' started on connector'); | |
60 | let transactionSkip = 0; | |
61 | let totalTransactionSkip = 0; | |
265e4266 | 62 | while (this.started) { |
7d75bee1 JB |
63 | if ((new Date()) > this.stopDate) { |
64 | await this.stop(); | |
17991e8c JB |
65 | break; |
66 | } | |
c0560973 JB |
67 | if (!this.chargingStation.isRegistered()) { |
68 | logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered'); | |
17991e8c JB |
69 | break; |
70 | } | |
c0560973 JB |
71 | if (!this.chargingStation.isChargingStationAvailable()) { |
72 | logger.info(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable'); | |
ab5f4b03 JB |
73 | await this.stop(); |
74 | break; | |
75 | } | |
c0560973 JB |
76 | if (!this.chargingStation.isConnectorAvailable(connectorId)) { |
77 | logger.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`); | |
17991e8c JB |
78 | break; |
79 | } | |
c0560973 JB |
80 | if (!this.chargingStation?.ocppRequestService) { |
81 | logger.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`); | |
82 | do { | |
a4cc42ea | 83 | await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); |
c0560973 JB |
84 | } while (!this.chargingStation?.ocppRequestService); |
85 | } | |
ad2f27c3 JB |
86 | const wait = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, |
87 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; | |
d7d1db72 | 88 | logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait)); |
6af9012e | 89 | await Utils.sleep(wait); |
c37528f1 | 90 | const start = Utils.secureRandom(); |
ad2f27c3 | 91 | if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { |
7d75bee1 | 92 | transactionSkip = 0; |
6af9012e | 93 | // Start transaction |
aef1b33a | 94 | const startResponse = await this.startTransaction(connectorId); |
ef6076c1 | 95 | if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { |
54b1efe0 | 96 | logger.warn(this.logPrefix(connectorId) + ' transaction rejected'); |
6af9012e JB |
97 | await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); |
98 | } else { | |
99 | // Wait until end of transaction | |
ad2f27c3 JB |
100 | const waitTrxEnd = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, |
101 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; | |
d7d1db72 | 102 | logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); |
6af9012e JB |
103 | await Utils.sleep(waitTrxEnd); |
104 | // Stop transaction | |
ad2f27c3 | 105 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { |
c0560973 | 106 | logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); |
aef1b33a | 107 | await this.stopTransaction(connectorId); |
6af9012e JB |
108 | } |
109 | } | |
110 | } else { | |
7d75bee1 JB |
111 | transactionSkip++; |
112 | totalTransactionSkip++; | |
113 | logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + transactionSkip.toString() + '/' + totalTransactionSkip.toString()); | |
6af9012e | 114 | } |
265e4266 | 115 | this.lastRunDate = new Date(); |
7d75bee1 JB |
116 | } |
117 | logger.info(this.logPrefix(connectorId) + ' stopped on connector'); | |
6af9012e JB |
118 | } |
119 | ||
aef1b33a JB |
120 | private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> { |
121 | const measureId = 'StartTransaction with ATG'; | |
122 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
123 | let startResponse: StartTransactionResponse; | |
124 | if (this.chargingStation.hasAuthorizedTags()) { | |
125 | const tagId = this.chargingStation.getRandomTagId(); | |
126 | if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { | |
5fdab605 | 127 | // Authorize tagId |
aef1b33a | 128 | const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, tagId); |
5fdab605 | 129 | if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
aef1b33a | 130 | logger.info(this.logPrefix(connectorId) + ' start transaction for tagID ' + tagId); |
5fdab605 | 131 | // Start transaction |
aef1b33a JB |
132 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId); |
133 | PerformanceStatistics.endMeasure(measureId, beginId); | |
134 | return startResponse; | |
5fdab605 | 135 | } |
aef1b33a | 136 | PerformanceStatistics.endMeasure(measureId, beginId); |
4faad557 | 137 | return authorizeResponse; |
ef6076c1 | 138 | } |
aef1b33a | 139 | logger.info(this.logPrefix(connectorId) + ' start transaction for tagID ' + tagId); |
5fdab605 | 140 | // Start transaction |
aef1b33a JB |
141 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId); |
142 | PerformanceStatistics.endMeasure(measureId, beginId); | |
143 | return startResponse; | |
6af9012e | 144 | } |
aef1b33a JB |
145 | logger.info(this.logPrefix(connectorId) + ' start transaction without a tagID'); |
146 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId); | |
147 | PerformanceStatistics.endMeasure(measureId, beginId); | |
148 | return startResponse; | |
6af9012e JB |
149 | } |
150 | ||
aef1b33a JB |
151 | private async stopTransaction(connectorId: number): Promise<StopTransactionResponse> { |
152 | const measureId = 'StopTransaction with ATG'; | |
153 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
154 | const transactionId = this.chargingStation.getConnector(connectorId).transactionId; | |
155 | const stopResponse = this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, | |
156 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId)); | |
157 | PerformanceStatistics.endMeasure(measureId, beginId); | |
158 | return stopResponse; | |
c0560973 JB |
159 | } |
160 | ||
6e0964c8 | 161 | private logPrefix(connectorId?: number): string { |
c0560973 | 162 | if (connectorId) { |
54b1efe0 | 163 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':'); |
c0560973 | 164 | } |
54b1efe0 | 165 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:'); |
6af9012e JB |
166 | } |
167 | } |