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