import { createHash } from 'node:crypto'
import { EventEmitter } from 'node:events'
import { type FSWatcher, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
-import { dirname, join } from 'node:path'
+import { dirname, join, parse } from 'node:path'
import { URL } from 'node:url'
import { parentPort } from 'node:worker_threads'
type OCPPIncomingRequestService,
type OCPPRequestService,
buildMeterValue,
- buildStatusNotificationRequest,
buildTransactionEndMeterValue,
getMessageTypeString,
sendAndSetConnectorStatus
type Response,
StandardParametersKey,
type Status,
- type StatusNotificationRequest,
- type StatusNotificationResponse,
type StopTransactionReason,
type StopTransactionRequest,
type StopTransactionResponse,
Configuration,
Constants,
DCElectricUtils,
+ buildAddedMessage,
buildChargingStationAutomaticTransactionGeneratorConfiguration,
buildConnectorsStatus,
buildEvsesStatus,
private ocppIncomingRequestService!: OCPPIncomingRequestService
private readonly messageBuffer: Set<string>
private configuredSupervisionUrl!: URL
- private autoReconnectRetryCount: number
+ private wsConnectionRetried: boolean
+ private wsConnectionRetryCount: number
private templateFileWatcher!: FSWatcher | undefined
private templateFileHash!: string
private readonly sharedLRUCache: SharedLRUCache
- private webSocketPingSetInterval?: NodeJS.Timeout
+ private wsPingSetInterval?: NodeJS.Timeout
private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel
private flushMessageBufferSetInterval?: NodeJS.Timeout
this.starting = false
this.stopping = false
this.wsConnection = null
- this.autoReconnectRetryCount = 0
+ this.wsConnectionRetried = false
+ this.wsConnectionRetryCount = 0
this.index = index
this.templateFile = templateFile
this.connectors = new Map<number, ConnectorStatus>()
this.idTagsCache = IdTagsCache.getInstance()
this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this)
+ this.on(ChargingStationEvents.added, () => {
+ parentPort?.postMessage(buildAddedMessage(this))
+ })
this.on(ChargingStationEvents.started, () => {
parentPort?.postMessage(buildStartedMessage(this))
})
this.on(ChargingStationEvents.updated, () => {
parentPort?.postMessage(buildUpdatedMessage(this))
})
+ this.on(ChargingStationEvents.accepted, () => {
+ this.startMessageSequence(
+ this.wsConnectionRetried
+ ? true
+ : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
+ ).catch(error => {
+ logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
+ })
+ this.wsConnectionRetried = false
+ })
+ this.on(ChargingStationEvents.rejected, () => {
+ this.wsConnectionRetried = false
+ })
+ this.on(ChargingStationEvents.disconnected, () => {
+ try {
+ this.internalStopMessageSequence()
+ } catch (error) {
+ logger.error(
+ `${this.logPrefix()} Error while stopping the internal message sequence:`,
+ error
+ )
+ }
+ })
this.initialize()
+
+ this.stationInfo?.autoStart === true && this.start()
}
public get hasEvses (): boolean {
}
}
+ public add (): void {
+ this.emit(ChargingStationEvents.added)
+ }
+
public start (): void {
if (!this.started) {
if (!this.starting) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo!)!)
// Restart the ATG
- this.stopAutomaticTransactionGenerator()
+ const ATGStarted = this.automaticTransactionGenerator?.started
+ if (ATGStarted === true) {
+ this.stopAutomaticTransactionGenerator()
+ }
delete this.automaticTransactionGeneratorConfiguration
- if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
- this.startAutomaticTransactionGenerator()
+ if (
+ this.getAutomaticTransactionGeneratorConfiguration()?.enable === true &&
+ ATGStarted === true
+ ) {
+ this.startAutomaticTransactionGenerator(undefined, true)
}
if (this.stationInfo?.enableStatistics === true) {
this.performanceStatistics?.restart()
}
}
- public async stop (reason?: StopTransactionReason, stopTransactions?: boolean): Promise<void> {
+ public async stop (
+ reason?: StopTransactionReason,
+ stopTransactions = this.stationInfo?.stopTransactionsOnStopped
+ ): Promise<void> {
if (this.started) {
if (!this.stopping) {
this.stopping = true
return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
}
- public startAutomaticTransactionGenerator (connectorIds?: number[]): void {
+ public startAutomaticTransactionGenerator (
+ connectorIds?: number[],
+ stopAbsoluteDuration?: boolean
+ ): void {
this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this)
if (isNotEmptyArray(connectorIds)) {
for (const connectorId of connectorIds) {
- this.automaticTransactionGenerator?.startConnector(connectorId)
+ this.automaticTransactionGenerator?.startConnector(connectorId, stopAbsoluteDuration)
}
} else {
- this.automaticTransactionGenerator?.start()
+ this.automaticTransactionGenerator?.start(stopAbsoluteDuration)
}
this.saveAutomaticTransactionGeneratorConfiguration()
this.emit(ChargingStationEvents.updated)
}
const stationInfo = stationTemplateToStationInfo(stationTemplate)
stationInfo.hashId = getHashId(this.index, stationTemplate)
+ stationInfo.autoStart = stationTemplate.autoStart ?? true
+ stationInfo.templateName = parse(this.templateFile).name
stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate)
stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16
createSerialNumber(stationTemplate, stationInfo)
stationInfo.resetTime =
stationTemplate.resetTime != null
? secondsToMilliseconds(stationTemplate.resetTime)
- : Constants.CHARGING_STATION_DEFAULT_RESET_TIME
+ : Constants.DEFAULT_CHARGING_STATION_RESET_TIME
return stationInfo
}
stationInfo = this.getConfigurationFromFile()?.stationInfo
if (stationInfo != null) {
delete stationInfo.infoHash
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ if (stationInfo.templateName == null) {
+ stationInfo.templateName = parse(this.templateFile).name
+ }
+ if (stationInfo.autoStart == null) {
+ stationInfo.autoStart = true
+ }
}
}
return stationInfo
this.ocppConfiguration = this.getOcppConfiguration()
this.initializeOcppConfiguration()
this.initializeOcppServices()
- this.once(ChargingStationEvents.accepted, () => {
- this.startMessageSequence().catch(error => {
- logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
- })
- })
if (this.stationInfo.autoRegister === true) {
this.bootNotificationResponse = {
currentTime: new Date(),
this.emit(ChargingStationEvents.accepted)
}
} else {
+ if (this.inRejectedState()) {
+ this.emit(ChargingStationEvents.rejected)
+ }
logger.error(
`${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${
this.stationInfo?.registrationMaxRetries
})`
)
}
- this.autoReconnectRetryCount = 0
+ this.wsConnectionRetryCount = 0
this.emit(ChargingStationEvents.updated)
} else {
logger.warn(
}
private onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): void {
+ this.emit(ChargingStationEvents.disconnected)
switch (code) {
// Normal close
case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
code
)}' and reason '${reason.toString()}'`
)
- this.autoReconnectRetryCount = 0
+ this.wsConnectionRetryCount = 0
break
// Abnormal close
default:
code
)}' and reason '${reason.toString()}'`
)
- this.started && this.reconnect().catch(Constants.EMPTY_FUNCTION)
+ this.started &&
+ this.reconnect().catch(error =>
+ logger.error(`${this.logPrefix()} Error while reconnecting:`, error)
+ )
break
}
this.emit(ChargingStationEvents.updated)
}
- private getCachedRequest (messageType: MessageType, messageId: string): CachedRequest | undefined {
+ private getCachedRequest (
+ messageType: MessageType | undefined,
+ messageId: string
+ ): CachedRequest | undefined {
const cachedRequest = this.requests.get(messageId)
if (Array.isArray(cachedRequest)) {
return cachedRequest
commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
// eslint-disable-next-line @typescript-eslint/no-base-to-string
}' message '${data.toString()}'${
- messageType !== MessageType.CALL_MESSAGE
- ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
+ this.requests.has(messageId)
+ ? ` matching cached request '${JSON.stringify(this.getCachedRequest(messageType, messageId))}'`
: ''
} processing error:`,
error
}
}
- private async startMessageSequence (): Promise<void> {
+ private async startMessageSequence (ATGStopAbsoluteDuration?: boolean): Promise<void> {
if (this.stationInfo?.autoRegister === true) {
await this.ocppRequestService.requestHandler<
BootNotificationRequest,
// Start the ATG
if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
- this.startAutomaticTransactionGenerator()
+ this.startAutomaticTransactionGenerator(undefined, ATGStopAbsoluteDuration)
}
this.flushMessageBuffer()
}
- private async stopMessageSequence (
- reason?: StopTransactionReason,
- stopTransactions = this.stationInfo?.stopTransactionsOnStopped
- ): Promise<void> {
+ private internalStopMessageSequence (): void {
// Stop WebSocket ping
this.stopWebSocketPing()
// Stop heartbeat
if (this.automaticTransactionGenerator?.started === true) {
this.stopAutomaticTransactionGenerator()
}
+ }
+
+ private async stopMessageSequence (
+ reason?: StopTransactionReason,
+ stopTransactions?: boolean
+ ): Promise<void> {
+ this.internalStopMessageSequence()
// Stop ongoing transactions
stopTransactions === true && (await this.stopRunningTransactions(reason))
if (this.hasEvses) {
for (const [evseId, evseStatus] of this.evses) {
if (evseId > 0) {
for (const [connectorId, connectorStatus] of evseStatus.connectors) {
- await this.ocppRequestService.requestHandler<
- StatusNotificationRequest,
- StatusNotificationResponse
- >(
+ await sendAndSetConnectorStatus(
this,
- RequestCommand.STATUS_NOTIFICATION,
- buildStatusNotificationRequest(
- this,
- connectorId,
- ConnectorStatusEnum.Unavailable,
- evseId
- )
+ connectorId,
+ ConnectorStatusEnum.Unavailable,
+ evseId
)
delete connectorStatus.status
}
} else {
for (const connectorId of this.connectors.keys()) {
if (connectorId > 0) {
- await this.ocppRequestService.requestHandler<
- StatusNotificationRequest,
- StatusNotificationResponse
- >(
- this,
- RequestCommand.STATUS_NOTIFICATION,
- buildStatusNotificationRequest(this, connectorId, ConnectorStatusEnum.Unavailable)
- )
+ await sendAndSetConnectorStatus(this, connectorId, ConnectorStatusEnum.Unavailable)
delete this.getConnectorStatus(connectorId)?.status
}
}
getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value
)
: 0
- if (webSocketPingInterval > 0 && this.webSocketPingSetInterval == null) {
- this.webSocketPingSetInterval = setInterval(() => {
+ if (webSocketPingInterval > 0 && this.wsPingSetInterval == null) {
+ this.wsPingSetInterval = setInterval(() => {
if (this.isWebSocketConnectionOpened()) {
this.wsConnection?.ping()
}
webSocketPingInterval
)}`
)
- } else if (this.webSocketPingSetInterval != null) {
+ } else if (this.wsPingSetInterval != null) {
logger.info(
`${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
webSocketPingInterval
}
private stopWebSocketPing (): void {
- if (this.webSocketPingSetInterval != null) {
- clearInterval(this.webSocketPingSetInterval)
- delete this.webSocketPingSetInterval
+ if (this.wsPingSetInterval != null) {
+ clearInterval(this.wsPingSetInterval)
+ delete this.wsPingSetInterval
}
}
}
private async reconnect (): Promise<void> {
- // Stop WebSocket ping
- this.stopWebSocketPing()
- // Stop heartbeat
- this.stopHeartbeat()
- // Stop the ATG if needed
- if (this.getAutomaticTransactionGeneratorConfiguration()?.stopOnConnectionFailure === true) {
- this.stopAutomaticTransactionGenerator()
- }
if (
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.autoReconnectRetryCount < this.stationInfo!.autoReconnectMaxRetries! ||
+ this.wsConnectionRetryCount < this.stationInfo!.autoReconnectMaxRetries! ||
this.stationInfo?.autoReconnectMaxRetries === -1
) {
- ++this.autoReconnectRetryCount
+ this.wsConnectionRetried = true
+ ++this.wsConnectionRetryCount
const reconnectDelay =
this.stationInfo?.reconnectExponentialDelay === true
- ? exponentialDelay(this.autoReconnectRetryCount)
+ ? exponentialDelay(this.wsConnectionRetryCount)
: secondsToMilliseconds(this.getConnectionTimeout())
const reconnectDelayWithdraw = 1000
const reconnectTimeout =
)
await sleep(reconnectDelay)
logger.error(
- `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
+ `${this.logPrefix()} WebSocket connection retry #${this.wsConnectionRetryCount.toString()}`
)
this.openWSConnection(
{
} else if (this.stationInfo?.autoReconnectMaxRetries !== -1) {
logger.error(
`${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
- this.autoReconnectRetryCount
+ this.wsConnectionRetryCount
}) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`
)
}