@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
145 lines • 8.83 kB
JavaScript
;
// Copyright (c) 2023 S44, LLC
// Copyright Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache 2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequelizeChargingProfileRepository = void 0;
const base_1 = require("@citrineos/base");
const Base_1 = require("./Base");
const DeviceModel_1 = require("../model/DeviceModel");
const ChargingProfile_1 = require("../model/ChargingProfile");
const TransactionEvent_1 = require("../model/TransactionEvent");
class SequelizeChargingProfileRepository extends Base_1.SequelizeRepository {
constructor(config, logger, sequelizeInstance, chargingNeeds, chargingSchedule, salesTariff, transaction, evse, compositeSchedule) {
super(config, ChargingProfile_1.ChargingProfile.MODEL_NAME, logger, sequelizeInstance);
this.chargingNeeds = chargingNeeds
? chargingNeeds
: new Base_1.SequelizeRepository(config, ChargingProfile_1.ChargingNeeds.MODEL_NAME, logger, sequelizeInstance);
this.chargingSchedule = chargingSchedule
? chargingSchedule
: new Base_1.SequelizeRepository(config, ChargingProfile_1.ChargingSchedule.MODEL_NAME, logger, sequelizeInstance);
this.evse = evse
? evse
: new Base_1.SequelizeRepository(config, DeviceModel_1.Evse.MODEL_NAME, logger, sequelizeInstance);
this.salesTariff = salesTariff
? salesTariff
: new Base_1.SequelizeRepository(config, ChargingProfile_1.SalesTariff.MODEL_NAME, logger, sequelizeInstance);
this.transaction = transaction
? transaction
: new Base_1.SequelizeRepository(config, TransactionEvent_1.Transaction.MODEL_NAME, logger, sequelizeInstance);
this.compositeSchedule = compositeSchedule
? compositeSchedule
: new Base_1.SequelizeRepository(config, ChargingProfile_1.CompositeSchedule.MODEL_NAME, logger, sequelizeInstance);
}
createOrUpdateChargingProfile(tenantId, chargingProfile, stationId, evseId, chargingLimitSource, isActive) {
return __awaiter(this, void 0, void 0, function* () {
let transactionDBId;
if (chargingProfile.transactionId) {
const activeTransaction = yield TransactionEvent_1.Transaction.findOne({
where: {
stationId,
transactionId: chargingProfile.transactionId,
},
});
transactionDBId = activeTransaction === null || activeTransaction === void 0 ? void 0 : activeTransaction.id;
}
const [savedChargingProfile, profileCreated] = yield this.readOrCreateByQuery(tenantId, {
where: {
tenantId: tenantId,
stationId: stationId,
id: chargingProfile.id,
},
defaults: Object.assign(Object.assign({}, chargingProfile), { evseId: evseId, transactionDatabaseId: transactionDBId, chargingLimitSource: chargingLimitSource !== null && chargingLimitSource !== void 0 ? chargingLimitSource : base_1.OCPP2_0_1.ChargingLimitSourceEnumType.CSO, isActive: isActive === undefined ? false : isActive }),
});
if (!profileCreated) {
yield this.updateByKey(tenantId, Object.assign(Object.assign({}, chargingProfile), { chargingSchedule: chargingProfile.chargingSchedule.map((s) => (Object.assign({}, s))), stationId: stationId, transactionDatabaseId: transactionDBId, evseId: evseId, chargingLimitSource: chargingLimitSource !== null && chargingLimitSource !== void 0 ? chargingLimitSource : base_1.OCPP2_0_1.ChargingLimitSourceEnumType.CSO, isActive: isActive === undefined ? false : isActive }), savedChargingProfile.databaseId.toString());
// delete existed charging schedules and sales tariff
const deletedChargingSchedules = yield this.chargingSchedule.deleteAllByQuery(tenantId, {
where: {
chargingProfileDatabaseId: savedChargingProfile.databaseId,
},
});
for (const deletedSchedule of deletedChargingSchedules) {
yield this.salesTariff.deleteAllByQuery(tenantId, {
where: {
chargingScheduleDatabaseId: deletedSchedule.databaseId,
},
});
}
}
for (const chargingSchedule of chargingProfile.chargingSchedule) {
const savedChargingSchedule = yield this.chargingSchedule.create(tenantId, ChargingProfile_1.ChargingSchedule.build(Object.assign({ tenantId,
stationId, chargingProfileDatabaseId: savedChargingProfile.databaseId }, chargingSchedule)));
if (chargingSchedule.salesTariff) {
yield this.salesTariff.create(tenantId, ChargingProfile_1.SalesTariff.build(Object.assign({ tenantId, chargingScheduleDatabaseId: savedChargingSchedule.databaseId }, chargingSchedule.salesTariff)));
}
}
return savedChargingProfile;
});
}
createChargingNeeds(tenantId, chargingNeedsReq, stationId) {
return __awaiter(this, void 0, void 0, function* () {
const activeTransaction = yield TransactionEvent_1.Transaction.findOne({
where: {
stationId,
isActive: true,
},
include: [{ model: DeviceModel_1.Evse, where: { id: chargingNeedsReq.evseId }, required: true }],
});
if (!activeTransaction) {
throw new Error(`No active transaction found on station ${stationId} evse ${chargingNeedsReq.evseId}`);
}
return yield this.chargingNeeds.create(tenantId, ChargingProfile_1.ChargingNeeds.build(Object.assign(Object.assign({ tenantId }, chargingNeedsReq.chargingNeeds), { evseDatabaseId: activeTransaction.evseDatabaseId, transactionDatabaseId: activeTransaction.id, maxScheduleTuples: chargingNeedsReq.maxScheduleTuples })));
});
}
findChargingNeedsByEvseDBIdAndTransactionDBId(tenantId, evseDBId, transactionDataBaseId) {
return __awaiter(this, void 0, void 0, function* () {
const chargingNeedsArray = yield this.chargingNeeds.readAllByQuery(tenantId, {
where: {
evseDatabaseId: evseDBId,
transactionDatabaseId: transactionDataBaseId,
},
order: [['createdAt', 'DESC']],
});
return chargingNeedsArray.length > 0 ? chargingNeedsArray[0] : undefined;
});
}
createCompositeSchedule(tenantId, compositeSchedule, stationId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.compositeSchedule.create(tenantId, ChargingProfile_1.CompositeSchedule.build(Object.assign(Object.assign({ tenantId }, compositeSchedule), { stationId })));
});
}
getNextChargingScheduleId(tenantId, stationId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.chargingSchedule.readNextValue(tenantId, 'id', { where: { stationId } });
});
}
getNextChargingProfileId(tenantId, stationId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.readNextValue(tenantId, 'id', { where: { stationId } });
});
}
getNextStackLevel(tenantId, stationId, transactionDatabaseId, profilePurpose) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.readNextValue(tenantId, 'stackLevel', {
where: {
stationId,
transactionDatabaseId: transactionDatabaseId,
chargingProfilePurpose: profilePurpose,
},
}, 0);
});
}
}
exports.SequelizeChargingProfileRepository = SequelizeChargingProfileRepository;
//# sourceMappingURL=ChargingProfile.js.map