import Constants from '../utils/Constants';
import { ErrorType } from '../types/ocpp/ErrorType';
import FileUtils from '../utils/FileUtils';
+import { JsonType } from '../types/JsonType';
import { MessageType } from '../types/ocpp/MessageType';
import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService';
import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService';
import path from 'path';
export default class ChargingStation {
+ public readonly id: string;
public readonly stationTemplateFile: string;
public authorizedTags: string[];
public stationInfo!: ChargingStationInfo;
private webSocketPingSetInterval!: NodeJS.Timeout;
constructor(index: number, stationTemplateFile: string) {
+ this.id = Utils.generateUUID();
this.index = index;
this.stationTemplateFile = stationTemplateFile;
- this.connectors = new Map<number, ConnectorStatus>();
- this.initialize();
-
this.stopped = false;
this.wsConnectionRestarted = false;
this.autoReconnectRetryCount = 0;
-
+ this.connectors = new Map<number, ConnectorStatus>();
this.requests = new Map<string, CachedRequest>();
this.messageBuffer = new Set<string>();
-
+ this.initialize();
this.authorizedTags = this.getAuthorizedTags();
}
return this?.wsConnection?.readyState === OPEN;
}
+ public getRegistrationStatus(): RegistrationStatus {
+ return this?.bootNotificationResponse?.status;
+ }
+
+ public isInUnknownState(): boolean {
+ return Utils.isNullOrUndefined(this?.bootNotificationResponse?.status);
+ }
+
public isInPendingState(): boolean {
return this?.bootNotificationResponse?.status === RegistrationStatus.PENDING;
}
}
public isRegistered(): boolean {
- return this.isInAcceptedState() || this.isInPendingState();
+ return !this.isInUnknownState() && (this.isInAcceptedState() || this.isInPendingState());
}
public isChargingStationAvailable(): boolean {
}
public isConnectorAvailable(id: number): boolean {
- return this.getConnectorStatus(id).availability === AvailabilityType.OPERATIVE;
+ return id > 0 && this.getConnectorStatus(id).availability === AvailabilityType.OPERATIVE;
}
public getNumberOfConnectors(): number {
return this.stationInfo.currentOutType ?? CurrentType.AC;
}
+ public getOcppStrictCompliance(): boolean {
+ return this.stationInfo.ocppStrictCompliance ?? false;
+ }
+
public getVoltageOut(): number | undefined {
const errMsg = `${this.logPrefix()} Unknown ${this.getCurrentOutType()} currentOutType in template file ${this.stationTemplateFile}, cannot define default voltage out`;
let defaultVoltageOut: number;
this.wsConfiguredConnectionUrl = new URL(this.getConfiguredSupervisionUrl().href + '/' + this.stationInfo.chargingStationId);
switch (this.getOcppVersion()) {
case OCPPVersion.VERSION_16:
- this.ocppIncomingRequestService = new OCPP16IncomingRequestService(this);
- this.ocppRequestService = new OCPP16RequestService(this, new OCPP16ResponseService(this));
+ this.ocppIncomingRequestService = OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>(this);
+ this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(this, OCPP16ResponseService.getInstance<OCPP16ResponseService>(this));
break;
default:
this.handleUnsupportedVersion(this.getOcppVersion());
}
this.stationInfo.powerDivider = this.getPowerDivider();
if (this.getEnableStatistics()) {
- this.performanceStatistics = new PerformanceStatistics(this.stationInfo.chargingStationId, this.wsConnectionUrl);
+ this.performanceStatistics = PerformanceStatistics.getInstance(this.id, this.stationInfo.chargingStationId, this.wsConnectionUrl);
}
}
private async onOpen(): Promise<void> {
logger.info(`${this.logPrefix()} Connected to OCPP server through ${this.wsConnectionUrl.toString()}`);
- if (!this.isRegistered()) {
+ if (!this.isInAcceptedState()) {
// Send BootNotification
let registrationRetryCount = 0;
do {
this.bootNotificationResponse = await this.ocppRequestService.sendBootNotification(this.bootNotificationRequest.chargePointModel,
this.bootNotificationRequest.chargePointVendor, this.bootNotificationRequest.chargeBoxSerialNumber, this.bootNotificationRequest.firmwareVersion);
- if (!this.isRegistered()) {
- registrationRetryCount++;
+ if (!this.isInAcceptedState()) {
+ this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
await Utils.sleep(this.bootNotificationResponse?.interval ? this.bootNotificationResponse.interval * 1000 : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL);
}
- } while (!this.isRegistered() && (registrationRetryCount <= this.getRegistrationMaxRetries() || this.getRegistrationMaxRetries() === -1));
- }
- if (this.isRegistered() && this.stationInfo.autoRegister) {
- await this.ocppRequestService.sendBootNotification(this.bootNotificationRequest.chargePointModel,
- this.bootNotificationRequest.chargePointVendor, this.bootNotificationRequest.chargeBoxSerialNumber, this.bootNotificationRequest.firmwareVersion);
+ } while (!this.isInAcceptedState() && (registrationRetryCount <= this.getRegistrationMaxRetries() || this.getRegistrationMaxRetries() === -1));
}
if (this.isInAcceptedState()) {
await this.startMessageSequence();
if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
this.flushMessageBuffer();
}
- } else if (this.isInPendingState()) {
- // The central server shall issue a triggerMessage to the charging station for the boot notification at the end of its configuration process
- while (!this.isInAcceptedState()) {
- await this.startMessageSequence();
- this.stopped && (this.stopped = false);
- if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
- this.flushMessageBuffer();
- }
- await Utils.sleep(Constants.CHARGING_STATION_DEFAULT_START_SEQUENCE_DELAY);
- }
} 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 responseCallback: (payload: JsonType | string, requestPayload: JsonType | OCPPError) => void;
let rejectCallback: (error: OCPPError, requestStatistic?: boolean) => void;
let requestCommandName: RequestCommand | IncomingRequestCommand;
- let requestPayload: Record<string, unknown>;
+ let requestPayload: JsonType | OCPPError;
let cachedRequest: CachedRequest;
let errMsg: string;
try {
}
private async startMessageSequence(): Promise<void> {
+ if (this.stationInfo.autoRegister) {
+ await this.ocppRequestService.sendBootNotification(this.bootNotificationRequest.chargePointModel,
+ this.bootNotificationRequest.chargePointVendor, this.bootNotificationRequest.chargeBoxSerialNumber, this.bootNotificationRequest.firmwareVersion);
+ }
// Start WebSocket ping
this.startWebSocketPing();
// Start heartbeat
this.stopHeartbeat();
// Stop the ATG
if (this.stationInfo.AutomaticTransactionGenerator.enable &&
- this.automaticTransactionGenerator &&
- this.automaticTransactionGenerator.started) {
+ this.automaticTransactionGenerator?.started) {
this.automaticTransactionGenerator.stop();
} else {
for (const connectorId of this.connectors.keys()) {
// Stop the ATG if needed
if (this.stationInfo.AutomaticTransactionGenerator.enable &&
this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure &&
- this.automaticTransactionGenerator &&
- this.automaticTransactionGenerator.started) {
+ this.automaticTransactionGenerator?.started) {
this.automaticTransactionGenerator.stop();
}
if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {