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 JB |
42 | private startConnectors(): void { |
43 | for (const connector in this.chargingStation.connectors) { | |
44 | const connectorId = Utils.convertToInt(connector); | |
45 | if (connectorId > 0) { | |
83a3286a | 46 | this.startConnector(connectorId); |
72740232 JB |
47 | } |
48 | } | |
49 | } | |
50 | ||
51 | private stopConnectors(): void { | |
52 | for (const connector in this.chargingStation.connectors) { | |
53 | const connectorId = Utils.convertToInt(connector); | |
54 | if (connectorId > 0) { | |
55 | this.stopConnector(connectorId); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
83a3286a | 60 | private async internalStartConnector(connectorId: number): Promise<void> { |
9664ec50 JB |
61 | this.initStartConnectorStatus(connectorId); |
62 | 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())); | |
63 | while (this.connectorsStatus.get(connectorId).start) { | |
64 | if ((new Date()) > this.connectorsStatus.get(connectorId).stopDate) { | |
65 | this.stopConnector(connectorId); | |
17991e8c JB |
66 | break; |
67 | } | |
c0560973 | 68 | if (!this.chargingStation.isRegistered()) { |
9664ec50 JB |
69 | logger.error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not registered'); |
70 | this.stopConnector(connectorId); | |
17991e8c JB |
71 | break; |
72 | } | |
c0560973 | 73 | if (!this.chargingStation.isChargingStationAvailable()) { |
9664ec50 JB |
74 | logger.info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable'); |
75 | this.stopConnector(connectorId); | |
ab5f4b03 JB |
76 | break; |
77 | } | |
c0560973 | 78 | if (!this.chargingStation.isConnectorAvailable(connectorId)) { |
9664ec50 | 79 | logger.info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`); |
9c7195b2 | 80 | this.stopConnector(connectorId); |
17991e8c JB |
81 | break; |
82 | } | |
c0560973 | 83 | if (!this.chargingStation?.ocppRequestService) { |
9664ec50 | 84 | logger.info(`${this.logPrefix(connectorId)} transaction loop waiting for charging station service to be initialized`); |
c0560973 | 85 | do { |
a4cc42ea | 86 | await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); |
c0560973 JB |
87 | } while (!this.chargingStation?.ocppRequestService); |
88 | } | |
72740232 | 89 | const wait = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, |
ad2f27c3 | 90 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; |
d7d1db72 | 91 | logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait)); |
6af9012e | 92 | await Utils.sleep(wait); |
c37528f1 | 93 | const start = Utils.secureRandom(); |
ad2f27c3 | 94 | if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { |
9664ec50 | 95 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; |
6af9012e | 96 | // Start transaction |
aef1b33a | 97 | const startResponse = await this.startTransaction(connectorId); |
ef6076c1 | 98 | if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { |
9664ec50 | 99 | logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); |
6af9012e JB |
100 | await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); |
101 | } else { | |
102 | // Wait until end of transaction | |
72740232 | 103 | const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, |
ad2f27c3 | 104 | this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; |
9664ec50 | 105 | logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); |
6af9012e JB |
106 | await Utils.sleep(waitTrxEnd); |
107 | // Stop transaction | |
85d20667 JB |
108 | logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); |
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 { | |
136 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; | |
137 | this.connectorsStatus.get(connectorId).skippedTransactions = 0; | |
138 | const previousRunDuration = (this?.connectorsStatus.get(connectorId)?.startDate && this?.connectorsStatus.get(connectorId)?.lastRunDate) | |
139 | ? (this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()) | |
140 | : 0; | |
141 | this.connectorsStatus.get(connectorId).startDate = new Date(); | |
142 | this.connectorsStatus.get(connectorId).stopDate = new Date(this.connectorsStatus.get(connectorId).startDate.getTime() | |
143 | + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 | |
144 | - previousRunDuration); | |
145 | this.connectorsStatus.get(connectorId).start = true; | |
72740232 JB |
146 | } |
147 | ||
aef1b33a JB |
148 | private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> { |
149 | const measureId = 'StartTransaction with ATG'; | |
150 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
151 | let startResponse: StartTransactionResponse; | |
152 | if (this.chargingStation.hasAuthorizedTags()) { | |
f4bf2abd | 153 | const idTag = this.chargingStation.getRandomIdTag(); |
aef1b33a | 154 | if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { |
f4bf2abd JB |
155 | // Authorize idTag |
156 | const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, idTag); | |
5fdab605 | 157 | if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
f4bf2abd | 158 | logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); |
5fdab605 | 159 | // Start transaction |
f4bf2abd | 160 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag); |
aef1b33a JB |
161 | PerformanceStatistics.endMeasure(measureId, beginId); |
162 | return startResponse; | |
5fdab605 | 163 | } |
aef1b33a | 164 | PerformanceStatistics.endMeasure(measureId, beginId); |
4faad557 | 165 | return authorizeResponse; |
ef6076c1 | 166 | } |
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; | |
6af9012e | 172 | } |
107efcc6 | 173 | logger.info(this.logPrefix(connectorId) + ' start transaction without an idTag'); |
aef1b33a JB |
174 | startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId); |
175 | PerformanceStatistics.endMeasure(measureId, beginId); | |
176 | return startResponse; | |
6af9012e JB |
177 | } |
178 | ||
0045cef5 | 179 | private async stopTransaction(connectorId: number, reason: StopTransactionReason = StopTransactionReason.NONE): Promise<StopTransactionResponse> { |
aef1b33a JB |
180 | const measureId = 'StopTransaction with ATG'; |
181 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
8eb02b62 | 182 | let transactionId = 0; |
0045cef5 JB |
183 | let stopResponse: StopTransactionResponse; |
184 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { | |
8eb02b62 | 185 | transactionId = this.chargingStation.getConnector(connectorId).transactionId; |
0045cef5 JB |
186 | stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, |
187 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), | |
188 | this.chargingStation.getTransactionIdTag(transactionId), | |
189 | reason); | |
190 | } else { | |
8eb02b62 | 191 | logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`); |
0045cef5 | 192 | } |
aef1b33a JB |
193 | PerformanceStatistics.endMeasure(measureId, beginId); |
194 | return stopResponse; | |
c0560973 JB |
195 | } |
196 | ||
6e0964c8 | 197 | private logPrefix(connectorId?: number): string { |
c0560973 | 198 | if (connectorId) { |
54b1efe0 | 199 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':'); |
c0560973 | 200 | } |
54b1efe0 | 201 | return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:'); |
6af9012e JB |
202 | } |
203 | } |