feat: add power clamp entities, DTOs, and repository for energy consumption tracking

This commit is contained in:
faris Aljohari
2025-04-23 15:08:51 +03:00
parent ce7fc0114e
commit 8228ccc293
10 changed files with 202 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import { SceneDeviceEntity } from '../../scene-device/entities';
import { SpaceEntity } from '../../space/entities/space.entity';
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
import { NewTagEntity } from '../../tag';
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
@Entity({ name: 'device' })
@Unique(['deviceTuyaUuid'])
@ -79,7 +80,8 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
// @JoinTable({ name: 'device_tags' })
public tag: NewTagEntity;
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
powerClampHourly: PowerClampHourlyEntity[];
constructor(partial: Partial<DeviceEntity>) {
super();
Object.assign(this, partial);

View File

@ -0,0 +1 @@
export * from './power-clamp.dto';

View File

@ -0,0 +1,43 @@
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class PowerClampDto {
@IsString()
@IsNotEmpty()
public uuid: string;
@IsString()
@IsNotEmpty()
public deviceUuid: string;
@IsString()
@IsOptional()
public hour?: string;
@IsString()
@IsOptional()
public day?: string;
@IsString()
@IsOptional()
public month?: string;
@IsString()
@IsNotEmpty()
public energyConsumedKw: string;
@IsString()
@IsNotEmpty()
public energyConsumedA: string;
@IsString()
@IsNotEmpty()
public energyConsumedB: string;
@IsString()
@IsNotEmpty()
public energyConsumedC: string;
@IsString()
@IsNotEmpty()
public prodType: string;
}

View File

@ -0,0 +1 @@
export * from './power-clamp.entity';

View File

@ -0,0 +1,92 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { PowerClampDto } from '../dtos';
import { DeviceEntity } from '../../device/entities/device.entity';
@Entity({ name: 'power-clamp-energy-consumed-hourly' })
export class PowerClampHourlyEntity extends AbstractEntity<PowerClampDto> {
@Column({ nullable: false })
public deviceUuid: string;
@Column({ nullable: false })
public hour: string;
@Column({ nullable: false, type: 'date' })
public date: string;
@Column({ nullable: true })
public energyConsumedKw: string;
@Column({ nullable: false })
public energyConsumedA: string;
@Column({ nullable: false })
public energyConsumedB: string;
@Column({ nullable: false })
public energyConsumedC: string;
@ManyToOne(() => DeviceEntity, (device) => device.powerClampHourly)
device: DeviceEntity;
constructor(partial: Partial<PowerClampHourlyEntity>) {
super();
Object.assign(this, partial);
}
}
@Entity({ name: 'power-clamp-energy-consumed-daily' })
export class PowerClampDailyEntity extends AbstractEntity<PowerClampDto> {
@Column({ nullable: false })
public deviceUuid: string;
@Column({ nullable: false, type: 'date' })
public date: string;
@Column({ nullable: true })
public energyConsumedKw: string;
@Column({ nullable: false })
public energyConsumedA: string;
@Column({ nullable: false })
public energyConsumedB: string;
@Column({ nullable: false })
public energyConsumedC: string;
@ManyToOne(() => DeviceEntity, (device) => device.powerClampHourly)
device: DeviceEntity;
constructor(partial: Partial<PowerClampHourlyEntity>) {
super();
Object.assign(this, partial);
}
}
@Entity({ name: 'power-clamp-energy-consumed-monthly' })
export class PowerClampMonthlyEntity extends AbstractEntity<PowerClampDto> {
@Column({ nullable: false })
public deviceUuid: string;
@Column({ nullable: false })
public month: string;
@Column({ nullable: true })
public energyConsumedKw: string;
@Column({ nullable: false })
public energyConsumedA: string;
@Column({ nullable: false })
public energyConsumedB: string;
@Column({ nullable: false })
public energyConsumedC: string;
@ManyToOne(() => DeviceEntity, (device) => device.powerClampHourly)
device: DeviceEntity;
constructor(partial: Partial<PowerClampHourlyEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PowerClampHourlyEntity } from './entities/power-clamp.entity';
@Module({
providers: [],
exports: [],
controllers: [],
imports: [TypeOrmModule.forFeature([PowerClampHourlyEntity])],
})
export class PowerClampRepositoryModule {}

View File

@ -0,0 +1 @@
export * from './power-clamp.repository';

View File

@ -0,0 +1,28 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import {
PowerClampDailyEntity,
PowerClampHourlyEntity,
PowerClampMonthlyEntity,
} from '../entities/power-clamp.entity';
@Injectable()
export class PowerClampHourlyRepository extends Repository<PowerClampHourlyEntity> {
constructor(private dataSource: DataSource) {
super(PowerClampHourlyEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class PowerClampDailyRepository extends Repository<PowerClampDailyEntity> {
constructor(private dataSource: DataSource) {
super(PowerClampDailyEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class PowerClampMonthlyRepository extends Repository<PowerClampMonthlyEntity> {
constructor(private dataSource: DataSource) {
super(PowerClampMonthlyEntity, dataSource.createEntityManager());
}
}