@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
240 lines • 12 kB
JavaScript
"use strict";
// 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.SequelizeLocationRepository = void 0;
const __1 = require("..");
const sequelize_1 = require("sequelize");
const LatestStatusNotification_1 = require("../model/Location/LatestStatusNotification");
class SequelizeLocationRepository extends __1.SequelizeRepository {
constructor(config, logger, sequelizeInstance, chargingStation, statusNotification, latestStatusNotification, connector) {
super(config, __1.Location.MODEL_NAME, logger, sequelizeInstance);
this.chargingStation = chargingStation
? chargingStation
: new __1.SequelizeRepository(config, __1.ChargingStation.MODEL_NAME, logger, sequelizeInstance);
this.statusNotification = statusNotification
? statusNotification
: new __1.SequelizeRepository(config, __1.StatusNotification.MODEL_NAME, logger, sequelizeInstance);
this.latestStatusNotification = latestStatusNotification
? latestStatusNotification
: new __1.SequelizeRepository(config, LatestStatusNotification_1.LatestStatusNotification.MODEL_NAME, logger, sequelizeInstance);
this.connector = connector
? connector
: new __1.SequelizeRepository(config, __1.Connector.MODEL_NAME, logger, sequelizeInstance);
}
readLocationById(tenantId, id) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.readOnlyOneByQuery(tenantId, {
where: { id },
include: [__1.ChargingStation],
});
});
}
readChargingStationByStationId(tenantId, stationId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.chargingStation.readByKey(tenantId, stationId);
});
}
setChargingStationIsOnlineAndOCPPVersion(tenantId, stationId, isOnline, ocppVersion) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.chargingStation.updateByKey(tenantId, { isOnline: isOnline, protocol: ocppVersion }, stationId);
});
}
doesChargingStationExistByStationId(tenantId, stationId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.chargingStation.existsByKey(tenantId, stationId);
});
}
addStatusNotificationToChargingStation(tenantId, stationId, statusNotification) {
return __awaiter(this, void 0, void 0, function* () {
const savedStatusNotification = yield this.statusNotification.create(tenantId, statusNotification);
try {
yield this.updateLatestStatusNotification(tenantId, stationId, savedStatusNotification);
}
catch (e) {
this.logger.error(`Failed to update latest status notification with error: ${e.message}`, e);
}
});
}
updateLatestStatusNotification(tenantId, stationId, statusNotification) {
return __awaiter(this, void 0, void 0, function* () {
const evseId = statusNotification.evseId;
const connectorId = statusNotification.connectorId;
const statusNotificationId = statusNotification.id;
// delete operation doesn't support "include" in query
// so we need to find them at first and then delete
const existingLatestStatusNotifications = yield this.latestStatusNotification.readAllByQuery(tenantId, {
where: {
stationId,
},
include: [
{
model: __1.StatusNotification,
where: {
evseId,
connectorId,
},
require: true,
},
],
});
const idsToDelete = existingLatestStatusNotifications.map((l) => l.id);
yield this.latestStatusNotification.deleteAllByQuery(tenantId, {
where: {
stationId,
id: {
[sequelize_1.Op.in]: idsToDelete,
},
},
});
yield this.latestStatusNotification.create(tenantId, LatestStatusNotification_1.LatestStatusNotification.build({
tenantId,
stationId,
statusNotificationId,
}));
});
}
getChargingStationsByIds(tenantId, stationIds) {
return __awaiter(this, void 0, void 0, function* () {
const query = {
where: {
id: {
[sequelize_1.Op.in]: stationIds,
},
},
};
return this.chargingStation.readAllByQuery(tenantId, query);
});
}
createOrUpdateLocationWithChargingStations(tenantId, location) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g;
location.tenantId = tenantId;
let savedLocation;
if (location.id) {
const result = yield this.readOrCreateByQuery(tenantId, {
where: {
tenantId,
id: location.id,
},
defaults: {
name: location.name,
address: location.address,
city: location.city,
postalCode: location.postalCode,
state: location.state,
country: location.country,
coordinates: location.coordinates,
},
});
savedLocation = result[0];
const locationCreated = result[1];
if (!locationCreated) {
const values = {};
values.name = (_a = location.name) !== null && _a !== void 0 ? _a : undefined;
values.address = (_b = location.address) !== null && _b !== void 0 ? _b : undefined;
values.city = (_c = location.city) !== null && _c !== void 0 ? _c : undefined;
values.postalCode = (_d = location.postalCode) !== null && _d !== void 0 ? _d : undefined;
values.state = (_e = location.state) !== null && _e !== void 0 ? _e : undefined;
values.country = (_f = location.country) !== null && _f !== void 0 ? _f : undefined;
values.coordinates = (_g = location.coordinates) !== null && _g !== void 0 ? _g : undefined;
yield this.updateByKey(tenantId, Object.assign({}, values), savedLocation.id);
}
}
else {
savedLocation = yield this.create(tenantId, __1.Location.build(Object.assign({}, location)));
}
if (location.chargingPool && location.chargingPool.length > 0) {
for (const chargingStation of location.chargingPool) {
chargingStation.locationId = savedLocation.id;
yield this.createOrUpdateChargingStation(tenantId, chargingStation);
}
}
return savedLocation.reload({ include: __1.ChargingStation });
});
}
createOrUpdateChargingStation(tenantId, chargingStation) {
return __awaiter(this, void 0, void 0, function* () {
chargingStation.tenantId = tenantId;
if (chargingStation.id) {
const [savedChargingStation, chargingStationCreated] = yield this.chargingStation.readOrCreateByQuery(tenantId, {
where: {
tenantId,
id: chargingStation.id,
},
defaults: {
locationId: chargingStation.locationId,
chargePointVendor: chargingStation.chargePointVendor,
chargePointModel: chargingStation.chargePointModel,
chargePointSerialNumber: chargingStation.chargePointSerialNumber,
chargeBoxSerialNumber: chargingStation.chargeBoxSerialNumber,
firmwareVersion: chargingStation.firmwareVersion,
iccid: chargingStation.iccid,
imsi: chargingStation.imsi,
meterType: chargingStation.meterType,
meterSerialNumber: chargingStation.meterSerialNumber,
},
});
if (!chargingStationCreated) {
yield this.chargingStation.updateByKey(tenantId, {
locationId: chargingStation.locationId,
chargePointVendor: chargingStation.chargePointVendor,
chargePointModel: chargingStation.chargePointModel,
chargePointSerialNumber: chargingStation.chargePointSerialNumber,
chargeBoxSerialNumber: chargingStation.chargeBoxSerialNumber,
firmwareVersion: chargingStation.firmwareVersion,
iccid: chargingStation.iccid,
imsi: chargingStation.imsi,
meterType: chargingStation.meterType,
meterSerialNumber: chargingStation.meterSerialNumber,
}, savedChargingStation.id);
}
return savedChargingStation;
}
else {
return yield this.chargingStation.create(tenantId, __1.ChargingStation.build(Object.assign({}, chargingStation)));
}
});
}
createOrUpdateConnector(tenantId, connector) {
return __awaiter(this, void 0, void 0, function* () {
let result;
yield this.s.transaction((sequelizeTransaction) => __awaiter(this, void 0, void 0, function* () {
const [savedConnector, connectorCreated] = yield this.connector.readOrCreateByQuery(tenantId, {
where: {
tenantId,
stationId: connector.stationId,
connectorId: connector.connectorId,
},
defaults: Object.assign({}, connector),
transaction: sequelizeTransaction,
});
if (!connectorCreated) {
const updatedConnectors = yield this.connector.updateAllByQuery(tenantId, connector, {
where: {
id: savedConnector.id,
},
transaction: sequelizeTransaction,
});
result = updatedConnectors.length > 0 ? updatedConnectors[0] : undefined;
}
else {
result = savedConnector;
}
}));
return result;
});
}
}
exports.SequelizeLocationRepository = SequelizeLocationRepository;
//# sourceMappingURL=Location.js.map