public starting: boolean;
public authorizedTagsCache: AuthorizedTagsCache;
public automaticTransactionGenerator!: AutomaticTransactionGenerator;
- public ocppConfiguration!: ChargingStationOcppConfiguration;
- public wsConnection!: WebSocket;
+ public ocppConfiguration!: ChargingStationOcppConfiguration | null;
+ public wsConnection!: WebSocket | null;
public readonly connectors: Map<number, ConnectorStatus>;
public readonly requests: Map<string, CachedRequest>;
public performanceStatistics!: PerformanceStatistics;
private configuredSupervisionUrlIndex!: number;
private wsConnectionRestarted: boolean;
private autoReconnectRetryCount: number;
- private templateFileWatcher!: fs.FSWatcher;
+ private templateFileWatcher!: fs.FSWatcher | undefined;
private readonly sharedLRUCache: SharedLRUCache;
private webSocketPingSetInterval!: NodeJS.Timeout;
private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
? ChargingStationConfigurationUtils.getConfigurationKey(
this,
this.getSupervisionUrlOcppKey()
- ).value
+ )?.value
: this.configuredSupervisionUrl.href
}/${this.stationInfo.chargingStationId}`
);
}
public getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType {
- return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC;
+ return (stationInfo ?? this.stationInfo)?.currentOutType ?? CurrentType.AC;
}
public getOcppStrictCompliance(): boolean {
let connectorAmperageLimitationPowerLimit: number;
if (
!Utils.isNullOrUndefined(this.getAmperageLimitation()) &&
- this.getAmperageLimitation() < this.stationInfo.maximumAmperage
+ this.getAmperageLimitation() < this.stationInfo?.maximumAmperage
) {
connectorAmperageLimitationPowerLimit =
(this.getCurrentOutType() === CurrentType.AC
connectorId > 0 &&
this.getConnectorStatus(connectorId)?.transactionId === transactionId
) {
- return this.getConnectorStatus(connectorId).transactionIdTag;
+ return this.getConnectorStatus(connectorId)?.transactionIdTag;
}
}
}
this.heartbeatSetInterval = setInterval(() => {
this.ocppRequestService
.requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
- .catch(error => {
+ .catch((error) => {
logger.error(
`${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
error
RequestCommand.METER_VALUES,
{
connectorId,
- transactionId: this.getConnectorStatus(connectorId).transactionId,
+ transactionId: this.getConnectorStatus(connectorId)?.transactionId,
meterValue: [meterValue],
}
)
- .catch(error => {
+ .catch((error) => {
logger.error(
`${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
error
this.performanceStatistics.stop();
}
this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
- this.templateFileWatcher.close();
+ this.templateFileWatcher?.close();
this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
this.bootNotificationResponse = undefined;
this.started = false;
parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
}
- public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean {
+ public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined {
return ChargingStationConfigurationUtils.getConfigurationKey(
this,
StandardParametersKey.SupportedFeatureProfiles
- )?.value.includes(featureProfile);
+ )?.value?.includes(featureProfile);
}
public bufferMessage(message: string): void {
public closeWSConnection(): void {
if (this.isWebSocketConnectionOpened() === true) {
- this.wsConnection.close();
+ this.wsConnection?.close();
this.wsConnection = null;
}
}
connectorId: number,
reason = StopTransactionReason.NONE
): Promise<StopTransactionResponse> {
- const transactionId = this.getConnectorStatus(connectorId).transactionId;
+ const transactionId = this.getConnectorStatus(connectorId)?.transactionId;
if (
this.getBeginEndMeterValues() === true &&
this.getOcppStrictCompliance() === true &&
private flushMessageBuffer(): void {
if (this.messageBuffer.size > 0) {
- this.messageBuffer.forEach(message => {
+ this.messageBuffer.forEach((message) => {
let beginId: string;
let commandName: RequestCommand;
const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse;
[, , commandName] = JSON.parse(message) as OutgoingRequest;
beginId = PerformanceStatistics.beginMeasure(commandName);
}
- this.wsConnection.send(message);
+ this.wsConnection?.send(message);
isRequest && PerformanceStatistics.endMeasure(commandName, beginId);
logger.debug(
`${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
}
- private getTemplateFromFile(): ChargingStationTemplate | null {
- let template: ChargingStationTemplate = null;
+ private getTemplateFromFile(): ChargingStationTemplate | undefined {
+ let template: ChargingStationTemplate;
try {
if (this.sharedLRUCache.hasChargingStationTemplate(this.stationInfo?.templateHash)) {
template = this.sharedLRUCache.getChargingStationTemplate(this.stationInfo.templateHash);
}
private getStationInfoFromTemplate(): ChargingStationInfo {
- const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile();
+ const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile();
if (Utils.isNullOrUndefined(stationTemplate)) {
const errorMsg = `Failed to read charging station template file ${this.templateFile}`;
logger.error(`${this.logPrefix()} ${errorMsg}`);
this.index,
stationTemplate
);
- stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16;
+ stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16;
ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo);
- if (!Utils.isEmptyArray(stationTemplate.power)) {
- stationTemplate.power = stationTemplate.power as number[];
+ if (!Utils.isEmptyArray(stationTemplate?.power)) {
+ stationTemplate.power = stationTemplate?.power as number[];
const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length);
stationInfo.maximumPower =
- stationTemplate.powerUnit === PowerUnits.KILO_WATT
+ stationTemplate?.powerUnit === PowerUnits.KILO_WATT
? stationTemplate.power[powerArrayRandomIndex] * 1000
: stationTemplate.power[powerArrayRandomIndex];
} else {
stationTemplate.power = stationTemplate.power as number;
stationInfo.maximumPower =
- stationTemplate.powerUnit === PowerUnits.KILO_WATT
+ stationTemplate?.powerUnit === PowerUnits.KILO_WATT
? stationTemplate.power * 1000
: stationTemplate.power;
}
stationInfo.firmwareVersionPattern =
- stationTemplate.firmwareVersionPattern ?? Constants.SEMVER_PATTERN;
+ stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN;
if (
stationInfo.firmwareVersion &&
new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion) === false
{
reset: true,
},
- stationTemplate.firmwareUpgrade ?? {}
+ stationTemplate?.firmwareUpgrade ?? {}
);
- stationInfo.resetTime = stationTemplate.resetTime
+ stationInfo.resetTime = stationTemplate?.resetTime
? stationTemplate.resetTime * 1000
: Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
const configuredMaxConnectors =
}
private getStationInfoFromFile(): ChargingStationInfo | null {
- let stationInfo: ChargingStationInfo = null;
+ let stationInfo: ChargingStationInfo | null = null;
this.getStationInfoPersistentConfiguration() &&
(stationInfo = this.getConfigurationFromFile()?.stationInfo ?? null);
stationInfo && ChargingStationUtils.createStationInfoHash(stationInfo);
private getStationInfo(): ChargingStationInfo {
const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate();
- const stationInfoFromFile: ChargingStationInfo = this.getStationInfoFromFile();
+ const stationInfoFromFile: ChargingStationInfo | null = this.getStationInfoFromFile();
// Priority: charging station info from template > charging station info from configuration file > charging station info attribute
if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) {
if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) {
const patternGroup: number =
this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ??
this.stationInfo.firmwareVersion.split('.').length;
- const match = this.stationInfo.firmwareVersion
- .match(new RegExp(this.stationInfo.firmwareVersionPattern))
- .slice(1, patternGroup + 1);
+ const match = this.stationInfo?.firmwareVersion
+ ?.match(new RegExp(this.stationInfo.firmwareVersionPattern))
+ ?.slice(1, patternGroup + 1);
const patchLevelIndex = match.length - 1;
match[patchLevelIndex] = (
Utils.convertToInt(match[patchLevelIndex]) + versionStep
).toString();
- this.stationInfo.firmwareVersion = match.join('.');
+ this.stationInfo.firmwareVersion = match?.join('.');
}
}
ChargingStationConfigurationUtils.getConfigurationKey(
this,
StandardParametersKey.SupportedFeatureProfiles
- )?.value.includes(SupportedFeatureProfiles.LocalAuthListManagement)
+ )?.value?.includes(SupportedFeatureProfiles.LocalAuthListManagement)
) {
ChargingStationConfigurationUtils.addConfigurationKey(
this,
}
// Initialize transaction attributes on connectors
for (const connectorId of this.connectors.keys()) {
- if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionStarted === true) {
+ if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
logger.warn(
`${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
- this.getConnectorStatus(connectorId).transactionId
+ this.getConnectorStatus(connectorId)?.transactionId
}`
);
}
if (
connectorId > 0 &&
- (this.getConnectorStatus(connectorId).transactionStarted === undefined ||
- this.getConnectorStatus(connectorId).transactionStarted === null)
+ (this.getConnectorStatus(connectorId)?.transactionStarted === undefined ||
+ this.getConnectorStatus(connectorId)?.transactionStarted === null)
) {
this.initializeConnectorStatus(connectorId);
}
}
private getConfigurationFromFile(): ChargingStationConfiguration | null {
- let configuration: ChargingStationConfiguration = null;
+ let configuration: ChargingStationConfiguration | null = null;
if (this.configurationFile && fs.existsSync(this.configurationFile)) {
try {
if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
}
private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | null {
- let configuration: ChargingStationConfiguration = null;
+ let configuration: ChargingStationConfiguration | null = null;
if (this.getOcppPersistentConfiguration() === true) {
const configurationFromFile = this.getConfigurationFromFile();
configuration = configurationFromFile?.configurationKey && configurationFromFile;
}
private getOcppConfiguration(): ChargingStationOcppConfiguration | null {
- let ocppConfiguration: ChargingStationOcppConfiguration = this.getOcppConfigurationFromFile();
+ let ocppConfiguration: ChargingStationOcppConfiguration | null =
+ this.getOcppConfigurationFromFile();
if (!ocppConfiguration) {
ocppConfiguration = this.getOcppConfigurationFromTemplate();
}
throw new OCPPError(
ErrorType.INTERNAL_ERROR,
`Response for unknown message id ${messageId}`,
- null,
+ undefined,
commandPayload
);
}
throw new OCPPError(
ErrorType.PROTOCOL_ERROR,
`Cached request for message id ${messageId} response is not an array`,
- null,
+ undefined,
cachedRequest as unknown as JsonType
);
}
throw new OCPPError(
ErrorType.INTERNAL_ERROR,
`Error response for unknown message id ${messageId}`,
- null,
+ undefined,
{ errorType, errorMessage, errorDetails }
);
}
throw new OCPPError(
ErrorType.PROTOCOL_ERROR,
`Cached request for message id ${messageId} error response is not an array`,
- null,
+ undefined,
cachedRequest as unknown as JsonType
);
}
}
// -1 for unlimited, 0 for disabling
- private getAutoReconnectMaxRetries(): number {
+ private getAutoReconnectMaxRetries(): number | undefined {
if (!Utils.isUndefined(this.stationInfo.autoReconnectMaxRetries)) {
return this.stationInfo.autoReconnectMaxRetries;
}
}
// 0 for disabling
- private getRegistrationMaxRetries(): number {
+ private getRegistrationMaxRetries(): number | undefined {
if (!Utils.isUndefined(this.stationInfo.registrationMaxRetries)) {
return this.stationInfo.registrationMaxRetries;
}
ChargingStationConfigurationUtils.getConfigurationKey(
this,
this.stationInfo.amperageLimitationOcppKey
- ).value
+ )?.value
) / ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
);
}
this.startHeartbeat();
// Initialize connectors status
for (const connectorId of this.connectors.keys()) {
- let connectorStatus: ConnectorStatusEnum;
+ let connectorStatus: ConnectorStatusEnum | undefined;
if (connectorId === 0) {
continue;
} else if (
this.getConnectorStatus(connectorId)?.bootStatus
) {
// Set boot status in template at startup
- connectorStatus = this.getConnectorStatus(connectorId).bootStatus;
+ connectorStatus = this.getConnectorStatus(connectorId)?.bootStatus;
} else if (this.getConnectorStatus(connectorId)?.status) {
// Set previous status at startup
- connectorStatus = this.getConnectorStatus(connectorId).status;
+ connectorStatus = this.getConnectorStatus(connectorId)?.status;
} else {
// Set default status
connectorStatus = ConnectorStatusEnum.AVAILABLE;
ConnectorStatusEnum.UNAVAILABLE
)
);
- this.getConnectorStatus(connectorId).status = null;
+ this.getConnectorStatus(connectorId).status = undefined;
}
}
}
ChargingStationConfigurationUtils.getConfigurationKey(
this,
StandardParametersKey.WebSocketPingInterval
- ).value
+ )?.value
)
: 0;
if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
this.webSocketPingSetInterval = setInterval(() => {
if (this.isWebSocketConnectionOpened() === true) {
- this.wsConnection.ping();
+ this.wsConnection?.ping();
}
}, webSocketPingInterval * 1000);
logger.info(
}
private getConfiguredSupervisionUrl(): URL {
- const supervisionUrls = this.stationInfo.supervisionUrls ?? Configuration.getSupervisionUrls();
+ const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls();
if (!Utils.isEmptyArray(supervisionUrls)) {
switch (Configuration.getSupervisionUrlDistribution()) {
case SupervisionUrlDistribution.ROUND_ROBIN:
private terminateWSConnection(): void {
if (this.isWebSocketConnectionOpened() === true) {
- this.wsConnection.terminate();
+ this.wsConnection?.terminate();
this.wsConnection = null;
}
}
private stopMeterValues(connectorId: number) {
if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
- clearInterval(this.getConnectorStatus(connectorId).transactionSetInterval);
+ clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
}
}