type ErrorCallback,
type ErrorResponse,
ErrorType,
+ type EvseStatus,
FileType,
FirmwareStatus,
type FirmwareStatusNotificationRequest,
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;
private configurationFile!: string;
private configurationFileHash!: string;
private connectorsConfigurationHash!: string;
+ private evsesConfigurationHash!: string;
private ocppIncomingRequestService!: OCPPIncomingRequestService;
private readonly messageBuffer: Set<string>;
private configuredSupervisionUrl!: URL;
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();
}
public isChargingStationAvailable(): boolean {
- return this.getConnectorStatus(0)?.availability === AvailabilityType.OPERATIVE;
+ return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative;
}
public isConnectorAvailable(id: number): boolean {
- return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.OPERATIVE;
+ return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.Operative;
}
public getNumberOfConnectors(): number {
public startMeterValues(connectorId: number, interval: number): void {
if (connectorId === 0) {
logger.error(
- `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
+ `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
);
return;
}
if (!this.getConnectorStatus(connectorId)) {
logger.error(
- `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
+ `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
);
return;
}
if (this.getConnectorStatus(connectorId)?.transactionStarted === false) {
logger.error(
- `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
+ `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
);
return;
} else if (
Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId)
) {
logger.error(
- `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
+ `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
);
return;
}
}
}
+ public stopMeterValues(connectorId: number) {
+ if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
+ clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
+ }
+ }
+
public start(): void {
if (this.started === false) {
if (this.starting === false) {
}
}
- public resetConnectorStatus(connectorId: number): void {
- this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
- this.getConnectorStatus(connectorId).idTagAuthorized = false;
- this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
- this.getConnectorStatus(connectorId).transactionStarted = false;
- delete this.getConnectorStatus(connectorId)?.localAuthorizeIdTag;
- delete this.getConnectorStatus(connectorId)?.authorizeIdTag;
- delete this.getConnectorStatus(connectorId)?.transactionId;
- delete this.getConnectorStatus(connectorId)?.transactionIdTag;
- this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
- delete this.getConnectorStatus(connectorId)?.transactionBeginMeterValue;
- this.stopMeterValues(connectorId);
- parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
- }
-
public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined {
return ChargingStationConfigurationUtils.getConfigurationKey(
this,
private flushMessageBuffer(): void {
if (this.messageBuffer.size > 0) {
- this.messageBuffer.forEach((message) => {
+ for (const message of this.messageBuffer.values()) {
let beginId: string;
let commandName: RequestCommand;
const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse;
)} payload sent: ${message}`
);
this.messageBuffer.delete(message);
- });
+ }
}
}
stationInfo.resetTime = !Utils.isNullOrUndefined(stationTemplate?.resetTime)
? stationTemplate.resetTime * 1000
: Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
- const configuredMaxConnectors =
- ChargingStationUtils.getConfiguredNumberOfConnectors(stationTemplate);
- ChargingStationUtils.checkConfiguredMaxConnectors(
- configuredMaxConnectors,
- this.templateFile,
- this.logPrefix()
- );
- const templateMaxConnectors =
- ChargingStationUtils.getTemplateMaxNumberOfConnectors(stationTemplate);
- ChargingStationUtils.checkTemplateMaxConnectors(
- templateMaxConnectors,
- this.templateFile,
- this.logPrefix()
- );
- if (
- configuredMaxConnectors >
- (stationTemplate?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) &&
- !stationTemplate?.randomConnectors
- ) {
- logger.warn(
- `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
- this.templateFile
- }, forcing random connector configurations affectation`
- );
- stationInfo.randomConnectors = true;
- }
- // Build connectors if needed (FIXME: should be factored out)
- this.initializeConnectors(stationInfo, configuredMaxConnectors, templateMaxConnectors);
+ // Initialize evses or connectors if needed (FIXME: should be factored out)
+ this.initializeConnectorsOrEvses(stationInfo);
stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
ChargingStationUtils.createStationInfoHash(stationInfo);
return stationInfo;
this.stationInfo.firmwareVersion = match?.join('.');
}
this.saveStationInfo();
- // Avoid duplication of connectors related information in RAM
+ // Avoid duplication of connectors or evses related information in RAM
delete this.stationInfo?.Connectors;
+ delete this.stationInfo?.Evses;
this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
if (this.getEnableStatistics() === true) {
this.performanceStatistics = PerformanceStatistics.getInstance(
this.saveOcppConfiguration();
}
- private initializeConnectors(
- stationInfo: ChargingStationInfo,
- configuredMaxConnectors: number,
- templateMaxConnectors: number
- ): void {
+ private initializeConnectorsOrEvses(stationInfo: ChargingStationInfo) {
+ if (stationInfo?.Connectors && !stationInfo?.Evses) {
+ this.initializeConnectors(stationInfo);
+ } else if (stationInfo?.Evses && !stationInfo?.Connectors) {
+ this.initializeEvses(stationInfo);
+ } else if (stationInfo?.Evses && stationInfo?.Connectors) {
+ const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}`;
+ logger.error(`${this.logPrefix()} ${errorMsg}`);
+ throw new BaseError(errorMsg);
+ } else {
+ const errorMsg = `No connectors or evses defined in template file ${this.templateFile}`;
+ logger.error(`${this.logPrefix()} ${errorMsg}`);
+ throw new BaseError(errorMsg);
+ }
+ }
+
+ private initializeConnectors(stationInfo: ChargingStationInfo): void {
if (!stationInfo?.Connectors && this.connectors.size === 0) {
const logMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
logger.error(`${this.logPrefix()} ${logMsg}`);
logger.warn(
`${this.logPrefix()} Charging station information from template ${
this.templateFile
- } with no connector Id 0 configuration`
+ } with no connector id 0 configuration`
);
}
if (stationInfo?.Connectors) {
+ const configuredMaxConnectors =
+ ChargingStationUtils.getConfiguredNumberOfConnectors(stationInfo);
+ ChargingStationUtils.checkConfiguredMaxConnectors(
+ configuredMaxConnectors,
+ this.templateFile,
+ this.logPrefix()
+ );
const connectorsConfigHash = crypto
.createHash(Constants.DEFAULT_HASH_ALGORITHM)
.update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
if (this.connectors?.size === 0 || connectorsConfigChanged) {
connectorsConfigChanged && this.connectors.clear();
this.connectorsConfigurationHash = connectorsConfigHash;
- // Add connector Id 0
- let lastConnector = '0';
- for (lastConnector in stationInfo?.Connectors) {
- const connectorStatus = stationInfo?.Connectors[lastConnector];
- const lastConnectorId = Utils.convertToInt(lastConnector);
- if (
- lastConnectorId === 0 &&
- this.getUseConnectorId0(stationInfo) === true &&
- connectorStatus
- ) {
- this.checkStationInfoConnectorStatus(lastConnectorId, connectorStatus);
- this.connectors.set(
- lastConnectorId,
- Utils.cloneObject<ConnectorStatus>(connectorStatus)
- );
- this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE;
- if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) {
- this.getConnectorStatus(lastConnectorId).chargingProfiles = [];
- }
+ const connectorZeroStatus = stationInfo?.Connectors[0];
+ // Add connector id 0
+ if (connectorZeroStatus && this.getUseConnectorId0(stationInfo) === true) {
+ ChargingStationUtils.checkStationInfoConnectorStatus(
+ 0,
+ connectorZeroStatus,
+ this.logPrefix(),
+ this.templateFile
+ );
+ this.connectors.set(0, Utils.cloneObject<ConnectorStatus>(connectorZeroStatus));
+ this.getConnectorStatus(0).availability = AvailabilityType.Operative;
+ if (Utils.isUndefined(this.getConnectorStatus(0)?.chargingProfiles)) {
+ this.getConnectorStatus(0).chargingProfiles = [];
}
}
- // Generate all connectors
- if ((stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) > 0) {
- for (let index = 1; index <= configuredMaxConnectors; index++) {
- const randConnectorId = stationInfo?.randomConnectors
- ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1)
- : index;
- const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()];
- this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus);
- this.connectors.set(index, Utils.cloneObject<ConnectorStatus>(connectorStatus));
- this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE;
- if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) {
- this.getConnectorStatus(index).chargingProfiles = [];
+ // Add remaining connectors
+ const templateMaxConnectors = ChargingStationUtils.getMaxNumberOfConnectors(
+ stationInfo.Connectors
+ );
+ ChargingStationUtils.checkTemplateMaxConnectors(
+ templateMaxConnectors,
+ this.templateFile,
+ this.logPrefix()
+ );
+ if (
+ configuredMaxConnectors >
+ (stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) &&
+ !stationInfo?.randomConnectors
+ ) {
+ logger.warn(
+ `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
+ this.templateFile
+ }, forcing random connector configurations affectation`
+ );
+ stationInfo.randomConnectors = true;
+ }
+ const templateMaxAvailableConnectors = stationInfo?.Connectors[0]
+ ? templateMaxConnectors - 1
+ : templateMaxConnectors;
+ if (templateMaxAvailableConnectors > 0) {
+ for (let connectorId = 1; connectorId <= configuredMaxConnectors; connectorId++) {
+ const templateConnectorId = stationInfo?.randomConnectors
+ ? Utils.getRandomInteger(templateMaxAvailableConnectors, 1)
+ : connectorId;
+ const connectorStatus = stationInfo?.Connectors[templateConnectorId];
+ ChargingStationUtils.checkStationInfoConnectorStatus(
+ templateConnectorId,
+ connectorStatus,
+ this.logPrefix(),
+ this.templateFile
+ );
+ this.connectors.set(connectorId, Utils.cloneObject<ConnectorStatus>(connectorStatus));
+ this.getConnectorStatus(connectorId).availability = AvailabilityType.Operative;
+ if (Utils.isUndefined(this.getConnectorStatus(connectorId)?.chargingProfiles)) {
+ this.getConnectorStatus(connectorId).chargingProfiles = [];
}
+ ChargingStationUtils.initializeConnectorsMapStatus(this.connectors, this.logPrefix());
}
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no connectors configuration defined, cannot create connectors`
+ );
}
}
} else {
} with no connectors configuration defined, using already defined connectors`
);
}
- // Initialize transaction attributes on connectors
- for (const connectorId of this.connectors.keys()) {
- if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
- logger.warn(
- `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
- this.getConnectorStatus(connectorId)?.transactionId
- }`
- );
- }
- if (
- connectorId > 0 &&
- Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted)
- ) {
- this.initializeConnectorStatus(connectorId);
- }
- }
}
- private checkStationInfoConnectorStatus(
- connectorId: number,
- connectorStatus: ConnectorStatus
- ): void {
- if (!Utils.isNullOrUndefined(connectorStatus?.status)) {
+ private initializeEvses(stationInfo: ChargingStationInfo): void {
+ if (!stationInfo?.Evses && this.evses.size === 0) {
+ const logMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
+ logger.error(`${this.logPrefix()} ${logMsg}`);
+ throw new BaseError(logMsg);
+ }
+ if (!stationInfo?.Evses[0]) {
logger.warn(
`${this.logPrefix()} Charging station information from template ${
this.templateFile
- } with connector ${connectorId} status configuration defined, undefine it`
+ } with no evse id 0 configuration`
);
- delete connectorStatus.status;
+ }
+ if (stationInfo?.Evses) {
+ const evsesConfigHash = crypto
+ .createHash(Constants.DEFAULT_HASH_ALGORITHM)
+ .update(`${JSON.stringify(stationInfo?.Evses)}`)
+ .digest('hex');
+ const evsesConfigChanged =
+ this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash;
+ if (this.evses?.size === 0 || evsesConfigChanged) {
+ evsesConfigChanged && this.evses.clear();
+ this.evsesConfigurationHash = evsesConfigHash;
+ const templateMaxEvses = ChargingStationUtils.getMaxNumberOfEvses(stationInfo?.Evses);
+ if (templateMaxEvses > 0) {
+ for (const evse in stationInfo.Evses) {
+ this.evses.set(Utils.convertToInt(evse), {
+ connectors: ChargingStationUtils.buildConnectorsMap(
+ stationInfo?.Evses[evse]?.Connectors,
+ this.logPrefix(),
+ this.templateFile
+ ),
+ availability: AvailabilityType.Operative,
+ });
+ ChargingStationUtils.initializeConnectorsMapStatus(
+ this.evses.get(Utils.convertToInt(evse))?.connectors,
+ this.logPrefix()
+ );
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no evses configuration defined, cannot create evses`
+ );
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no evses configuration defined, using already defined evses`
+ );
+ }
}
}
// Set default status
connectorStatus = ConnectorStatusEnum.Available;
}
- await this.ocppRequestService.requestHandler<
- StatusNotificationRequest,
- StatusNotificationResponse
- >(
- this,
- RequestCommand.STATUS_NOTIFICATION,
- OCPPServiceUtils.buildStatusNotificationRequest(this, connectorId, connectorStatus)
- );
- this.getConnectorStatus(connectorId).status = connectorStatus;
+ await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorStatus);
}
if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) {
await this.ocppRequestService.requestHandler<
}
}
- private stopMeterValues(connectorId: number) {
- if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
- clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
- }
- }
-
private getReconnectExponentialDelay(): boolean {
return this.stationInfo?.reconnectExponentialDelay ?? false;
}
| undefined {
return this.getTemplateFromFile()?.AutomaticTransactionGenerator;
}
-
- private initializeConnectorStatus(connectorId: number): void {
- this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
- this.getConnectorStatus(connectorId).idTagAuthorized = false;
- this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
- this.getConnectorStatus(connectorId).transactionStarted = false;
- this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0;
- this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
- }
}