-} from '../utils';
-
-export class ChargingStation {
- public readonly index: number;
- public readonly templateFile: string;
- public stationInfo!: ChargingStationInfo;
- public started: boolean;
- public starting: boolean;
- public idTagsCache: IdTagsCache;
- public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined;
- public ocppConfiguration!: ChargingStationOcppConfiguration | undefined;
- public wsConnection!: WebSocket | null;
- public readonly connectors: Map<number, ConnectorStatus>;
- public readonly evses: Map<number, EvseStatus>;
- public readonly requests: Map<string, CachedRequest>;
- public performanceStatistics!: PerformanceStatistics | undefined;
- public heartbeatSetInterval!: NodeJS.Timeout;
- public ocppRequestService!: OCPPRequestService;
- public bootNotificationRequest!: BootNotificationRequest;
- public bootNotificationResponse!: BootNotificationResponse | undefined;
- public powerDivider!: number;
- private stopping: boolean;
- private configurationFile!: string;
- private configurationFileHash!: string;
- private connectorsConfigurationHash!: string;
- private evsesConfigurationHash!: string;
- private ocppIncomingRequestService!: OCPPIncomingRequestService;
- private readonly messageBuffer: Set<string>;
- private configuredSupervisionUrl!: URL;
- private wsConnectionRestarted: boolean;
- private autoReconnectRetryCount: number;
- private templateFileWatcher!: fs.FSWatcher | undefined;
- private readonly sharedLRUCache: SharedLRUCache;
- private webSocketPingSetInterval!: NodeJS.Timeout;
- private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
-
- constructor(index: number, templateFile: string) {
- this.started = false;
- this.starting = false;
- this.stopping = false;
- this.wsConnectionRestarted = false;
- this.autoReconnectRetryCount = 0;
- this.index = index;
- this.templateFile = templateFile;
- this.connectors = new Map<number, ConnectorStatus>();
- this.evses = new Map<number, EvseStatus>();
- this.requests = new Map<string, CachedRequest>();
- this.messageBuffer = new Set<string>();
- this.sharedLRUCache = SharedLRUCache.getInstance();
- this.idTagsCache = IdTagsCache.getInstance();
- this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this);
-
- this.initialize();
- }
-
- private get wsConnectionUrl(): URL {
- return new URL(
- `${
- this.getSupervisionUrlOcppConfiguration() &&
- Utils.isNotEmptyString(this.getSupervisionUrlOcppKey())
- ? ChargingStationConfigurationUtils.getConfigurationKey(
- this,
- this.getSupervisionUrlOcppKey()
- )?.value
- : this.configuredSupervisionUrl.href
- }/${this.stationInfo.chargingStationId}`
- );
- }
+ logPrefix,
+ min,
+ roundTo,
+ secureRandom,
+ sleep,
+ watchJsonFile
+} from '../utils/index.js'
+import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator.js'
+import { ChargingStationWorkerBroadcastChannel } from './broadcast-channel/ChargingStationWorkerBroadcastChannel.js'
+import {
+ addConfigurationKey,
+ deleteConfigurationKey,
+ getConfigurationKey,
+ setConfigurationKeyValue
+} from './ConfigurationKeyUtils.js'
+import {
+ buildConnectorsMap,
+ buildTemplateName,
+ checkChargingStation,
+ checkConfiguration,
+ checkConnectorsConfiguration,
+ checkStationInfoConnectorStatus,
+ checkTemplate,
+ createBootNotificationRequest,
+ createSerialNumber,
+ getAmperageLimitationUnitDivider,
+ getBootConnectorStatus,
+ getChargingStationConnectorChargingProfilesPowerLimit,
+ getChargingStationId,
+ getDefaultVoltageOut,
+ getHashId,
+ getIdTagsFile,
+ getMaxNumberOfEvses,
+ getNumberOfReservableConnectors,
+ getPhaseRotationValue,
+ hasFeatureProfile,
+ hasReservationExpired,
+ initializeConnectorsMapStatus,
+ propagateSerialNumber,
+ setChargingStationOptions,
+ stationTemplateToStationInfo,
+ warnTemplateKeysDeprecation
+} from './Helpers.js'
+import { IdTagsCache } from './IdTagsCache.js'
+import {
+ buildMeterValue,
+ buildTransactionEndMeterValue,
+ getMessageTypeString,
+ OCPP16IncomingRequestService,
+ OCPP16RequestService,
+ OCPP16ResponseService,
+ OCPP20IncomingRequestService,
+ OCPP20RequestService,
+ OCPP20ResponseService,
+ type OCPPIncomingRequestService,
+ type OCPPRequestService,
+ sendAndSetConnectorStatus
+} from './ocpp/index.js'
+import { SharedLRUCache } from './SharedLRUCache.js'
+
+export class ChargingStation extends EventEmitter {
+ public readonly index: number
+ public readonly templateFile: string
+ public stationInfo?: ChargingStationInfo
+ public started: boolean
+ public starting: boolean
+ public idTagsCache: IdTagsCache
+ public automaticTransactionGenerator?: AutomaticTransactionGenerator
+ public ocppConfiguration?: ChargingStationOcppConfiguration
+ public wsConnection: WebSocket | null
+ public readonly connectors: Map<number, ConnectorStatus>
+ public readonly evses: Map<number, EvseStatus>
+ public readonly requests: Map<string, CachedRequest>
+ public performanceStatistics?: PerformanceStatistics
+ public heartbeatSetInterval?: NodeJS.Timeout
+ public ocppRequestService!: OCPPRequestService
+ public bootNotificationRequest?: BootNotificationRequest
+ public bootNotificationResponse?: BootNotificationResponse
+ public powerDivider?: number
+ private stopping: boolean
+ private configurationFile!: string
+ private configurationFileHash!: string
+ private connectorsConfigurationHash!: string
+ private evsesConfigurationHash!: string
+ private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration
+ private ocppIncomingRequestService!: OCPPIncomingRequestService
+ private readonly messageBuffer: Set<string>
+ private configuredSupervisionUrl!: URL
+ private wsConnectionRetried: boolean
+ private wsConnectionRetryCount: number
+ private templateFileWatcher?: FSWatcher
+ private templateFileHash!: string
+ private readonly sharedLRUCache: SharedLRUCache
+ private wsPingSetInterval?: NodeJS.Timeout
+ private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel
+ private flushMessageBufferSetInterval?: NodeJS.Timeout
+
+ constructor (index: number, templateFile: string, options?: ChargingStationOptions) {
+ super()
+ this.started = false
+ this.starting = false
+ this.stopping = false
+ this.wsConnection = null
+ this.wsConnectionRetried = false
+ this.wsConnectionRetryCount = 0
+ this.index = index
+ this.templateFile = templateFile
+ this.connectors = new Map<number, ConnectorStatus>()
+ this.evses = new Map<number, EvseStatus>()
+ this.requests = new Map<string, CachedRequest>()
+ this.messageBuffer = new Set<string>()
+ this.sharedLRUCache = SharedLRUCache.getInstance()
+ this.idTagsCache = IdTagsCache.getInstance()
+ this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this)
+
+ this.on(ChargingStationEvents.added, () => {
+ parentPort?.postMessage(buildAddedMessage(this))
+ })
+ this.on(ChargingStationEvents.deleted, () => {
+ parentPort?.postMessage(buildDeletedMessage(this))
+ })
+ this.on(ChargingStationEvents.started, () => {
+ parentPort?.postMessage(buildStartedMessage(this))
+ })
+ this.on(ChargingStationEvents.stopped, () => {
+ parentPort?.postMessage(buildStoppedMessage(this))
+ })
+ this.on(ChargingStationEvents.updated, () => {
+ parentPort?.postMessage(buildUpdatedMessage(this))
+ })
+ this.on(ChargingStationEvents.accepted, () => {
+ this.startMessageSequence(
+ this.wsConnectionRetried
+ ? true
+ : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration
+ ).catch((error: unknown) => {
+ 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
+ )
+ }
+ })