mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 11:44:53 +00:00
Merge branch 'dev' of https://github.com/SyncrowIOT/backend into feature/space-management
This commit is contained in:
2
libs/common/src/constants/default.scene.icon.ts
Normal file
2
libs/common/src/constants/default.scene.icon.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export const defaultSceneIcon =
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAh9SURBVHgB3ZxfbFtXHce/59xrO7GTkLZUA6KpAa1aM62a80AZAymuxgs8sOZhvC7lCQkk2geg2UBJJMTgqZ2EBG/JkICtrdR24gGJSkklGgo8zBsP7WilOava/enauEsbx/a95+z3u+5N/D++9r3+k4+U2Nc+vvb9+vv7nd85514LtJH9f34QN2Q4rgXikHqfgIg7TwiMljTUSEml01qLtGGpd8wNpEKfWEv/fWUgiTYhECCj83q4fyA7pSFfoDeKa2AYTSKURngdaTOrlyKruQsK9tKV6V0pBEQgwhz4ayYB05ihuwkEgJHX6F+1IXP6vJGxX//Xy0Pn4TO+CnPgdG6KbmYqQiMgWKDImkLfqkpB23PL00ML8AlfhHEcYhjz7RKkHBYodseGmdG+CdSSMCTIKIXMPAIKGa9E79kUYopyNxaUzs21koOaFmbszMYxQM60klCDgJIzBj+yIPNIa9jHm3WPZ2EKPU2eBTmGLoVDa+g2iWPRhtCnLv9i4Dg84kkYDh1tGOcE1yFdTok4GkkbuUkvodWwME4+MYzFTiXYZigWhxyeorxzuFFxGhKmF0VxaVYcuV2DXhaFsUMCa18ynfvkglEpwovPvro6ut3rthWGc0qviuJiRQQe7ikcKotjIHwuMbtatzetK8zYmdzJXki0jbAxbCDf/yhz0DHl+0Mz9drXFIbL+27ukpvh4V4D2j1iLY598zcPah5f1eTLeUWYxtvdVrz5QV/aRuyuKmxo0FZuvFoyru4YKWf9FiVqoivgkFLuZxEYNkRovlq7Csc4I2SBefjE1/cK/OQpiUN7JTboi7p8V+MvN2ws39LoFOGHioYN9ua20OrwP6cHl4rbVPse6yYlLzz/FYHfP7f1Fn3kz+dJqKeHTby5R+H0dRt3M2g7uZgk19iFqphQQrIRvlrcpsQxfrvl4vdMjESr15Dsnps5OO75x4pqu0B9aUW5Zss1WttHiwec5TnGN7ccGBY1RXE+GL3z/j7gR08a+NU3TDw3EugsawXZIbnVQzHCKDn2zafGzmaP+FnIDYYaa/cYtZvYLXBi3MRvJ0w8ubs9ArEoG4NbynDh9+1X1xLu9uYzSmEKHcJ1z7Pksl+Se44eNLCnH4GTj5V+CVqITdc4wjh1ixAvoMOwe54mQSb3SfxuIoTvP7HtiKUl8v1l4QSRcIcKhYcNI4EuwXXPExHgxf2GE15B5p9cmWuy4dAU3zrCaOgj6DLYPfEocHBI4IcHC/kniPDK95W6Uko4kSMLG2ICXYhJX+bXIoXw4h6Ow8vv/LM5sHTRhdVRsf90Lm4IvA2f4Yr3TxP+jQMsKpQ/oLrnwzywTn8XV2y8dUPBD3a/n6eVzq1tbetxaQqVQA9Q7J5dYVBi9i//8HxNCVLFOS2Poof4glHIPV+mHPTF/kL++fF4a+GlKowt46YWeAY9huuePXRA1zeA8cek83f5lqLw8j7+snmHKB7UapoC7eE5l2L3MN8akfjZIe/hpcyy9kI+I7XWu9DDuO5hkRg3vL6zr/HiUBuVj0kIsQ87gMfDpducnBtFl2norCZghxArO5Iohdfjg833WDtGGNPnUcOOEea+Xbr9KfVMN9eanz6lOkavYAfAFXEx3G03Cy/ldsncffPwUIGnSO8+mr9dp9u3rvNke+PDBVkmKi0ypk3ab5LCsyd7Jg4fLvCyjyLmYko546d1y1sIybL2Wqn7JvXXqc4tZDSH65Lbj77p9+5pvHHNxs3PmjsSUWEukXQcgx6i2CWcYOf/ZznCtIKZq3h9klZXkGy8FOocxS7hPMJhw39ew6YaZrZsH4qEuf6DcHLsbC6tdfeOmYpdwgPFN6/6Iwhj0E5LQonWs5dfGUg6vRKJwuGUQJdR7BIOF+6CWw2bcsrdoqEuOY/zPwF1QUMm0EW4Lrm1XqhJvHS/XjA3yoUWzun3jjARM7KwYeVPogtwXXIj428eqUUoU7pvBWOJbx1hkpMifeBMjh9IoIO4LrnyscIbV3k9O9hCIpQhGYpE11pduDIdS/H9osrXnqNUlEAHcF2y+HEweaQW4bKxlJZ6wb1ferbD2dz7lH1G4QMjtJB18bvbjzjYJe8+AE7/36aZ/2DySC2GV6xNx1DflFo+Ed08FaT0kyvM+XUayK2H2kmcI9Hqz7su+ePV4PNINSKflYYRjQPmip+vPKPKR9ccorWl16usLbFL/nZb4w/v2oHnkVrUcwtTOR+j7aPwif/c0XjpkoVr6cIH4M/x9w8Lj/3631bHRNnOLUz1szbP5BYRQA8VpWm2dodMNUrdgoXlE7EKM1SfwTPtozwnAZ/pBlH67he5hY5RaTlXrV1VYa5N9qdUFXv1OjwhxefeuWilj1+Z7k9VbVtrJ++92HeKXPMadhB85e1mCGn92vL0wEKttnUnwyNGaFb02HxNLaIkSt+ae0a4SJIodS8HqCsMDxW0aU9SLKbQw5hZcsu9gijcNdsQk9u9ZtvlE843MOzDvSoO55WBjwoz5SyK0uJwrbxSTOOX/p3LjMI2Fv0q/toBi8JXt3Fe8SKK81o0iOucXsk5xaJwTvEiCtPUwiZNhZ6iWb+fokspcQr1PuFsbHZpVniqy5pe8eXrDoTEyW6bK+ZEyznFsHUatp67/PLAKTRBaz9hwHnHkrO0m5fQBXBVG/vUZpdcovp2ykvolOPPj17wVSsSM51KzE7Pc8eCmUFK0iC4/NqjZvD/Z1LaKBAve7BLqMxPCVvN1atkPe8bATB21jpC0xdT1EUGcn3CliDWJcols344pOI9ECBODsobCSH0EQgx0UqiZjHC6zodemC/E1pX5yOZ2ILXnsbT+6GNPHUuF9eWStBQf5R/swrOGaN6uOI8QK1XhO18uKS0dMq0dDJ6x04u/7x9P971OV/Bx65CjlpJAAAAAElFTkSuQmCC';
|
||||||
@ -15,4 +15,5 @@ export enum ProductType {
|
|||||||
WL = 'WL',
|
WL = 'WL',
|
||||||
GD = 'GD',
|
GD = 'GD',
|
||||||
CUR = 'CUR',
|
CUR = 'CUR',
|
||||||
|
PC = 'PC',
|
||||||
}
|
}
|
||||||
|
|||||||
4
libs/common/src/constants/secne-icon-type.enum.ts
Normal file
4
libs/common/src/constants/secne-icon-type.enum.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export enum SceneIconType {
|
||||||
|
Default = 'default',
|
||||||
|
Other = 'other',
|
||||||
|
}
|
||||||
7
libs/common/src/constants/source-type.enum.ts
Normal file
7
libs/common/src/constants/source-type.enum.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export enum SourceType {
|
||||||
|
Device = 1,
|
||||||
|
Client = 2,
|
||||||
|
ThirdParty = 3,
|
||||||
|
Cloud = 4,
|
||||||
|
Unknown = -1,
|
||||||
|
}
|
||||||
@ -20,6 +20,8 @@ import { RegionEntity } from '../modules/region/entities';
|
|||||||
import { TimeZoneEntity } from '../modules/timezone/entities';
|
import { TimeZoneEntity } from '../modules/timezone/entities';
|
||||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||||
import { CommunityEntity } from '../modules/community/entities';
|
import { CommunityEntity } from '../modules/community/entities';
|
||||||
|
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
||||||
|
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -55,6 +57,9 @@ import { CommunityEntity } from '../modules/community/entities';
|
|||||||
RegionEntity,
|
RegionEntity,
|
||||||
TimeZoneEntity,
|
TimeZoneEntity,
|
||||||
VisitorPasswordEntity,
|
VisitorPasswordEntity,
|
||||||
|
DeviceStatusLogEntity,
|
||||||
|
SceneEntity,
|
||||||
|
SceneIconEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
|
|||||||
@ -2,10 +2,15 @@ import { Module } from '@nestjs/common';
|
|||||||
import { DeviceStatusFirebaseController } from './controllers/devices-status.controller';
|
import { DeviceStatusFirebaseController } from './controllers/devices-status.controller';
|
||||||
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories/device-status.repository';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [DeviceStatusFirebaseService, DeviceRepository],
|
providers: [
|
||||||
|
DeviceStatusFirebaseService,
|
||||||
|
DeviceRepository,
|
||||||
|
DeviceStatusLogRepository,
|
||||||
|
],
|
||||||
controllers: [DeviceStatusFirebaseController],
|
controllers: [DeviceStatusFirebaseController],
|
||||||
exports: [DeviceStatusFirebaseService],
|
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
||||||
})
|
})
|
||||||
export class DeviceStatusFirebaseModule {}
|
export class DeviceStatusFirebaseModule {}
|
||||||
|
|||||||
@ -35,4 +35,7 @@ export class AddDeviceStatusDto {
|
|||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => StatusDto)
|
@Type(() => StatusDto)
|
||||||
status: StatusDto[];
|
status: StatusDto[];
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
log?: any;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { firebaseDataBase } from '../../firebase.config';
|
import { firebaseDataBase } from '../../firebase.config';
|
||||||
import { Database, DataSnapshot, get, ref, set } from 'firebase/database';
|
import { Database, DataSnapshot, get, ref, set } from 'firebase/database';
|
||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -18,6 +19,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
|
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
@ -175,6 +177,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Update or add status codes
|
// Update or add status codes
|
||||||
|
|
||||||
for (const statusItem of addDeviceStatusDto.status) {
|
for (const statusItem of addDeviceStatusDto.status) {
|
||||||
statusMap.set(statusItem.code, statusItem.value);
|
statusMap.set(statusItem.code, statusItem.value);
|
||||||
}
|
}
|
||||||
@ -184,7 +187,19 @@ export class DeviceStatusFirebaseService {
|
|||||||
code,
|
code,
|
||||||
value,
|
value,
|
||||||
}));
|
}));
|
||||||
|
const newLog = this.deviceStatusLogRepository.create({
|
||||||
|
deviceId: addDeviceStatusDto.deviceUuid,
|
||||||
|
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
||||||
|
productId: addDeviceStatusDto.log.productId,
|
||||||
|
log: addDeviceStatusDto.log,
|
||||||
|
code: existingData.status[0].code,
|
||||||
|
value: existingData.status[0].value,
|
||||||
|
eventId: addDeviceStatusDto.log.dataId,
|
||||||
|
eventTime: new Date(
|
||||||
|
addDeviceStatusDto.log.properties[0].time,
|
||||||
|
).toISOString(),
|
||||||
|
});
|
||||||
|
await this.deviceStatusLogRepository.save(newLog);
|
||||||
// Save the updated data to Firebase
|
// Save the updated data to Firebase
|
||||||
await set(dataRef, existingData);
|
await set(dataRef, existingData);
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@ export class TuyaWebSocketService {
|
|||||||
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
|
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
|
||||||
accessKey: this.configService.get<string>('tuya-config.TUYA_ACCESS_KEY'),
|
accessKey: this.configService.get<string>('tuya-config.TUYA_ACCESS_KEY'),
|
||||||
url: TuyaWebsocket.URL.EU,
|
url: TuyaWebsocket.URL.EU,
|
||||||
env: TuyaWebsocket.env.TEST,
|
env: TuyaWebsocket.env.PROD,
|
||||||
maxRetryTimes: 100,
|
maxRetryTimes: 100,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -40,6 +40,7 @@ export class TuyaWebSocketService {
|
|||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: message.payload.data.bizData.devId,
|
deviceTuyaUuid: message.payload.data.bizData.devId,
|
||||||
status: message.payload.data.bizData.properties,
|
status: message.payload.data.bizData.properties,
|
||||||
|
log: message.payload.data.bizData,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
|
|||||||
@ -0,0 +1,43 @@
|
|||||||
|
import { SourceType } from '@app/common/constants/source-type.enum';
|
||||||
|
import { Entity, Column, PrimaryColumn, Unique } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity('device-status-log')
|
||||||
|
@Unique('event_time_idx', ['eventTime'])
|
||||||
|
export class DeviceStatusLogEntity {
|
||||||
|
@Column({ type: 'int', generated: true, unsigned: true })
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
eventId: string;
|
||||||
|
|
||||||
|
@PrimaryColumn({ type: 'timestamptz' })
|
||||||
|
eventTime: Date;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'enum',
|
||||||
|
enum: SourceType,
|
||||||
|
default: SourceType.Device,
|
||||||
|
})
|
||||||
|
eventFrom: SourceType;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
deviceId: string;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
deviceTuyaId: string;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
productId: string;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
code: any;
|
||||||
|
|
||||||
|
@Column({ type: 'text' })
|
||||||
|
value: any;
|
||||||
|
|
||||||
|
@Column({ type: 'jsonb' })
|
||||||
|
log: any;
|
||||||
|
|
||||||
|
@Column({ type: 'timestamptz', default: new Date() })
|
||||||
|
ingestionTime: Date;
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export * from './device-status-log.entity';
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { DeviceStatusLogEntity } from '../entities/device-status-log.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DeviceStatusLogRepository extends Repository<DeviceStatusLogEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(DeviceStatusLogEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export * from './device-status.repository';
|
||||||
1
libs/common/src/modules/scene/dtos/index.ts
Normal file
1
libs/common/src/modules/scene/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.dto';
|
||||||
30
libs/common/src/modules/scene/dtos/scene.dto.ts
Normal file
30
libs/common/src/modules/scene/dtos/scene.dto.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class SceneDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public sceneTuyaUuid: string;
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public unitUuid: string;
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public iconUuid: string;
|
||||||
|
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public inHomePage: boolean;
|
||||||
|
}
|
||||||
|
export class SceneIconDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public icon: string;
|
||||||
|
}
|
||||||
1
libs/common/src/modules/scene/entities/index.ts
Normal file
1
libs/common/src/modules/scene/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.entity';
|
||||||
65
libs/common/src/modules/scene/entities/scene.entity.ts
Normal file
65
libs/common/src/modules/scene/entities/scene.entity.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
||||||
|
import { SceneDto, SceneIconDto } from '../dtos';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||||
|
|
||||||
|
// Define SceneIconEntity before SceneEntity
|
||||||
|
@Entity({ name: 'scene-icon' })
|
||||||
|
export class SceneIconEntity extends AbstractEntity<SceneIconDto> {
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: 'text',
|
||||||
|
})
|
||||||
|
public icon: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'enum',
|
||||||
|
enum: SceneIconType,
|
||||||
|
default: SceneIconType.Other,
|
||||||
|
})
|
||||||
|
iconType: SceneIconType;
|
||||||
|
|
||||||
|
@OneToMany(
|
||||||
|
() => SceneEntity,
|
||||||
|
(scenesIconEntity) => scenesIconEntity.sceneIcon,
|
||||||
|
)
|
||||||
|
scenesIconEntity: SceneEntity[];
|
||||||
|
|
||||||
|
constructor(partial: Partial<SceneIconEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity({ name: 'scene' })
|
||||||
|
export class SceneEntity extends AbstractEntity<SceneDto> {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()',
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
sceneTuyaUuid: string;
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
unitUuid: string;
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
showInHomePage: boolean;
|
||||||
|
|
||||||
|
@ManyToOne(() => SceneIconEntity, (icon) => icon.scenesIconEntity, {
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
sceneIcon: SceneIconEntity;
|
||||||
|
|
||||||
|
constructor(partial: Partial<SceneEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
libs/common/src/modules/scene/repositories/index.ts
Normal file
1
libs/common/src/modules/scene/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.repository';
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { SceneEntity, SceneIconEntity } from '../entities';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SceneRepository extends Repository<SceneEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(SceneEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SceneIconRepository extends Repository<SceneIconEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(SceneIconEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
11
libs/common/src/modules/scene/scene.repository.module.ts
Normal file
11
libs/common/src/modules/scene/scene.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { SceneEntity, SceneIconEntity } from './entities';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([SceneEntity, SceneIconEntity])],
|
||||||
|
})
|
||||||
|
export class SceneRepositoryModule {}
|
||||||
@ -18,6 +18,8 @@ import { RegionSeeder } from './services/regions.seeder';
|
|||||||
import { RegionRepository } from '../modules/region/repositories';
|
import { RegionRepository } from '../modules/region/repositories';
|
||||||
import { TimeZoneSeeder } from './services/timezone.seeder';
|
import { TimeZoneSeeder } from './services/timezone.seeder';
|
||||||
import { TimeZoneRepository } from '../modules/timezone/repositories';
|
import { TimeZoneRepository } from '../modules/timezone/repositories';
|
||||||
|
import { SceneIconSeeder } from './services/scene.icon.seeder';
|
||||||
|
import { SceneIconRepository } from '../modules/scene/repositories';
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
@ -35,6 +37,8 @@ import { TimeZoneRepository } from '../modules/timezone/repositories';
|
|||||||
RegionRepository,
|
RegionRepository,
|
||||||
TimeZoneSeeder,
|
TimeZoneSeeder,
|
||||||
TimeZoneRepository,
|
TimeZoneRepository,
|
||||||
|
SceneIconSeeder,
|
||||||
|
SceneIconRepository,
|
||||||
],
|
],
|
||||||
exports: [SeederService],
|
exports: [SeederService],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
|
|||||||
38
libs/common/src/seed/services/scene.icon.seeder.ts
Normal file
38
libs/common/src/seed/services/scene.icon.seeder.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { SceneIconRepository } from '@app/common/modules/scene/repositories';
|
||||||
|
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||||
|
import { defaultSceneIcon } from '@app/common/constants/default.scene.icon';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SceneIconSeeder {
|
||||||
|
constructor(private readonly sceneIconRepository: SceneIconRepository) {}
|
||||||
|
|
||||||
|
async createSceneDefaultIconIfNotFound(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const defaultSceneIconData = await this.sceneIconRepository.find({
|
||||||
|
where: { iconType: SceneIconType.Default },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (defaultSceneIconData.length <= 0) {
|
||||||
|
console.log('Creating default scene icon...');
|
||||||
|
|
||||||
|
await this.createDefaultSceneIcon();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error while checking default scene icon:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createDefaultSceneIcon(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.sceneIconRepository.save({
|
||||||
|
icon: defaultSceneIcon,
|
||||||
|
iconType: SceneIconType.Default,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error while creating default scene icon:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@ import { SpaceTypeSeeder } from './space.type.seeder';
|
|||||||
import { SuperAdminSeeder } from './supper.admin.seeder';
|
import { SuperAdminSeeder } from './supper.admin.seeder';
|
||||||
import { RegionSeeder } from './regions.seeder';
|
import { RegionSeeder } from './regions.seeder';
|
||||||
import { TimeZoneSeeder } from './timezone.seeder';
|
import { TimeZoneSeeder } from './timezone.seeder';
|
||||||
|
import { SceneIconSeeder } from './scene.icon.seeder';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SeederService {
|
export class SeederService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -14,6 +15,7 @@ export class SeederService {
|
|||||||
private readonly regionSeeder: RegionSeeder,
|
private readonly regionSeeder: RegionSeeder,
|
||||||
private readonly timeZoneSeeder: TimeZoneSeeder,
|
private readonly timeZoneSeeder: TimeZoneSeeder,
|
||||||
private readonly superAdminSeeder: SuperAdminSeeder,
|
private readonly superAdminSeeder: SuperAdminSeeder,
|
||||||
|
private readonly sceneIconSeeder: SceneIconSeeder,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async seed() {
|
async seed() {
|
||||||
@ -23,5 +25,6 @@ export class SeederService {
|
|||||||
await this.regionSeeder.addRegionDataIfNotFound();
|
await this.regionSeeder.addRegionDataIfNotFound();
|
||||||
await this.timeZoneSeeder.addTimeZoneDataIfNotFound();
|
await this.timeZoneSeeder.addTimeZoneDataIfNotFound();
|
||||||
await this.superAdminSeeder.createSuperAdminIfNotFound();
|
await this.superAdminSeeder.createSuperAdminIfNotFound();
|
||||||
|
await this.sceneIconSeeder.createSceneDefaultIconIfNotFound();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
BadRequestException,
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
@ -16,6 +17,7 @@ import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
|||||||
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
||||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { OtpType } from '@app/common/constants/otp-type.enum';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
version: EnableDisableStatusEnum.ENABLED,
|
version: EnableDisableStatusEnum.ENABLED,
|
||||||
@ -74,12 +76,27 @@ export class UserAuthController {
|
|||||||
|
|
||||||
@Post('user/forget-password')
|
@Post('user/forget-password')
|
||||||
async forgetPassword(@Body() forgetPasswordDto: ForgetPasswordDto) {
|
async forgetPassword(@Body() forgetPasswordDto: ForgetPasswordDto) {
|
||||||
await this.userAuthService.forgetPassword(forgetPasswordDto);
|
const otpResult = await this.userAuthService.verifyOTP(
|
||||||
return {
|
{
|
||||||
statusCode: HttpStatus.OK,
|
otpCode: forgetPasswordDto.otpCode,
|
||||||
|
email: forgetPasswordDto.email,
|
||||||
|
type: OtpType.PASSWORD,
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
if (otpResult) {
|
||||||
|
await this.userAuthService.forgetPassword(forgetPasswordDto);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
data: {},
|
||||||
|
message: 'Password changed successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
throw new BadRequestException({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
data: {},
|
data: {},
|
||||||
message: 'Password changed successfully',
|
message: 'Otp is incorrect',
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
|
|||||||
@ -22,4 +22,9 @@ export class ForgetPasswordDto {
|
|||||||
'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.',
|
'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.',
|
||||||
})
|
})
|
||||||
public password: string;
|
public password: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public otpCode: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import * as argon2 from 'argon2';
|
|||||||
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
||||||
import { LessThan, MoreThan } from 'typeorm';
|
import { LessThan, MoreThan } from 'typeorm';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { UUID } from 'typeorm/driver/mongodb/bson.typings';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserAuthService {
|
export class UserAuthService {
|
||||||
@ -211,7 +210,7 @@ export class UserAuthService {
|
|||||||
}
|
}
|
||||||
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
|
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
|
||||||
const expiryTime = new Date();
|
const expiryTime = new Date();
|
||||||
expiryTime.setMinutes(expiryTime.getMinutes() + 1);
|
expiryTime.setMinutes(expiryTime.getMinutes() + 10);
|
||||||
await this.otpRepository.save({
|
await this.otpRepository.save({
|
||||||
email: data.email,
|
email: data.email,
|
||||||
otpCode,
|
otpCode,
|
||||||
@ -233,7 +232,10 @@ export class UserAuthService {
|
|||||||
return { otpCode, cooldown };
|
return { otpCode, cooldown };
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyOTP(data: VerifyOtpDto): Promise<boolean> {
|
async verifyOTP(
|
||||||
|
data: VerifyOtpDto,
|
||||||
|
fromNewPassword: boolean = false,
|
||||||
|
): Promise<boolean> {
|
||||||
const otp = await this.otpRepository.findOne({
|
const otp = await this.otpRepository.findOne({
|
||||||
where: { email: data.email, type: data.type },
|
where: { email: data.email, type: data.type },
|
||||||
});
|
});
|
||||||
@ -258,7 +260,9 @@ export class UserAuthService {
|
|||||||
await this.otpRepository.delete(otp.uuid);
|
await this.otpRepository.delete(otp.uuid);
|
||||||
throw new BadRequestException('OTP expired');
|
throw new BadRequestException('OTP expired');
|
||||||
}
|
}
|
||||||
|
if (fromNewPassword) {
|
||||||
|
await this.otpRepository.delete(otp.uuid);
|
||||||
|
}
|
||||||
if (data.type == OtpType.VERIFICATION) {
|
if (data.type == OtpType.VERIFICATION) {
|
||||||
await this.userRepository.update(
|
await this.userRepository.update(
|
||||||
{ email: data.email },
|
{ email: data.email },
|
||||||
|
|||||||
@ -173,4 +173,14 @@ export class DeviceController {
|
|||||||
batchFactoryResetDevicesDto,
|
batchFactoryResetDevicesDto,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get(':powerClampUuid/power-clamp/status')
|
||||||
|
async getPowerClampInstructionStatus(
|
||||||
|
@Param('powerClampUuid') powerClampUuid: string,
|
||||||
|
) {
|
||||||
|
return await this.deviceService.getPowerClampInstructionStatus(
|
||||||
|
powerClampUuid,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,3 +70,10 @@ export interface getDeviceLogsInterface {
|
|||||||
endTime: string;
|
endTime: string;
|
||||||
deviceUuid?: string;
|
deviceUuid?: string;
|
||||||
}
|
}
|
||||||
|
export interface GetPowerClampFunctionsStatusInterface {
|
||||||
|
result: {
|
||||||
|
properties: [];
|
||||||
|
};
|
||||||
|
success: boolean;
|
||||||
|
msg: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import {
|
|||||||
GetDeviceDetailsFunctionsInterface,
|
GetDeviceDetailsFunctionsInterface,
|
||||||
GetDeviceDetailsFunctionsStatusInterface,
|
GetDeviceDetailsFunctionsStatusInterface,
|
||||||
GetDeviceDetailsInterface,
|
GetDeviceDetailsInterface,
|
||||||
|
GetPowerClampFunctionsStatusInterface,
|
||||||
controlDeviceInterface,
|
controlDeviceInterface,
|
||||||
getDeviceLogsInterface,
|
getDeviceLogsInterface,
|
||||||
updateDeviceFirmwareInterface,
|
updateDeviceFirmwareInterface,
|
||||||
@ -985,7 +986,6 @@ export class DeviceService {
|
|||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
): Promise<{ uuid: string; spaceName: string }[]> {
|
): Promise<{ uuid: string; spaceName: string }[]> {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Fetch only the relevant spaces, starting with the target space
|
// Fetch only the relevant spaces, starting with the target space
|
||||||
const targetSpace = await this.spaceRepository.findOne({
|
const targetSpace = await this.spaceRepository.findOne({
|
||||||
where: { uuid: space.uuid },
|
where: { uuid: space.uuid },
|
||||||
@ -1015,6 +1015,84 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPowerClampInstructionStatus(powerClampUuid: string) {
|
||||||
|
try {
|
||||||
|
const deviceDetails = await this.getDeviceByDeviceUuid(powerClampUuid);
|
||||||
|
if (!deviceDetails) {
|
||||||
|
throw new NotFoundException('Device Not Found');
|
||||||
|
} else if (deviceDetails.productDevice.prodType !== ProductType.PC) {
|
||||||
|
throw new BadRequestException('This is not a power clamp device');
|
||||||
|
}
|
||||||
|
const deviceStatus = await this.getPowerClampInstructionStatusTuya(
|
||||||
|
deviceDetails.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
const statusList = deviceStatus.result.properties as {
|
||||||
|
code: string;
|
||||||
|
value: any;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const groupedStatus = statusList.reduce(
|
||||||
|
(acc, currentStatus) => {
|
||||||
|
const { code } = currentStatus;
|
||||||
|
|
||||||
|
if (code.endsWith('A')) {
|
||||||
|
acc.phaseA.push(currentStatus);
|
||||||
|
} else if (code.endsWith('B')) {
|
||||||
|
acc.phaseB.push(currentStatus);
|
||||||
|
} else if (code.endsWith('C')) {
|
||||||
|
acc.phaseC.push(currentStatus);
|
||||||
|
} else {
|
||||||
|
acc.general.push(currentStatus);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
phaseA: [] as { code: string; value: any }[],
|
||||||
|
phaseB: [] as { code: string; value: any }[],
|
||||||
|
phaseC: [] as { code: string; value: any }[],
|
||||||
|
general: [] as { code: string; value: any }[],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
productUuid: deviceDetails.productDevice.uuid,
|
||||||
|
productType: deviceDetails.productDevice.prodType,
|
||||||
|
status: {
|
||||||
|
phaseA: groupedStatus.phaseA,
|
||||||
|
phaseB: groupedStatus.phaseB,
|
||||||
|
phaseC: groupedStatus.phaseC,
|
||||||
|
general: groupedStatus.general,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Error fetching power clamp functions status',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getPowerClampInstructionStatusTuya(
|
||||||
|
deviceUuid: string,
|
||||||
|
): Promise<GetPowerClampFunctionsStatusInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/thing/${deviceUuid}/shadow/properties`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
query: {
|
||||||
|
device_ids: deviceUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const camelCaseResponse = convertKeysToCamelCase(response);
|
||||||
|
return camelCaseResponse as GetPowerClampFunctionsStatusInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching power clamp functions status from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async fetchAncestors(space: SpaceEntity): Promise<SpaceEntity[]> {
|
private async fetchAncestors(space: SpaceEntity): Promise<SpaceEntity[]> {
|
||||||
const ancestors: SpaceEntity[] = [];
|
const ancestors: SpaceEntity[] = [];
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [DoorLockController],
|
controllers: [DoorLockController],
|
||||||
@ -22,6 +23,7 @@ import { SpaceRepository } from '@app/common/modules/space/repositories';
|
|||||||
ProductRepository,
|
ProductRepository,
|
||||||
DeviceStatusFirebaseService,
|
DeviceStatusFirebaseService,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
|
DeviceStatusLogRepository,
|
||||||
],
|
],
|
||||||
exports: [DoorLockService],
|
exports: [DoorLockService],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -8,10 +8,16 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos/scene.dto';
|
import {
|
||||||
|
AddSceneIconDto,
|
||||||
|
AddSceneTapToRunDto,
|
||||||
|
GetSceneDto,
|
||||||
|
UpdateSceneTapToRunDto,
|
||||||
|
} from '../dtos/scene.dto';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
import { DeleteSceneParamDto, SceneParamDto, SpaceParamDto } from '../dtos';
|
import { DeleteSceneParamDto, SceneParamDto, SpaceParamDto } from '../dtos';
|
||||||
@ -41,9 +47,13 @@ export class SceneController {
|
|||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('tap-to-run/:spaceUuid')
|
@Get('tap-to-run/:spaceUuid')
|
||||||
async getTapToRunSceneByUnit(@Param() param: SpaceParamDto) {
|
async getTapToRunSceneByUnit(
|
||||||
|
@Param() param: SpaceParamDto,
|
||||||
|
@Query() inHomePage: GetSceneDto,
|
||||||
|
) {
|
||||||
const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(
|
const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(
|
||||||
param.spaceUuid,
|
param.spaceUuid,
|
||||||
|
inHomePage,
|
||||||
);
|
);
|
||||||
return tapToRunScenes;
|
return tapToRunScenes;
|
||||||
}
|
}
|
||||||
@ -98,4 +108,23 @@ export class SceneController {
|
|||||||
data: tapToRunScene,
|
data: tapToRunScene,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('icon')
|
||||||
|
async addSceneIcon(@Body() addSceneIconDto: AddSceneIconDto) {
|
||||||
|
const tapToRunScene = await this.sceneService.addSceneIcon(addSceneIconDto);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Icon added successfully',
|
||||||
|
data: tapToRunScene,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('icon')
|
||||||
|
async getAllIcons() {
|
||||||
|
const icons = await this.sceneService.getAllIcons();
|
||||||
|
return icons;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,8 +6,10 @@ import {
|
|||||||
ValidateNested,
|
ValidateNested,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
|
IsBoolean,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Transform, Type } from 'class-transformer';
|
||||||
|
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||||
|
|
||||||
class ExecutorProperty {
|
class ExecutorProperty {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -71,6 +73,20 @@ export class AddSceneTapToRunDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public spaceUuid: string;
|
public spaceUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon UUID',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
public iconUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'show in home page',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public showInHomePage: boolean;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Scene name',
|
description: 'Scene name',
|
||||||
required: true,
|
required: true,
|
||||||
@ -109,7 +125,20 @@ export class UpdateSceneTapToRunDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public sceneName: string;
|
public sceneName: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon UUID',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public iconUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'show in home page',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public showInHomePage: boolean;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Decision expression',
|
description: 'Decision expression',
|
||||||
required: true,
|
required: true,
|
||||||
@ -132,3 +161,30 @@ export class UpdateSceneTapToRunDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddSceneIconDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public icon: string;
|
||||||
|
|
||||||
|
constructor(dto: Partial<AddSceneIconDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class GetSceneDto {
|
||||||
|
@ApiProperty({
|
||||||
|
example: true,
|
||||||
|
description: 'showInHomePage',
|
||||||
|
required: true,
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsBoolean()
|
||||||
|
@Transform((value) => {
|
||||||
|
return value.obj.showInHomePage === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
public showInHomePage: boolean = false;
|
||||||
|
}
|
||||||
|
|||||||
@ -8,6 +8,10 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
||||||
|
import {
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
|
} from '@app/common/modules/scene/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||||
@ -18,6 +22,8 @@ import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/
|
|||||||
DeviceService,
|
DeviceService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
ProductRepository,
|
ProductRepository,
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
],
|
],
|
||||||
exports: [SceneService],
|
exports: [SceneService],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -6,8 +6,10 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import {
|
import {
|
||||||
|
AddSceneIconDto,
|
||||||
AddSceneTapToRunDto,
|
AddSceneTapToRunDto,
|
||||||
DeleteSceneParamDto,
|
DeleteSceneParamDto,
|
||||||
|
GetSceneDto,
|
||||||
UpdateSceneTapToRunDto,
|
UpdateSceneTapToRunDto,
|
||||||
} from '../dtos';
|
} from '../dtos';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
@ -17,11 +19,15 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import {
|
import {
|
||||||
AddTapToRunSceneInterface,
|
AddTapToRunSceneInterface,
|
||||||
DeleteTapToRunSceneInterface,
|
DeleteTapToRunSceneInterface,
|
||||||
GetTapToRunSceneByUnitInterface,
|
|
||||||
SceneDetailsResult,
|
SceneDetailsResult,
|
||||||
} from '../interface/scene.interface';
|
} from '../interface/scene.interface';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { ActionExecutorEnum } from '@app/common/constants/automation.enum';
|
import { ActionExecutorEnum } from '@app/common/constants/automation.enum';
|
||||||
|
import {
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
|
} from '@app/common/modules/scene/repositories';
|
||||||
|
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SceneService {
|
export class SceneService {
|
||||||
@ -29,6 +35,8 @@ export class SceneService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly sceneIconRepository: SceneIconRepository,
|
||||||
|
private readonly sceneRepository: SceneRepository,
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -44,6 +52,7 @@ export class SceneService {
|
|||||||
async addTapToRunScene(
|
async addTapToRunScene(
|
||||||
addSceneTapToRunDto: AddSceneTapToRunDto,
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
spaceTuyaId = null,
|
spaceTuyaId = null,
|
||||||
|
spaceUuid?: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
let unitSpaceTuyaId;
|
let unitSpaceTuyaId;
|
||||||
@ -91,6 +100,21 @@ export class SceneService {
|
|||||||
});
|
});
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||||
|
} else {
|
||||||
|
const defaultSceneIcon = await this.sceneIconRepository.findOne({
|
||||||
|
where: { iconType: SceneIconType.Default },
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.sceneRepository.save({
|
||||||
|
sceneTuyaUuid: response.result.id,
|
||||||
|
sceneIcon: {
|
||||||
|
uuid: addSceneTapToRunDto.iconUuid
|
||||||
|
? addSceneTapToRunDto.iconUuid
|
||||||
|
: defaultSceneIcon.uuid,
|
||||||
|
},
|
||||||
|
showInHomePage: addSceneTapToRunDto.showInHomePage,
|
||||||
|
unitUuid: spaceUuid ? spaceUuid : addSceneTapToRunDto.spaceUuid,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
id: response.result.id,
|
id: response.result.id,
|
||||||
@ -136,33 +160,32 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getTapToRunSceneByUnit(spaceUuid: string) {
|
async getTapToRunSceneByUnit(unitUuid: string, inHomePage: GetSceneDto) {
|
||||||
try {
|
try {
|
||||||
const space = await this.getSpaceByUuid(spaceUuid);
|
const showInHomePage = inHomePage?.showInHomePage;
|
||||||
if (!space.spaceTuyaUuid) {
|
|
||||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?space_id=${space.spaceTuyaUuid}&type=scene`;
|
const scenesData = await this.sceneRepository.find({
|
||||||
const response: GetTapToRunSceneByUnitInterface = await this.tuya.request(
|
where: {
|
||||||
{
|
unitUuid,
|
||||||
method: 'GET',
|
...(showInHomePage ? { showInHomePage } : {}),
|
||||||
path,
|
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const scenes = await Promise.all(
|
||||||
|
scenesData.map(async (scene) => {
|
||||||
|
const sceneData = await this.getTapToRunSceneDetails(
|
||||||
|
scene.sceneTuyaUuid,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { actions, ...rest } = sceneData;
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
};
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!response.success) {
|
return scenes;
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.result.list.map((item) => {
|
|
||||||
return {
|
|
||||||
id: item.id,
|
|
||||||
name: item.name,
|
|
||||||
status: item.status,
|
|
||||||
type: 'tap_to_run',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
@ -196,6 +219,8 @@ export class SceneService {
|
|||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
await this.sceneRepository.delete({ sceneTuyaUuid: sceneUuid });
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@ -273,11 +298,17 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: { sceneTuyaUuid: sceneId },
|
||||||
|
relations: ['sceneIcon'],
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
id: responseData.id,
|
id: responseData.id,
|
||||||
name: responseData.name,
|
name: responseData.name,
|
||||||
status: responseData.status,
|
status: responseData.status,
|
||||||
|
icon: scene.sceneIcon?.icon,
|
||||||
|
iconUuid: scene.sceneIcon?.uuid,
|
||||||
|
showInHome: scene.showInHomePage,
|
||||||
type: 'tap_to_run',
|
type: 'tap_to_run',
|
||||||
actions: actions,
|
actions: actions,
|
||||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||||
@ -334,10 +365,17 @@ export class SceneService {
|
|||||||
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
||||||
...updateSceneTapToRunDto,
|
...updateSceneTapToRunDto,
|
||||||
spaceUuid: null,
|
spaceUuid: null,
|
||||||
|
iconUuid: updateSceneTapToRunDto.iconUuid,
|
||||||
|
showInHomePage: updateSceneTapToRunDto.showInHomePage,
|
||||||
};
|
};
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: { sceneTuyaUuid: sceneUuid },
|
||||||
|
});
|
||||||
|
|
||||||
const newTapToRunScene = await this.addTapToRunScene(
|
const newTapToRunScene = await this.addTapToRunScene(
|
||||||
addSceneTapToRunDto,
|
addSceneTapToRunDto,
|
||||||
spaceTuyaId.spaceId,
|
spaceTuyaId.spaceId,
|
||||||
|
scene.unitUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const param: DeleteSceneParamDto = {
|
const param: DeleteSceneParamDto = {
|
||||||
@ -347,6 +385,18 @@ export class SceneService {
|
|||||||
|
|
||||||
if (newTapToRunScene.id) {
|
if (newTapToRunScene.id) {
|
||||||
await this.deleteTapToRunScene(null, param);
|
await this.deleteTapToRunScene(null, param);
|
||||||
|
|
||||||
|
await this.sceneRepository.update(
|
||||||
|
{ sceneTuyaUuid: sceneUuid },
|
||||||
|
{
|
||||||
|
sceneTuyaUuid: newTapToRunScene.id,
|
||||||
|
showInHomePage: addSceneTapToRunDto.showInHomePage,
|
||||||
|
sceneIcon: {
|
||||||
|
uuid: addSceneTapToRunDto.iconUuid,
|
||||||
|
},
|
||||||
|
unitUuid: scene.unitUuid,
|
||||||
|
},
|
||||||
|
);
|
||||||
return newTapToRunScene;
|
return newTapToRunScene;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -360,4 +410,38 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async addSceneIcon(addSceneIconDto: AddSceneIconDto) {
|
||||||
|
try {
|
||||||
|
const icon = await this.sceneIconRepository.save({
|
||||||
|
icon: addSceneIconDto.icon,
|
||||||
|
});
|
||||||
|
return icon;
|
||||||
|
} catch (err) {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
error: 'Failed to add scene icon',
|
||||||
|
message: err.message || 'Unexpected error occurred',
|
||||||
|
},
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getAllIcons() {
|
||||||
|
try {
|
||||||
|
const icons = await this.sceneIconRepository.find();
|
||||||
|
return icons;
|
||||||
|
} catch (err) {
|
||||||
|
// Improved error handling
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
message: 'Failed to fetch icons',
|
||||||
|
error:
|
||||||
|
err.message || 'An unexpected error occurred while fetching icons',
|
||||||
|
},
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { ProductRepository } from '@app/common/modules/product/repositories';
|
|||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/repositories';
|
import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/repositories';
|
||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||||
controllers: [VisitorPasswordController],
|
controllers: [VisitorPasswordController],
|
||||||
@ -25,6 +26,7 @@ import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/
|
|||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
VisitorPasswordRepository,
|
VisitorPasswordRepository,
|
||||||
|
DeviceStatusLogRepository,
|
||||||
],
|
],
|
||||||
exports: [VisitorPasswordService],
|
exports: [VisitorPasswordService],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user