private bootNotificationResponse!: BootNotificationResponse | null;
private connectorsConfigurationHash!: string;
private ocppIncomingRequestService!: OCPPIncomingRequestService;
- private readonly messageQueue: string[];
+ private readonly messageBuffer: Set<string>;
private wsConnectionUrl!: URL;
private wsConnectionRestarted: boolean;
private stopped: boolean;
this.autoReconnectRetryCount = 0;
this.requests = new Map<string, CachedRequest>();
- this.messageQueue = new Array<string>();
+ this.messageBuffer = new Set<string>();
this.authorizedTags = this.getAuthorizedTags();
}
!cpReplaced && this.getConnectorStatus(connectorId).chargingProfiles?.push(cp);
}
- public resetTransactionOnConnector(connectorId: number): void {
- this.getConnectorStatus(connectorId).authorized = 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.stopMeterValues(connectorId);
}
- public addToMessageQueue(message: string): void {
- let dups = false;
- // Handle dups in message queue
- for (const bufferedMessage of this.messageQueue) {
- // Message already in the queue
- if (message === bufferedMessage) {
- dups = true;
- break;
- }
- }
- if (!dups) {
- // Queue message
- this.messageQueue.push(message);
- }
+ public bufferMessage(message: string): void {
+ this.messageBuffer.add(message);
}
- private flushMessageQueue() {
- if (!Utils.isEmptyArray(this.messageQueue)) {
- this.messageQueue.forEach((message, index) => {
- this.messageQueue.splice(index, 1);
+ private flushMessageBuffer() {
+ if (this.messageBuffer.size > 0) {
+ this.messageBuffer.forEach((message) => {
// TODO: evaluate the need to track performance
this.wsConnection.send(message);
+ this.messageBuffer.delete(message);
});
}
}
FileUtils.handleFileException(this.logPrefix(), 'Template', this.stationTemplateFile, error);
}
const stationInfo: ChargingStationInfo = stationTemplateFromFile ?? {} as ChargingStationInfo;
+ stationInfo.wsOptions = stationTemplateFromFile?.wsOptions ?? {};
if (!Utils.isEmptyArray(stationTemplateFromFile.power)) {
stationTemplateFromFile.power = stationTemplateFromFile.power as number[];
const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplateFromFile.power.length);
private initialize(): void {
this.stationInfo = this.buildStationInfo();
+ this.configuration = this.getTemplateChargingStationConfiguration();
+ delete this.stationInfo.Configuration;
this.bootNotificationRequest = {
chargePointModel: this.stationInfo.chargePointModel,
chargePointVendor: this.stationInfo.chargePointVendor,
...!Utils.isUndefined(this.stationInfo.chargeBoxSerialNumberPrefix) && { chargeBoxSerialNumber: this.stationInfo.chargeBoxSerialNumberPrefix },
...!Utils.isUndefined(this.stationInfo.firmwareVersion) && { firmwareVersion: this.stationInfo.firmwareVersion },
};
- this.configuration = this.getTemplateChargingStationConfiguration();
this.wsConnectionUrl = new URL(this.getSupervisionURL().href + '/' + this.stationInfo.chargingStationId);
// Build connectors if needed
const maxConnectors = this.getMaxNumberOfConnectors();
// Initialize transaction attributes on connectors
for (const connectorId of this.connectors.keys()) {
if (connectorId > 0 && !this.getConnectorStatus(connectorId)?.transactionStarted) {
- this.initTransactionAttributesOnConnector(connectorId);
+ this.initializeConnectorStatus(connectorId);
}
}
switch (this.getOCPPVersion()) {
await this.startMessageSequence();
this.stopped && (this.stopped = false);
if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
- this.flushMessageQueue();
+ this.flushMessageBuffer();
}
} else {
logger.error(`${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`);
private async onMessage(data: Data): Promise<void> {
let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, {}, {}];
let responseCallback: (payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>) => void;
- let rejectCallback: (error: OCPPError) => void;
+ let rejectCallback: (error: OCPPError, requestStatistic?: boolean) => void;
let requestCommandName: RequestCommand | IncomingRequestCommand;
let requestPayload: Record<string, unknown>;
let cachedRequest: CachedRequest;
}
} catch (error) {
// Log
- logger.error('%s Incoming OCPP message %j matching cached request %j processing error %j', this.logPrefix(), data, this.requests.get(messageId), error);
+ logger.error('%s Incoming OCPP message %j matching cached request %j processing error %j', this.logPrefix(), data.toString(), this.requests.get(messageId), error);
// Send error
messageType === MessageType.CALL_MESSAGE && await this.ocppRequestService.sendError(messageId, error, commandName);
}
}
}
- private openWSConnection(options?: ClientOptions & ClientRequestArgs, forceCloseOpened = false): void {
- options = options ?? {};
+ private openWSConnection(options: ClientOptions & ClientRequestArgs = this.stationInfo.wsOptions, forceCloseOpened = false): void {
options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
if (!Utils.isNullOrUndefined(this.stationInfo.supervisionUser) && !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword)) {
options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {
this.autoReconnectRetryCount++;
const reconnectDelay = (this.getReconnectExponentialDelay() ? Utils.exponentialDelay(this.autoReconnectRetryCount) : this.getConnectionTimeout() * 1000);
- const reconnectTimeout = reconnectDelay - 100;
+ const reconnectTimeout = (reconnectDelay - 100) > 0 && reconnectDelay;
logger.error(`${this.logPrefix()} WebSocket: connection retry in ${Utils.roundTo(reconnectDelay, 2)}ms, timeout ${reconnectTimeout}ms`);
await Utils.sleep(reconnectDelay);
logger.error(this.logPrefix() + ' WebSocket: reconnecting try #' + this.autoReconnectRetryCount.toString());
- this.openWSConnection({ handshakeTimeout: reconnectTimeout }, true);
+ this.openWSConnection({ ...this.stationInfo.wsOptions, handshakeTimeout: reconnectTimeout }, true);
this.wsConnectionRestarted = true;
} else if (this.getAutoReconnectMaxRetries() !== -1) {
logger.error(`${this.logPrefix()} WebSocket reconnect failure: max retries reached (${this.autoReconnectRetryCount}) or retry disabled (${this.getAutoReconnectMaxRetries()})`);
}
}
- private initTransactionAttributesOnConnector(connectorId: number): void {
- this.getConnectorStatus(connectorId).authorized = false;
+ 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;