UNPKG

@citrineos/data

Version:

The OCPP data module which includes all persistence layer implementation.

185 lines 10.1 kB
"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.SequelizeVariableMonitoringRepository = void 0; const Base_1 = require("./Base"); const VariableMonitoring_1 = require("../model/VariableMonitoring"); const base_1 = require("@citrineos/base"); const DeviceModel_1 = require("../model/DeviceModel"); class SequelizeVariableMonitoringRepository extends Base_1.SequelizeRepository { constructor(config, logger, sequelizeInstance, eventData, variableMonitoringStatus) { super(config, VariableMonitoring_1.VariableMonitoring.MODEL_NAME, logger, sequelizeInstance); this.eventData = eventData ? eventData : new Base_1.SequelizeRepository(config, VariableMonitoring_1.EventData.MODEL_NAME, logger, sequelizeInstance); this.variableMonitoringStatus = variableMonitoringStatus ? variableMonitoringStatus : new Base_1.SequelizeRepository(config, VariableMonitoring_1.VariableMonitoringStatus.MODEL_NAME, logger, sequelizeInstance); } createOrUpdateByMonitoringDataTypeAndStationId(tenantId, value, componentId, variableId, stationId) { return __awaiter(this, void 0, void 0, function* () { return yield Promise.all(value.variableMonitoring.map((variableMonitoring) => __awaiter(this, void 0, void 0, function* () { const savedVariableMonitoring = yield this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () { const existingVariableMonitoring = yield this.s.models[VariableMonitoring_1.VariableMonitoring.MODEL_NAME].findOne({ where: { stationId, variableId, componentId }, transaction, }); if (!existingVariableMonitoring) { // If the record does not exist, build and save a new instance const vm = VariableMonitoring_1.VariableMonitoring.build(Object.assign({ tenantId, stationId, variableId, componentId }, variableMonitoring)); const createdVariableMonitoring = yield vm.save({ transaction }); this.emit('created', [createdVariableMonitoring]); return createdVariableMonitoring; } else { // If the record exists, update it return (yield this.updateByKey(tenantId, Object.assign({}, variableMonitoring), existingVariableMonitoring.dataValues.databaseId)); } })); yield this.createVariableMonitoringStatus(tenantId, base_1.OCPP2_0_1.SetMonitoringStatusEnumType.Accepted, base_1.OCPP2_0_1_CallAction.NotifyMonitoringReport, savedVariableMonitoring.get('databaseId')); return savedVariableMonitoring; }))); }); } createVariableMonitoringStatus(tenantId, status, action, variableMonitoringId) { return __awaiter(this, void 0, void 0, function* () { yield this.variableMonitoringStatus.create(tenantId, VariableMonitoring_1.VariableMonitoringStatus.build({ tenantId, status, statusInfo: { reasonCode: action }, variableMonitoringId, })); }); } createOrUpdateBySetMonitoringDataTypeAndStationId(tenantId, value, componentId, variableId, stationId) { return __awaiter(this, void 0, void 0, function* () { let result = null; yield this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () { const savedVariableMonitoring = yield this.s.models[VariableMonitoring_1.VariableMonitoring.MODEL_NAME].findOne({ where: { stationId, variableId, componentId }, transaction, }); if (!savedVariableMonitoring) { const variableMonitoring = VariableMonitoring_1.VariableMonitoring.build(Object.assign({ tenantId, stationId, variableId, componentId }, value)); result = yield variableMonitoring.save({ transaction }); this.emit('created', [result]); } else { const updatedVariableMonitoring = yield savedVariableMonitoring.update(value, { transaction, returning: true, }); result = updatedVariableMonitoring; this.emit('updated', [result]); } })); if (!result) { throw new Error('VariableMonitoring could not be created or updated'); } return result; }); } rejectAllVariableMonitoringsByStationId(tenantId, action, stationId) { return __awaiter(this, void 0, void 0, function* () { yield this.readAllByQuery(tenantId, { where: { stationId, }, }).then((variableMonitorings) => __awaiter(this, void 0, void 0, function* () { for (const variableMonitoring of variableMonitorings) { yield this.createVariableMonitoringStatus(tenantId, base_1.OCPP2_0_1.SetMonitoringStatusEnumType.Rejected, action, variableMonitoring.databaseId); } })); }); } rejectVariableMonitoringByIdAndStationId(tenantId, action, id, stationId) { return __awaiter(this, void 0, void 0, function* () { yield this.readAllByQuery(tenantId, { where: { id, stationId, }, }).then((variableMonitorings) => __awaiter(this, void 0, void 0, function* () { for (const variableMonitoring of variableMonitorings) { yield this.createVariableMonitoringStatus(tenantId, base_1.OCPP2_0_1.SetMonitoringStatusEnumType.Rejected, action, variableMonitoring.databaseId); } })); }); } updateResultByStationId(tenantId, result, stationId) { const _super = Object.create(null, { readAllByQuery: { get: () => super.readAllByQuery } }); return __awaiter(this, void 0, void 0, function* () { const savedVariableMonitoring = yield _super.readAllByQuery.call(this, tenantId, { where: { stationId, type: result.type, severity: result.severity }, include: [ { model: DeviceModel_1.Component, where: { name: result.component.name, instance: result.component.instance ? result.component.instance : null, }, }, { model: DeviceModel_1.Variable, where: { name: result.variable.name, instance: result.variable.instance ? result.variable.instance : null, }, }, ], }) .then((variableMonitorings) => variableMonitorings[0]); // TODO: Make sure this uniqueness constraint is actually enforced. if (savedVariableMonitoring) { // The Id is only returned from Charging Station when status is accepted. if (result.status === base_1.OCPP2_0_1.SetMonitoringStatusEnumType.Accepted && result.id) { yield this.updateByKey(tenantId, { id: result.id, }, savedVariableMonitoring.get('databaseId').toString()); } yield this.variableMonitoringStatus.create(tenantId, VariableMonitoring_1.VariableMonitoringStatus.build({ tenantId, status: result.status, statusInfo: result.statusInfo, variableMonitoringId: savedVariableMonitoring.get('databaseId'), })); // Reload in order to include the statuses return yield this.readAllByQuery(tenantId, { where: { databaseId: savedVariableMonitoring.get('databaseId') }, include: [VariableMonitoring_1.VariableMonitoringStatus], }).then((variableMonitorings) => variableMonitorings[0]); } else { throw new Error(`Unable to update set monitoring result: ${result}`); } }); } createEventDatumByComponentIdAndVariableIdAndStationId(tenantId, event, componentId, variableId, stationId) { return __awaiter(this, void 0, void 0, function* () { return yield this.eventData.create(tenantId, VariableMonitoring_1.EventData.build(Object.assign({ tenantId, stationId, variableId, componentId }, event))); }); } } exports.SequelizeVariableMonitoringRepository = SequelizeVariableMonitoringRepository; //# sourceMappingURL=VariableMonitoring.js.map