Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
b4d34251 | 2 | |
66a7748d JB |
3 | import { createHash } from 'node:crypto' |
4 | import { EventEmitter } from 'node:events' | |
5 | import { type FSWatcher, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' | |
1fdb60b6 | 6 | import { dirname, join, parse } from 'node:path' |
66a7748d JB |
7 | import { URL } from 'node:url' |
8 | import { parentPort } from 'node:worker_threads' | |
9 | ||
10 | import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns' | |
11 | import merge from 'just-merge' | |
12 | import { type RawData, WebSocket } from 'ws' | |
13 | ||
14 | import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator.js' | |
15 | import { ChargingStationWorkerBroadcastChannel } from './broadcast-channel/ChargingStationWorkerBroadcastChannel.js' | |
f2d5e3d9 JB |
16 | import { |
17 | addConfigurationKey, | |
18 | deleteConfigurationKey, | |
19 | getConfigurationKey, | |
66a7748d JB |
20 | setConfigurationKeyValue |
21 | } from './ConfigurationKeyUtils.js' | |
fba11dc6 JB |
22 | import { |
23 | buildConnectorsMap, | |
e6a33233 | 24 | checkChargingStation, |
73edcc94 | 25 | checkConfiguration, |
fba11dc6 JB |
26 | checkConnectorsConfiguration, |
27 | checkStationInfoConnectorStatus, | |
28 | checkTemplate, | |
fba11dc6 JB |
29 | createBootNotificationRequest, |
30 | createSerialNumber, | |
31 | getAmperageLimitationUnitDivider, | |
32 | getBootConnectorStatus, | |
33 | getChargingStationConnectorChargingProfilesPowerLimit, | |
34 | getChargingStationId, | |
35 | getDefaultVoltageOut, | |
36 | getHashId, | |
37 | getIdTagsFile, | |
38 | getMaxNumberOfEvses, | |
90aceaf6 | 39 | getNumberOfReservableConnectors, |
fba11dc6 | 40 | getPhaseRotationValue, |
a807045b | 41 | hasFeatureProfile, |
90aceaf6 | 42 | hasReservationExpired, |
fba11dc6 JB |
43 | initializeConnectorsMapStatus, |
44 | propagateSerialNumber, | |
45 | stationTemplateToStationInfo, | |
66a7748d JB |
46 | warnTemplateKeysDeprecation |
47 | } from './Helpers.js' | |
48 | import { IdTagsCache } from './IdTagsCache.js' | |
08b58f00 JB |
49 | import { |
50 | OCPP16IncomingRequestService, | |
51 | OCPP16RequestService, | |
52 | OCPP16ResponseService, | |
08b58f00 JB |
53 | OCPP20IncomingRequestService, |
54 | OCPP20RequestService, | |
55 | OCPP20ResponseService, | |
56 | type OCPPIncomingRequestService, | |
57 | type OCPPRequestService, | |
41f3983a | 58 | buildMeterValue, |
41f3983a | 59 | buildTransactionEndMeterValue, |
041365be | 60 | getMessageTypeString, |
66a7748d JB |
61 | sendAndSetConnectorStatus |
62 | } from './ocpp/index.js' | |
63 | import { SharedLRUCache } from './SharedLRUCache.js' | |
64 | import { BaseError, OCPPError } from '../exception/index.js' | |
65 | import { PerformanceStatistics } from '../performance/index.js' | |
e7aeea18 | 66 | import { |
268a74bb | 67 | type AutomaticTransactionGeneratorConfiguration, |
e7aeea18 | 68 | AvailabilityType, |
e0b0ee21 | 69 | type BootNotificationRequest, |
268a74bb | 70 | type BootNotificationResponse, |
e0b0ee21 | 71 | type CachedRequest, |
268a74bb | 72 | type ChargingStationConfiguration, |
db54d2e0 | 73 | ChargingStationEvents, |
268a74bb JB |
74 | type ChargingStationInfo, |
75 | type ChargingStationOcppConfiguration, | |
71ac2bd7 | 76 | type ChargingStationOptions, |
268a74bb | 77 | type ChargingStationTemplate, |
c8aafe0d | 78 | type ConnectorStatus, |
268a74bb JB |
79 | ConnectorStatusEnum, |
80 | CurrentType, | |
e0b0ee21 | 81 | type ErrorCallback, |
268a74bb JB |
82 | type ErrorResponse, |
83 | ErrorType, | |
2585c6e9 | 84 | type EvseStatus, |
52952bf8 | 85 | type EvseStatusConfiguration, |
268a74bb | 86 | FileType, |
c9a4f9ea JB |
87 | FirmwareStatus, |
88 | type FirmwareStatusNotificationRequest, | |
268a74bb JB |
89 | type FirmwareStatusNotificationResponse, |
90 | type FirmwareUpgrade, | |
e0b0ee21 | 91 | type HeartbeatRequest, |
268a74bb | 92 | type HeartbeatResponse, |
e0b0ee21 | 93 | type IncomingRequest, |
268a74bb | 94 | type IncomingRequestCommand, |
268a74bb | 95 | MessageType, |
268a74bb | 96 | MeterValueMeasurand, |
e0b0ee21 | 97 | type MeterValuesRequest, |
268a74bb JB |
98 | type MeterValuesResponse, |
99 | OCPPVersion, | |
8ca6874c | 100 | type OutgoingRequest, |
268a74bb JB |
101 | PowerUnits, |
102 | RegistrationStatusEnumType, | |
e7aeea18 | 103 | RequestCommand, |
66dd3447 | 104 | type Reservation, |
366f75f6 | 105 | type ReservationKey, |
66dd3447 | 106 | ReservationTerminationReason, |
268a74bb | 107 | type Response, |
268a74bb | 108 | StandardParametersKey, |
5ced7e80 | 109 | type Status, |
66a7748d | 110 | type StopTransactionReason, |
e0b0ee21 JB |
111 | type StopTransactionRequest, |
112 | type StopTransactionResponse, | |
268a74bb JB |
113 | SupervisionUrlDistribution, |
114 | SupportedFeatureProfiles, | |
66a7748d | 115 | type Voltage, |
268a74bb JB |
116 | type WSError, |
117 | WebSocketCloseEventStatusCode, | |
66a7748d JB |
118 | type WsOptions |
119 | } from '../types/index.js' | |
60a74391 JB |
120 | import { |
121 | ACElectricUtils, | |
1227a6f1 JB |
122 | AsyncLock, |
123 | AsyncLockType, | |
60a74391 JB |
124 | Configuration, |
125 | Constants, | |
126 | DCElectricUtils, | |
244c1396 | 127 | buildAddedMessage, |
179ed367 JB |
128 | buildChargingStationAutomaticTransactionGeneratorConfiguration, |
129 | buildConnectorsStatus, | |
130 | buildEvsesStatus, | |
c8faabc8 JB |
131 | buildStartedMessage, |
132 | buildStoppedMessage, | |
133 | buildUpdatedMessage, | |
40615072 | 134 | clone, |
9bf0ef23 | 135 | convertToBoolean, |
95dab6cf | 136 | convertToDate, |
9bf0ef23 JB |
137 | convertToInt, |
138 | exponentialDelay, | |
139 | formatDurationMilliSeconds, | |
140 | formatDurationSeconds, | |
141 | getRandomInteger, | |
142 | getWebSocketCloseEventStatusString, | |
fa5995d6 | 143 | handleFileException, |
9bf0ef23 JB |
144 | isNotEmptyArray, |
145 | isNotEmptyString, | |
9bf0ef23 | 146 | logPrefix, |
60a74391 | 147 | logger, |
5adf6ca4 | 148 | min, |
5f742aac | 149 | once, |
9bf0ef23 JB |
150 | roundTo, |
151 | secureRandom, | |
152 | sleep, | |
66a7748d JB |
153 | watchJsonFile |
154 | } from '../utils/index.js' | |
3f40bc9c | 155 | |
db54d2e0 | 156 | export class ChargingStation extends EventEmitter { |
66a7748d JB |
157 | public readonly index: number |
158 | public readonly templateFile: string | |
5199f9fd | 159 | public stationInfo?: ChargingStationInfo |
66a7748d JB |
160 | public started: boolean |
161 | public starting: boolean | |
162 | public idTagsCache: IdTagsCache | |
163 | public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined | |
164 | public ocppConfiguration!: ChargingStationOcppConfiguration | undefined | |
165 | public wsConnection: WebSocket | null | |
166 | public readonly connectors: Map<number, ConnectorStatus> | |
167 | public readonly evses: Map<number, EvseStatus> | |
168 | public readonly requests: Map<string, CachedRequest> | |
169 | public performanceStatistics!: PerformanceStatistics | undefined | |
170 | public heartbeatSetInterval?: NodeJS.Timeout | |
171 | public ocppRequestService!: OCPPRequestService | |
79534cce JB |
172 | public bootNotificationRequest?: BootNotificationRequest |
173 | public bootNotificationResponse?: BootNotificationResponse | |
5199f9fd | 174 | public powerDivider?: number |
66a7748d JB |
175 | private stopping: boolean |
176 | private configurationFile!: string | |
177 | private configurationFileHash!: string | |
178 | private connectorsConfigurationHash!: string | |
179 | private evsesConfigurationHash!: string | |
180 | private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration | |
181 | private ocppIncomingRequestService!: OCPPIncomingRequestService | |
182 | private readonly messageBuffer: Set<string> | |
183 | private configuredSupervisionUrl!: URL | |
2960841f JB |
184 | private wsConnectionRetried: boolean |
185 | private wsConnectionRetryCount: number | |
66a7748d JB |
186 | private templateFileWatcher!: FSWatcher | undefined |
187 | private templateFileHash!: string | |
188 | private readonly sharedLRUCache: SharedLRUCache | |
2960841f | 189 | private wsPingSetInterval?: NodeJS.Timeout |
66a7748d JB |
190 | private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel |
191 | private flushMessageBufferSetInterval?: NodeJS.Timeout | |
192 | ||
71ac2bd7 | 193 | constructor (index: number, templateFile: string, options?: ChargingStationOptions) { |
66a7748d JB |
194 | super() |
195 | this.started = false | |
196 | this.starting = false | |
197 | this.stopping = false | |
198 | this.wsConnection = null | |
2960841f JB |
199 | this.wsConnectionRetried = false |
200 | this.wsConnectionRetryCount = 0 | |
66a7748d JB |
201 | this.index = index |
202 | this.templateFile = templateFile | |
203 | this.connectors = new Map<number, ConnectorStatus>() | |
204 | this.evses = new Map<number, EvseStatus>() | |
205 | this.requests = new Map<string, CachedRequest>() | |
206 | this.messageBuffer = new Set<string>() | |
207 | this.sharedLRUCache = SharedLRUCache.getInstance() | |
208 | this.idTagsCache = IdTagsCache.getInstance() | |
209 | this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this) | |
32de5a57 | 210 | |
244c1396 JB |
211 | this.on(ChargingStationEvents.added, () => { |
212 | parentPort?.postMessage(buildAddedMessage(this)) | |
213 | }) | |
db54d2e0 | 214 | this.on(ChargingStationEvents.started, () => { |
66a7748d JB |
215 | parentPort?.postMessage(buildStartedMessage(this)) |
216 | }) | |
db54d2e0 | 217 | this.on(ChargingStationEvents.stopped, () => { |
66a7748d JB |
218 | parentPort?.postMessage(buildStoppedMessage(this)) |
219 | }) | |
db54d2e0 | 220 | this.on(ChargingStationEvents.updated, () => { |
66a7748d JB |
221 | parentPort?.postMessage(buildUpdatedMessage(this)) |
222 | }) | |
b88c8cf6 | 223 | this.on(ChargingStationEvents.accepted, () => { |
e054fc1c | 224 | this.startMessageSequence( |
2960841f | 225 | this.wsConnectionRetried |
e054fc1c JB |
226 | ? true |
227 | : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration | |
228 | ).catch(error => { | |
b88c8cf6 JB |
229 | logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error) |
230 | }) | |
2960841f | 231 | this.wsConnectionRetried = false |
b88c8cf6 | 232 | }) |
3f597174 JB |
233 | this.on(ChargingStationEvents.rejected, () => { |
234 | this.wsConnectionRetried = false | |
235 | }) | |
e054fc1c JB |
236 | this.on(ChargingStationEvents.disconnected, () => { |
237 | try { | |
238 | this.internalStopMessageSequence() | |
239 | } catch (error) { | |
240 | logger.error( | |
241 | `${this.logPrefix()} Error while stopping the internal message sequence:`, | |
242 | error | |
243 | ) | |
244 | } | |
245 | }) | |
db54d2e0 | 246 | |
52c58949 | 247 | this.initialize(options) |
244c1396 | 248 | |
e9e43cff JB |
249 | this.add() |
250 | ||
52c58949 JB |
251 | if (options?.autoStart != null) { |
252 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
253 | this.stationInfo!.autoStart = options.autoStart | |
254 | } | |
255 | ||
256 | if (this.stationInfo?.autoStart === true) { | |
71ac2bd7 JB |
257 | this.start() |
258 | } | |
c0560973 JB |
259 | } |
260 | ||
66a7748d JB |
261 | public get hasEvses (): boolean { |
262 | return this.connectors.size === 0 && this.evses.size > 0 | |
a14022a2 JB |
263 | } |
264 | ||
1d41bc6b | 265 | public get wsConnectionUrl (): URL { |
fa7bccf4 | 266 | return new URL( |
44eb6026 | 267 | `${ |
4e3b1d6b | 268 | this.stationInfo?.supervisionUrlOcppConfiguration === true && |
5199f9fd | 269 | isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && |
5dc7c990 JB |
270 | isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value) |
271 | ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey)?.value | |
44eb6026 | 272 | : this.configuredSupervisionUrl.href |
5199f9fd | 273 | }/${this.stationInfo?.chargingStationId}` |
66a7748d | 274 | ) |
12fc74d6 JB |
275 | } |
276 | ||
8b7072dc | 277 | public logPrefix = (): string => { |
41f18326 JB |
278 | if ( |
279 | this instanceof ChargingStation && | |
280 | this.stationInfo != null && | |
281 | isNotEmptyString(this.stationInfo.chargingStationId) | |
282 | ) { | |
283 | return logPrefix(` ${this.stationInfo.chargingStationId} |`) | |
c1f16afd | 284 | } |
66a7748d | 285 | let stationTemplate: ChargingStationTemplate | undefined |
c1f16afd JB |
286 | try { |
287 | stationTemplate = JSON.parse( | |
66a7748d JB |
288 | readFileSync(this.templateFile, 'utf8') |
289 | ) as ChargingStationTemplate | |
c1f16afd | 290 | } catch { |
66a7748d | 291 | stationTemplate = undefined |
c1f16afd | 292 | } |
66a7748d JB |
293 | return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`) |
294 | } | |
c0560973 | 295 | |
66a7748d JB |
296 | public hasIdTags (): boolean { |
297 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 298 | return isNotEmptyArray(this.idTagsCache.getIdTags(getIdTagsFile(this.stationInfo!)!)) |
c0560973 JB |
299 | } |
300 | ||
66a7748d | 301 | public getNumberOfPhases (stationInfo?: ChargingStationInfo): number { |
5199f9fd | 302 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
97608fbd | 303 | const localStationInfo = stationInfo ?? this.stationInfo! |
fa7bccf4 | 304 | switch (this.getCurrentOutType(stationInfo)) { |
4c2b4904 | 305 | case CurrentType.AC: |
66a7748d | 306 | return localStationInfo.numberOfPhases ?? 3 |
4c2b4904 | 307 | case CurrentType.DC: |
66a7748d | 308 | return 0 |
c0560973 JB |
309 | } |
310 | } | |
311 | ||
66a7748d | 312 | public isWebSocketConnectionOpened (): boolean { |
5199f9fd | 313 | return this.wsConnection?.readyState === WebSocket.OPEN |
c0560973 JB |
314 | } |
315 | ||
66a7748d | 316 | public inUnknownState (): boolean { |
5199f9fd | 317 | return this.bootNotificationResponse?.status == null |
73c4266d JB |
318 | } |
319 | ||
66a7748d | 320 | public inPendingState (): boolean { |
5199f9fd | 321 | return this.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING |
16cd35ad JB |
322 | } |
323 | ||
66a7748d | 324 | public inAcceptedState (): boolean { |
5199f9fd | 325 | return this.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED |
c0560973 JB |
326 | } |
327 | ||
66a7748d | 328 | public inRejectedState (): boolean { |
5199f9fd | 329 | return this.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED |
16cd35ad JB |
330 | } |
331 | ||
66a7748d JB |
332 | public isRegistered (): boolean { |
333 | return !this.inUnknownState() && (this.inAcceptedState() || this.inPendingState()) | |
16cd35ad JB |
334 | } |
335 | ||
66a7748d JB |
336 | public isChargingStationAvailable (): boolean { |
337 | return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative | |
c0560973 JB |
338 | } |
339 | ||
66a7748d | 340 | public hasConnector (connectorId: number): boolean { |
a14022a2 JB |
341 | if (this.hasEvses) { |
342 | for (const evseStatus of this.evses.values()) { | |
343 | if (evseStatus.connectors.has(connectorId)) { | |
66a7748d | 344 | return true |
a14022a2 JB |
345 | } |
346 | } | |
66a7748d | 347 | return false |
a14022a2 | 348 | } |
66a7748d | 349 | return this.connectors.has(connectorId) |
a14022a2 JB |
350 | } |
351 | ||
66a7748d | 352 | public isConnectorAvailable (connectorId: number): boolean { |
28e78158 JB |
353 | return ( |
354 | connectorId > 0 && | |
355 | this.getConnectorStatus(connectorId)?.availability === AvailabilityType.Operative | |
66a7748d | 356 | ) |
c0560973 JB |
357 | } |
358 | ||
66a7748d | 359 | public getNumberOfConnectors (): number { |
28e78158 | 360 | if (this.hasEvses) { |
66a7748d | 361 | let numberOfConnectors = 0 |
28e78158 | 362 | for (const [evseId, evseStatus] of this.evses) { |
4334db72 | 363 | if (evseId > 0) { |
66a7748d | 364 | numberOfConnectors += evseStatus.connectors.size |
28e78158 | 365 | } |
28e78158 | 366 | } |
66a7748d | 367 | return numberOfConnectors |
28e78158 | 368 | } |
66a7748d | 369 | return this.connectors.has(0) ? this.connectors.size - 1 : this.connectors.size |
54544ef1 JB |
370 | } |
371 | ||
66a7748d JB |
372 | public getNumberOfEvses (): number { |
373 | return this.evses.has(0) ? this.evses.size - 1 : this.evses.size | |
28e78158 JB |
374 | } |
375 | ||
66a7748d | 376 | public getConnectorStatus (connectorId: number): ConnectorStatus | undefined { |
28e78158 JB |
377 | if (this.hasEvses) { |
378 | for (const evseStatus of this.evses.values()) { | |
379 | if (evseStatus.connectors.has(connectorId)) { | |
66a7748d | 380 | return evseStatus.connectors.get(connectorId) |
28e78158 JB |
381 | } |
382 | } | |
66a7748d | 383 | return undefined |
28e78158 | 384 | } |
66a7748d | 385 | return this.connectors.get(connectorId) |
c0560973 JB |
386 | } |
387 | ||
66a7748d JB |
388 | public getConnectorMaximumAvailablePower (connectorId: number): number { |
389 | let connectorAmperageLimitationPowerLimit: number | undefined | |
2466918c | 390 | const amperageLimitation = this.getAmperageLimitation() |
b47d68d7 | 391 | if ( |
2466918c | 392 | amperageLimitation != null && |
66a7748d | 393 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2466918c | 394 | amperageLimitation < this.stationInfo!.maximumAmperage! |
b47d68d7 | 395 | ) { |
4160ae28 | 396 | connectorAmperageLimitationPowerLimit = |
5398cecf | 397 | (this.stationInfo?.currentOutType === CurrentType.AC |
cc6e8ab5 | 398 | ? ACElectricUtils.powerTotal( |
66a7748d JB |
399 | this.getNumberOfPhases(), |
400 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
401 | this.stationInfo.voltageOut!, | |
2466918c | 402 | amperageLimitation * |
66a7748d JB |
403 | (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()) |
404 | ) | |
405 | : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
2466918c | 406 | DCElectricUtils.power(this.stationInfo!.voltageOut!, amperageLimitation)) / |
5199f9fd JB |
407 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
408 | this.powerDivider! | |
cc6e8ab5 | 409 | } |
66a7748d | 410 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
5199f9fd | 411 | const connectorMaximumPower = this.stationInfo!.maximumPower! / this.powerDivider! |
15068be9 | 412 | const connectorChargingProfilesPowerLimit = |
66a7748d | 413 | getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId) |
5adf6ca4 | 414 | return min( |
ad8537a7 | 415 | isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, |
66a7748d | 416 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 417 | isNaN(connectorAmperageLimitationPowerLimit!) |
ad8537a7 | 418 | ? Infinity |
66a7748d JB |
419 | : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
420 | connectorAmperageLimitationPowerLimit!, | |
421 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
422 | isNaN(connectorChargingProfilesPowerLimit!) ? Infinity : connectorChargingProfilesPowerLimit! | |
423 | ) | |
cc6e8ab5 JB |
424 | } |
425 | ||
66a7748d | 426 | public getTransactionIdTag (transactionId: number): string | undefined { |
28e78158 JB |
427 | if (this.hasEvses) { |
428 | for (const evseStatus of this.evses.values()) { | |
429 | for (const connectorStatus of evseStatus.connectors.values()) { | |
430 | if (connectorStatus.transactionId === transactionId) { | |
66a7748d | 431 | return connectorStatus.transactionIdTag |
28e78158 JB |
432 | } |
433 | } | |
434 | } | |
435 | } else { | |
436 | for (const connectorId of this.connectors.keys()) { | |
3fa7f799 | 437 | if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) { |
66a7748d | 438 | return this.getConnectorStatus(connectorId)?.transactionIdTag |
28e78158 | 439 | } |
c0560973 JB |
440 | } |
441 | } | |
442 | } | |
443 | ||
66a7748d JB |
444 | public getNumberOfRunningTransactions (): number { |
445 | let numberOfRunningTransactions = 0 | |
ded57f02 | 446 | if (this.hasEvses) { |
3fa7f799 JB |
447 | for (const [evseId, evseStatus] of this.evses) { |
448 | if (evseId === 0) { | |
66a7748d | 449 | continue |
3fa7f799 | 450 | } |
ded57f02 JB |
451 | for (const connectorStatus of evseStatus.connectors.values()) { |
452 | if (connectorStatus.transactionStarted === true) { | |
66a7748d | 453 | ++numberOfRunningTransactions |
ded57f02 JB |
454 | } |
455 | } | |
456 | } | |
457 | } else { | |
458 | for (const connectorId of this.connectors.keys()) { | |
459 | if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { | |
66a7748d | 460 | ++numberOfRunningTransactions |
ded57f02 JB |
461 | } |
462 | } | |
463 | } | |
66a7748d | 464 | return numberOfRunningTransactions |
ded57f02 JB |
465 | } |
466 | ||
f938317f JB |
467 | public getConnectorIdByTransactionId (transactionId: number | undefined): number | undefined { |
468 | if (transactionId == null) { | |
469 | return undefined | |
470 | } else if (this.hasEvses) { | |
28e78158 JB |
471 | for (const evseStatus of this.evses.values()) { |
472 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
473 | if (connectorStatus.transactionId === transactionId) { | |
66a7748d | 474 | return connectorId |
28e78158 JB |
475 | } |
476 | } | |
477 | } | |
478 | } else { | |
479 | for (const connectorId of this.connectors.keys()) { | |
3fa7f799 | 480 | if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) { |
66a7748d | 481 | return connectorId |
28e78158 | 482 | } |
c0560973 JB |
483 | } |
484 | } | |
485 | } | |
486 | ||
66a7748d | 487 | public getEnergyActiveImportRegisterByTransactionId ( |
f938317f | 488 | transactionId: number | undefined, |
66a7748d | 489 | rounded = false |
07989fad JB |
490 | ): number { |
491 | return this.getEnergyActiveImportRegister( | |
66a7748d | 492 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
f938317f | 493 | this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)!), |
66a7748d JB |
494 | rounded |
495 | ) | |
cbad1217 JB |
496 | } |
497 | ||
66a7748d | 498 | public getEnergyActiveImportRegisterByConnectorId (connectorId: number, rounded = false): number { |
f938317f | 499 | return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId), rounded) |
6ed92bc1 JB |
500 | } |
501 | ||
66a7748d | 502 | public getAuthorizeRemoteTxRequests (): boolean { |
f2d5e3d9 | 503 | const authorizeRemoteTxRequests = getConfigurationKey( |
17ac262c | 504 | this, |
66a7748d JB |
505 | StandardParametersKey.AuthorizeRemoteTxRequests |
506 | ) | |
a807045b | 507 | return authorizeRemoteTxRequests != null |
4e3b1d6b | 508 | ? convertToBoolean(authorizeRemoteTxRequests.value) |
66a7748d | 509 | : false |
c0560973 JB |
510 | } |
511 | ||
66a7748d | 512 | public getLocalAuthListEnabled (): boolean { |
f2d5e3d9 | 513 | const localAuthListEnabled = getConfigurationKey( |
17ac262c | 514 | this, |
66a7748d JB |
515 | StandardParametersKey.LocalAuthListEnabled |
516 | ) | |
a807045b | 517 | return localAuthListEnabled != null ? convertToBoolean(localAuthListEnabled.value) : false |
c0560973 JB |
518 | } |
519 | ||
66a7748d JB |
520 | public getHeartbeatInterval (): number { |
521 | const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval) | |
a807045b | 522 | if (HeartbeatInterval != null) { |
66a7748d | 523 | return secondsToMilliseconds(convertToInt(HeartbeatInterval.value)) |
8f953431 | 524 | } |
66a7748d | 525 | const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval) |
a807045b | 526 | if (HeartBeatInterval != null) { |
66a7748d | 527 | return secondsToMilliseconds(convertToInt(HeartBeatInterval.value)) |
8f953431 JB |
528 | } |
529 | this.stationInfo?.autoRegister === false && | |
530 | logger.warn( | |
531 | `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${ | |
532 | Constants.DEFAULT_HEARTBEAT_INTERVAL | |
66a7748d JB |
533 | }` |
534 | ) | |
535 | return Constants.DEFAULT_HEARTBEAT_INTERVAL | |
8f953431 JB |
536 | } |
537 | ||
66a7748d | 538 | public setSupervisionUrl (url: string): void { |
269de583 | 539 | if ( |
3e888c65 | 540 | this.stationInfo?.supervisionUrlOcppConfiguration === true && |
5199f9fd | 541 | isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) |
269de583 | 542 | ) { |
5dc7c990 | 543 | setConfigurationKeyValue(this, this.stationInfo.supervisionUrlOcppKey, url) |
269de583 | 544 | } else { |
5199f9fd JB |
545 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
546 | this.stationInfo!.supervisionUrls = url | |
66a7748d JB |
547 | this.saveStationInfo() |
548 | this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl() | |
269de583 JB |
549 | } |
550 | } | |
551 | ||
66a7748d | 552 | public startHeartbeat (): void { |
a807045b | 553 | if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval == null) { |
6a8329b4 JB |
554 | this.heartbeatSetInterval = setInterval(() => { |
555 | this.ocppRequestService | |
556 | .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT) | |
a974c8e4 | 557 | .catch(error => { |
6a8329b4 JB |
558 | logger.error( |
559 | `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`, | |
66a7748d JB |
560 | error |
561 | ) | |
562 | }) | |
563 | }, this.getHeartbeatInterval()) | |
e7aeea18 | 564 | logger.info( |
9bf0ef23 | 565 | `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds( |
66a7748d JB |
566 | this.getHeartbeatInterval() |
567 | )}` | |
568 | ) | |
a807045b | 569 | } else if (this.heartbeatSetInterval != null) { |
e7aeea18 | 570 | logger.info( |
9bf0ef23 | 571 | `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds( |
66a7748d JB |
572 | this.getHeartbeatInterval() |
573 | )}` | |
574 | ) | |
c0560973 | 575 | } else { |
e7aeea18 | 576 | logger.error( |
66a7748d JB |
577 | `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat` |
578 | ) | |
c0560973 JB |
579 | } |
580 | } | |
581 | ||
66a7748d | 582 | public restartHeartbeat (): void { |
c0560973 | 583 | // Stop heartbeat |
66a7748d | 584 | this.stopHeartbeat() |
c0560973 | 585 | // Start heartbeat |
66a7748d | 586 | this.startHeartbeat() |
c0560973 JB |
587 | } |
588 | ||
66a7748d | 589 | public restartWebSocketPing (): void { |
17ac262c | 590 | // Stop WebSocket ping |
66a7748d | 591 | this.stopWebSocketPing() |
17ac262c | 592 | // Start WebSocket ping |
66a7748d | 593 | this.startWebSocketPing() |
17ac262c JB |
594 | } |
595 | ||
66a7748d | 596 | public startMeterValues (connectorId: number, interval: number): void { |
c0560973 | 597 | if (connectorId === 0) { |
66a7748d JB |
598 | logger.error(`${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`) |
599 | return | |
c0560973 | 600 | } |
f938317f JB |
601 | const connectorStatus = this.getConnectorStatus(connectorId) |
602 | if (connectorStatus == null) { | |
e7aeea18 | 603 | logger.error( |
66dd3447 | 604 | `${this.logPrefix()} Trying to start MeterValues on non existing connector id |
66a7748d JB |
605 | ${connectorId}` |
606 | ) | |
607 | return | |
c0560973 | 608 | } |
f938317f | 609 | if (connectorStatus.transactionStarted === false) { |
e7aeea18 | 610 | logger.error( |
66a7748d JB |
611 | `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started` |
612 | ) | |
613 | return | |
e7aeea18 | 614 | } else if ( |
f938317f JB |
615 | connectorStatus.transactionStarted === true && |
616 | connectorStatus.transactionId == null | |
e7aeea18 JB |
617 | ) { |
618 | logger.error( | |
66a7748d JB |
619 | `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id` |
620 | ) | |
621 | return | |
c0560973 JB |
622 | } |
623 | if (interval > 0) { | |
f938317f | 624 | connectorStatus.transactionSetInterval = setInterval(() => { |
6a5f5908 | 625 | const meterValue = buildMeterValue( |
6a8329b4 JB |
626 | this, |
627 | connectorId, | |
66a7748d | 628 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
f938317f | 629 | connectorStatus.transactionId!, |
66a7748d JB |
630 | interval |
631 | ) | |
6a8329b4 JB |
632 | this.ocppRequestService |
633 | .requestHandler<MeterValuesRequest, MeterValuesResponse>( | |
66a7748d JB |
634 | this, |
635 | RequestCommand.METER_VALUES, | |
636 | { | |
637 | connectorId, | |
f938317f | 638 | transactionId: connectorStatus.transactionId, |
66a7748d JB |
639 | meterValue: [meterValue] |
640 | } | |
641 | ) | |
a974c8e4 | 642 | .catch(error => { |
6a8329b4 JB |
643 | logger.error( |
644 | `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`, | |
66a7748d JB |
645 | error |
646 | ) | |
647 | }) | |
648 | }, interval) | |
c0560973 | 649 | } else { |
e7aeea18 JB |
650 | logger.error( |
651 | `${this.logPrefix()} Charging station ${ | |
652 | StandardParametersKey.MeterValueSampleInterval | |
66a7748d JB |
653 | } configuration set to ${interval}, not sending MeterValues` |
654 | ) | |
c0560973 JB |
655 | } |
656 | } | |
657 | ||
66a7748d | 658 | public stopMeterValues (connectorId: number): void { |
f938317f JB |
659 | const connectorStatus = this.getConnectorStatus(connectorId) |
660 | if (connectorStatus?.transactionSetInterval != null) { | |
661 | clearInterval(connectorStatus.transactionSetInterval) | |
04b1261c JB |
662 | } |
663 | } | |
664 | ||
e9e43cff | 665 | private add (): void { |
244c1396 JB |
666 | this.emit(ChargingStationEvents.added) |
667 | } | |
668 | ||
66a7748d JB |
669 | public start (): void { |
670 | if (!this.started) { | |
671 | if (!this.starting) { | |
672 | this.starting = true | |
5398cecf | 673 | if (this.stationInfo?.enableStatistics === true) { |
66a7748d | 674 | this.performanceStatistics?.start() |
0d8852a5 | 675 | } |
66a7748d | 676 | this.openWSConnection() |
0d8852a5 | 677 | // Monitor charging station template file |
1f8f6332 JB |
678 | this.templateFileWatcher = watchJsonFile( |
679 | this.templateFile, | |
680 | FileType.ChargingStationTemplate, | |
681 | this.logPrefix(), | |
682 | undefined, | |
683 | (event, filename): void => { | |
684 | if (isNotEmptyString(filename) && event === 'change') { | |
685 | try { | |
686 | logger.debug( | |
687 | `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${ | |
688 | this.templateFile | |
66a7748d JB |
689 | } file have changed, reload` |
690 | ) | |
691 | this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash) | |
1f8f6332 | 692 | // Initialize |
66a7748d JB |
693 | this.initialize() |
694 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 695 | this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo!)!) |
1f8f6332 | 696 | // Restart the ATG |
e054fc1c JB |
697 | const ATGStarted = this.automaticTransactionGenerator?.started |
698 | if (ATGStarted === true) { | |
699 | this.stopAutomaticTransactionGenerator() | |
700 | } | |
66a7748d | 701 | delete this.automaticTransactionGeneratorConfiguration |
e054fc1c JB |
702 | if ( |
703 | this.getAutomaticTransactionGeneratorConfiguration()?.enable === true && | |
704 | ATGStarted === true | |
705 | ) { | |
706 | this.startAutomaticTransactionGenerator(undefined, true) | |
1f8f6332 | 707 | } |
5398cecf | 708 | if (this.stationInfo?.enableStatistics === true) { |
66a7748d | 709 | this.performanceStatistics?.restart() |
1f8f6332 | 710 | } else { |
66a7748d | 711 | this.performanceStatistics?.stop() |
1f8f6332 JB |
712 | } |
713 | // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed | |
714 | } catch (error) { | |
715 | logger.error( | |
716 | `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`, | |
66a7748d JB |
717 | error |
718 | ) | |
1f8f6332 JB |
719 | } |
720 | } | |
66a7748d JB |
721 | } |
722 | ) | |
723 | this.started = true | |
724 | this.emit(ChargingStationEvents.started) | |
725 | this.starting = false | |
0d8852a5 | 726 | } else { |
66a7748d | 727 | logger.warn(`${this.logPrefix()} Charging station is already starting...`) |
0d8852a5 | 728 | } |
950b1349 | 729 | } else { |
66a7748d | 730 | logger.warn(`${this.logPrefix()} Charging station is already started...`) |
950b1349 | 731 | } |
c0560973 JB |
732 | } |
733 | ||
7e3bde4f JB |
734 | public async stop ( |
735 | reason?: StopTransactionReason, | |
736 | stopTransactions = this.stationInfo?.stopTransactionsOnStopped | |
737 | ): Promise<void> { | |
66a7748d JB |
738 | if (this.started) { |
739 | if (!this.stopping) { | |
740 | this.stopping = true | |
741 | await this.stopMessageSequence(reason, stopTransactions) | |
742 | this.closeWSConnection() | |
5398cecf | 743 | if (this.stationInfo?.enableStatistics === true) { |
66a7748d | 744 | this.performanceStatistics?.stop() |
0d8852a5 | 745 | } |
66a7748d JB |
746 | this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash) |
747 | this.templateFileWatcher?.close() | |
748 | this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash) | |
749 | delete this.bootNotificationResponse | |
750 | this.started = false | |
751 | this.saveConfiguration() | |
752 | this.emit(ChargingStationEvents.stopped) | |
753 | this.stopping = false | |
0d8852a5 | 754 | } else { |
66a7748d | 755 | logger.warn(`${this.logPrefix()} Charging station is already stopping...`) |
c0560973 | 756 | } |
950b1349 | 757 | } else { |
66a7748d | 758 | logger.warn(`${this.logPrefix()} Charging station is already stopped...`) |
c0560973 | 759 | } |
c0560973 JB |
760 | } |
761 | ||
66a7748d JB |
762 | public async reset (reason?: StopTransactionReason): Promise<void> { |
763 | await this.stop(reason) | |
764 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 765 | await sleep(this.stationInfo!.resetTime!) |
66a7748d JB |
766 | this.initialize() |
767 | this.start() | |
94ec7e96 JB |
768 | } |
769 | ||
66a7748d | 770 | public saveOcppConfiguration (): void { |
5398cecf | 771 | if (this.stationInfo?.ocppPersistentConfiguration === true) { |
66a7748d | 772 | this.saveConfiguration() |
e6895390 JB |
773 | } |
774 | } | |
775 | ||
66a7748d JB |
776 | public bufferMessage (message: string): void { |
777 | this.messageBuffer.add(message) | |
778 | this.setIntervalFlushMessageBuffer() | |
3ba2381e JB |
779 | } |
780 | ||
66a7748d | 781 | public openWSConnection ( |
7f3decca | 782 | options?: WsOptions, |
66a7748d | 783 | params?: { closeOpened?: boolean, terminateOpened?: boolean } |
db2336d9 | 784 | ): void { |
7f3decca JB |
785 | options = { |
786 | handshakeTimeout: secondsToMilliseconds(this.getConnectionTimeout()), | |
e6a33233 | 787 | ...this.stationInfo?.wsOptions, |
66a7748d JB |
788 | ...options |
789 | } | |
790 | params = { ...{ closeOpened: false, terminateOpened: false }, ...params } | |
e6a33233 | 791 | if (!checkChargingStation(this, this.logPrefix())) { |
66a7748d | 792 | return |
d1c6c833 | 793 | } |
5199f9fd | 794 | if (this.stationInfo?.supervisionUser != null && this.stationInfo.supervisionPassword != null) { |
66a7748d | 795 | options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}` |
db2336d9 | 796 | } |
5199f9fd | 797 | if (params.closeOpened === true) { |
66a7748d | 798 | this.closeWSConnection() |
db2336d9 | 799 | } |
5199f9fd | 800 | if (params.terminateOpened === true) { |
66a7748d | 801 | this.terminateWSConnection() |
db2336d9 | 802 | } |
db2336d9 | 803 | |
66a7748d | 804 | if (this.isWebSocketConnectionOpened()) { |
0a03f36c | 805 | logger.warn( |
1d41bc6b | 806 | `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.href} is already opened` |
66a7748d JB |
807 | ) |
808 | return | |
0a03f36c JB |
809 | } |
810 | ||
1d41bc6b | 811 | logger.info(`${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.href}`) |
db2336d9 | 812 | |
feff11ec JB |
813 | this.wsConnection = new WebSocket( |
814 | this.wsConnectionUrl, | |
9a77cc07 | 815 | `ocpp${this.stationInfo?.ocppVersion}`, |
66a7748d JB |
816 | options |
817 | ) | |
db2336d9 JB |
818 | |
819 | // Handle WebSocket message | |
968f0e47 JB |
820 | this.wsConnection.on('message', data => { |
821 | this.onMessage(data).catch(Constants.EMPTY_FUNCTION) | |
822 | }) | |
db2336d9 | 823 | // Handle WebSocket error |
ba9a56a6 | 824 | this.wsConnection.on('error', this.onError.bind(this)) |
db2336d9 | 825 | // Handle WebSocket close |
ba9a56a6 | 826 | this.wsConnection.on('close', this.onClose.bind(this)) |
db2336d9 | 827 | // Handle WebSocket open |
968f0e47 | 828 | this.wsConnection.on('open', () => { |
5a15db90 JB |
829 | this.onOpen().catch(error => |
830 | logger.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error) | |
831 | ) | |
968f0e47 | 832 | }) |
db2336d9 | 833 | // Handle WebSocket ping |
ba9a56a6 | 834 | this.wsConnection.on('ping', this.onPing.bind(this)) |
db2336d9 | 835 | // Handle WebSocket pong |
ba9a56a6 | 836 | this.wsConnection.on('pong', this.onPong.bind(this)) |
db2336d9 JB |
837 | } |
838 | ||
66a7748d JB |
839 | public closeWSConnection (): void { |
840 | if (this.isWebSocketConnectionOpened()) { | |
841 | this.wsConnection?.close() | |
842 | this.wsConnection = null | |
db2336d9 JB |
843 | } |
844 | } | |
845 | ||
5199f9fd JB |
846 | public getAutomaticTransactionGeneratorConfiguration (): |
847 | | AutomaticTransactionGeneratorConfiguration | |
848 | | undefined { | |
aa63c9b7 | 849 | if (this.automaticTransactionGeneratorConfiguration == null) { |
c7db8ecb | 850 | let automaticTransactionGeneratorConfiguration: |
66a7748d JB |
851 | | AutomaticTransactionGeneratorConfiguration |
852 | | undefined | |
853 | const stationTemplate = this.getTemplateFromFile() | |
854 | const stationConfiguration = this.getConfigurationFromFile() | |
c7db8ecb | 855 | if ( |
5398cecf | 856 | this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true && |
61854f7c | 857 | stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash && |
66a7748d | 858 | stationConfiguration?.automaticTransactionGenerator != null |
c7db8ecb JB |
859 | ) { |
860 | automaticTransactionGeneratorConfiguration = | |
5199f9fd | 861 | stationConfiguration.automaticTransactionGenerator |
c7db8ecb | 862 | } else { |
66a7748d | 863 | automaticTransactionGeneratorConfiguration = stationTemplate?.AutomaticTransactionGenerator |
c7db8ecb JB |
864 | } |
865 | this.automaticTransactionGeneratorConfiguration = { | |
866 | ...Constants.DEFAULT_ATG_CONFIGURATION, | |
66a7748d JB |
867 | ...automaticTransactionGeneratorConfiguration |
868 | } | |
ac7f79af | 869 | } |
aa63c9b7 | 870 | return this.automaticTransactionGeneratorConfiguration |
ac7f79af JB |
871 | } |
872 | ||
66a7748d JB |
873 | public getAutomaticTransactionGeneratorStatuses (): Status[] | undefined { |
874 | return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses | |
5ced7e80 JB |
875 | } |
876 | ||
e054fc1c JB |
877 | public startAutomaticTransactionGenerator ( |
878 | connectorIds?: number[], | |
879 | stopAbsoluteDuration?: boolean | |
880 | ): void { | |
66a7748d | 881 | this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this) |
9bf0ef23 | 882 | if (isNotEmptyArray(connectorIds)) { |
5dc7c990 | 883 | for (const connectorId of connectorIds) { |
e054fc1c | 884 | this.automaticTransactionGenerator?.startConnector(connectorId, stopAbsoluteDuration) |
a5e9befc JB |
885 | } |
886 | } else { | |
e054fc1c | 887 | this.automaticTransactionGenerator?.start(stopAbsoluteDuration) |
4f69be04 | 888 | } |
66a7748d JB |
889 | this.saveAutomaticTransactionGeneratorConfiguration() |
890 | this.emit(ChargingStationEvents.updated) | |
4f69be04 JB |
891 | } |
892 | ||
66a7748d | 893 | public stopAutomaticTransactionGenerator (connectorIds?: number[]): void { |
9bf0ef23 | 894 | if (isNotEmptyArray(connectorIds)) { |
5dc7c990 | 895 | for (const connectorId of connectorIds) { |
66a7748d | 896 | this.automaticTransactionGenerator?.stopConnector(connectorId) |
a5e9befc JB |
897 | } |
898 | } else { | |
66a7748d | 899 | this.automaticTransactionGenerator?.stop() |
4f69be04 | 900 | } |
66a7748d JB |
901 | this.saveAutomaticTransactionGeneratorConfiguration() |
902 | this.emit(ChargingStationEvents.updated) | |
4f69be04 JB |
903 | } |
904 | ||
66a7748d | 905 | public async stopTransactionOnConnector ( |
5e3cb728 | 906 | connectorId: number, |
66a7748d | 907 | reason?: StopTransactionReason |
5e3cb728 | 908 | ): Promise<StopTransactionResponse> { |
f938317f | 909 | const transactionId = this.getConnectorStatus(connectorId)?.transactionId |
5e3cb728 | 910 | if ( |
5398cecf | 911 | this.stationInfo?.beginEndMeterValues === true && |
5199f9fd JB |
912 | this.stationInfo.ocppStrictCompliance === true && |
913 | this.stationInfo.outOfOrderEndMeterValues === false | |
5e3cb728 | 914 | ) { |
41f3983a | 915 | const transactionEndMeterValue = buildTransactionEndMeterValue( |
5e3cb728 JB |
916 | this, |
917 | connectorId, | |
2466918c | 918 | this.getEnergyActiveImportRegisterByTransactionId(transactionId) |
66a7748d | 919 | ) |
5e3cb728 JB |
920 | await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>( |
921 | this, | |
922 | RequestCommand.METER_VALUES, | |
923 | { | |
924 | connectorId, | |
925 | transactionId, | |
66a7748d JB |
926 | meterValue: [transactionEndMeterValue] |
927 | } | |
928 | ) | |
5e3cb728 | 929 | } |
66a7748d JB |
930 | return await this.ocppRequestService.requestHandler< |
931 | StopTransactionRequest, | |
932 | StopTransactionResponse | |
933 | >(this, RequestCommand.STOP_TRANSACTION, { | |
934 | transactionId, | |
2466918c | 935 | meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId, true), |
aa63c9b7 | 936 | ...(reason != null && { reason }) |
66a7748d | 937 | }) |
5e3cb728 JB |
938 | } |
939 | ||
66a7748d | 940 | public getReserveConnectorZeroSupported (): boolean { |
9bf0ef23 | 941 | return convertToBoolean( |
66a7748d JB |
942 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
943 | getConfigurationKey(this, StandardParametersKey.ReserveConnectorZeroSupported)!.value | |
944 | ) | |
24578c31 JB |
945 | } |
946 | ||
66a7748d JB |
947 | public async addReservation (reservation: Reservation): Promise<void> { |
948 | const reservationFound = this.getReservationBy('reservationId', reservation.reservationId) | |
a807045b | 949 | if (reservationFound != null) { |
66a7748d | 950 | await this.removeReservation(reservationFound, ReservationTerminationReason.REPLACE_EXISTING) |
d193a949 | 951 | } |
66a7748d JB |
952 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
953 | this.getConnectorStatus(reservation.connectorId)!.reservation = reservation | |
041365be | 954 | await sendAndSetConnectorStatus( |
d193a949 | 955 | this, |
ec94a3cf JB |
956 | reservation.connectorId, |
957 | ConnectorStatusEnum.Reserved, | |
e1d9a0f4 | 958 | undefined, |
66a7748d JB |
959 | { send: reservation.connectorId !== 0 } |
960 | ) | |
24578c31 JB |
961 | } |
962 | ||
66a7748d | 963 | public async removeReservation ( |
d193a949 | 964 | reservation: Reservation, |
66a7748d | 965 | reason: ReservationTerminationReason |
d193a949 | 966 | ): Promise<void> { |
66a7748d JB |
967 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
968 | const connector = this.getConnectorStatus(reservation.connectorId)! | |
d193a949 | 969 | switch (reason) { |
96d96b12 | 970 | case ReservationTerminationReason.CONNECTOR_STATE_CHANGED: |
ec94a3cf | 971 | case ReservationTerminationReason.TRANSACTION_STARTED: |
66a7748d JB |
972 | delete connector.reservation |
973 | break | |
e74bc549 JB |
974 | case ReservationTerminationReason.RESERVATION_CANCELED: |
975 | case ReservationTerminationReason.REPLACE_EXISTING: | |
976 | case ReservationTerminationReason.EXPIRED: | |
041365be | 977 | await sendAndSetConnectorStatus( |
d193a949 | 978 | this, |
ec94a3cf JB |
979 | reservation.connectorId, |
980 | ConnectorStatusEnum.Available, | |
e1d9a0f4 | 981 | undefined, |
66a7748d JB |
982 | { send: reservation.connectorId !== 0 } |
983 | ) | |
984 | delete connector.reservation | |
985 | break | |
b029e74e | 986 | default: |
90aceaf6 | 987 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
66a7748d | 988 | throw new BaseError(`Unknown reservation termination reason '${reason}'`) |
d193a949 | 989 | } |
24578c31 JB |
990 | } |
991 | ||
66a7748d | 992 | public getReservationBy ( |
366f75f6 | 993 | filterKey: ReservationKey, |
66a7748d | 994 | value: number | string |
3fa7f799 | 995 | ): Reservation | undefined { |
66dd3447 | 996 | if (this.hasEvses) { |
3fa7f799 JB |
997 | for (const evseStatus of this.evses.values()) { |
998 | for (const connectorStatus of evseStatus.connectors.values()) { | |
5199f9fd | 999 | if (connectorStatus.reservation?.[filterKey] === value) { |
66a7748d | 1000 | return connectorStatus.reservation |
66dd3447 JB |
1001 | } |
1002 | } | |
1003 | } | |
1004 | } else { | |
3fa7f799 | 1005 | for (const connectorStatus of this.connectors.values()) { |
5199f9fd | 1006 | if (connectorStatus.reservation?.[filterKey] === value) { |
66a7748d | 1007 | return connectorStatus.reservation |
66dd3447 JB |
1008 | } |
1009 | } | |
1010 | } | |
d193a949 JB |
1011 | } |
1012 | ||
66a7748d | 1013 | public isConnectorReservable ( |
e6948a57 JB |
1014 | reservationId: number, |
1015 | idTag?: string, | |
66a7748d | 1016 | connectorId?: number |
e6948a57 | 1017 | ): boolean { |
66a7748d | 1018 | const reservation = this.getReservationBy('reservationId', reservationId) |
300418e9 | 1019 | const reservationExists = reservation !== undefined && !hasReservationExpired(reservation) |
e6948a57 | 1020 | if (arguments.length === 1) { |
66a7748d | 1021 | return !reservationExists |
e6948a57 | 1022 | } else if (arguments.length > 1) { |
300418e9 JB |
1023 | const userReservation = |
1024 | idTag !== undefined ? this.getReservationBy('idTag', idTag) : undefined | |
e6948a57 | 1025 | const userReservationExists = |
300418e9 JB |
1026 | userReservation !== undefined && !hasReservationExpired(userReservation) |
1027 | const notConnectorZero = connectorId === undefined ? true : connectorId > 0 | |
66a7748d | 1028 | const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0 |
e6948a57 JB |
1029 | return ( |
1030 | !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable | |
66a7748d | 1031 | ) |
e6948a57 | 1032 | } |
66a7748d | 1033 | return false |
e6948a57 JB |
1034 | } |
1035 | ||
66a7748d | 1036 | private setIntervalFlushMessageBuffer (): void { |
a807045b | 1037 | if (this.flushMessageBufferSetInterval == null) { |
2a2ad81b | 1038 | this.flushMessageBufferSetInterval = setInterval(() => { |
66a7748d JB |
1039 | if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) { |
1040 | this.flushMessageBuffer() | |
2a2ad81b JB |
1041 | } |
1042 | if (this.messageBuffer.size === 0) { | |
66a7748d | 1043 | this.clearIntervalFlushMessageBuffer() |
2a2ad81b | 1044 | } |
66a7748d | 1045 | }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL) |
2a2ad81b JB |
1046 | } |
1047 | } | |
1048 | ||
66a7748d | 1049 | private clearIntervalFlushMessageBuffer (): void { |
a807045b | 1050 | if (this.flushMessageBufferSetInterval != null) { |
66a7748d JB |
1051 | clearInterval(this.flushMessageBufferSetInterval) |
1052 | delete this.flushMessageBufferSetInterval | |
2a2ad81b JB |
1053 | } |
1054 | } | |
1055 | ||
66a7748d JB |
1056 | private getNumberOfReservableConnectors (): number { |
1057 | let numberOfReservableConnectors = 0 | |
66dd3447 | 1058 | if (this.hasEvses) { |
3fa7f799 | 1059 | for (const evseStatus of this.evses.values()) { |
66a7748d | 1060 | numberOfReservableConnectors += getNumberOfReservableConnectors(evseStatus.connectors) |
66dd3447 JB |
1061 | } |
1062 | } else { | |
66a7748d | 1063 | numberOfReservableConnectors = getNumberOfReservableConnectors(this.connectors) |
66dd3447 | 1064 | } |
66a7748d | 1065 | return numberOfReservableConnectors - this.getNumberOfReservationsOnConnectorZero() |
66dd3447 JB |
1066 | } |
1067 | ||
66a7748d | 1068 | private getNumberOfReservationsOnConnectorZero (): number { |
6913d568 | 1069 | if ( |
66a7748d JB |
1070 | (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation != null) || |
1071 | (!this.hasEvses && this.connectors.get(0)?.reservation != null) | |
6913d568 | 1072 | ) { |
66a7748d | 1073 | return 1 |
66dd3447 | 1074 | } |
66a7748d | 1075 | return 0 |
24578c31 JB |
1076 | } |
1077 | ||
66a7748d | 1078 | private flushMessageBuffer (): void { |
8e242273 | 1079 | if (this.messageBuffer.size > 0) { |
7d3b0f64 | 1080 | for (const message of this.messageBuffer.values()) { |
66a7748d JB |
1081 | let beginId: string | undefined |
1082 | let commandName: RequestCommand | undefined | |
1083 | const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse | |
1084 | const isRequest = messageType === MessageType.CALL_MESSAGE | |
1431af78 | 1085 | if (isRequest) { |
66a7748d JB |
1086 | [, , commandName] = JSON.parse(message) as OutgoingRequest |
1087 | beginId = PerformanceStatistics.beginMeasure(commandName) | |
1431af78 | 1088 | } |
d42379d8 | 1089 | this.wsConnection?.send(message, (error?: Error) => { |
66a7748d JB |
1090 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1091 | isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!) | |
aa63c9b7 | 1092 | if (error == null) { |
d42379d8 | 1093 | logger.debug( |
041365be | 1094 | `${this.logPrefix()} >> Buffered ${getMessageTypeString( |
66a7748d JB |
1095 | messageType |
1096 | )} OCPP message sent '${JSON.stringify(message)}'` | |
1097 | ) | |
1098 | this.messageBuffer.delete(message) | |
041365be JB |
1099 | } else { |
1100 | logger.debug( | |
1101 | `${this.logPrefix()} >> Buffered ${getMessageTypeString( | |
66a7748d | 1102 | messageType |
041365be | 1103 | )} OCPP message '${JSON.stringify(message)}' send failed:`, |
66a7748d JB |
1104 | error |
1105 | ) | |
d42379d8 | 1106 | } |
66a7748d | 1107 | }) |
7d3b0f64 | 1108 | } |
77f00f84 JB |
1109 | } |
1110 | } | |
1111 | ||
66a7748d JB |
1112 | private getTemplateFromFile (): ChargingStationTemplate | undefined { |
1113 | let template: ChargingStationTemplate | undefined | |
5ad8570f | 1114 | try { |
cda5d0fb | 1115 | if (this.sharedLRUCache.hasChargingStationTemplate(this.templateFileHash)) { |
66a7748d | 1116 | template = this.sharedLRUCache.getChargingStationTemplate(this.templateFileHash) |
7c72977b | 1117 | } else { |
66a7748d JB |
1118 | const measureId = `${FileType.ChargingStationTemplate} read` |
1119 | const beginId = PerformanceStatistics.beginMeasure(measureId) | |
1120 | template = JSON.parse(readFileSync(this.templateFile, 'utf8')) as ChargingStationTemplate | |
1121 | PerformanceStatistics.endMeasure(measureId, beginId) | |
d972af76 | 1122 | template.templateHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
7c72977b | 1123 | .update(JSON.stringify(template)) |
66a7748d JB |
1124 | .digest('hex') |
1125 | this.sharedLRUCache.setChargingStationTemplate(template) | |
1126 | this.templateFileHash = template.templateHash | |
7c72977b | 1127 | } |
5ad8570f | 1128 | } catch (error) { |
fa5995d6 | 1129 | handleFileException( |
2484ac1e | 1130 | this.templateFile, |
7164966d JB |
1131 | FileType.ChargingStationTemplate, |
1132 | error as NodeJS.ErrnoException, | |
66a7748d JB |
1133 | this.logPrefix() |
1134 | ) | |
1135 | } | |
1136 | return template | |
1137 | } | |
1138 | ||
1139 | private getStationInfoFromTemplate (): ChargingStationInfo { | |
1140 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
97608fbd | 1141 | const stationTemplate = this.getTemplateFromFile()! |
66a7748d JB |
1142 | checkTemplate(stationTemplate, this.logPrefix(), this.templateFile) |
1143 | const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this) | |
1144 | warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile) | |
5199f9fd | 1145 | if (stationTemplate.Connectors != null) { |
66a7748d JB |
1146 | checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile) |
1147 | } | |
97608fbd | 1148 | const stationInfo = stationTemplateToStationInfo(stationTemplate) |
66a7748d | 1149 | stationInfo.hashId = getHashId(this.index, stationTemplate) |
244c1396 | 1150 | stationInfo.autoStart = stationTemplate.autoStart ?? true |
1fdb60b6 | 1151 | stationInfo.templateName = parse(this.templateFile).name |
66a7748d | 1152 | stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate) |
5199f9fd | 1153 | stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16 |
66a7748d JB |
1154 | createSerialNumber(stationTemplate, stationInfo) |
1155 | stationInfo.voltageOut = this.getVoltageOut(stationInfo) | |
5199f9fd | 1156 | if (isNotEmptyArray(stationTemplate.power)) { |
66a7748d | 1157 | const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length) |
cc6e8ab5 | 1158 | stationInfo.maximumPower = |
5199f9fd | 1159 | stationTemplate.powerUnit === PowerUnits.KILO_WATT |
fa7bccf4 | 1160 | ? stationTemplate.power[powerArrayRandomIndex] * 1000 |
66a7748d | 1161 | : stationTemplate.power[powerArrayRandomIndex] |
5ad8570f | 1162 | } else { |
cc6e8ab5 | 1163 | stationInfo.maximumPower = |
5199f9fd | 1164 | stationTemplate.powerUnit === PowerUnits.KILO_WATT |
5dc7c990 JB |
1165 | ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1166 | stationTemplate.power! * 1000 | |
66a7748d | 1167 | : stationTemplate.power |
fa7bccf4 | 1168 | } |
66a7748d | 1169 | stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo) |
3637ca2c | 1170 | stationInfo.firmwareVersionPattern = |
5199f9fd | 1171 | stationTemplate.firmwareVersionPattern ?? Constants.SEMVER_PATTERN |
3637ca2c | 1172 | if ( |
9bf0ef23 | 1173 | isNotEmptyString(stationInfo.firmwareVersion) && |
5dc7c990 | 1174 | !new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion) |
3637ca2c JB |
1175 | ) { |
1176 | logger.warn( | |
1177 | `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${ | |
1178 | this.templateFile | |
66a7748d JB |
1179 | } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'` |
1180 | ) | |
3637ca2c | 1181 | } |
598c886d | 1182 | stationInfo.firmwareUpgrade = merge<FirmwareUpgrade>( |
15748260 | 1183 | { |
598c886d | 1184 | versionUpgrade: { |
66a7748d | 1185 | step: 1 |
598c886d | 1186 | }, |
66a7748d | 1187 | reset: true |
15748260 | 1188 | }, |
5199f9fd | 1189 | stationTemplate.firmwareUpgrade ?? {} |
66a7748d | 1190 | ) |
aa63c9b7 | 1191 | stationInfo.resetTime = |
5199f9fd | 1192 | stationTemplate.resetTime != null |
aa63c9b7 | 1193 | ? secondsToMilliseconds(stationTemplate.resetTime) |
7e3bde4f | 1194 | : Constants.DEFAULT_CHARGING_STATION_RESET_TIME |
66a7748d | 1195 | return stationInfo |
5ad8570f JB |
1196 | } |
1197 | ||
66a7748d | 1198 | private getStationInfoFromFile ( |
52c58949 | 1199 | stationInfoPersistentConfiguration?: boolean |
78786898 | 1200 | ): ChargingStationInfo | undefined { |
66a7748d | 1201 | let stationInfo: ChargingStationInfo | undefined |
52c58949 | 1202 | if (stationInfoPersistentConfiguration === true) { |
66a7748d JB |
1203 | stationInfo = this.getConfigurationFromFile()?.stationInfo |
1204 | if (stationInfo != null) { | |
5199f9fd | 1205 | delete stationInfo.infoHash |
1fdb60b6 JB |
1206 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
1207 | if (stationInfo.templateName == null) { | |
1208 | stationInfo.templateName = parse(this.templateFile).name | |
1209 | } | |
244c1396 JB |
1210 | if (stationInfo.autoStart == null) { |
1211 | stationInfo.autoStart = true | |
1212 | } | |
f832e5df JB |
1213 | } |
1214 | } | |
66a7748d | 1215 | return stationInfo |
2484ac1e JB |
1216 | } |
1217 | ||
52c58949 | 1218 | private getStationInfo (stationInfoPersistentConfiguration?: boolean): ChargingStationInfo { |
97608fbd | 1219 | const stationInfoFromTemplate = this.getStationInfoFromTemplate() |
52c58949 JB |
1220 | stationInfoPersistentConfiguration != null && |
1221 | (stationInfoFromTemplate.stationInfoPersistentConfiguration = | |
1222 | stationInfoPersistentConfiguration) | |
97608fbd | 1223 | const stationInfoFromFile = this.getStationInfoFromFile( |
5199f9fd | 1224 | stationInfoFromTemplate.stationInfoPersistentConfiguration |
66a7748d | 1225 | ) |
6b90dcca JB |
1226 | // Priority: |
1227 | // 1. charging station info from template | |
1228 | // 2. charging station info from configuration file | |
2466918c JB |
1229 | if ( |
1230 | stationInfoFromFile != null && | |
1231 | stationInfoFromFile.templateHash === stationInfoFromTemplate.templateHash | |
1232 | ) { | |
52c58949 | 1233 | return { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromFile } |
f765beaa | 1234 | } |
66a7748d | 1235 | stationInfoFromFile != null && |
fba11dc6 | 1236 | propagateSerialNumber( |
5199f9fd | 1237 | this.getTemplateFromFile(), |
fec4d204 | 1238 | stationInfoFromFile, |
66a7748d JB |
1239 | stationInfoFromTemplate |
1240 | ) | |
52c58949 | 1241 | return { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromTemplate } |
2484ac1e JB |
1242 | } |
1243 | ||
66a7748d | 1244 | private saveStationInfo (): void { |
5398cecf | 1245 | if (this.stationInfo?.stationInfoPersistentConfiguration === true) { |
66a7748d | 1246 | this.saveConfiguration() |
ccb1d6e9 | 1247 | } |
2484ac1e JB |
1248 | } |
1249 | ||
66a7748d JB |
1250 | private handleUnsupportedVersion (version: OCPPVersion | undefined): void { |
1251 | const errorMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}` | |
1252 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1253 | throw new BaseError(errorMsg) | |
c0560973 JB |
1254 | } |
1255 | ||
52c58949 | 1256 | private initialize (options?: ChargingStationOptions): void { |
66a7748d JB |
1257 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1258 | const stationTemplate = this.getTemplateFromFile()! | |
1259 | checkTemplate(stationTemplate, this.logPrefix(), this.templateFile) | |
d972af76 JB |
1260 | this.configurationFile = join( |
1261 | dirname(this.templateFile.replace('station-templates', 'configurations')), | |
66a7748d JB |
1262 | `${getHashId(this.index, stationTemplate)}.json` |
1263 | ) | |
1264 | const stationConfiguration = this.getConfigurationFromFile() | |
a4f7c75f | 1265 | if ( |
5199f9fd | 1266 | stationConfiguration?.stationInfo?.templateHash === stationTemplate.templateHash && |
66a7748d | 1267 | (stationConfiguration?.connectorsStatus != null || stationConfiguration?.evsesStatus != null) |
a4f7c75f | 1268 | ) { |
66a7748d JB |
1269 | checkConfiguration(stationConfiguration, this.logPrefix(), this.configurationFile) |
1270 | this.initializeConnectorsOrEvsesFromFile(stationConfiguration) | |
a4f7c75f | 1271 | } else { |
66a7748d | 1272 | this.initializeConnectorsOrEvsesFromTemplate(stationTemplate) |
a4f7c75f | 1273 | } |
52c58949 JB |
1274 | this.stationInfo = this.getStationInfo(options?.persistentConfiguration) |
1275 | if (options?.persistentConfiguration != null) { | |
1276 | this.stationInfo.ocppPersistentConfiguration = options.persistentConfiguration | |
1277 | } | |
1278 | if (options?.persistentConfiguration != null) { | |
1279 | this.stationInfo.automaticTransactionGeneratorPersistentConfiguration = | |
1280 | options.persistentConfiguration | |
1281 | } | |
1282 | if (options?.autoRegister != null) { | |
1283 | this.stationInfo.autoRegister = options.autoRegister | |
1284 | } | |
3637ca2c JB |
1285 | if ( |
1286 | this.stationInfo.firmwareStatus === FirmwareStatus.Installing && | |
9bf0ef23 JB |
1287 | isNotEmptyString(this.stationInfo.firmwareVersion) && |
1288 | isNotEmptyString(this.stationInfo.firmwareVersionPattern) | |
3637ca2c | 1289 | ) { |
2466918c | 1290 | const patternGroup = |
15748260 | 1291 | this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ?? |
5dc7c990 JB |
1292 | this.stationInfo.firmwareVersion.split('.').length |
1293 | const match = new RegExp(this.stationInfo.firmwareVersionPattern) | |
1294 | .exec(this.stationInfo.firmwareVersion) | |
1295 | ?.slice(1, patternGroup + 1) | |
aa63c9b7 JB |
1296 | if (match != null) { |
1297 | const patchLevelIndex = match.length - 1 | |
1298 | match[patchLevelIndex] = ( | |
1299 | convertToInt(match[patchLevelIndex]) + | |
1300 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1301 | this.stationInfo.firmwareUpgrade!.versionUpgrade!.step! | |
1302 | ).toString() | |
1303 | this.stationInfo.firmwareVersion = match.join('.') | |
77807350 | 1304 | } |
3637ca2c | 1305 | } |
66a7748d JB |
1306 | this.saveStationInfo() |
1307 | this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl() | |
5199f9fd | 1308 | if (this.stationInfo.enableStatistics === true) { |
6bccfcbc JB |
1309 | this.performanceStatistics = PerformanceStatistics.getInstance( |
1310 | this.stationInfo.hashId, | |
2466918c | 1311 | this.stationInfo.chargingStationId, |
66a7748d JB |
1312 | this.configuredSupervisionUrl |
1313 | ) | |
6bccfcbc | 1314 | } |
2466918c JB |
1315 | const bootNotificationRequest = createBootNotificationRequest(this.stationInfo) |
1316 | if (bootNotificationRequest == null) { | |
1317 | const errorMsg = 'Error while creating boot notification request' | |
1318 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1319 | throw new BaseError(errorMsg) | |
1320 | } | |
1321 | this.bootNotificationRequest = bootNotificationRequest | |
66a7748d | 1322 | this.powerDivider = this.getPowerDivider() |
692f2f64 | 1323 | // OCPP configuration |
52c58949 | 1324 | this.ocppConfiguration = this.getOcppConfiguration(options?.persistentConfiguration) |
66a7748d JB |
1325 | this.initializeOcppConfiguration() |
1326 | this.initializeOcppServices() | |
5199f9fd | 1327 | if (this.stationInfo.autoRegister === true) { |
692f2f64 JB |
1328 | this.bootNotificationResponse = { |
1329 | currentTime: new Date(), | |
be4c6702 | 1330 | interval: millisecondsToSeconds(this.getHeartbeatInterval()), |
66a7748d JB |
1331 | status: RegistrationStatusEnumType.ACCEPTED |
1332 | } | |
692f2f64 | 1333 | } |
147d0e0f JB |
1334 | } |
1335 | ||
66a7748d JB |
1336 | private initializeOcppServices (): void { |
1337 | const ocppVersion = this.stationInfo?.ocppVersion | |
feff11ec JB |
1338 | switch (ocppVersion) { |
1339 | case OCPPVersion.VERSION_16: | |
1340 | this.ocppIncomingRequestService = | |
66a7748d | 1341 | OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>() |
feff11ec | 1342 | this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>( |
66a7748d JB |
1343 | OCPP16ResponseService.getInstance<OCPP16ResponseService>() |
1344 | ) | |
1345 | break | |
feff11ec JB |
1346 | case OCPPVersion.VERSION_20: |
1347 | case OCPPVersion.VERSION_201: | |
1348 | this.ocppIncomingRequestService = | |
66a7748d | 1349 | OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>() |
feff11ec | 1350 | this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>( |
66a7748d JB |
1351 | OCPP20ResponseService.getInstance<OCPP20ResponseService>() |
1352 | ) | |
1353 | break | |
feff11ec | 1354 | default: |
66a7748d JB |
1355 | this.handleUnsupportedVersion(ocppVersion) |
1356 | break | |
feff11ec JB |
1357 | } |
1358 | } | |
1359 | ||
66a7748d | 1360 | private initializeOcppConfiguration (): void { |
aa63c9b7 | 1361 | if (getConfigurationKey(this, StandardParametersKey.HeartbeatInterval) == null) { |
66a7748d | 1362 | addConfigurationKey(this, StandardParametersKey.HeartbeatInterval, '0') |
f0f65a62 | 1363 | } |
aa63c9b7 | 1364 | if (getConfigurationKey(this, StandardParametersKey.HeartBeatInterval) == null) { |
66a7748d | 1365 | addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { visible: false }) |
f0f65a62 | 1366 | } |
e7aeea18 | 1367 | if ( |
4e3b1d6b | 1368 | this.stationInfo?.supervisionUrlOcppConfiguration === true && |
5199f9fd | 1369 | isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && |
5dc7c990 | 1370 | getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey) == null |
e7aeea18 | 1371 | ) { |
f2d5e3d9 | 1372 | addConfigurationKey( |
17ac262c | 1373 | this, |
5dc7c990 | 1374 | this.stationInfo.supervisionUrlOcppKey, |
fa7bccf4 | 1375 | this.configuredSupervisionUrl.href, |
66a7748d JB |
1376 | { reboot: true } |
1377 | ) | |
e6895390 | 1378 | } else if ( |
4e3b1d6b | 1379 | this.stationInfo?.supervisionUrlOcppConfiguration === false && |
5199f9fd | 1380 | isNotEmptyString(this.stationInfo.supervisionUrlOcppKey) && |
5dc7c990 | 1381 | getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey) != null |
e6895390 | 1382 | ) { |
5dc7c990 | 1383 | deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey, { save: false }) |
12fc74d6 | 1384 | } |
cc6e8ab5 | 1385 | if ( |
9bf0ef23 | 1386 | isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && |
5dc7c990 | 1387 | getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey) == null |
cc6e8ab5 | 1388 | ) { |
f2d5e3d9 | 1389 | addConfigurationKey( |
17ac262c | 1390 | this, |
5dc7c990 | 1391 | this.stationInfo.amperageLimitationOcppKey, |
66a7748d JB |
1392 | // prettier-ignore |
1393 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5dc7c990 | 1394 | (this.stationInfo.maximumAmperage! * getAmperageLimitationUnitDivider(this.stationInfo)).toString() |
66a7748d | 1395 | ) |
cc6e8ab5 | 1396 | } |
aa63c9b7 | 1397 | if (getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles) == null) { |
f2d5e3d9 | 1398 | addConfigurationKey( |
17ac262c | 1399 | this, |
e7aeea18 | 1400 | StandardParametersKey.SupportedFeatureProfiles, |
66a7748d JB |
1401 | `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}` |
1402 | ) | |
e7aeea18 | 1403 | } |
f2d5e3d9 | 1404 | addConfigurationKey( |
17ac262c | 1405 | this, |
e7aeea18 JB |
1406 | StandardParametersKey.NumberOfConnectors, |
1407 | this.getNumberOfConnectors().toString(), | |
a95873d8 | 1408 | { readonly: true }, |
66a7748d JB |
1409 | { overwrite: true } |
1410 | ) | |
aa63c9b7 | 1411 | if (getConfigurationKey(this, StandardParametersKey.MeterValuesSampledData) == null) { |
f2d5e3d9 | 1412 | addConfigurationKey( |
17ac262c | 1413 | this, |
e7aeea18 | 1414 | StandardParametersKey.MeterValuesSampledData, |
66a7748d JB |
1415 | MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER |
1416 | ) | |
7abfea5f | 1417 | } |
aa63c9b7 | 1418 | if (getConfigurationKey(this, StandardParametersKey.ConnectorPhaseRotation) == null) { |
66a7748d | 1419 | const connectorsPhaseRotation: string[] = [] |
28e78158 JB |
1420 | if (this.hasEvses) { |
1421 | for (const evseStatus of this.evses.values()) { | |
1422 | for (const connectorId of evseStatus.connectors.keys()) { | |
dd08d43d | 1423 | connectorsPhaseRotation.push( |
66a7748d JB |
1424 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1425 | getPhaseRotationValue(connectorId, this.getNumberOfPhases())! | |
1426 | ) | |
28e78158 JB |
1427 | } |
1428 | } | |
1429 | } else { | |
1430 | for (const connectorId of this.connectors.keys()) { | |
dd08d43d | 1431 | connectorsPhaseRotation.push( |
66a7748d JB |
1432 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1433 | getPhaseRotationValue(connectorId, this.getNumberOfPhases())! | |
1434 | ) | |
7e1dc878 JB |
1435 | } |
1436 | } | |
f2d5e3d9 | 1437 | addConfigurationKey( |
17ac262c | 1438 | this, |
e7aeea18 | 1439 | StandardParametersKey.ConnectorPhaseRotation, |
66a7748d JB |
1440 | connectorsPhaseRotation.toString() |
1441 | ) | |
7e1dc878 | 1442 | } |
aa63c9b7 | 1443 | if (getConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests) == null) { |
66a7748d | 1444 | addConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests, 'true') |
36f6a92e | 1445 | } |
17ac262c | 1446 | if ( |
aa63c9b7 | 1447 | getConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled) == null && |
a807045b | 1448 | hasFeatureProfile(this, SupportedFeatureProfiles.LocalAuthListManagement) === true |
17ac262c | 1449 | ) { |
66a7748d | 1450 | addConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled, 'false') |
f2d5e3d9 | 1451 | } |
aa63c9b7 | 1452 | if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut) == null) { |
f2d5e3d9 | 1453 | addConfigurationKey( |
17ac262c | 1454 | this, |
e7aeea18 | 1455 | StandardParametersKey.ConnectionTimeOut, |
66a7748d JB |
1456 | Constants.DEFAULT_CONNECTION_TIMEOUT.toString() |
1457 | ) | |
8bce55bf | 1458 | } |
66a7748d | 1459 | this.saveOcppConfiguration() |
073bd098 JB |
1460 | } |
1461 | ||
66a7748d | 1462 | private initializeConnectorsOrEvsesFromFile (configuration: ChargingStationConfiguration): void { |
5199f9fd | 1463 | if (configuration.connectorsStatus != null && configuration.evsesStatus == null) { |
8df5ae48 | 1464 | for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) { |
40615072 | 1465 | this.connectors.set(connectorId, clone<ConnectorStatus>(connectorStatus)) |
8df5ae48 | 1466 | } |
5199f9fd | 1467 | } else if (configuration.evsesStatus != null && configuration.connectorsStatus == null) { |
a4f7c75f | 1468 | for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) { |
40615072 | 1469 | const evseStatus = clone<EvseStatusConfiguration>(evseStatusConfiguration) |
66a7748d | 1470 | delete evseStatus.connectorsStatus |
a4f7c75f | 1471 | this.evses.set(evseId, { |
8df5ae48 | 1472 | ...(evseStatus as EvseStatus), |
a4f7c75f | 1473 | connectors: new Map<number, ConnectorStatus>( |
66a7748d | 1474 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 1475 | evseStatusConfiguration.connectorsStatus!.map((connectorStatus, connectorId) => [ |
a4f7c75f | 1476 | connectorId, |
66a7748d JB |
1477 | connectorStatus |
1478 | ]) | |
1479 | ) | |
1480 | }) | |
a4f7c75f | 1481 | } |
5199f9fd | 1482 | } else if (configuration.evsesStatus != null && configuration.connectorsStatus != null) { |
66a7748d JB |
1483 | const errorMsg = `Connectors and evses defined at the same time in configuration file ${this.configurationFile}` |
1484 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1485 | throw new BaseError(errorMsg) | |
a4f7c75f | 1486 | } else { |
66a7748d JB |
1487 | const errorMsg = `No connectors or evses defined in configuration file ${this.configurationFile}` |
1488 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1489 | throw new BaseError(errorMsg) | |
a4f7c75f JB |
1490 | } |
1491 | } | |
1492 | ||
66a7748d | 1493 | private initializeConnectorsOrEvsesFromTemplate (stationTemplate: ChargingStationTemplate): void { |
5199f9fd | 1494 | if (stationTemplate.Connectors != null && stationTemplate.Evses == null) { |
66a7748d | 1495 | this.initializeConnectorsFromTemplate(stationTemplate) |
5199f9fd | 1496 | } else if (stationTemplate.Evses != null && stationTemplate.Connectors == null) { |
66a7748d | 1497 | this.initializeEvsesFromTemplate(stationTemplate) |
5199f9fd | 1498 | } else if (stationTemplate.Evses != null && stationTemplate.Connectors != null) { |
66a7748d JB |
1499 | const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}` |
1500 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1501 | throw new BaseError(errorMsg) | |
ae25f265 | 1502 | } else { |
66a7748d JB |
1503 | const errorMsg = `No connectors or evses defined in template file ${this.templateFile}` |
1504 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1505 | throw new BaseError(errorMsg) | |
ae25f265 JB |
1506 | } |
1507 | } | |
1508 | ||
66a7748d | 1509 | private initializeConnectorsFromTemplate (stationTemplate: ChargingStationTemplate): void { |
5199f9fd | 1510 | if (stationTemplate.Connectors == null && this.connectors.size === 0) { |
66a7748d JB |
1511 | const errorMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined` |
1512 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1513 | throw new BaseError(errorMsg) | |
3d25cc86 | 1514 | } |
5199f9fd | 1515 | if (stationTemplate.Connectors?.[0] == null) { |
3d25cc86 JB |
1516 | logger.warn( |
1517 | `${this.logPrefix()} Charging station information from template ${ | |
1518 | this.templateFile | |
66a7748d JB |
1519 | } with no connector id 0 configuration` |
1520 | ) | |
3d25cc86 | 1521 | } |
5199f9fd | 1522 | if (stationTemplate.Connectors != null) { |
cda5d0fb | 1523 | const { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors } = |
66a7748d | 1524 | checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile) |
d972af76 | 1525 | const connectorsConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
cda5d0fb | 1526 | .update( |
5199f9fd | 1527 | `${JSON.stringify(stationTemplate.Connectors)}${configuredMaxConnectors.toString()}` |
cda5d0fb | 1528 | ) |
66a7748d | 1529 | .digest('hex') |
3d25cc86 | 1530 | const connectorsConfigChanged = |
5199f9fd JB |
1531 | this.connectors.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash |
1532 | if (this.connectors.size === 0 || connectorsConfigChanged) { | |
66a7748d JB |
1533 | connectorsConfigChanged && this.connectors.clear() |
1534 | this.connectorsConfigurationHash = connectorsConfigHash | |
269196a8 JB |
1535 | if (templateMaxConnectors > 0) { |
1536 | for (let connectorId = 0; connectorId <= configuredMaxConnectors; connectorId++) { | |
1537 | if ( | |
1538 | connectorId === 0 && | |
5199f9fd JB |
1539 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
1540 | (stationTemplate.Connectors[connectorId] == null || | |
66a7748d | 1541 | !this.getUseConnectorId0(stationTemplate)) |
269196a8 | 1542 | ) { |
66a7748d | 1543 | continue |
269196a8 JB |
1544 | } |
1545 | const templateConnectorId = | |
5199f9fd | 1546 | connectorId > 0 && stationTemplate.randomConnectors === true |
9bf0ef23 | 1547 | ? getRandomInteger(templateMaxAvailableConnectors, 1) |
66a7748d | 1548 | : connectorId |
5199f9fd | 1549 | const connectorStatus = stationTemplate.Connectors[templateConnectorId] |
fba11dc6 | 1550 | checkStationInfoConnectorStatus( |
ae25f265 | 1551 | templateConnectorId, |
04b1261c JB |
1552 | connectorStatus, |
1553 | this.logPrefix(), | |
66a7748d JB |
1554 | this.templateFile |
1555 | ) | |
40615072 | 1556 | this.connectors.set(connectorId, clone<ConnectorStatus>(connectorStatus)) |
3d25cc86 | 1557 | } |
66a7748d JB |
1558 | initializeConnectorsMapStatus(this.connectors, this.logPrefix()) |
1559 | this.saveConnectorsStatus() | |
ae25f265 JB |
1560 | } else { |
1561 | logger.warn( | |
1562 | `${this.logPrefix()} Charging station information from template ${ | |
1563 | this.templateFile | |
66a7748d JB |
1564 | } with no connectors configuration defined, cannot create connectors` |
1565 | ) | |
3d25cc86 JB |
1566 | } |
1567 | } | |
1568 | } else { | |
1569 | logger.warn( | |
1570 | `${this.logPrefix()} Charging station information from template ${ | |
1571 | this.templateFile | |
66a7748d JB |
1572 | } with no connectors configuration defined, using already defined connectors` |
1573 | ) | |
3d25cc86 | 1574 | } |
3d25cc86 JB |
1575 | } |
1576 | ||
66a7748d | 1577 | private initializeEvsesFromTemplate (stationTemplate: ChargingStationTemplate): void { |
5199f9fd | 1578 | if (stationTemplate.Evses == null && this.evses.size === 0) { |
66a7748d JB |
1579 | const errorMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined` |
1580 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
1581 | throw new BaseError(errorMsg) | |
2585c6e9 | 1582 | } |
5199f9fd | 1583 | if (stationTemplate.Evses?.[0] == null) { |
2585c6e9 JB |
1584 | logger.warn( |
1585 | `${this.logPrefix()} Charging station information from template ${ | |
1586 | this.templateFile | |
66a7748d JB |
1587 | } with no evse id 0 configuration` |
1588 | ) | |
2585c6e9 | 1589 | } |
5199f9fd | 1590 | if (stationTemplate.Evses?.[0]?.Connectors[0] == null) { |
59a0f26d JB |
1591 | logger.warn( |
1592 | `${this.logPrefix()} Charging station information from template ${ | |
1593 | this.templateFile | |
66a7748d JB |
1594 | } with evse id 0 with no connector id 0 configuration` |
1595 | ) | |
59a0f26d | 1596 | } |
5199f9fd | 1597 | if (Object.keys(stationTemplate.Evses?.[0]?.Connectors as object).length > 1) { |
491dad29 JB |
1598 | logger.warn( |
1599 | `${this.logPrefix()} Charging station information from template ${ | |
1600 | this.templateFile | |
66a7748d JB |
1601 | } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used` |
1602 | ) | |
491dad29 | 1603 | } |
5199f9fd | 1604 | if (stationTemplate.Evses != null) { |
d972af76 | 1605 | const evsesConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
5199f9fd | 1606 | .update(JSON.stringify(stationTemplate.Evses)) |
66a7748d | 1607 | .digest('hex') |
2585c6e9 | 1608 | const evsesConfigChanged = |
5199f9fd JB |
1609 | this.evses.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash |
1610 | if (this.evses.size === 0 || evsesConfigChanged) { | |
66a7748d JB |
1611 | evsesConfigChanged && this.evses.clear() |
1612 | this.evsesConfigurationHash = evsesConfigHash | |
5199f9fd | 1613 | const templateMaxEvses = getMaxNumberOfEvses(stationTemplate.Evses) |
ae25f265 | 1614 | if (templateMaxEvses > 0) { |
eb979012 | 1615 | for (const evseKey in stationTemplate.Evses) { |
66a7748d | 1616 | const evseId = convertToInt(evseKey) |
52952bf8 | 1617 | this.evses.set(evseId, { |
fba11dc6 | 1618 | connectors: buildConnectorsMap( |
5199f9fd | 1619 | stationTemplate.Evses[evseKey].Connectors, |
ae25f265 | 1620 | this.logPrefix(), |
66a7748d | 1621 | this.templateFile |
ae25f265 | 1622 | ), |
66a7748d JB |
1623 | availability: AvailabilityType.Operative |
1624 | }) | |
1625 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1626 | initializeConnectorsMapStatus(this.evses.get(evseId)!.connectors, this.logPrefix()) | |
ae25f265 | 1627 | } |
66a7748d | 1628 | this.saveEvsesStatus() |
ae25f265 JB |
1629 | } else { |
1630 | logger.warn( | |
1631 | `${this.logPrefix()} Charging station information from template ${ | |
04b1261c | 1632 | this.templateFile |
66a7748d JB |
1633 | } with no evses configuration defined, cannot create evses` |
1634 | ) | |
2585c6e9 JB |
1635 | } |
1636 | } | |
513db108 JB |
1637 | } else { |
1638 | logger.warn( | |
1639 | `${this.logPrefix()} Charging station information from template ${ | |
1640 | this.templateFile | |
66a7748d JB |
1641 | } with no evses configuration defined, using already defined evses` |
1642 | ) | |
2585c6e9 JB |
1643 | } |
1644 | } | |
1645 | ||
66a7748d JB |
1646 | private getConfigurationFromFile (): ChargingStationConfiguration | undefined { |
1647 | let configuration: ChargingStationConfiguration | undefined | |
9bf0ef23 | 1648 | if (isNotEmptyString(this.configurationFile) && existsSync(this.configurationFile)) { |
073bd098 | 1649 | try { |
57adbebc JB |
1650 | if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { |
1651 | configuration = this.sharedLRUCache.getChargingStationConfiguration( | |
66a7748d JB |
1652 | this.configurationFileHash |
1653 | ) | |
7c72977b | 1654 | } else { |
66a7748d JB |
1655 | const measureId = `${FileType.ChargingStationConfiguration} read` |
1656 | const beginId = PerformanceStatistics.beginMeasure(measureId) | |
7c72977b | 1657 | configuration = JSON.parse( |
66a7748d JB |
1658 | readFileSync(this.configurationFile, 'utf8') |
1659 | ) as ChargingStationConfiguration | |
1660 | PerformanceStatistics.endMeasure(measureId, beginId) | |
1661 | this.sharedLRUCache.setChargingStationConfiguration(configuration) | |
1662 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1663 | this.configurationFileHash = configuration.configurationHash! | |
7c72977b | 1664 | } |
073bd098 | 1665 | } catch (error) { |
fa5995d6 | 1666 | handleFileException( |
073bd098 | 1667 | this.configurationFile, |
7164966d JB |
1668 | FileType.ChargingStationConfiguration, |
1669 | error as NodeJS.ErrnoException, | |
66a7748d JB |
1670 | this.logPrefix() |
1671 | ) | |
073bd098 JB |
1672 | } |
1673 | } | |
66a7748d | 1674 | return configuration |
073bd098 JB |
1675 | } |
1676 | ||
66a7748d | 1677 | private saveAutomaticTransactionGeneratorConfiguration (): void { |
5398cecf | 1678 | if (this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true) { |
66a7748d | 1679 | this.saveConfiguration() |
5ced7e80 | 1680 | } |
ac7f79af JB |
1681 | } |
1682 | ||
66a7748d JB |
1683 | private saveConnectorsStatus (): void { |
1684 | this.saveConfiguration() | |
52952bf8 JB |
1685 | } |
1686 | ||
66a7748d JB |
1687 | private saveEvsesStatus (): void { |
1688 | this.saveConfiguration() | |
52952bf8 JB |
1689 | } |
1690 | ||
66a7748d | 1691 | private saveConfiguration (): void { |
9bf0ef23 | 1692 | if (isNotEmptyString(this.configurationFile)) { |
2484ac1e | 1693 | try { |
d972af76 | 1694 | if (!existsSync(dirname(this.configurationFile))) { |
66a7748d | 1695 | mkdirSync(dirname(this.configurationFile), { recursive: true }) |
073bd098 | 1696 | } |
2466918c | 1697 | const configurationFromFile = this.getConfigurationFromFile() |
66a7748d | 1698 | let configurationData: ChargingStationConfiguration = |
2466918c | 1699 | configurationFromFile != null |
40615072 | 1700 | ? clone<ChargingStationConfiguration>(configurationFromFile) |
66a7748d | 1701 | : {} |
5199f9fd | 1702 | if (this.stationInfo?.stationInfoPersistentConfiguration === true) { |
66a7748d | 1703 | configurationData.stationInfo = this.stationInfo |
5ced7e80 | 1704 | } else { |
66a7748d | 1705 | delete configurationData.stationInfo |
52952bf8 | 1706 | } |
5398cecf JB |
1707 | if ( |
1708 | this.stationInfo?.ocppPersistentConfiguration === true && | |
755a76d5 | 1709 | Array.isArray(this.ocppConfiguration?.configurationKey) |
5398cecf | 1710 | ) { |
5199f9fd | 1711 | configurationData.configurationKey = this.ocppConfiguration.configurationKey |
5ced7e80 | 1712 | } else { |
66a7748d | 1713 | delete configurationData.configurationKey |
52952bf8 | 1714 | } |
179ed367 JB |
1715 | configurationData = merge<ChargingStationConfiguration>( |
1716 | configurationData, | |
66a7748d JB |
1717 | buildChargingStationAutomaticTransactionGeneratorConfiguration(this) |
1718 | ) | |
5ced7e80 | 1719 | if ( |
52c58949 | 1720 | this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration !== true || |
66a7748d | 1721 | this.getAutomaticTransactionGeneratorConfiguration() == null |
5ced7e80 | 1722 | ) { |
66a7748d | 1723 | delete configurationData.automaticTransactionGenerator |
5ced7e80 | 1724 | } |
b1bbdae5 | 1725 | if (this.connectors.size > 0) { |
66a7748d | 1726 | configurationData.connectorsStatus = buildConnectorsStatus(this) |
5ced7e80 | 1727 | } else { |
66a7748d | 1728 | delete configurationData.connectorsStatus |
52952bf8 | 1729 | } |
b1bbdae5 | 1730 | if (this.evses.size > 0) { |
66a7748d | 1731 | configurationData.evsesStatus = buildEvsesStatus(this) |
5ced7e80 | 1732 | } else { |
66a7748d | 1733 | delete configurationData.evsesStatus |
52952bf8 | 1734 | } |
66a7748d | 1735 | delete configurationData.configurationHash |
d972af76 | 1736 | const configurationHash = createHash(Constants.DEFAULT_HASH_ALGORITHM) |
5ced7e80 JB |
1737 | .update( |
1738 | JSON.stringify({ | |
1739 | stationInfo: configurationData.stationInfo, | |
1740 | configurationKey: configurationData.configurationKey, | |
1741 | automaticTransactionGenerator: configurationData.automaticTransactionGenerator, | |
8ab96efb | 1742 | ...(this.connectors.size > 0 && { |
66a7748d | 1743 | connectorsStatus: configurationData.connectorsStatus |
8ab96efb | 1744 | }), |
66a7748d JB |
1745 | ...(this.evses.size > 0 && { evsesStatus: configurationData.evsesStatus }) |
1746 | } satisfies ChargingStationConfiguration) | |
5ced7e80 | 1747 | ) |
66a7748d | 1748 | .digest('hex') |
7c72977b | 1749 | if (this.configurationFileHash !== configurationHash) { |
0ebf7c2e | 1750 | AsyncLock.runExclusive(AsyncLockType.configuration, () => { |
66a7748d JB |
1751 | configurationData.configurationHash = configurationHash |
1752 | const measureId = `${FileType.ChargingStationConfiguration} write` | |
1753 | const beginId = PerformanceStatistics.beginMeasure(measureId) | |
0ebf7c2e JB |
1754 | writeFileSync( |
1755 | this.configurationFile, | |
4ed03b6e | 1756 | JSON.stringify(configurationData, undefined, 2), |
66a7748d JB |
1757 | 'utf8' |
1758 | ) | |
1759 | PerformanceStatistics.endMeasure(measureId, beginId) | |
1760 | this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash) | |
1761 | this.sharedLRUCache.setChargingStationConfiguration(configurationData) | |
1762 | this.configurationFileHash = configurationHash | |
a974c8e4 | 1763 | }).catch(error => { |
0ebf7c2e JB |
1764 | handleFileException( |
1765 | this.configurationFile, | |
1766 | FileType.ChargingStationConfiguration, | |
1767 | error as NodeJS.ErrnoException, | |
66a7748d JB |
1768 | this.logPrefix() |
1769 | ) | |
1770 | }) | |
7c72977b JB |
1771 | } else { |
1772 | logger.debug( | |
1773 | `${this.logPrefix()} Not saving unchanged charging station configuration file ${ | |
1774 | this.configurationFile | |
66a7748d JB |
1775 | }` |
1776 | ) | |
2484ac1e | 1777 | } |
2484ac1e | 1778 | } catch (error) { |
fa5995d6 | 1779 | handleFileException( |
2484ac1e | 1780 | this.configurationFile, |
7164966d JB |
1781 | FileType.ChargingStationConfiguration, |
1782 | error as NodeJS.ErrnoException, | |
66a7748d JB |
1783 | this.logPrefix() |
1784 | ) | |
073bd098 | 1785 | } |
2484ac1e JB |
1786 | } else { |
1787 | logger.error( | |
66a7748d JB |
1788 | `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file` |
1789 | ) | |
073bd098 JB |
1790 | } |
1791 | } | |
1792 | ||
66a7748d JB |
1793 | private getOcppConfigurationFromTemplate (): ChargingStationOcppConfiguration | undefined { |
1794 | return this.getTemplateFromFile()?.Configuration | |
2484ac1e JB |
1795 | } |
1796 | ||
52c58949 JB |
1797 | private getOcppConfigurationFromFile ( |
1798 | ocppPersistentConfiguration?: boolean | |
1799 | ): ChargingStationOcppConfiguration | undefined { | |
66a7748d | 1800 | const configurationKey = this.getConfigurationFromFile()?.configurationKey |
52c58949 | 1801 | if (ocppPersistentConfiguration === true && Array.isArray(configurationKey)) { |
66a7748d | 1802 | return { configurationKey } |
648512ce | 1803 | } |
66a7748d | 1804 | return undefined |
7dde0b73 JB |
1805 | } |
1806 | ||
52c58949 JB |
1807 | private getOcppConfiguration ( |
1808 | ocppPersistentConfiguration: boolean | undefined = this.stationInfo?.ocppPersistentConfiguration | |
1809 | ): ChargingStationOcppConfiguration | undefined { | |
551e477c | 1810 | let ocppConfiguration: ChargingStationOcppConfiguration | undefined = |
52c58949 | 1811 | this.getOcppConfigurationFromFile(ocppPersistentConfiguration) |
66a7748d JB |
1812 | if (ocppConfiguration == null) { |
1813 | ocppConfiguration = this.getOcppConfigurationFromTemplate() | |
2484ac1e | 1814 | } |
66a7748d | 1815 | return ocppConfiguration |
2484ac1e JB |
1816 | } |
1817 | ||
66a7748d JB |
1818 | private async onOpen (): Promise<void> { |
1819 | if (this.isWebSocketConnectionOpened()) { | |
5144f4d1 | 1820 | logger.info( |
1d41bc6b | 1821 | `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded` |
66a7748d JB |
1822 | ) |
1823 | let registrationRetryCount = 0 | |
1824 | if (!this.isRegistered()) { | |
5144f4d1 | 1825 | // Send BootNotification |
5144f4d1 | 1826 | do { |
f7f98c68 | 1827 | this.bootNotificationResponse = await this.ocppRequestService.requestHandler< |
66a7748d JB |
1828 | BootNotificationRequest, |
1829 | BootNotificationResponse | |
8bfbc743 | 1830 | >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { |
66a7748d JB |
1831 | skipBufferingOnError: true |
1832 | }) | |
01d2a2c7 JB |
1833 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
1834 | if (this.bootNotificationResponse?.currentTime != null) { | |
1835 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1836 | this.bootNotificationResponse.currentTime = convertToDate( | |
1837 | this.bootNotificationResponse.currentTime | |
1838 | )! | |
1839 | } | |
66a7748d JB |
1840 | if (!this.isRegistered()) { |
1841 | this.stationInfo?.registrationMaxRetries !== -1 && ++registrationRetryCount | |
9bf0ef23 | 1842 | await sleep( |
5199f9fd | 1843 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
73b78a1f | 1844 | this.bootNotificationResponse?.interval != null |
be4c6702 | 1845 | ? secondsToMilliseconds(this.bootNotificationResponse.interval) |
66a7748d JB |
1846 | : Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL |
1847 | ) | |
5144f4d1 JB |
1848 | } |
1849 | } while ( | |
66a7748d JB |
1850 | !this.isRegistered() && |
1851 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 1852 | (registrationRetryCount <= this.stationInfo!.registrationMaxRetries! || |
9a77cc07 | 1853 | this.stationInfo?.registrationMaxRetries === -1) |
66a7748d | 1854 | ) |
5144f4d1 | 1855 | } |
66a7748d JB |
1856 | if (this.isRegistered()) { |
1857 | this.emit(ChargingStationEvents.registered) | |
1858 | if (this.inAcceptedState()) { | |
1859 | this.emit(ChargingStationEvents.accepted) | |
c0560973 | 1860 | } |
5144f4d1 | 1861 | } else { |
e054fc1c JB |
1862 | if (this.inRejectedState()) { |
1863 | this.emit(ChargingStationEvents.rejected) | |
1864 | } | |
5144f4d1 | 1865 | logger.error( |
a223d9be JB |
1866 | `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${ |
1867 | this.stationInfo?.registrationMaxRetries | |
1868 | })` | |
66a7748d | 1869 | ) |
caad9d6b | 1870 | } |
2960841f | 1871 | this.wsConnectionRetryCount = 0 |
66a7748d | 1872 | this.emit(ChargingStationEvents.updated) |
2e6f5966 | 1873 | } else { |
5144f4d1 | 1874 | logger.warn( |
1d41bc6b | 1875 | `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} failed` |
66a7748d | 1876 | ) |
2e6f5966 | 1877 | } |
2e6f5966 JB |
1878 | } |
1879 | ||
ba9a56a6 | 1880 | private onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): void { |
e054fc1c | 1881 | this.emit(ChargingStationEvents.disconnected) |
d09085e9 | 1882 | switch (code) { |
6c65a295 JB |
1883 | // Normal close |
1884 | case WebSocketCloseEventStatusCode.CLOSE_NORMAL: | |
c0560973 | 1885 | case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS: |
e7aeea18 | 1886 | logger.info( |
9bf0ef23 | 1887 | `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString( |
66a7748d JB |
1888 | code |
1889 | )}' and reason '${reason.toString()}'` | |
1890 | ) | |
2960841f | 1891 | this.wsConnectionRetryCount = 0 |
66a7748d | 1892 | break |
6c65a295 JB |
1893 | // Abnormal close |
1894 | default: | |
e7aeea18 | 1895 | logger.error( |
9bf0ef23 | 1896 | `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString( |
66a7748d JB |
1897 | code |
1898 | )}' and reason '${reason.toString()}'` | |
1899 | ) | |
7c974155 JB |
1900 | this.started && |
1901 | this.reconnect().catch(error => | |
1902 | logger.error(`${this.logPrefix()} Error while reconnecting:`, error) | |
1903 | ) | |
66a7748d | 1904 | break |
c0560973 | 1905 | } |
66a7748d | 1906 | this.emit(ChargingStationEvents.updated) |
2e6f5966 JB |
1907 | } |
1908 | ||
c510c989 JB |
1909 | private getCachedRequest ( |
1910 | messageType: MessageType | undefined, | |
1911 | messageId: string | |
1912 | ): CachedRequest | undefined { | |
66a7748d JB |
1913 | const cachedRequest = this.requests.get(messageId) |
1914 | if (Array.isArray(cachedRequest)) { | |
1915 | return cachedRequest | |
56d09fd7 JB |
1916 | } |
1917 | throw new OCPPError( | |
1918 | ErrorType.PROTOCOL_ERROR, | |
041365be | 1919 | `Cached request for message id ${messageId} ${getMessageTypeString( |
66a7748d | 1920 | messageType |
56d09fd7 JB |
1921 | )} is not an array`, |
1922 | undefined, | |
66a7748d JB |
1923 | cachedRequest |
1924 | ) | |
56d09fd7 JB |
1925 | } |
1926 | ||
66a7748d JB |
1927 | private async handleIncomingMessage (request: IncomingRequest): Promise<void> { |
1928 | const [messageType, messageId, commandName, commandPayload] = request | |
9a77cc07 | 1929 | if (this.stationInfo?.enableStatistics === true) { |
66a7748d | 1930 | this.performanceStatistics?.addRequestStatistic(commandName, messageType) |
56d09fd7 JB |
1931 | } |
1932 | logger.debug( | |
1933 | `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify( | |
66a7748d JB |
1934 | request |
1935 | )}` | |
1936 | ) | |
56d09fd7 JB |
1937 | // Process the message |
1938 | await this.ocppIncomingRequestService.incomingRequestHandler( | |
1939 | this, | |
1940 | messageId, | |
1941 | commandName, | |
66a7748d JB |
1942 | commandPayload |
1943 | ) | |
1944 | this.emit(ChargingStationEvents.updated) | |
56d09fd7 JB |
1945 | } |
1946 | ||
66a7748d JB |
1947 | private handleResponseMessage (response: Response): void { |
1948 | const [messageType, messageId, commandPayload] = response | |
1949 | if (!this.requests.has(messageId)) { | |
56d09fd7 JB |
1950 | // Error |
1951 | throw new OCPPError( | |
1952 | ErrorType.INTERNAL_ERROR, | |
1953 | `Response for unknown message id ${messageId}`, | |
1954 | undefined, | |
66a7748d JB |
1955 | commandPayload |
1956 | ) | |
56d09fd7 JB |
1957 | } |
1958 | // Respond | |
66a7748d | 1959 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
56d09fd7 JB |
1960 | const [responseCallback, , requestCommandName, requestPayload] = this.getCachedRequest( |
1961 | messageType, | |
66a7748d JB |
1962 | messageId |
1963 | )! | |
56d09fd7 | 1964 | logger.debug( |
5199f9fd JB |
1965 | `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify( |
1966 | response | |
1967 | )}` | |
66a7748d JB |
1968 | ) |
1969 | responseCallback(commandPayload, requestPayload) | |
56d09fd7 JB |
1970 | } |
1971 | ||
66a7748d JB |
1972 | private handleErrorMessage (errorResponse: ErrorResponse): void { |
1973 | const [messageType, messageId, errorType, errorMessage, errorDetails] = errorResponse | |
1974 | if (!this.requests.has(messageId)) { | |
56d09fd7 JB |
1975 | // Error |
1976 | throw new OCPPError( | |
1977 | ErrorType.INTERNAL_ERROR, | |
1978 | `Error response for unknown message id ${messageId}`, | |
1979 | undefined, | |
66a7748d JB |
1980 | { errorType, errorMessage, errorDetails } |
1981 | ) | |
56d09fd7 | 1982 | } |
66a7748d JB |
1983 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1984 | const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)! | |
56d09fd7 | 1985 | logger.debug( |
5199f9fd JB |
1986 | `${this.logPrefix()} << Command '${requestCommandName}' received error response payload: ${JSON.stringify( |
1987 | errorResponse | |
1988 | )}` | |
66a7748d JB |
1989 | ) |
1990 | errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)) | |
56d09fd7 JB |
1991 | } |
1992 | ||
66a7748d JB |
1993 | private async onMessage (data: RawData): Promise<void> { |
1994 | let request: IncomingRequest | Response | ErrorResponse | undefined | |
1995 | let messageType: MessageType | undefined | |
1996 | let errorMsg: string | |
c0560973 | 1997 | try { |
e1d9a0f4 | 1998 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
66a7748d JB |
1999 | request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse |
2000 | if (Array.isArray(request)) { | |
2001 | [messageType] = request | |
b3ec7bc1 JB |
2002 | // Check the type of message |
2003 | switch (messageType) { | |
2004 | // Incoming Message | |
2005 | case MessageType.CALL_MESSAGE: | |
66a7748d JB |
2006 | await this.handleIncomingMessage(request as IncomingRequest) |
2007 | break | |
56d09fd7 | 2008 | // Response Message |
b3ec7bc1 | 2009 | case MessageType.CALL_RESULT_MESSAGE: |
66a7748d JB |
2010 | this.handleResponseMessage(request as Response) |
2011 | break | |
a2d1c0f1 JB |
2012 | // Error Message |
2013 | case MessageType.CALL_ERROR_MESSAGE: | |
66a7748d JB |
2014 | this.handleErrorMessage(request as ErrorResponse) |
2015 | break | |
56d09fd7 | 2016 | // Unknown Message |
b3ec7bc1 JB |
2017 | default: |
2018 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
66a7748d JB |
2019 | errorMsg = `Wrong message type ${messageType}` |
2020 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
2021 | throw new OCPPError(ErrorType.PROTOCOL_ERROR, errorMsg) | |
b3ec7bc1 | 2022 | } |
47e22477 | 2023 | } else { |
e1d9a0f4 JB |
2024 | throw new OCPPError( |
2025 | ErrorType.PROTOCOL_ERROR, | |
2026 | 'Incoming message is not an array', | |
2027 | undefined, | |
2028 | { | |
66a7748d JB |
2029 | request |
2030 | } | |
2031 | ) | |
47e22477 | 2032 | } |
c0560973 | 2033 | } catch (error) { |
c3c8ae3f JB |
2034 | if (!Array.isArray(request)) { |
2035 | logger.error(`${this.logPrefix()} Incoming message '${request}' parsing error:`, error) | |
2036 | return | |
2037 | } | |
66a7748d JB |
2038 | let commandName: IncomingRequestCommand | undefined |
2039 | let requestCommandName: RequestCommand | IncomingRequestCommand | undefined | |
2040 | let errorCallback: ErrorCallback | |
c3c8ae3f | 2041 | const [, messageId] = request |
13701f69 JB |
2042 | switch (messageType) { |
2043 | case MessageType.CALL_MESSAGE: | |
66a7748d | 2044 | [, , commandName] = request as IncomingRequest |
13701f69 | 2045 | // Send error |
66a7748d JB |
2046 | await this.ocppRequestService.sendError(this, messageId, error as OCPPError, commandName) |
2047 | break | |
13701f69 JB |
2048 | case MessageType.CALL_RESULT_MESSAGE: |
2049 | case MessageType.CALL_ERROR_MESSAGE: | |
66a7748d JB |
2050 | if (this.requests.has(messageId)) { |
2051 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
2052 | [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)! | |
13701f69 | 2053 | // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op) |
66a7748d | 2054 | errorCallback(error as OCPPError, false) |
13701f69 JB |
2055 | } else { |
2056 | // Remove the request from the cache in case of error at response handling | |
66a7748d | 2057 | this.requests.delete(messageId) |
13701f69 | 2058 | } |
66a7748d | 2059 | break |
ba7965c4 | 2060 | } |
66a7748d | 2061 | if (!(error instanceof OCPPError)) { |
56d09fd7 JB |
2062 | logger.warn( |
2063 | `${this.logPrefix()} Error thrown at incoming OCPP command '${ | |
2064 | commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND | |
e1d9a0f4 | 2065 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
56d09fd7 | 2066 | }' message '${data.toString()}' handling is not an OCPPError:`, |
66a7748d JB |
2067 | error |
2068 | ) | |
56d09fd7 JB |
2069 | } |
2070 | logger.error( | |
2071 | `${this.logPrefix()} Incoming OCPP command '${ | |
2072 | commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND | |
e1d9a0f4 | 2073 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
56d09fd7 | 2074 | }' message '${data.toString()}'${ |
c510c989 JB |
2075 | this.requests.has(messageId) |
2076 | ? ` matching cached request '${JSON.stringify(this.getCachedRequest(messageType, messageId))}'` | |
56d09fd7 JB |
2077 | : '' |
2078 | } processing error:`, | |
66a7748d JB |
2079 | error |
2080 | ) | |
c0560973 | 2081 | } |
2328be1e JB |
2082 | } |
2083 | ||
66a7748d JB |
2084 | private onPing (): void { |
2085 | logger.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`) | |
c0560973 JB |
2086 | } |
2087 | ||
66a7748d JB |
2088 | private onPong (): void { |
2089 | logger.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`) | |
c0560973 JB |
2090 | } |
2091 | ||
66a7748d JB |
2092 | private onError (error: WSError): void { |
2093 | this.closeWSConnection() | |
2094 | logger.error(`${this.logPrefix()} WebSocket error:`, error) | |
c0560973 JB |
2095 | } |
2096 | ||
f938317f JB |
2097 | private getEnergyActiveImportRegister ( |
2098 | connectorStatus: ConnectorStatus | undefined, | |
2099 | rounded = false | |
2100 | ): number { | |
5398cecf | 2101 | if (this.stationInfo?.meteringPerTransaction === true) { |
07989fad | 2102 | return ( |
66a7748d | 2103 | (rounded |
f938317f JB |
2104 | ? connectorStatus?.transactionEnergyActiveImportRegisterValue != null |
2105 | ? Math.round(connectorStatus.transactionEnergyActiveImportRegisterValue) | |
2106 | : undefined | |
2107 | : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0 | |
66a7748d | 2108 | ) |
07989fad JB |
2109 | } |
2110 | return ( | |
66a7748d | 2111 | (rounded |
f938317f JB |
2112 | ? connectorStatus?.energyActiveImportRegisterValue != null |
2113 | ? Math.round(connectorStatus.energyActiveImportRegisterValue) | |
2114 | : undefined | |
2115 | : connectorStatus?.energyActiveImportRegisterValue) ?? 0 | |
66a7748d | 2116 | ) |
07989fad JB |
2117 | } |
2118 | ||
66a7748d JB |
2119 | private getUseConnectorId0 (stationTemplate?: ChargingStationTemplate): boolean { |
2120 | return stationTemplate?.useConnectorId0 ?? true | |
8bce55bf JB |
2121 | } |
2122 | ||
66a7748d | 2123 | private async stopRunningTransactions (reason?: StopTransactionReason): Promise<void> { |
28e78158 | 2124 | if (this.hasEvses) { |
3fa7f799 JB |
2125 | for (const [evseId, evseStatus] of this.evses) { |
2126 | if (evseId === 0) { | |
66a7748d | 2127 | continue |
3fa7f799 | 2128 | } |
28e78158 JB |
2129 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { |
2130 | if (connectorStatus.transactionStarted === true) { | |
66a7748d | 2131 | await this.stopTransactionOnConnector(connectorId, reason) |
28e78158 JB |
2132 | } |
2133 | } | |
2134 | } | |
2135 | } else { | |
2136 | for (const connectorId of this.connectors.keys()) { | |
2137 | if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { | |
66a7748d | 2138 | await this.stopTransactionOnConnector(connectorId, reason) |
28e78158 | 2139 | } |
60ddad53 JB |
2140 | } |
2141 | } | |
2142 | } | |
2143 | ||
1f761b9a | 2144 | // 0 for disabling |
66a7748d | 2145 | private getConnectionTimeout (): number { |
a807045b | 2146 | if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut) != null) { |
4e3b1d6b | 2147 | return convertToInt( |
5199f9fd | 2148 | getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)?.value ?? |
66a7748d JB |
2149 | Constants.DEFAULT_CONNECTION_TIMEOUT |
2150 | ) | |
291cb255 | 2151 | } |
66a7748d | 2152 | return Constants.DEFAULT_CONNECTION_TIMEOUT |
3574dfd3 JB |
2153 | } |
2154 | ||
66a7748d JB |
2155 | private getPowerDivider (): number { |
2156 | let powerDivider = this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors() | |
be1e907c | 2157 | if (this.stationInfo?.powerSharedByConnectors === true) { |
66a7748d | 2158 | powerDivider = this.getNumberOfRunningTransactions() |
6ecb15e4 | 2159 | } |
66a7748d | 2160 | return powerDivider |
6ecb15e4 JB |
2161 | } |
2162 | ||
66a7748d JB |
2163 | private getMaximumAmperage (stationInfo?: ChargingStationInfo): number | undefined { |
2164 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 2165 | const maximumPower = (stationInfo ?? this.stationInfo!).maximumPower! |
fa7bccf4 | 2166 | switch (this.getCurrentOutType(stationInfo)) { |
cc6e8ab5 JB |
2167 | case CurrentType.AC: |
2168 | return ACElectricUtils.amperagePerPhaseFromPower( | |
fa7bccf4 | 2169 | this.getNumberOfPhases(stationInfo), |
b1bbdae5 | 2170 | maximumPower / (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()), |
66a7748d JB |
2171 | this.getVoltageOut(stationInfo) |
2172 | ) | |
cc6e8ab5 | 2173 | case CurrentType.DC: |
66a7748d | 2174 | return DCElectricUtils.amperage(maximumPower, this.getVoltageOut(stationInfo)) |
cc6e8ab5 JB |
2175 | } |
2176 | } | |
2177 | ||
66a7748d | 2178 | private getCurrentOutType (stationInfo?: ChargingStationInfo): CurrentType { |
5199f9fd JB |
2179 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2180 | return (stationInfo ?? this.stationInfo!).currentOutType ?? CurrentType.AC | |
5398cecf JB |
2181 | } |
2182 | ||
66a7748d | 2183 | private getVoltageOut (stationInfo?: ChargingStationInfo): Voltage { |
74ed61d9 | 2184 | return ( |
5199f9fd JB |
2185 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2186 | (stationInfo ?? this.stationInfo!).voltageOut ?? | |
74ed61d9 | 2187 | getDefaultVoltageOut(this.getCurrentOutType(stationInfo), this.logPrefix(), this.templateFile) |
66a7748d | 2188 | ) |
5398cecf JB |
2189 | } |
2190 | ||
66a7748d | 2191 | private getAmperageLimitation (): number | undefined { |
cc6e8ab5 | 2192 | if ( |
9bf0ef23 | 2193 | isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && |
5dc7c990 | 2194 | getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey) != null |
cc6e8ab5 JB |
2195 | ) { |
2196 | return ( | |
5dc7c990 JB |
2197 | convertToInt(getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey)?.value) / |
2198 | getAmperageLimitationUnitDivider(this.stationInfo) | |
66a7748d | 2199 | ) |
cc6e8ab5 JB |
2200 | } |
2201 | } | |
2202 | ||
e054fc1c | 2203 | private async startMessageSequence (ATGStopAbsoluteDuration?: boolean): Promise<void> { |
b7f9e41d | 2204 | if (this.stationInfo?.autoRegister === true) { |
f7f98c68 | 2205 | await this.ocppRequestService.requestHandler< |
66a7748d JB |
2206 | BootNotificationRequest, |
2207 | BootNotificationResponse | |
8bfbc743 | 2208 | >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, { |
66a7748d JB |
2209 | skipBufferingOnError: true |
2210 | }) | |
6114e6f1 | 2211 | } |
136c90ba | 2212 | // Start WebSocket ping |
66a7748d | 2213 | this.startWebSocketPing() |
5ad8570f | 2214 | // Start heartbeat |
66a7748d | 2215 | this.startHeartbeat() |
0a60c33c | 2216 | // Initialize connectors status |
c3b83130 JB |
2217 | if (this.hasEvses) { |
2218 | for (const [evseId, evseStatus] of this.evses) { | |
4334db72 JB |
2219 | if (evseId > 0) { |
2220 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
66a7748d JB |
2221 | const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus) |
2222 | await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus, evseId) | |
4334db72 | 2223 | } |
c3b83130 | 2224 | } |
4334db72 JB |
2225 | } |
2226 | } else { | |
2227 | for (const connectorId of this.connectors.keys()) { | |
2228 | if (connectorId > 0) { | |
fba11dc6 | 2229 | const connectorBootStatus = getBootConnectorStatus( |
c3b83130 JB |
2230 | this, |
2231 | connectorId, | |
66a7748d JB |
2232 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2233 | this.getConnectorStatus(connectorId)! | |
2234 | ) | |
2235 | await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus) | |
c3b83130 JB |
2236 | } |
2237 | } | |
5ad8570f | 2238 | } |
5199f9fd | 2239 | if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) { |
c9a4f9ea | 2240 | await this.ocppRequestService.requestHandler< |
66a7748d JB |
2241 | FirmwareStatusNotificationRequest, |
2242 | FirmwareStatusNotificationResponse | |
c9a4f9ea | 2243 | >(this, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, { |
66a7748d JB |
2244 | status: FirmwareStatus.Installed |
2245 | }) | |
2246 | this.stationInfo.firmwareStatus = FirmwareStatus.Installed | |
c9a4f9ea | 2247 | } |
3637ca2c | 2248 | |
0a60c33c | 2249 | // Start the ATG |
5199f9fd | 2250 | if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) { |
e054fc1c | 2251 | this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration) |
fa7bccf4 | 2252 | } |
66a7748d | 2253 | this.flushMessageBuffer() |
fa7bccf4 JB |
2254 | } |
2255 | ||
e054fc1c | 2256 | private internalStopMessageSequence (): void { |
136c90ba | 2257 | // Stop WebSocket ping |
66a7748d | 2258 | this.stopWebSocketPing() |
79411696 | 2259 | // Stop heartbeat |
66a7748d | 2260 | this.stopHeartbeat() |
9ff486f4 | 2261 | // Stop the ATG |
b20eb107 | 2262 | if (this.automaticTransactionGenerator?.started === true) { |
66a7748d | 2263 | this.stopAutomaticTransactionGenerator() |
79411696 | 2264 | } |
e054fc1c JB |
2265 | } |
2266 | ||
2267 | private async stopMessageSequence ( | |
2268 | reason?: StopTransactionReason, | |
7e3bde4f | 2269 | stopTransactions?: boolean |
e054fc1c JB |
2270 | ): Promise<void> { |
2271 | this.internalStopMessageSequence() | |
3e888c65 | 2272 | // Stop ongoing transactions |
66a7748d | 2273 | stopTransactions === true && (await this.stopRunningTransactions(reason)) |
039211f9 JB |
2274 | if (this.hasEvses) { |
2275 | for (const [evseId, evseStatus] of this.evses) { | |
2276 | if (evseId > 0) { | |
2277 | for (const [connectorId, connectorStatus] of evseStatus.connectors) { | |
e054fc1c | 2278 | await sendAndSetConnectorStatus( |
039211f9 | 2279 | this, |
e054fc1c JB |
2280 | connectorId, |
2281 | ConnectorStatusEnum.Unavailable, | |
2282 | evseId | |
66a7748d | 2283 | ) |
5199f9fd | 2284 | delete connectorStatus.status |
039211f9 JB |
2285 | } |
2286 | } | |
2287 | } | |
2288 | } else { | |
2289 | for (const connectorId of this.connectors.keys()) { | |
2290 | if (connectorId > 0) { | |
e054fc1c | 2291 | await sendAndSetConnectorStatus(this, connectorId, ConnectorStatusEnum.Unavailable) |
66a7748d | 2292 | delete this.getConnectorStatus(connectorId)?.status |
039211f9 | 2293 | } |
45c0ae82 JB |
2294 | } |
2295 | } | |
79411696 JB |
2296 | } |
2297 | ||
66a7748d | 2298 | private startWebSocketPing (): void { |
97608fbd | 2299 | const webSocketPingInterval = |
a807045b | 2300 | getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval) != null |
4e3b1d6b | 2301 | ? convertToInt( |
66a7748d JB |
2302 | getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value |
2303 | ) | |
2304 | : 0 | |
2960841f JB |
2305 | if (webSocketPingInterval > 0 && this.wsPingSetInterval == null) { |
2306 | this.wsPingSetInterval = setInterval(() => { | |
66a7748d JB |
2307 | if (this.isWebSocketConnectionOpened()) { |
2308 | this.wsConnection?.ping() | |
136c90ba | 2309 | } |
66a7748d | 2310 | }, secondsToMilliseconds(webSocketPingInterval)) |
e7aeea18 | 2311 | logger.info( |
9bf0ef23 | 2312 | `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds( |
66a7748d JB |
2313 | webSocketPingInterval |
2314 | )}` | |
2315 | ) | |
2960841f | 2316 | } else if (this.wsPingSetInterval != null) { |
e7aeea18 | 2317 | logger.info( |
9bf0ef23 | 2318 | `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds( |
66a7748d JB |
2319 | webSocketPingInterval |
2320 | )}` | |
2321 | ) | |
136c90ba | 2322 | } else { |
e7aeea18 | 2323 | logger.error( |
66a7748d JB |
2324 | `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping` |
2325 | ) | |
136c90ba JB |
2326 | } |
2327 | } | |
2328 | ||
66a7748d | 2329 | private stopWebSocketPing (): void { |
2960841f JB |
2330 | if (this.wsPingSetInterval != null) { |
2331 | clearInterval(this.wsPingSetInterval) | |
2332 | delete this.wsPingSetInterval | |
136c90ba JB |
2333 | } |
2334 | } | |
2335 | ||
66a7748d JB |
2336 | private getConfiguredSupervisionUrl (): URL { |
2337 | let configuredSupervisionUrl: string | |
2338 | const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls() | |
9bf0ef23 | 2339 | if (isNotEmptyArray(supervisionUrls)) { |
66a7748d | 2340 | let configuredSupervisionUrlIndex: number |
2dcfe98e | 2341 | switch (Configuration.getSupervisionUrlDistribution()) { |
2dcfe98e | 2342 | case SupervisionUrlDistribution.RANDOM: |
5dc7c990 | 2343 | configuredSupervisionUrlIndex = Math.floor(secureRandom() * supervisionUrls.length) |
66a7748d | 2344 | break |
a52a6446 | 2345 | case SupervisionUrlDistribution.ROUND_ROBIN: |
c72f6634 | 2346 | case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY: |
2dcfe98e | 2347 | default: |
66a7748d JB |
2348 | !Object.values(SupervisionUrlDistribution).includes( |
2349 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
2350 | Configuration.getSupervisionUrlDistribution()! | |
2351 | ) && | |
a52a6446 | 2352 | logger.error( |
e1d9a0f4 | 2353 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
a52a6446 JB |
2354 | `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${ |
2355 | SupervisionUrlDistribution.CHARGING_STATION_AFFINITY | |
66a7748d JB |
2356 | }` |
2357 | ) | |
5dc7c990 | 2358 | configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length |
66a7748d | 2359 | break |
c0560973 | 2360 | } |
5dc7c990 | 2361 | configuredSupervisionUrl = supervisionUrls[configuredSupervisionUrlIndex] |
d5c3df49 | 2362 | } else { |
5dc7c990 JB |
2363 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2364 | configuredSupervisionUrl = supervisionUrls! | |
d5c3df49 | 2365 | } |
9bf0ef23 | 2366 | if (isNotEmptyString(configuredSupervisionUrl)) { |
66a7748d | 2367 | return new URL(configuredSupervisionUrl) |
c0560973 | 2368 | } |
66a7748d JB |
2369 | const errorMsg = 'No supervision url(s) configured' |
2370 | logger.error(`${this.logPrefix()} ${errorMsg}`) | |
5199f9fd | 2371 | throw new BaseError(errorMsg) |
136c90ba JB |
2372 | } |
2373 | ||
66a7748d | 2374 | private stopHeartbeat (): void { |
a807045b | 2375 | if (this.heartbeatSetInterval != null) { |
66a7748d JB |
2376 | clearInterval(this.heartbeatSetInterval) |
2377 | delete this.heartbeatSetInterval | |
7dde0b73 | 2378 | } |
5ad8570f JB |
2379 | } |
2380 | ||
66a7748d JB |
2381 | private terminateWSConnection (): void { |
2382 | if (this.isWebSocketConnectionOpened()) { | |
2383 | this.wsConnection?.terminate() | |
2384 | this.wsConnection = null | |
55516218 JB |
2385 | } |
2386 | } | |
2387 | ||
66a7748d | 2388 | private async reconnect (): Promise<void> { |
e7aeea18 | 2389 | if ( |
66a7748d | 2390 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2960841f | 2391 | this.wsConnectionRetryCount < this.stationInfo!.autoReconnectMaxRetries! || |
5398cecf | 2392 | this.stationInfo?.autoReconnectMaxRetries === -1 |
e7aeea18 | 2393 | ) { |
2960841f JB |
2394 | this.wsConnectionRetried = true |
2395 | ++this.wsConnectionRetryCount | |
5398cecf JB |
2396 | const reconnectDelay = |
2397 | this.stationInfo?.reconnectExponentialDelay === true | |
2960841f | 2398 | ? exponentialDelay(this.wsConnectionRetryCount) |
66a7748d JB |
2399 | : secondsToMilliseconds(this.getConnectionTimeout()) |
2400 | const reconnectDelayWithdraw = 1000 | |
1e080116 | 2401 | const reconnectTimeout = |
5199f9fd | 2402 | reconnectDelay - reconnectDelayWithdraw > 0 ? reconnectDelay - reconnectDelayWithdraw : 0 |
e7aeea18 | 2403 | logger.error( |
9bf0ef23 | 2404 | `${this.logPrefix()} WebSocket connection retry in ${roundTo( |
e7aeea18 | 2405 | reconnectDelay, |
66a7748d JB |
2406 | 2 |
2407 | )}ms, timeout ${reconnectTimeout}ms` | |
2408 | ) | |
2409 | await sleep(reconnectDelay) | |
e7aeea18 | 2410 | logger.error( |
2960841f | 2411 | `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}` |
66a7748d | 2412 | ) |
e7aeea18 | 2413 | this.openWSConnection( |
59b6ed8d | 2414 | { |
66a7748d | 2415 | handshakeTimeout: reconnectTimeout |
59b6ed8d | 2416 | }, |
66a7748d JB |
2417 | { closeOpened: true } |
2418 | ) | |
5398cecf | 2419 | } else if (this.stationInfo?.autoReconnectMaxRetries !== -1) { |
e7aeea18 | 2420 | logger.error( |
d56ea27c | 2421 | `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${ |
2960841f | 2422 | this.wsConnectionRetryCount |
66a7748d JB |
2423 | }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})` |
2424 | ) | |
5ad8570f JB |
2425 | } |
2426 | } | |
7dde0b73 | 2427 | } |