logger,
secureRandom,
} from '../utils';
+import { isValidDate } from '../utils/Utils';
const moduleName = 'ChargingStationUtils';
): ChargingProfilesLimit | undefined => {
const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
const currentDate = new Date();
+ const connectorStatus = chargingStation.getConnectorStatus(connectorId);
for (const chargingProfile of chargingProfiles) {
if (
chargingProfile.validFrom &&
continue;
}
const chargingSchedule = chargingProfile.chargingSchedule;
- if (!chargingSchedule?.startSchedule) {
+ if (connectorStatus?.transactionStarted && !chargingSchedule?.startSchedule) {
logger.debug(
`${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}. Trying to set it to the connector transaction start date`,
);
// OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
- chargingSchedule.startSchedule =
- chargingStation.getConnectorStatus(connectorId)?.transactionStart;
+ chargingSchedule.startSchedule = connectorStatus?.transactionStart;
}
if (!(chargingSchedule?.startSchedule instanceof Date)) {
logger.warn(
}
break;
}
- } else if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE) {
- chargingSchedule.startSchedule =
- chargingStation.getConnectorStatus(connectorId)?.transactionStart;
+ } else if (
+ chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE &&
+ connectorStatus?.transactionStarted
+ ) {
+ chargingSchedule.startSchedule = connectorStatus?.transactionStart;
}
// Check if the charging profile is active
if (
+ isValidDate(chargingSchedule.startSchedule) &&
isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate)
) {
if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
- // Handling of only one period
+ // Handling of only one schedule period
if (
chargingSchedule.chargingSchedulePeriod.length === 1 &&
chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0
currentDate,
)
) {
- // Found the schedule: last but one is the correct one
+ // Found the schedule period: last but one is the correct one
const result: ChargingProfilesLimit = {
limit: lastButOneSchedule!.limit,
matchingChargingProfile: chargingProfile,
import {
formatDuration,
+ isDate,
millisecondsToHours,
millisecondsToMinutes,
millisecondsToSeconds,
return formatDurationMilliSeconds(secondsToMilliseconds(duration));
};
+// More efficient date validation function than the one provided by date-fns
+export const isValidDate = (date: unknown): boolean => {
+ if (typeof date === 'number') {
+ return !isNaN(date);
+ } else if (isDate(date)) {
+ return !isNaN((date as Date).getTime());
+ }
+ return false;
+};
+
export const convertToDate = (
value: Date | string | number | null | undefined,
): Date | null | undefined => {