import { AsyncResource } from 'node:async_hooks';
+import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns';
+
import type { ChargingStation } from './ChargingStation';
import { checkChargingStation } from './ChargingStationUtils';
import { IdTagsCache } from './IdTagsCache';
await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
} while (!this.chargingStation?.ocppRequestService);
}
- const wait =
+ const wait = secondsToMilliseconds(
getRandomInteger(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
.maxDelayBetweenTwoTransactions,
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
.minDelayBetweenTwoTransactions,
- ) * 1000;
+ ),
+ );
logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`);
await sleep(wait);
const start = secureRandom();
const startResponse = await this.startTransaction(connectorId);
if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
// Wait until end of transaction
- const waitTrxEnd =
+ const waitTrxEnd = secondsToMilliseconds(
getRandomInteger(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration().maxDuration,
this.chargingStation.getAutomaticTransactionGeneratorConfiguration().minDuration,
- ) * 1000;
+ ),
+ );
logger.info(
`${this.logPrefix(connectorId)} transaction started with id ${this.chargingStation
.getConnectorStatus(connectorId)
this.connectorsStatus.get(connectorId)!.startDate = new Date();
this.connectorsStatus.get(connectorId)!.stopDate = new Date(
this.connectorsStatus.get(connectorId)!.startDate!.getTime() +
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours *
- 3600 *
- 1000 -
+ hoursToMilliseconds(
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours,
+ ) -
previousRunDuration,
);
this.connectorsStatus.get(connectorId)!.start = true;
import { URL } from 'node:url';
import { parentPort } from 'node:worker_threads';
+import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns';
import merge from 'just-merge';
import { type RawData, WebSocket } from 'ws';
public getHeartbeatInterval(): number {
const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval);
if (HeartbeatInterval) {
- return convertToInt(HeartbeatInterval.value) * 1000;
+ return secondsToMilliseconds(convertToInt(HeartbeatInterval.value));
}
const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval);
if (HeartBeatInterval) {
- return convertToInt(HeartBeatInterval.value) * 1000;
+ return secondsToMilliseconds(convertToInt(HeartBeatInterval.value));
}
this.stationInfo?.autoRegister === false &&
logger.warn(
terminateOpened: false,
},
): void {
- options = { handshakeTimeout: this.getConnectionTimeout() * 1000, ...options };
+ options = { handshakeTimeout: secondsToMilliseconds(this.getConnectionTimeout()), ...options };
params = { ...{ closeOpened: false, terminateOpened: false }, ...params };
if (this.started === false && this.starting === false) {
logger.warn(
stationTemplate?.firmwareUpgrade ?? {},
);
stationInfo.resetTime = !isNullOrUndefined(stationTemplate?.resetTime)
- ? stationTemplate.resetTime! * 1000
+ ? secondsToMilliseconds(stationTemplate.resetTime!)
: Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
return stationInfo;
if (this.stationInfo?.autoRegister === true) {
this.bootNotificationResponse = {
currentTime: new Date(),
- interval: this.getHeartbeatInterval() / 1000,
+ interval: millisecondsToSeconds(this.getHeartbeatInterval()),
status: RegistrationStatusEnumType.ACCEPTED,
};
}
this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount;
await sleep(
this?.bootNotificationResponse?.interval
- ? this.bootNotificationResponse.interval * 1000
+ ? secondsToMilliseconds(this.bootNotificationResponse.interval)
: Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL,
);
}
if (this.isWebSocketConnectionOpened() === true) {
this.wsConnection?.ping();
}
- }, webSocketPingInterval * 1000);
+ }, secondsToMilliseconds(webSocketPingInterval));
logger.info(
`${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
webSocketPingInterval,
++this.autoReconnectRetryCount;
const reconnectDelay = this.getReconnectExponentialDelay()
? exponentialDelay(this.autoReconnectRetryCount)
- : this.getConnectionTimeout() * 1000;
+ : secondsToMilliseconds(this.getConnectionTimeout());
const reconnectDelayWithdraw = 1000;
const reconnectTimeout =
reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0
+import { secondsToMilliseconds } from 'date-fns';
+
import { WorkerBroadcastChannel } from './WorkerBroadcastChannel';
import { BaseError, type OCPPError } from '../../exception';
import {
this.chargingStation.getConnectorStatus(requestPayload!.connectorId!)!
.transactionId!,
configuredMeterValueSampleInterval
- ? convertToInt(configuredMeterValueSampleInterval.value) * 1000
+ ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value))
: Constants.DEFAULT_METER_VALUES_INTERVAL,
),
],
import type { JSONSchemaType } from 'ajv';
import { Client, type FTPResponse } from 'basic-ftp';
+import { secondsToMilliseconds } from 'date-fns';
import { create } from 'tar';
import { OCPP16Constants } from './OCPP16Constants';
return OCPP16Constants.OCPP_RESPONSE_REJECTED;
}
const startDate = new Date();
- const endDate = new Date(startDate.getTime() + commandPayload.duration * 1000);
+ const endDate = new Date(startDate.getTime() + secondsToMilliseconds(commandPayload.duration));
let compositeSchedule: OCPP16ChargingSchedule | undefined;
for (const chargingProfile of chargingStation.getConnectorStatus(commandPayload.connectorId)!
.chargingProfiles!) {
chargingStation.stationInfo?.firmwareUpgrade?.failureStatus ===
OCPP16FirmwareStatus.DownloadFailed
) {
- await sleep(getRandomInteger(maxDelay, minDelay) * 1000);
+ await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)));
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
chargingStation.stationInfo?.firmwareUpgrade?.failureStatus;
return;
}
- await sleep(getRandomInteger(maxDelay, minDelay) * 1000);
+ await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)));
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
do {
const runningTransactions = chargingStation.getNumberOfRunningTransactions();
if (runningTransactions > 0) {
- const waitTime = 15 * 1000;
+ const waitTime = secondsToMilliseconds(15);
logger.debug(
`${chargingStation.logPrefix()} ${moduleName}.updateFirmwareSimulation:
- ${runningTransactions} transaction(s) in progress, waiting ${
- waitTime / 1000
- } seconds before continuing firmware update simulation`,
+ ${runningTransactions} transaction(s) in progress, waiting ${formatDurationMilliSeconds(
+ waitTime,
+ )} before continuing firmware update simulation`,
);
await sleep(waitTime);
transactionsStarted = true;
transactionsStarted = false;
}
} while (transactionsStarted);
- !wasTransactionsStarted && (await sleep(getRandomInteger(maxDelay, minDelay) * 1000));
+ !wasTransactionsStarted &&
+ (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))));
if (checkChargingStation(chargingStation, chargingStation.logPrefix()) === false) {
return;
}
chargingStation.stationInfo?.firmwareUpgrade?.failureStatus ===
OCPP16FirmwareStatus.InstallationFailed
) {
- await sleep(getRandomInteger(maxDelay, minDelay) * 1000);
+ await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)));
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
return;
}
if (chargingStation.stationInfo?.firmwareUpgrade?.reset === true) {
- await sleep(getRandomInteger(maxDelay, minDelay) * 1000);
+ await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)));
await chargingStation.reset(OCPP16StopTransactionReason.REBOOT);
}
}
import { parentPort } from 'node:worker_threads';
import type { JSONSchemaType } from 'ajv';
+import { secondsToMilliseconds } from 'date-fns';
import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
import {
chargingStation.startMeterValues(
transactionConnectorId,
configuredMeterValueSampleInterval
- ? convertToInt(configuredMeterValueSampleInterval.value) * 1000
+ ? secondsToMilliseconds(convertToInt(configuredMeterValueSampleInterval.value))
: Constants.DEFAULT_METER_VALUES_INTERVAL,
);
} else {
import type { URL } from 'node:url';
import { parentPort } from 'node:worker_threads';
+import { secondsToMilliseconds } from 'date-fns';
+
import {
ConfigurationSection,
type IncomingRequestCommand,
if (logStatisticsInterval > 0 && !this.displayInterval) {
this.displayInterval = setInterval(() => {
this.logStatistics();
- }, logStatisticsInterval * 1000);
+ }, secondsToMilliseconds(logStatisticsInterval));
logger.info(
`${this.logPrefix()} logged every ${formatDurationSeconds(logStatisticsInterval)}`,
);
import {
formatDuration,
+ hoursToMinutes,
+ hoursToSeconds,
isDate,
millisecondsToHours,
millisecondsToMinutes,
millisecondsToSeconds,
+ minutesToSeconds,
secondsToMilliseconds,
} from 'date-fns';
import clone from 'just-clone';
duration = convertToInt(duration);
const days = Math.floor(duration / (24 * 3600 * 1000));
const hours = Math.floor(millisecondsToHours(duration) - days * 24);
- const minutes = Math.floor(millisecondsToMinutes(duration) - days * 24 * 60 - hours * 60);
+ const minutes = Math.floor(
+ millisecondsToMinutes(duration) - days * 24 * 60 - hoursToMinutes(hours),
+ );
const seconds = Math.floor(
- millisecondsToSeconds(duration) - days * 24 * 3600 - hours * 3600 - minutes * 60,
+ millisecondsToSeconds(duration) -
+ days * 24 * 3600 -
+ hoursToSeconds(hours) -
+ minutesToSeconds(minutes),
);
return formatDuration({
days,
+import { hoursToMilliseconds } from 'date-fns';
import { expect } from 'expect';
import { Constants } from '../../src/utils/Constants';
convertToDate,
convertToFloat,
convertToInt,
+ formatDurationMilliSeconds,
generateUUID,
getRandomFloat,
getRandomInteger,
expect(end - start).toBeGreaterThanOrEqual(1000);
});
+ it('Verify formatDurationMilliSeconds()', () => {
+ expect(formatDurationMilliSeconds(0)).toBe('');
+ expect(formatDurationMilliSeconds(1000)).toBe('1 second');
+ expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours');
+ });
+
it('Verify isValidDate()', () => {
expect(isValidDate(undefined)).toBe(false);
expect(isValidDate(null)).toBe(false);