@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
152 lines • 6.84 kB
JavaScript
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
import { ChargingLimitSourceEnum, ChargingProfileKindEnum, ChargingProfilePurposeEnum, ChargingRateUnitEnum, OCPP2_0_1, RecurrencyKindEnum, } from '@citrineos/base';
export class ChargingProfileMapper {
// =========================================================================
// Enum converters: Native → OCPP 2.0.1
// Note: Native enum values are identical to OCPP 2.0.1 enum values,
// so these are type-safe casts rather than value transformations.
// =========================================================================
static toChargingProfileKindEnumType(kind) {
return kind;
}
static fromChargingProfileKindEnumType(kind) {
return kind;
}
static toChargingProfilePurposeEnumType(purpose) {
return purpose;
}
static fromChargingProfilePurposeEnumType(purpose) {
return purpose;
}
static toRecurrencyKindEnumType(kind) {
if (!kind)
return undefined;
return kind;
}
static fromRecurrencyKindEnumType(kind) {
if (!kind)
return undefined;
return kind;
}
static toChargingRateUnitEnumType(unit) {
return unit;
}
static fromChargingRateUnitEnumType(unit) {
return unit;
}
static toChargingLimitSourceEnumType(source) {
if (!source)
return undefined;
return source;
}
static fromChargingLimitSourceEnumType(source) {
if (!source)
return undefined;
return source;
}
// =========================================================================
// Object converters: OCPP 2.0.1 → Native
// =========================================================================
/**
* Converts OCPP2_0_1.ChargingProfileType to a native ChargingProfileInput.
*/
static fromChargingProfileType(chargingProfile) {
return {
id: chargingProfile.id,
stackLevel: chargingProfile.stackLevel,
chargingProfilePurpose: ChargingProfileMapper.fromChargingProfilePurposeEnumType(chargingProfile.chargingProfilePurpose),
chargingProfileKind: ChargingProfileMapper.fromChargingProfileKindEnumType(chargingProfile.chargingProfileKind),
recurrencyKind: ChargingProfileMapper.fromRecurrencyKindEnumType(chargingProfile.recurrencyKind),
validFrom: chargingProfile.validFrom,
validTo: chargingProfile.validTo,
chargingSchedule: chargingProfile.chargingSchedule.map((schedule) => ChargingProfileMapper.fromChargingScheduleType(schedule)),
transactionId: chargingProfile.transactionId,
};
}
/**
* Converts OCPP2_0_1.ChargingScheduleType to a native ChargingScheduleInput.
*/
static fromChargingScheduleType(schedule) {
return {
id: schedule.id,
startSchedule: schedule.startSchedule,
duration: schedule.duration,
chargingRateUnit: ChargingProfileMapper.fromChargingRateUnitEnumType(schedule.chargingRateUnit),
chargingSchedulePeriod: schedule.chargingSchedulePeriod.map((period) => ({
startPeriod: period.startPeriod,
limit: period.limit,
numberPhases: period.numberPhases,
phaseToUse: period.phaseToUse,
})),
minChargingRate: schedule.minChargingRate,
salesTariff: schedule.salesTariff
? {
id: schedule.salesTariff.id,
salesTariffDescription: schedule.salesTariff.salesTariffDescription,
numEPriceLevels: schedule.salesTariff.numEPriceLevels,
salesTariffEntry: schedule.salesTariff.salesTariffEntry,
}
: undefined,
};
}
/**
* Converts OCPP2_0_1.CompositeScheduleType to a native CompositeScheduleInput.
*/
static fromCompositeScheduleType(compositeSchedule) {
return {
chargingSchedulePeriod: compositeSchedule.chargingSchedulePeriod.map((period) => ({
startPeriod: period.startPeriod,
limit: period.limit,
numberPhases: period.numberPhases,
phaseToUse: period.phaseToUse,
})),
evseId: compositeSchedule.evseId,
duration: compositeSchedule.duration,
scheduleStart: compositeSchedule.scheduleStart,
chargingRateUnit: ChargingProfileMapper.fromChargingRateUnitEnumType(compositeSchedule.chargingRateUnit),
};
}
// =========================================================================
// Object converters: Native → OCPP 2.0.1
// =========================================================================
/**
* Converts a native ChargingProfile (Sequelize model) to OCPP2_0_1.ChargingProfileType.
*/
static toChargingProfileType(chargingProfile, transactionId) {
return {
id: chargingProfile.id,
stackLevel: chargingProfile.stackLevel,
chargingProfilePurpose: ChargingProfileMapper.toChargingProfilePurposeEnumType(chargingProfile.chargingProfilePurpose),
chargingProfileKind: ChargingProfileMapper.toChargingProfileKindEnumType(chargingProfile.chargingProfileKind),
recurrencyKind: ChargingProfileMapper.toRecurrencyKindEnumType(chargingProfile.recurrencyKind),
validFrom: chargingProfile.validFrom,
validTo: chargingProfile.validTo,
chargingSchedule: chargingProfile.chargingSchedule.map((schedule) => ChargingProfileMapper.toChargingScheduleType(schedule)),
transactionId: transactionId,
};
}
/**
* Converts a native ChargingScheduleDto to OCPP2_0_1.ChargingScheduleType.
*/
static toChargingScheduleType(schedule) {
return {
id: schedule.id,
startSchedule: schedule.startSchedule,
duration: schedule.duration,
chargingRateUnit: ChargingProfileMapper.toChargingRateUnitEnumType(schedule.chargingRateUnit),
chargingSchedulePeriod: schedule.chargingSchedulePeriod,
minChargingRate: schedule.minChargingRate,
salesTariff: schedule.salesTariff
? {
id: schedule.salesTariff.id,
salesTariffDescription: schedule.salesTariff.salesTariffDescription,
numEPriceLevels: schedule.salesTariff.numEPriceLevels,
salesTariffEntry: schedule.salesTariff.salesTariffEntry,
}
: undefined,
};
}
}
//# sourceMappingURL=ChargingProfileMapper.js.map