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'; |
9664ec50 | 8 | import { Status } from '../types/AutomaticTransactionGenerator'; |
6af9012e JB |
9 | import Utils from '../utils/Utils'; |
10 | import logger from '../utils/Logger'; | |
11 | ||
12 | export default class AutomaticTransactionGenerator { | |
265e4266 | 13 | public started: boolean; |
72740232 | 14 | private chargingStation: ChargingStation; |
9664ec50 | 15 | private connectorsStatus: Map<number, Status>; |
6af9012e JB |
16 | |
17 | constructor(chargingStation: ChargingStation) { | |
ad2f27c3 | 18 | this.chargingStation = chargingStation; |
9664ec50 | 19 | this.connectorsStatus = new Map<number, Status>(); |
72740232 | 20 | this.stopConnectors(); |
265e4266 | 21 | this.started = false; |
6af9012e JB |
22 | } |
23 | ||
7d75bee1 | 24 | public start(): void { |
b809adf1 JB |
25 | if (this.started) { |
26 | logger.error(`${this.logPrefix()} trying to start while already started`); | |
27 | return; | |
28 | } | |
72740232 | 29 | this.startConnectors(); |
265e4266 | 30 | this.started = true; |
6af9012e JB |
31 | } |
32 | ||
0045cef5 | 33 | public stop(): void { |
265e4266 JB |
34 | if (!this.started) { |
35 | logger.error(`${this.logPrefix()} trying to stop while not started`); | |
36 | return; | |
37 | } | |
72740232 | 38 | this.stopConnectors(); |
265e4266 | 39 | this.started = false; |
6af9012e JB |
40 | } |
41 | ||
72740232 | 42 | private startConnectors(): void { |
734d790d | 43 | for (const connectorId of this.chargingStation.connectors.keys()) { |
72740232 | 44 | if (connectorId > 0) { |
83a3286a | 45 | this.startConnector(connectorId); |
72740232 JB |
46 | } |
47 | } | |
48 | } | |
49 | ||
50 | private stopConnectors(): void { | |
734d790d | 51 | for (const connectorId of this.chargingStation.connectors.keys()) { |
72740232 JB |
52 | if (connectorId > 0) { |
53 | this.stopConnector(connectorId); | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
83a3286a | 58 | private async internalStartConnector(connectorId: number): Promise<void> { |
9664ec50 JB |
59 | this.initStartConnectorStatus(connectorId); |
60 | 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())); | |
61 | while (this.connectorsStatus.get(connectorId).start) { | |
62 | if ((new Date()) > this.connectorsStatus.get(connectorId).stopDate) { | |
63 | this.stopConnector(connectorId); | |
17991e8c JB |
64 | break; |
65 | } | |
c0560973 | 66 | if (!this.chargingStation.isRegistered()) { |
9664ec50 JB |
67 | logger.error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not registered'); |
68 | this.stopConnector(connectorId); | |
17991e8c JB |
69 | break; |
70 | } | |
c0560973 | 71 | if (!this.chargingStation.isChargingStationAvailable()) { |
9664ec50 JB |
72 | logger.info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable'); |
73 | this.stopConnector(connectorId); | |
ab5f4b03 JB |
74 | break; |
75 | } | |
c0560973 | 76 | if (!this.chargingStation.isConnectorAvailable(connectorId)) { |
9664ec50 | 77 | logger.info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`); |
9c7195b2 | 78 | this.stopConnector(connectorId); |
17991e8c JB |
79 | break; |
80 | } | |
c0560973 | 81 | if (!this.chargingStation?.ocppRequestService) { |
9664ec50 | 82 | logger.info(`${this.logPrefix(connectorId)} transaction loop waiting for charging station service to be initialized`); |
c0560973 | 83 | do { |
a4cc42ea | 84 | await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); |
c0560973 JB |
85 | } while (!this.chargingStation?.ocppRequestService); |
86 | } | |
72740232 | 87 | const wait = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, |
ad2f27c3 | 88 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; |
d7d1db72 | 89 | logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait)); |
6af9012e | 90 | await Utils.sleep(wait); |
c37528f1 | 91 | const start = Utils.secureRandom(); |
ad2f27c3 | 92 | if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { |
9664ec50 | 93 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; |
6af9012e | 94 | // Start transaction |
aef1b33a | 95 | const startResponse = await this.startTransaction(connectorId); |
071a9315 | 96 | this.connectorsStatus.get(connectorId).startTransactionRequests++; |
ef6076c1 | 97 | if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { |
9664ec50 | 98 | logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); |
071a9315 | 99 | this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; |
6af9012e JB |
100 | } else { |
101 | // Wait until end of transaction | |
72740232 | 102 | const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, |
ad2f27c3 | 103 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; |
734d790d | 104 | logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); |
071a9315 | 105 | this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; |
6af9012e JB |
106 | await Utils.sleep(waitTrxEnd); |
107 | // Stop transaction | |
734d790d | 108 | logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString()); |
85d20667 | 109 | await this.stopTransaction(connectorId); |
6af9012e JB |
110 | } |
111 | } else { | |
9664ec50 JB |
112 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++; |
113 | this.connectorsStatus.get(connectorId).skippedTransactions++; | |
114 | logger.info(this.logPrefix(connectorId) + ' skipped consecutively ' + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + '/' + this.connectorsStatus.get(connectorId).skippedTransactions.toString() + ' transaction(s)'); | |
6af9012e | 115 | } |
9664ec50 | 116 | this.connectorsStatus.get(connectorId).lastRunDate = new Date(); |
7d75bee1 | 117 | } |
0045cef5 | 118 | await this.stopTransaction(connectorId); |
9664ec50 JB |
119 | this.connectorsStatus.get(connectorId).stoppedDate = new Date(); |
120 | 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())); | |
121 | logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId)); | |
6af9012e JB |
122 | } |
123 | ||
83a3286a JB |
124 | private startConnector(connectorId: number): void { |
125 | // Avoid hogging the event loop with a busy loop | |
126 | setImmediate(() => { | |
127 | this.internalStartConnector(connectorId).catch(() => { /* This is intentional */ }); | |
128 | }); | |
129 | } | |
130 | ||
72740232 | 131 | private stopConnector(connectorId: number): void { |
b2bece24 | 132 | this.connectorsStatus.set(connectorId, { ...this.connectorsStatus.get(connectorId), start: false }); |
9664ec50 JB |
133 | } |
134 | ||
135 | private initStartConnectorStatus(connectorId: number): void { | |
071a9315 JB |
136 | this.connectorsStatus.get(connectorId).authorizeRequests = this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0; |
137 | this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0; | |
138 | this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0; | |
139 | this.connectorsStatus.get(connectorId).startTransactionRequests = this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0; | |
140 | this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0; | |
141 | this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0; | |
142 | this.connectorsStatus.get(connectorId).stopTransactionRequests = this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0; | |
9664ec50 | 143 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; |
071a9315 | 144 | this.connectorsStatus.get(connectorId).skippedTransactions = this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0; |
9664ec50 JB |
145 | const previousRunDuration = (this?.connectorsStatus.get(connectorId)?.startDate && this?.connectorsStatus.get(connectorId)?.lastRunDate) |
146 | ? (this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()) | |
147 | : 0; | |
148 | this.connectorsStatus.get(connectorId).startDate = new Date(); | |
149 | this.connectorsStatus.get(connectorId).stopDate = new Date(this.connectorsStatus.get(connectorId).startDate.getTime() | |
150 | + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 | |
151 | - previousRunDuration); | |
152 | this.connectorsStatus.get(connectorId).start = true; | |
72740232 JB |
153 | } |
154 | ||
aef1b33a JB |
155 | private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> { |
156 | const measureId = 'StartTransaction with ATG'; | |
157 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
158 | let startResponse: StartTransactionResponse; | |
159 | if (this.chargingStation.hasAuthorizedTags()) { | |
f4bf2abd | 160 | const idTag = this.chargingStation.getRandomIdTag(); |
aef1b33a | 161 | if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { |
f4bf2abd JB |
162 | // Authorize idTag |
163 | const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, idTag); | |
071a9315 | 164 | this.connectorsStatus.get(connectorId).authorizeRequests++; |
5fdab605 | 165 | if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
071a9315 | 166 | this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; |
f4bf2abd | 167 | logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); |
5fdab605 | 168 | // Start transaction |
f4bf2abd | 169 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag); |
aef1b33a JB |
170 | PerformanceStatistics.endMeasure(measureId, beginId); |
171 | return startResponse; | |
5fdab605 | 172 | } |
071a9315 | 173 | this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++; |
aef1b33a | 174 | PerformanceStatistics.endMeasure(measureId, beginId); |
4faad557 | 175 | return authorizeResponse; |
ef6076c1 | 176 | } |
f4bf2abd | 177 | logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); |
5fdab605 | 178 | // Start transaction |
f4bf2abd | 179 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag); |
aef1b33a JB |
180 | PerformanceStatistics.endMeasure(measureId, beginId); |
181 | return startResponse; | |
6af9012e | 182 | } |
107efcc6 | 183 | logger.info(this.logPrefix(connectorId) + ' start transaction without an idTag'); |
aef1b33a JB |
184 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId); |
185 | PerformanceStatistics.endMeasure(measureId, beginId); | |
186 | return startResponse; | |
6af9012e JB |
187 | } |
188 | ||
0045cef5 | 189 | private async stopTransaction(connectorId: number, reason: StopTransactionReason = StopTransactionReason.NONE): Promise<StopTransactionResponse> { |
aef1b33a JB |
190 | const measureId = 'StopTransaction with ATG'; |
191 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
8eb02b62 | 192 | let transactionId = 0; |
0045cef5 | 193 | let stopResponse: StopTransactionResponse; |
734d790d JB |
194 | if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { |
195 | transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; | |
0045cef5 JB |
196 | stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, |
197 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), | |
198 | this.chargingStation.getTransactionIdTag(transactionId), | |
199 | reason); | |
071a9315 | 200 | this.connectorsStatus.get(connectorId).stopTransactionRequests++; |
0045cef5 | 201 | } else { |
8eb02b62 | 202 | logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`); |
0045cef5 | 203 | } |
aef1b33a JB |
204 | PerformanceStatistics.endMeasure(measureId, beginId); |
205 | return stopResponse; | |
c0560973 JB |
206 | } |
207 | ||
6e0964c8 | 208 | private logPrefix(connectorId?: number): string { |
c0560973 | 209 | if (connectorId) { |
54b1efe0 | 210 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':'); |
c0560973 | 211 | } |
54b1efe0 | 212 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:'); |
6af9012e JB |
213 | } |
214 | } |