60 lines
3.7 KiB
TypeScript
60 lines
3.7 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { InjectModel } from '@nestjs/mongoose';
|
|
import { Model, Types } from 'mongoose';
|
|
import { AnlageService } from '../../rk-anlagen/anlage/anlage.service';
|
|
import { DrawService } from '../../draw/draw.service';
|
|
import { Unit } from '../../_schemas/anlagen/units';
|
|
import { prepareBinding } from '../../visualisierung/shared/graphics';
|
|
import { type AnalogTable, validateAnalogTable } from './sensoren-analog.dto';
|
|
import type { AnalogDocument } from './sensoren-analog.schema';
|
|
import { analogFileName } from './sensoren-analog.excel';
|
|
|
|
@Injectable()
|
|
export class AnalogService {
|
|
constructor(@InjectModel('SensorenAnalog', 'anlagen') private readonly model: Model<AnalogDocument>,
|
|
@InjectModel(Unit.name, 'anlagen') private readonly units: Model<Unit>,
|
|
private readonly anlagen: AnlageService, private readonly draw: DrawService) {}
|
|
async get(): Promise<AnalogTable> {
|
|
const anlageId = (await this.anlagen.getSelected()).id;
|
|
const doc = await this.model.findOne({ anlageId }).lean().exec();
|
|
return { anlageId, version: doc?.revision ?? 0, reservedCount: doc?.reservedCount ?? 40, rows: doc?.rows ?? [] };
|
|
}
|
|
async sources() {
|
|
const anlageId = (await this.anlagen.getSelected()).id;
|
|
const units = await this.units.find({ anlageId: new Types.ObjectId(anlageId) }).lean().exec();
|
|
return { anlageId, sources: units.flatMap(unit => unit.components.map(component => ({ componentId: component._id!.toString(),
|
|
label: [unit.prefix, unit.bmk, unit.name, unit.postfix, component.name, component.index].filter(v => v !== undefined && v !== '').join('_'), type: component.type }))) };
|
|
}
|
|
async save(input: AnalogTable) {
|
|
validateAnalogTable(input);
|
|
if ((await this.anlagen.getSelected()).id !== input.anlageId) throw new BadRequestException('Anlage geändert. Neu laden.');
|
|
const linked = input.rows.filter(row => row.componentId);
|
|
if (linked.length) {
|
|
const { anlageId, sources } = await this.sources();
|
|
if (anlageId !== input.anlageId || linked.some(row => !sources.some(source => source.componentId === row.componentId)))
|
|
throw new BadRequestException('Unit-Komponente dieser Anlage nicht mehr vorhanden');
|
|
}
|
|
const rows = input.rows.map(row => ({ id: row.id, datenNr: row.datenNr, beschreibung: row.beschreibung, statistikTyp: row.statistikTyp,
|
|
statistikProconId: row.statistikProconId, einheit: row.einheit, hinzugefuegtAm: row.hinzugefuegtAm, componentId: row.componentId,
|
|
prozentkasten: row.prozentkasten, valuePlacementId: row.valuePlacementId,
|
|
placements: row.placements.map(({ id, x, y, width, height, z, show }) => ({ id, x, y, width, height, z, show })),
|
|
graphics: row.graphics.map(binding => prepareBinding(this.draw, binding)) }));
|
|
try {
|
|
const result = await this.model.updateOne({ anlageId: input.anlageId, revision: input.version },
|
|
{ $set: { rows, reservedCount: input.reservedCount }, $inc: { revision: 1 } }, { upsert: input.version === 0, runValidators: true });
|
|
if (!result.matchedCount && !result.upsertedCount) throw new BadRequestException('Sensoren inzwischen geändert. Neu laden.');
|
|
} catch (error) {
|
|
if ((error as { code?: number }).code === 11000) throw new BadRequestException('Sensoren inzwischen geändert. Neu laden.');
|
|
throw error;
|
|
}
|
|
return this.get();
|
|
}
|
|
async xamlFiles() {
|
|
const table = await this.get();
|
|
return table.rows.flatMap(row => row.graphics.map(binding => {
|
|
const graphic = prepareBinding(this.draw, binding);
|
|
return { name: analogFileName(row, graphic.slot, graphic.variant), data: graphic.generator.ergebnis!.varianten.find(v => v.state === graphic.variant)!.xaml };
|
|
}));
|
|
}
|
|
}
|