import {
buildConnectorsMap,
checkChargingStation,
+ checkConfiguration,
checkConnectorsConfiguration,
checkStationInfoConnectorStatus,
checkTemplate,
getMaxNumberOfEvses,
getNumberOfReservableConnectors,
getPhaseRotationValue,
- hasFeatureProfile,
hasReservationExpired,
initializeConnectorsMapStatus,
propagateSerialNumber,
- removeExpiredReservations,
stationTemplateToStationInfo,
warnTemplateKeysDeprecation,
} from './Helpers';
OCPP16IncomingRequestService,
OCPP16RequestService,
OCPP16ResponseService,
- OCPP16ServiceUtils,
OCPP20IncomingRequestService,
OCPP20RequestService,
OCPP20ResponseService,
type OCPPIncomingRequestService,
type OCPPRequestService,
- OCPPServiceUtils,
+ buildMeterValue,
+ buildStatusNotificationRequest,
+ buildTransactionEndMeterValue,
+ getMessageTypeString,
+ sendAndSetConnectorStatus,
} from './ocpp';
import { SharedLRUCache } from './SharedLRUCache';
import { BaseError, OCPPError } from '../exception';
type IncomingRequestCommand,
type JsonType,
MessageType,
- type MeterValue,
MeterValueMeasurand,
type MeterValuesRequest,
type MeterValuesResponse,
private readonly sharedLRUCache: SharedLRUCache;
private webSocketPingSetInterval?: NodeJS.Timeout;
private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
- private reservationExpirationSetInterval?: NodeJS.Timeout;
+ private flushMessageBufferSetInterval?: NodeJS.Timeout;
constructor(index: number, templateFile: string) {
super();
}
public logPrefix = (): string => {
- return logPrefix(
- ` ${
- (isNotEmptyString(this?.stationInfo?.chargingStationId)
- ? this?.stationInfo?.chargingStationId
- : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
- 'Error at building log prefix'
- } |`,
- );
+ if (isNotEmptyString(this?.stationInfo?.chargingStationId)) {
+ return logPrefix(` ${this?.stationInfo?.chargingStationId} |`);
+ }
+ let stationTemplate: ChargingStationTemplate | undefined;
+ try {
+ stationTemplate = JSON.parse(
+ readFileSync(this.templateFile, 'utf8'),
+ ) as ChargingStationTemplate;
+ } catch {
+ stationTemplate = undefined;
+ }
+ return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`);
};
public hasIdTags(): boolean {
return this?.wsConnection?.readyState === WebSocket.OPEN;
}
- public getRegistrationStatus(): RegistrationStatusEnumType | undefined {
- return this?.bootNotificationResponse?.status;
- }
-
public inUnknownState(): boolean {
return isNullOrUndefined(this?.bootNotificationResponse?.status);
}
}
public startHeartbeat(): void {
- if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval) {
+ if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval === undefined) {
this.heartbeatSetInterval = setInterval(() => {
this.ocppRequestService
.requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
this.getHeartbeatInterval(),
)}`,
);
- } else if (this.heartbeatSetInterval) {
+ } else if (this.heartbeatSetInterval !== undefined) {
logger.info(
`${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
this.getHeartbeatInterval(),
}
if (interval > 0) {
this.getConnectorStatus(connectorId)!.transactionSetInterval = setInterval(() => {
- // FIXME: Implement OCPP version agnostic helpers
- const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
+ const meterValue = buildMeterValue(
this,
connectorId,
this.getConnectorStatus(connectorId)!.transactionId!,
}
public stopMeterValues(connectorId: number) {
- if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
+ if (this.getConnectorStatus(connectorId)?.transactionSetInterval !== undefined) {
clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
}
}
if (this.stationInfo?.enableStatistics === true) {
this.performanceStatistics?.start();
}
- if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
- this.startReservationExpirationSetInterval();
- }
this.openWSConnection();
// Monitor charging station template file
this.templateFileWatcher = watchJsonFile(
if (this.stationInfo?.enableStatistics === true) {
this.performanceStatistics?.stop();
}
- if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
- this.stopReservationExpirationSetInterval();
- }
this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
this.templateFileWatcher?.close();
this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
public bufferMessage(message: string): void {
this.messageBuffer.add(message);
+ this.setIntervalFlushMessageBuffer();
}
public openWSConnection(
this.stationInfo?.ocppStrictCompliance === true &&
this.stationInfo?.outOfOrderEndMeterValues === false
) {
- // FIXME: Implement OCPP version agnostic helpers
- const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+ const transactionEndMeterValue = buildTransactionEndMeterValue(
this,
connectorId,
this.getEnergyActiveImportRegisterByTransactionId(transactionId!),
await this.removeReservation(reservationFound, ReservationTerminationReason.REPLACE_EXISTING);
}
this.getConnectorStatus(reservation.connectorId)!.reservation = reservation;
- await OCPPServiceUtils.sendAndSetConnectorStatus(
+ await sendAndSetConnectorStatus(
this,
reservation.connectorId,
ConnectorStatusEnum.Reserved,
case ReservationTerminationReason.RESERVATION_CANCELED:
case ReservationTerminationReason.REPLACE_EXISTING:
case ReservationTerminationReason.EXPIRED:
- await OCPPServiceUtils.sendAndSetConnectorStatus(
+ await sendAndSetConnectorStatus(
this,
reservation.connectorId,
ConnectorStatusEnum.Available,
return false;
}
- private startReservationExpirationSetInterval(customInterval?: number): void {
- const interval = customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_INTERVAL;
- if (interval > 0) {
- logger.info(
- `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds(
- interval,
- )}`,
- );
- this.reservationExpirationSetInterval = setInterval((): void => {
- removeExpiredReservations(this).catch(Constants.EMPTY_FUNCTION);
- }, interval);
+ private setIntervalFlushMessageBuffer(): void {
+ if (this.flushMessageBufferSetInterval === undefined) {
+ this.flushMessageBufferSetInterval = setInterval(() => {
+ if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
+ this.flushMessageBuffer();
+ }
+ if (this.messageBuffer.size === 0) {
+ this.clearIntervalFlushMessageBuffer();
+ }
+ }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL);
}
}
- private stopReservationExpirationSetInterval(): void {
- if (!isNullOrUndefined(this.reservationExpirationSetInterval)) {
- clearInterval(this.reservationExpirationSetInterval);
+ private clearIntervalFlushMessageBuffer() {
+ if (this.flushMessageBufferSetInterval !== undefined) {
+ clearInterval(this.flushMessageBufferSetInterval);
+ delete this.flushMessageBufferSetInterval;
}
}
- // private restartReservationExpiryDateSetInterval(): void {
- // this.stopReservationExpirationSetInterval();
- // this.startReservationExpirationSetInterval();
- // }
-
private getNumberOfReservableConnectors(): number {
let numberOfReservableConnectors = 0;
if (this.hasEvses) {
isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!);
if (isNullOrUndefined(error)) {
logger.debug(
- `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
+ `${this.logPrefix()} >> Buffered ${getMessageTypeString(
messageType,
- )} payload sent: ${message}`,
+ )} OCPP message sent '${JSON.stringify(message)}'`,
);
this.messageBuffer.delete(message);
+ } else {
+ logger.debug(
+ `${this.logPrefix()} >> Buffered ${getMessageTypeString(
+ messageType,
+ )} OCPP message '${JSON.stringify(message)}' send failed:`,
+ error,
+ );
}
});
}
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(stationConfiguration?.connectorsStatus || stationConfiguration?.evsesStatus)
) {
+ checkConfiguration(stationConfiguration, this.logPrefix(), this.configurationFile);
this.initializeConnectorsOrEvsesFromFile(stationConfiguration);
} else {
this.initializeConnectorsOrEvsesFromTemplate(stationTemplate);
}
if (
this.stationInfo?.ocppPersistentConfiguration === true &&
- this.ocppConfiguration?.configurationKey
+ Array.isArray(this.ocppConfiguration?.configurationKey)
) {
- configurationData.configurationKey = this.ocppConfiguration.configurationKey;
+ configurationData.configurationKey = this.ocppConfiguration?.configurationKey;
} else {
delete configurationData.configurationKey;
}
}
throw new OCPPError(
ErrorType.PROTOCOL_ERROR,
- `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
+ `Cached request for message id ${messageId} ${getMessageTypeString(
messageType,
)} is not an array`,
undefined,
if (evseId > 0) {
for (const [connectorId, connectorStatus] of evseStatus.connectors) {
const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus);
- await OCPPServiceUtils.sendAndSetConnectorStatus(
- this,
- connectorId,
- connectorBootStatus,
- evseId,
- );
+ await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus, evseId);
}
}
}
connectorId,
this.getConnectorStatus(connectorId)!,
);
- await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorBootStatus);
+ await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus);
}
}
}
>(
this,
RequestCommand.STATUS_NOTIFICATION,
- OCPPServiceUtils.buildStatusNotificationRequest(
+ buildStatusNotificationRequest(
this,
connectorId,
ConnectorStatusEnum.Unavailable,
>(
this,
RequestCommand.STATUS_NOTIFICATION,
- OCPPServiceUtils.buildStatusNotificationRequest(
- this,
- connectorId,
- ConnectorStatusEnum.Unavailable,
- ),
+ buildStatusNotificationRequest(this, connectorId, ConnectorStatusEnum.Unavailable),
);
delete this.getConnectorStatus(connectorId)?.status;
}
getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value,
)
: 0;
- if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
+ if (webSocketPingInterval > 0 && this.webSocketPingSetInterval === undefined) {
this.webSocketPingSetInterval = setInterval(() => {
if (this.isWebSocketConnectionOpened() === true) {
this.wsConnection?.ping();
webSocketPingInterval,
)}`,
);
- } else if (this.webSocketPingSetInterval) {
+ } else if (this.webSocketPingSetInterval !== undefined) {
logger.info(
`${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
webSocketPingInterval,
}
private stopWebSocketPing(): void {
- if (this.webSocketPingSetInterval) {
+ if (this.webSocketPingSetInterval !== undefined) {
clearInterval(this.webSocketPingSetInterval);
delete this.webSocketPingSetInterval;
}
}
private stopHeartbeat(): void {
- if (this.heartbeatSetInterval) {
+ if (this.heartbeatSetInterval !== undefined) {
clearInterval(this.heartbeatSetInterval);
delete this.heartbeatSetInterval;
}