diff --git a/libs/common/src/database/database.module.ts b/libs/common/src/database/database.module.ts index fa83c76..f65cb1d 100644 --- a/libs/common/src/database/database.module.ts +++ b/libs/common/src/database/database.module.ts @@ -51,6 +51,7 @@ import { PowerClampHourlyEntity, PowerClampMonthlyEntity, } from '../modules/power-clamp/entities/power-clamp.entity'; +import { PresenceSensorDailyEntity } from '../modules/presence-sensor/entities'; @Module({ imports: [ TypeOrmModule.forRootAsync({ @@ -109,6 +110,7 @@ import { PowerClampHourlyEntity, PowerClampDailyEntity, PowerClampMonthlyEntity, + PresenceSensorDailyEntity, ], namingStrategy: new SnakeNamingStrategy(), synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))), diff --git a/libs/common/src/modules/device/entities/device.entity.ts b/libs/common/src/modules/device/entities/device.entity.ts index 9015c40..4dc9519 100644 --- a/libs/common/src/modules/device/entities/device.entity.ts +++ b/libs/common/src/modules/device/entities/device.entity.ts @@ -18,6 +18,7 @@ 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'; +import { PresenceSensorDailyEntity } from '../../presence-sensor/entities'; @Entity({ name: 'device' }) @Unique(['deviceTuyaUuid']) @@ -82,6 +83,8 @@ export class DeviceEntity extends AbstractEntity { public tag: NewTagEntity; @OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device) powerClampHourly: PowerClampHourlyEntity[]; + @OneToMany(() => PresenceSensorDailyEntity, (sensor) => sensor.device) + presenceSensorDaily: PresenceSensorDailyEntity[]; constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/libs/common/src/modules/presence-sensor/dtos/index.ts b/libs/common/src/modules/presence-sensor/dtos/index.ts new file mode 100644 index 0000000..9993c83 --- /dev/null +++ b/libs/common/src/modules/presence-sensor/dtos/index.ts @@ -0,0 +1 @@ +export * from './presence-sensor.dto'; diff --git a/libs/common/src/modules/presence-sensor/dtos/presence-sensor.dto.ts b/libs/common/src/modules/presence-sensor/dtos/presence-sensor.dto.ts new file mode 100644 index 0000000..e37f9db --- /dev/null +++ b/libs/common/src/modules/presence-sensor/dtos/presence-sensor.dto.ts @@ -0,0 +1,27 @@ +import { IsNotEmpty, IsNumber, IsString } from 'class-validator'; + +export class PresenceSensorDto { + @IsString() + @IsNotEmpty() + public uuid: string; + + @IsString() + @IsNotEmpty() + public deviceUuid: string; + + @IsString() + @IsNotEmpty() + public eventDate: string; + + @IsNumber() + @IsNotEmpty() + public CountMotionDetected: number; + + @IsNumber() + @IsNotEmpty() + public CountPresenceDetected: number; + + @IsNumber() + @IsNotEmpty() + public CountTotalPresenceDetected: number; +} diff --git a/libs/common/src/modules/presence-sensor/entities/index.ts b/libs/common/src/modules/presence-sensor/entities/index.ts new file mode 100644 index 0000000..b578244 --- /dev/null +++ b/libs/common/src/modules/presence-sensor/entities/index.ts @@ -0,0 +1 @@ +export * from './presence-sensor.entity'; diff --git a/libs/common/src/modules/presence-sensor/entities/presence-sensor.entity.ts b/libs/common/src/modules/presence-sensor/entities/presence-sensor.entity.ts new file mode 100644 index 0000000..710b245 --- /dev/null +++ b/libs/common/src/modules/presence-sensor/entities/presence-sensor.entity.ts @@ -0,0 +1,31 @@ +import { Column, Entity, ManyToOne, Unique } from 'typeorm'; +import { AbstractEntity } from '../../abstract/entities/abstract.entity'; +import { PresenceSensorDto } from '../dtos'; +import { DeviceEntity } from '../../device/entities/device.entity'; + +@Entity({ name: 'presence-sensor-daily-detection' }) +@Unique(['deviceUuid', 'eventDate']) +export class PresenceSensorDailyEntity extends AbstractEntity { + @Column({ nullable: false }) + public deviceUuid: string; + + @Column({ nullable: false, type: 'date' }) + public eventDate: string; + + @Column({ nullable: false }) + public CountMotionDetected: number; + + @Column({ nullable: false }) + public CountPresenceDetected: number; + + @Column({ nullable: false }) + public CountTotalPresenceDetected: number; + + @ManyToOne(() => DeviceEntity, (device) => device.presenceSensorDaily) + device: DeviceEntity; + + constructor(partial: Partial) { + super(); + Object.assign(this, partial); + } +} diff --git a/libs/common/src/modules/presence-sensor/presence-sensor.repository.module.ts b/libs/common/src/modules/presence-sensor/presence-sensor.repository.module.ts new file mode 100644 index 0000000..54849b2 --- /dev/null +++ b/libs/common/src/modules/presence-sensor/presence-sensor.repository.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { PresenceSensorDailyEntity } from './entities/presence-sensor.entity'; + +@Module({ + providers: [], + exports: [], + controllers: [], + imports: [TypeOrmModule.forFeature([PresenceSensorDailyEntity])], +}) +export class PresenceSensorRepositoryModule {} diff --git a/libs/common/src/modules/presence-sensor/repositories/index.ts b/libs/common/src/modules/presence-sensor/repositories/index.ts new file mode 100644 index 0000000..8b64ee8 --- /dev/null +++ b/libs/common/src/modules/presence-sensor/repositories/index.ts @@ -0,0 +1 @@ +export * from './presence-sensor.repository'; diff --git a/libs/common/src/modules/presence-sensor/repositories/presence-sensor.repository.ts b/libs/common/src/modules/presence-sensor/repositories/presence-sensor.repository.ts new file mode 100644 index 0000000..2dcd8bc --- /dev/null +++ b/libs/common/src/modules/presence-sensor/repositories/presence-sensor.repository.ts @@ -0,0 +1,10 @@ +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; +import { PresenceSensorDailyEntity } from '../entities'; + +@Injectable() +export class PresenceSensorDailyRepository extends Repository { + constructor(private dataSource: DataSource) { + super(PresenceSensorDailyEntity, dataSource.createEntityManager()); + } +}