mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
feat: add power clamp entities, DTOs, and repository for energy consumption tracking
This commit is contained in:
@ -43,6 +43,11 @@ import { SubspaceProductAllocationEntity } from '../modules/space/entities/subsp
|
|||||||
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
||||||
import { TagEntity } from '../modules/space/entities/tag.entity';
|
import { TagEntity } from '../modules/space/entities/tag.entity';
|
||||||
import { ClientEntity } from '../modules/client/entities';
|
import { ClientEntity } from '../modules/client/entities';
|
||||||
|
import {
|
||||||
|
PowerClampDailyEntity,
|
||||||
|
PowerClampHourlyEntity,
|
||||||
|
PowerClampMonthlyEntity,
|
||||||
|
} from '../modules/power-clamp/entities/power-clamp.entity';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
@ -95,6 +100,9 @@ import { ClientEntity } from '../modules/client/entities';
|
|||||||
SpaceProductAllocationEntity,
|
SpaceProductAllocationEntity,
|
||||||
SubspaceProductAllocationEntity,
|
SubspaceProductAllocationEntity,
|
||||||
ClientEntity,
|
ClientEntity,
|
||||||
|
PowerClampHourlyEntity,
|
||||||
|
PowerClampDailyEntity,
|
||||||
|
PowerClampMonthlyEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
|
@ -18,6 +18,7 @@ import {
|
|||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -74,6 +75,19 @@ export class DeviceStatusFirebaseService {
|
|||||||
const device = await this.getDeviceByDeviceTuyaUuid(
|
const device = await this.getDeviceByDeviceTuyaUuid(
|
||||||
addDeviceStatusDto.deviceTuyaUuid,
|
addDeviceStatusDto.deviceTuyaUuid,
|
||||||
);
|
);
|
||||||
|
if (device.productDevice.prodType === ProductType.PC) {
|
||||||
|
const energyStatus = addDeviceStatusDto.status.find(
|
||||||
|
(status) =>
|
||||||
|
status.code === 'EnergyConsumed' ||
|
||||||
|
status.code === 'EnergyConsumedA' ||
|
||||||
|
status.code === 'EnergyConsumedB' ||
|
||||||
|
status.code === 'EnergyConsumedC',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (energyStatus) {
|
||||||
|
console.log(device.productDevice.prodType, addDeviceStatusDto.status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (device?.uuid) {
|
if (device?.uuid) {
|
||||||
return await this.createDeviceStatusFirebase({
|
return await this.createDeviceStatusFirebase({
|
||||||
|
@ -17,6 +17,7 @@ import { SceneDeviceEntity } from '../../scene-device/entities';
|
|||||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||||
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
||||||
import { NewTagEntity } from '../../tag';
|
import { NewTagEntity } from '../../tag';
|
||||||
|
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
||||||
|
|
||||||
@Entity({ name: 'device' })
|
@Entity({ name: 'device' })
|
||||||
@Unique(['deviceTuyaUuid'])
|
@Unique(['deviceTuyaUuid'])
|
||||||
@ -79,7 +80,8 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
|
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
|
||||||
// @JoinTable({ name: 'device_tags' })
|
// @JoinTable({ name: 'device_tags' })
|
||||||
public tag: NewTagEntity;
|
public tag: NewTagEntity;
|
||||||
|
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
|
||||||
|
powerClampHourly: PowerClampHourlyEntity[];
|
||||||
constructor(partial: Partial<DeviceEntity>) {
|
constructor(partial: Partial<DeviceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
1
libs/common/src/modules/power-clamp/dtos/index.ts
Normal file
1
libs/common/src/modules/power-clamp/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './power-clamp.dto';
|
43
libs/common/src/modules/power-clamp/dtos/power-clamp.dto.ts
Normal file
43
libs/common/src/modules/power-clamp/dtos/power-clamp.dto.ts
Normal 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;
|
||||||
|
}
|
1
libs/common/src/modules/power-clamp/entities/index.ts
Normal file
1
libs/common/src/modules/power-clamp/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './power-clamp.entity';
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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 {}
|
@ -0,0 +1 @@
|
|||||||
|
export * from './power-clamp.repository';
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user