2026_08_31
This commit is contained in:
commit
73015386e9
2320 files changed
+80291
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GroupsModule } from './groups/groups.module';
|
||||
import { UnitsModule } from './units/units.module';
|
||||
import { SignalsModule } from './signals/signals.module';
|
||||
import { IosModule } from './ios/ios.module';
|
||||
import { VisualizationsModule } from './visualizations/visualizations.module';
|
||||
import { ComponentsModule } from './components/components.module';
|
||||
|
||||
@Module({
|
||||
imports: [GroupsModule, UnitsModule, SignalsModule, IosModule, VisualizationsModule, ComponentsModule],
|
||||
|
||||
})
|
||||
export class AnlagenModule {}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ComponentsService } from './components.service';
|
||||
|
||||
@Controller('rk2/components')
|
||||
export class ComponentsController {
|
||||
constructor(private readonly service: ComponentsService) { }
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ComponentsService } from './components.service';
|
||||
import { ComponentsController } from './components.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Unit, UnitSchema } from 'src/_schemas/anlagen/units';
|
||||
|
||||
@Module({
|
||||
controllers: [ComponentsController],
|
||||
providers: [ComponentsService],
|
||||
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen")],
|
||||
|
||||
})
|
||||
export class ComponentsModule { }
|
||||
@@ -0,0 +1,57 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { ComponentOfUnitDto, ComponentResponseDto, ComponentTypes } from 'src/_dto/anlagen/components';
|
||||
import { UnitResponseDto } from 'src/_dto/anlagen/units';
|
||||
import { Component } from 'src/_schemas/anlagen/component';
|
||||
import { Unit } from 'src/_schemas/anlagen/units';
|
||||
|
||||
@Injectable()
|
||||
export class ComponentsService {
|
||||
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>) { }
|
||||
|
||||
|
||||
//--- Find BY component id
|
||||
async findOne(id: string): Promise<ComponentOfUnitDto> {
|
||||
if(!Types.ObjectId.isValid(id)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
|
||||
const item = await this.model.findOne({ 'components._id': id }).exec();
|
||||
|
||||
|
||||
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${id} gefunden`)
|
||||
let comp = item.components.find((c) => c._id?.toString() === id);
|
||||
if (!comp) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${id} gefunden`)
|
||||
return {
|
||||
unitId: item._id.toString(),
|
||||
component: this.toDto(comp)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(component: Component): ComponentResponseDto {
|
||||
|
||||
|
||||
let compRet =
|
||||
{
|
||||
id: component._id!.toString(),
|
||||
name: component.name,
|
||||
type: this.getComponentType(component.type.toString()),
|
||||
signalIds: component.signalIds.map((signal) => signal.toString()),
|
||||
properties: component.properties
|
||||
}
|
||||
|
||||
return compRet;
|
||||
}
|
||||
|
||||
|
||||
|
||||
getComponentType(name: string) {
|
||||
let retValue = Object.values(ComponentTypes).find((c) => c === name)
|
||||
if (!retValue) return ComponentTypes.UNKNOWN;
|
||||
return retValue
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { GroupCreateDto, GroupUpdateDto } from 'src/_dto/anlagen/groups';
|
||||
|
||||
@Controller('rk2/groups')
|
||||
export class GroupsController {
|
||||
constructor(private readonly service: GroupsService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Get('childs/:id')
|
||||
getChilds(@Param('id') id: string) {
|
||||
return this.service.findChilds(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: GroupCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: GroupUpdateDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GroupsController } from './groups.controller';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Group, GroupSchema } from 'src/_schemas/anlagen/groups';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: Group.name, schema: GroupSchema }],"anlagen")],
|
||||
controllers: [GroupsController],
|
||||
providers: [GroupsService]
|
||||
})
|
||||
export class GroupsModule { }
|
||||
@@ -0,0 +1,79 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { GroupCreateDto, GroupResponseDto, GroupUpdateDto } from 'src/_dto/anlagen/groups';
|
||||
import { Group, GroupDocument } from 'src/_schemas/anlagen/groups';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class GroupsService {
|
||||
constructor(@InjectModel(Group.name,"anlagen") private model: Model<Group>) { }
|
||||
|
||||
async findAll(): Promise<GroupResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Group ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
async findChilds(id:string): Promise<GroupResponseDto[]> {
|
||||
const responsData = await this.model.find({parentId:new Types.ObjectId(id)}).exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async create(dto: GroupCreateDto):Promise<GroupResponseDto> {
|
||||
const newItem = new this.model(this.toDocument(dto));
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
|
||||
async update(id: string, dto: GroupUpdateDto) {
|
||||
let update = this.toDocument(dto);
|
||||
console.log(update);
|
||||
const changedItem = await this.model.findByIdAndUpdate(id, {
|
||||
$set: {
|
||||
...update
|
||||
|
||||
}
|
||||
}, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!changedItem) throw new NotFoundException(`Group ${id} nicht gefunden`);
|
||||
|
||||
return this.toDto(changedItem);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!removedItem) throw new NotFoundException(`Group ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(dbData: GroupDocument): GroupResponseDto {
|
||||
return {
|
||||
id: dbData._id.toString(),
|
||||
name: dbData.name,
|
||||
container: dbData.container,
|
||||
nr: dbData.nr,
|
||||
parentId: dbData.parentId?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
toDocument(dto: GroupCreateDto|GroupUpdateDto) {
|
||||
return {
|
||||
name: dto.name,
|
||||
container: dto.container,
|
||||
nr: dto.nr,
|
||||
parentId: dto.parentId ? new Types.ObjectId(dto.parentId) : null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { IosService } from './ios.service';
|
||||
import { IoCreateDto, IoUpdateDto } from 'src/_dto/anlagen/ios';
|
||||
|
||||
@Controller('rk2/ios')
|
||||
export class IosController {
|
||||
constructor(private readonly service: IosService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: IoCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: IoUpdateDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { IosService } from './ios.service';
|
||||
import { IosController } from './ios.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { IO, IOSchema } from 'src/_schemas/anlagen/ios';
|
||||
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([{ name: IO.name, schema: IOSchema }], "anlagen"),
|
||||
AnlageModule
|
||||
],
|
||||
providers: [IosService],
|
||||
controllers: [IosController]
|
||||
})
|
||||
export class IosModule { }
|
||||
@@ -0,0 +1,100 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { IoCreateDto, IoResponseDto, IoUpdateDto } from 'src/_dto/anlagen/ios';
|
||||
import { IO, IoDocument } from 'src/_schemas/anlagen/ios';
|
||||
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
|
||||
|
||||
@Injectable()
|
||||
export class IosService {
|
||||
constructor(
|
||||
@InjectModel(IO.name, "anlagen") private model: Model<IO>,
|
||||
private readonly anlageService: AnlageService
|
||||
) { }
|
||||
|
||||
async findAll(): Promise<IoResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<IoResponseDto> {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Signal ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
|
||||
async create(dto: IoCreateDto): Promise<IoResponseDto> {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
console.log(currentAnlage);
|
||||
if (!currentAnlage) throw new NotFoundException('Es ist keine Anlage ausgewählt');
|
||||
const newItem = new this.model(this.toDocument(dto, currentAnlage.id));
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
|
||||
async update(id: string, dto: IoUpdateDto) {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
let anlage = dto.anlageId ? dto.anlageId : currentAnlage.id;
|
||||
if (!anlage) throw new NotFoundException('Anlage Id muss gesetzt sein')
|
||||
|
||||
let update = this.toDocument(dto, anlage);
|
||||
console.log(update);
|
||||
const changedItem = await this.model.findByIdAndUpdate(id, {
|
||||
$set: {
|
||||
...update
|
||||
|
||||
}
|
||||
}, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!changedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
|
||||
return this.toDto(changedItem);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!removedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(dbData: IoDocument): IoResponseDto {
|
||||
return {
|
||||
id: dbData._id.toString(),
|
||||
anlageId: dbData.anlageId.toString(),
|
||||
type: dbData.type,
|
||||
address: dbData.address,
|
||||
adrBit: dbData.adrBit,
|
||||
plcId: dbData.plcId?.toString(),
|
||||
moduleId: dbData.moduleId?.toString(),
|
||||
channel: dbData.channel,
|
||||
text: dbData.text,
|
||||
seite: dbData.seite,
|
||||
seiteName: dbData.seiteName,
|
||||
excelData: dbData.excelData,
|
||||
plcData: dbData.plcData
|
||||
}
|
||||
}
|
||||
|
||||
toDocument(dto: IoCreateDto | IoUpdateDto, anlage: string) {
|
||||
return {
|
||||
anlageId: new Types.ObjectId(anlage),
|
||||
type: dto.type,
|
||||
address: dto.address,
|
||||
adrBit: dto.adrBit,
|
||||
plcId: dto.plcId ? new Types.ObjectId(dto.plcId) : null,
|
||||
moduleId: dto.moduleId ? new Types.ObjectId(dto.moduleId) : null,
|
||||
channel: dto.channel,
|
||||
text: dto.text,
|
||||
seite: dto.seite,
|
||||
seiteName: dto.seiteName,
|
||||
excelData: dto.excelData,
|
||||
plcData: dto.plcData
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { SignalsService } from './signals.service';
|
||||
import { SignalCreateDto, SignalUpdateDto } from 'src/_dto/anlagen/signals';
|
||||
|
||||
@Controller('rk2/signals')
|
||||
export class SignalsController {
|
||||
constructor(private readonly service: SignalsService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Get("byComponent/:id")
|
||||
getByComponent(@Param('id') id: string) {
|
||||
return this.service.findByComponent(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: SignalCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: SignalUpdateDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SignalsService } from './signals.service';
|
||||
import { SignalsController } from './signals.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Signal, SignalSchema } from 'src/_schemas/anlagen/singnals';
|
||||
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([{ name: Signal.name, schema: SignalSchema }], "anlagen"),
|
||||
AnlageModule
|
||||
],
|
||||
providers: [SignalsService],
|
||||
controllers: [SignalsController]
|
||||
})
|
||||
export class SignalsModule { }
|
||||
@@ -0,0 +1,97 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { SignalCreateDto, SignalResponseDto, SignalUpdateDto } from 'src/_dto/anlagen/signals';
|
||||
import { Signal, SignalDocument } from 'src/_schemas/anlagen/singnals';
|
||||
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
|
||||
|
||||
@Injectable()
|
||||
export class SignalsService {
|
||||
constructor(
|
||||
@InjectModel(Signal.name, "anlagen") private model: Model<Signal>,
|
||||
private readonly anlageService: AnlageService
|
||||
) { }
|
||||
|
||||
async findAll(): Promise<SignalResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<SignalResponseDto> {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Signal ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
async findByComponent(componentId:string): Promise<SignalResponseDto[]> {
|
||||
const responsData = await this.model.find({componentId:new Types.ObjectId(componentId)}).exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
|
||||
async create(dto: SignalCreateDto): Promise<SignalResponseDto> {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
|
||||
if(!currentAnlage) throw new NotFoundException('Es ist keine Anlage ausgewählt');
|
||||
|
||||
|
||||
const newItem = new this.model(this.toDocument(dto,currentAnlage.id));
|
||||
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
|
||||
async update(id: string, dto: SignalUpdateDto) {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
let anlage = dto.anlageId ? dto.anlageId : currentAnlage.id;
|
||||
if(!anlage) throw new NotFoundException('Anlage Id muss gesetzt sein')
|
||||
|
||||
let update = this.toDocument(dto,anlage);
|
||||
console.log(update);
|
||||
const changedItem = await this.model.findByIdAndUpdate(id, {
|
||||
$set: {
|
||||
...update
|
||||
|
||||
}
|
||||
}, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!changedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
|
||||
return this.toDto(changedItem);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!removedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(dbData: SignalDocument): SignalResponseDto {
|
||||
return {
|
||||
id: dbData._id.toString(),
|
||||
anlageId: dbData.anlageId.toString(),
|
||||
name: dbData.name,
|
||||
type: dbData.type,
|
||||
description: dbData.description,
|
||||
ioId: dbData.ioId?.toString(),
|
||||
componentId: dbData.componentId?.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
toDocument(dto: SignalCreateDto | SignalUpdateDto,anlage:string) {
|
||||
return {
|
||||
anlageId: new Types.ObjectId(anlage),
|
||||
name: dto.name,
|
||||
type: dto.type,
|
||||
description: dto.description,
|
||||
ioId: dto.ioId ? new Types.ObjectId(dto.ioId) : null,
|
||||
componentId: dto.componentId ? new Types.ObjectId(dto.componentId) : null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
import { Types } from "mongoose";
|
||||
|
||||
|
||||
export const Unit_query_get = (anlageId: string, filter?: string) => {
|
||||
return [
|
||||
{
|
||||
$match: {
|
||||
anlageId: new Types.ObjectId(anlageId)
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
$lookup: {
|
||||
from: 'signals',
|
||||
localField: "_id", // Feld in signals
|
||||
foreignField: "signalId", // Feld in unit
|
||||
let: { unitId: "$_id", anlageId: "$anlageId" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ["$unitId", "$$unitId"] },
|
||||
{ $eq: ["$anlageId", "$$anlageId"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // weg
|
||||
anlageId: 0, // weg
|
||||
groupId: 0 // weg
|
||||
}
|
||||
},
|
||||
],
|
||||
as: "signals"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
id: 1,
|
||||
_id: 0,
|
||||
anlageId: 1,
|
||||
name: 1,
|
||||
type: 1,
|
||||
nr: 1,
|
||||
funcTxt: 1,
|
||||
displayName: 1,
|
||||
ort: 1,
|
||||
info: 1,
|
||||
options: 1,
|
||||
elements: 1
|
||||
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
nr: 1 as 1
|
||||
}
|
||||
}
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
export const Unit_query_getSAVE = (anlageId: string, filter?: string) => {
|
||||
return [
|
||||
{
|
||||
$match: {
|
||||
anlageId: new Types.ObjectId(anlageId)
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
$lookup: {
|
||||
from: 'signals',
|
||||
localField: "_id", // Feld in signals
|
||||
foreignField: "signalId", // Feld in unit
|
||||
let: { unitId: "$_id", anlageId: "$anlageId" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ["$unitId", "$$unitId"] },
|
||||
{ $eq: ["$anlageId", "$$anlageId"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // weg
|
||||
anlageId: 0, // weg
|
||||
groupId: 0 // weg
|
||||
}
|
||||
},
|
||||
],
|
||||
as: "signals"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
id: 1,
|
||||
_id: 0,
|
||||
anlageId: 1,
|
||||
name: 1,
|
||||
type: 1,
|
||||
nr: 1,
|
||||
funcTxt: 1,
|
||||
displayName: 1,
|
||||
ort: 1,
|
||||
info: 1,
|
||||
options: 1,
|
||||
elements: 1
|
||||
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
nr: 1 as 1
|
||||
}
|
||||
}
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
export const Group_query_get_with_elements =
|
||||
[
|
||||
{
|
||||
$lookup: {
|
||||
from: 'elements',
|
||||
localField: "_id", // Feld in users
|
||||
foreignField: "groupId", // Feld in posts
|
||||
let: { groupId: "$_id", anlageId: "$anlageId" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ["$groupId", "$$groupId"] },
|
||||
{ $eq: ["$anlageId", "$$anlageId"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // weg
|
||||
anlageId: 0, // weg
|
||||
groupId: 0 // weg
|
||||
}
|
||||
}
|
||||
],
|
||||
as: "elements"
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
id: 1,
|
||||
_id: 0,
|
||||
name: 1,
|
||||
type: 1,
|
||||
nr: 1,
|
||||
funcTxt: 1,
|
||||
displayName: 1,
|
||||
ort: 1,
|
||||
info: 1,
|
||||
options: 1,
|
||||
elements: 1
|
||||
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
export const Group_query_get_with_peripherie =
|
||||
[
|
||||
{
|
||||
$lookup: {
|
||||
from: 'peripheries',
|
||||
localField: "_id", // Feld in users
|
||||
foreignField: "groupId", // Feld in posts
|
||||
let: { groupId: "$_id", anlageId: "$anlageId" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ["$groupId", "$$groupId"] },
|
||||
{ $eq: ["$anlageId", "$$anlageId"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // weg
|
||||
adrType: 1,
|
||||
adrByte: 1,
|
||||
adrBit: 1,
|
||||
karte: 1,
|
||||
text: 1,
|
||||
seite: 1,
|
||||
seiteName: 1,
|
||||
}
|
||||
}
|
||||
],
|
||||
as: "peripheries"
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
id: "$_id"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
id: 1,
|
||||
_id: 0,
|
||||
name: 1,
|
||||
type: 1,
|
||||
nr: 1,
|
||||
funcTxt: 1,
|
||||
displayName: 1,
|
||||
ort: 1,
|
||||
info: 1,
|
||||
options: 1,
|
||||
peripheries: 1
|
||||
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { UnitsService } from './units.service';
|
||||
import { UnitCreateDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
|
||||
import { ComponentCreateDto, ComponentUpdateDto } from 'src/_dto/anlagen/components';
|
||||
|
||||
|
||||
@Controller('rk2/units')
|
||||
export class UnitsController {
|
||||
constructor(private readonly service: UnitsService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get("/expand")
|
||||
getAllExpand() {
|
||||
return this.service.findAllExp();
|
||||
}
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: UnitCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UnitUpdateBaseDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
@Patch(':id/component/:compId')
|
||||
updateComp(@Param('id') id: string,@Param('compId') compId: string, @Body() dto: ComponentUpdateDto) {
|
||||
return this.service.updateComponent(id,compId, dto);
|
||||
}
|
||||
|
||||
@Patch(':id/addComponent')
|
||||
updateUnitAddComp(@Param('id') id: string, @Body() dto: ComponentCreateDto) {
|
||||
return this.service.addComponent(id,dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UnitsService } from './units.service';
|
||||
import { UnitsController } from './units.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Unit, UnitSchema } from 'src/_schemas/anlagen/units';
|
||||
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen"), AnlageModule],
|
||||
|
||||
providers: [UnitsService],
|
||||
controllers: [UnitsController]
|
||||
})
|
||||
export class UnitsModule { }
|
||||
@@ -0,0 +1,244 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { UnitCreateDto, UnitResponseDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
|
||||
import { Component } from 'src/_schemas/anlagen/component';
|
||||
import { Unit, UnitDocument } from 'src/_schemas/anlagen/units';
|
||||
import { ComponentCreateDto, ComponentResponseDto, ComponentTypes, ComponentUpdateDto, } from 'src/_dto/anlagen/components';
|
||||
import { Unit_query_get } from './unit.querry';
|
||||
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class UnitsService {
|
||||
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>,
|
||||
private readonly anlageService: AnlageService) { }
|
||||
|
||||
async findAllExp(): Promise<any[]> {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
|
||||
|
||||
|
||||
let items = await this.model.aggregate<any>(Unit_query_get(currentAnlage.id));
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
//--- Find ALL
|
||||
async findAll(): Promise<UnitResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
//--- Find BY id
|
||||
async findOne(id: string) {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Unit ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
|
||||
//--- Create new Unit
|
||||
async create(dto: UnitCreateDto): Promise<UnitResponseDto> {
|
||||
let { components, ...base } = dto;
|
||||
components = components.map((c) => ({ name: c.name, signalIds: c.signalIds, type: c.type, properties: c.properties, id: c.id == "" ? undefined : c.id }))
|
||||
let cComponents = components.map((c) => this.handleComponents(c));
|
||||
console.log(base);
|
||||
const newItem = new this.model({ ...base, components: cComponents });
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
//--- Update a Unit
|
||||
async update(id: string, dto: UnitUpdateBaseDto) {
|
||||
|
||||
let update = {
|
||||
...(dto.name !== undefined && { name: dto.name }),
|
||||
...(dto.groupId !== undefined && { groupId: dto.groupId }),
|
||||
...(dto.type !== undefined && { type: dto.type.toString() }),
|
||||
...(dto.components !== undefined && { components: dto.components.map((c) => this.handleComponents(c)) }),
|
||||
}
|
||||
|
||||
console.log(update.components);
|
||||
const changedItem = await this.model.findByIdAndUpdate({ _id: id }, {
|
||||
$set: {
|
||||
...update,
|
||||
|
||||
}
|
||||
}
|
||||
, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
|
||||
const newUnit = await this.findOne(id);
|
||||
if (!newUnit) throw new NotFoundException('The updated Unit are not found');
|
||||
|
||||
return newUnit;
|
||||
}
|
||||
|
||||
//--- Remove
|
||||
async remove(id: string) {
|
||||
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!removedItem) throw new NotFoundException(`Group ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
//--- Update Components of a Unit
|
||||
async updateComponent(unitId: string, componentId: string, dto: ComponentUpdateDto) {
|
||||
|
||||
const unit = await this.model
|
||||
.findOneAndUpdate(
|
||||
{
|
||||
_id: unitId,
|
||||
'components._id': componentId,
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
'components.$.name': dto.name,
|
||||
'components.$.type': dto.type,
|
||||
'components.$.properties': dto.properties,
|
||||
'components.$.signalIds': dto.signalIds,
|
||||
},
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
after: true,
|
||||
},
|
||||
)
|
||||
.exec();
|
||||
|
||||
if (!unit) {
|
||||
throw new NotFoundException(
|
||||
`Unit oder Component nicht gefunden`,
|
||||
);
|
||||
}
|
||||
|
||||
return unit;
|
||||
}
|
||||
|
||||
//--- Update Components of a Unit
|
||||
async addComponent(unitId: string, dto: ComponentCreateDto) {
|
||||
let currentUnit = await this.model.findById(unitId).exec();
|
||||
if (!currentUnit) throw new NotFoundException('Unit id ncht gefunden');
|
||||
|
||||
let signale = dto.signalIds ? dto.signalIds.map((s) => new Types.ObjectId(s)) : [];
|
||||
let pros = { ...dto.properties };
|
||||
currentUnit.components = [...currentUnit.components, {
|
||||
name: dto.name,
|
||||
signalIds: [...signale], //dto.signalIds ? dto.signalIds.map((s)=>new ObjectId(s)): [],
|
||||
type: dto.type,
|
||||
properties: { ...pros }
|
||||
}]
|
||||
|
||||
|
||||
const unit = await this.model.findOneAndUpdate({ _id: unitId }, currentUnit, { new: true, runValidators: true, }).exec();
|
||||
|
||||
if (!unit) {
|
||||
throw new NotFoundException(
|
||||
`Unit oder Component nicht gefunden`,
|
||||
);
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
|
||||
toDto(dbData: Unit): UnitResponseDto {
|
||||
|
||||
let comop = dbData.components.map((c) => (
|
||||
|
||||
{
|
||||
id: c._id!.toString(),
|
||||
name: c.name,
|
||||
type: this.getComponentType(c.type.toString()),
|
||||
signalIds: c.signalIds.map((signal) => signal.toString()),
|
||||
properties: c.properties
|
||||
}))
|
||||
|
||||
return {
|
||||
id: dbData._id.toString() || '',
|
||||
name: dbData.name,
|
||||
bmk: dbData.bmk,
|
||||
prefix: dbData.prefix,
|
||||
info: dbData.info,
|
||||
anlageId: dbData.anlageId,
|
||||
type: dbData.type,
|
||||
groupId: dbData.groupId.toString(),
|
||||
components: comop
|
||||
}
|
||||
}
|
||||
|
||||
getToUpdateComponents(currentDbComp: Component[], updateComp: ComponentResponseDto[]) {
|
||||
|
||||
}
|
||||
|
||||
getComponentType(name: string) {
|
||||
let retValue = Object.values(ComponentTypes).find((c) => c === name)
|
||||
if (!retValue) return ComponentTypes.UNKNOWN;
|
||||
return retValue
|
||||
}
|
||||
|
||||
componentToDocument(compDto: ComponentResponseDto): Component {
|
||||
return {
|
||||
_id: new Types.ObjectId(compDto.id),
|
||||
name: compDto.name,
|
||||
type: compDto.type?.toString(),
|
||||
signalIds: compDto.signalIds.map((s) => new Types.ObjectId(s)),
|
||||
properties: { ...compDto.properties }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDocument(dto: UnitCreateDto): Partial<UnitDocument> {
|
||||
|
||||
return {
|
||||
name: dto.name,
|
||||
type: dto.type,
|
||||
groupId: dto.groupId ? new Types.ObjectId(dto.groupId) : undefined,
|
||||
components: this.createComponents(dto.components)
|
||||
};
|
||||
}
|
||||
|
||||
createComponents(comps: ComponentCreateDto[] | undefined): Component[] {
|
||||
if (!comps) return [];
|
||||
|
||||
return comps.map((c) => ({
|
||||
name: c.name,
|
||||
type: c.type?.toString(),
|
||||
signalIds: c.signalIds ? c.signalIds.map((s) => new Types.ObjectId(s)) : [],
|
||||
properties: { ...c.properties }
|
||||
}));
|
||||
}
|
||||
|
||||
handleComponents(component: ComponentResponseDto): Component {
|
||||
let { id, name, signalIds, type, properties } = component;
|
||||
|
||||
const cid = (id && Types.ObjectId.isValid(id)) ? new Types.ObjectId(id) : undefined;
|
||||
const cSignalIds = signalIds ? this.handleSignals(signalIds) : [];
|
||||
const cProperties = properties ? this.handleProperties(properties) : {};
|
||||
const cName = name;
|
||||
const cType = type;
|
||||
|
||||
return { name: cName, type: cType, signalIds: cSignalIds, properties: cProperties, _id: cid }
|
||||
|
||||
}
|
||||
|
||||
handleProperties(properties: Record<string, unknown>): Record<string, unknown> {
|
||||
|
||||
for (const [key, value] of Object.entries(properties)) {
|
||||
if (value === null || value === "") {
|
||||
delete properties[key];
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
handleSignals(signals: string[]): Types.ObjectId[] {
|
||||
let retValue: Types.ObjectId[] = []
|
||||
signals.forEach((s) => Types.ObjectId.isValid(s) && retValue.push(new Types.ObjectId(s)));
|
||||
return retValue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
|
||||
@Controller('visualizations')
|
||||
export class VisualizationsController {}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { VisualizationsService } from './visualizations.service';
|
||||
import { VisualizationsController } from './visualizations.controller';
|
||||
|
||||
@Module({
|
||||
providers: [VisualizationsService],
|
||||
controllers: [VisualizationsController]
|
||||
})
|
||||
export class VisualizationsModule {}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class VisualizationsService {}
|
||||
Reference in new issue
Block a user