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