mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
Merge pull request #149 from SyncrowIOT/SP-845-be-implement-the-6-scene-device-configuration
Sp 845 be implement the 6 scene device configuration
This commit is contained in:
@ -7,3 +7,13 @@ export enum ActionExecutorEnum {
|
|||||||
export enum EntityTypeEnum {
|
export enum EntityTypeEnum {
|
||||||
DEVICE_REPORT = 'device_report',
|
DEVICE_REPORT = 'device_report',
|
||||||
}
|
}
|
||||||
|
export const AUTOMATION_CONFIG = {
|
||||||
|
DEFAULT_START_TIME: '00:00',
|
||||||
|
DEFAULT_END_TIME: '23:59',
|
||||||
|
DEFAULT_LOOPS: '1111111',
|
||||||
|
DECISION_EXPR: 'and',
|
||||||
|
CONDITION_TYPE: 'device_report',
|
||||||
|
ACTION_EXECUTOR: 'rule_trigger',
|
||||||
|
COMPARATOR: '==',
|
||||||
|
SCENE_STATUS_VALUE: 'scene',
|
||||||
|
};
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
export enum FourSceneSwitchesEnum {
|
|
||||||
Scene_1 = 'scene_1',
|
|
||||||
Scene_2 = 'scene_2',
|
|
||||||
Scene_3 = 'scene_3',
|
|
||||||
Scene_4 = 'scene_4',
|
|
||||||
}
|
|
||||||
@ -16,4 +16,6 @@ export enum ProductType {
|
|||||||
GD = 'GD',
|
GD = 'GD',
|
||||||
CUR = 'CUR',
|
CUR = 'CUR',
|
||||||
PC = 'PC',
|
PC = 'PC',
|
||||||
|
FOUR_S = '4S',
|
||||||
|
SIX_S = '6S',
|
||||||
}
|
}
|
||||||
|
|||||||
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export enum SceneSwitchesTypeEnum {
|
||||||
|
SCENE_1 = 'scene_1',
|
||||||
|
SCENE_2 = 'scene_2',
|
||||||
|
SCENE_3 = 'scene_3',
|
||||||
|
SCENE_4 = 'scene_4',
|
||||||
|
SCENE_5 = 'scene_5',
|
||||||
|
SCENE_6 = 'scene_6',
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import { Column, Entity, JoinColumn, ManyToOne, Unique } from 'typeorm';
|
import { Column, Entity, JoinColumn, ManyToOne, Unique } from 'typeorm';
|
||||||
import { SceneDeviceDto } from '../dtos';
|
import { SceneDeviceDto } from '../dtos';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
import { DeviceEntity } from '../../device/entities';
|
import { DeviceEntity } from '../../device/entities';
|
||||||
import { SceneEntity } from '../../scene/entities';
|
import { SceneEntity } from '../../scene/entities';
|
||||||
|
|
||||||
@ -29,9 +29,9 @@ export class SceneDeviceEntity extends AbstractEntity<SceneDeviceDto> {
|
|||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
type: 'enum',
|
type: 'enum',
|
||||||
enum: FourSceneSwitchesEnum,
|
enum: SceneSwitchesTypeEnum,
|
||||||
})
|
})
|
||||||
switchName: FourSceneSwitchesEnum;
|
switchName: SceneSwitchesTypeEnum;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
|
|||||||
@ -31,6 +31,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|||||||
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
import { CheckDeviceGuard } from 'src/guards/device.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 { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -211,13 +212,13 @@ export class DeviceController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||||
@Post('four-scene/:deviceUuid')
|
@Post(':deviceUuid/scenes')
|
||||||
async addSceneToFourSceneDevice(
|
async addSceneToSceneDevice(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||||
) {
|
) {
|
||||||
const device = await this.deviceService.addSceneToFourSceneDevice(
|
const device = await this.deviceService.addSceneToSceneDevice(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
addSceneToFourSceneDeviceDto,
|
addSceneToFourSceneDeviceDto,
|
||||||
);
|
);
|
||||||
@ -230,21 +231,15 @@ export class DeviceController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||||
@Get('four-scene/switch/:deviceUuid')
|
@Get(':deviceUuid/scenes')
|
||||||
async getSceneFourSceneDevice(
|
async getScenesBySceneDevice(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.getSceneFourSceneDevice(
|
return await this.deviceService.getScenesBySceneDevice(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
getSceneFourSceneDeviceDto,
|
getSceneFourSceneDeviceDto,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Get('four-scene/:deviceUuid')
|
|
||||||
async getScenesFourSceneDevice(@Param('deviceUuid') deviceUuid: string) {
|
|
||||||
return await this.deviceService.getScenesFourSceneDevice(deviceUuid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
@ -41,9 +41,9 @@ export class AddSceneToFourSceneDeviceDto {
|
|||||||
description: 'switchName',
|
description: 'switchName',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsEnum(FourSceneSwitchesEnum)
|
@IsEnum(SceneSwitchesTypeEnum)
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
switchName: FourSceneSwitchesEnum;
|
switchName: SceneSwitchesTypeEnum;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'sceneUuid',
|
description: 'sceneUuid',
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
|
import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class ControlDeviceDto {
|
export class ControlDeviceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -57,9 +57,9 @@ export class BatchFactoryResetDevicesDto {
|
|||||||
export class GetSceneFourSceneDeviceDto {
|
export class GetSceneFourSceneDeviceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'switchName',
|
description: 'switchName',
|
||||||
required: true,
|
required: false,
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsOptional()
|
||||||
public switchName: string;
|
public switchName?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,7 +53,8 @@ import { SceneService } from 'src/scene/services';
|
|||||||
import { AddAutomationDto } from 'src/automation/dtos';
|
import { AddAutomationDto } from 'src/automation/dtos';
|
||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
|
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
@ -1220,7 +1221,8 @@ export class DeviceService {
|
|||||||
|
|
||||||
return descendants;
|
return descendants;
|
||||||
}
|
}
|
||||||
async addSceneToFourSceneDevice(
|
|
||||||
|
async addSceneToSceneDevice(
|
||||||
deviceUuid: string,
|
deviceUuid: string,
|
||||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||||
) {
|
) {
|
||||||
@ -1237,7 +1239,6 @@ export class DeviceService {
|
|||||||
this.getDeviceByDeviceUuid(deviceUuid),
|
this.getDeviceByDeviceUuid(deviceUuid),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Generate a shorter automation name (e.g., "Auto_ABC123_169")
|
|
||||||
const shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID
|
const shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID
|
||||||
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
|
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
|
||||||
const automationName = `Auto_${shortUuid}_${timestamp}`;
|
const automationName = `Auto_${shortUuid}_${timestamp}`;
|
||||||
@ -1245,33 +1246,32 @@ export class DeviceService {
|
|||||||
const addAutomationData: AddAutomationDto = {
|
const addAutomationData: AddAutomationDto = {
|
||||||
spaceUuid: spaceData.spaceTuyaUuid,
|
spaceUuid: spaceData.spaceTuyaUuid,
|
||||||
automationName,
|
automationName,
|
||||||
decisionExpr: 'and',
|
decisionExpr: AUTOMATION_CONFIG.DECISION_EXPR,
|
||||||
effectiveTime: {
|
effectiveTime: {
|
||||||
start: '00:00',
|
start: AUTOMATION_CONFIG.DEFAULT_START_TIME,
|
||||||
end: '23:59',
|
end: AUTOMATION_CONFIG.DEFAULT_END_TIME,
|
||||||
loops: '1111111',
|
loops: AUTOMATION_CONFIG.DEFAULT_LOOPS,
|
||||||
},
|
},
|
||||||
conditions: [
|
conditions: [
|
||||||
{
|
{
|
||||||
code: 1,
|
code: 1,
|
||||||
entityId: deviceData.deviceTuyaUuid,
|
entityId: deviceData.deviceTuyaUuid,
|
||||||
entityType: 'device_report',
|
entityType: AUTOMATION_CONFIG.CONDITION_TYPE,
|
||||||
expr: {
|
expr: {
|
||||||
comparator: '==',
|
comparator: AUTOMATION_CONFIG.COMPARATOR,
|
||||||
statusCode: switchName,
|
statusCode: switchName,
|
||||||
statusValue: 'scene',
|
statusValue: AUTOMATION_CONFIG.SCENE_STATUS_VALUE,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
actionExecutor: 'rule_trigger',
|
actionExecutor: AUTOMATION_CONFIG.ACTION_EXECUTOR,
|
||||||
entityId: sceneData.sceneTuyaUuid,
|
entityId: sceneData.sceneTuyaUuid,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create automation
|
|
||||||
const automation = await this.tuyaService.createAutomation(
|
const automation = await this.tuyaService.createAutomation(
|
||||||
addAutomationData.spaceUuid,
|
addAutomationData.spaceUuid,
|
||||||
addAutomationData.automationName,
|
addAutomationData.automationName,
|
||||||
@ -1318,39 +1318,44 @@ export class DeviceService {
|
|||||||
throw new HttpException(errorMessage, errorStatus);
|
throw new HttpException(errorMessage, errorStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getSceneFourSceneDevice(
|
|
||||||
|
async getScenesBySceneDevice(
|
||||||
deviceUuid: string,
|
deviceUuid: string,
|
||||||
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||||
) {
|
): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const sceneDevice = await this.sceneDeviceRepository.findOne({
|
if (getSceneFourSceneDeviceDto.switchName) {
|
||||||
where: {
|
// Query for a single record directly when switchName is provided
|
||||||
device: { uuid: deviceUuid },
|
const sceneDevice = await this.sceneDeviceRepository.findOne({
|
||||||
switchName:
|
where: {
|
||||||
getSceneFourSceneDeviceDto.switchName as FourSceneSwitchesEnum, // Cast the string to the enum
|
device: { uuid: deviceUuid },
|
||||||
},
|
switchName:
|
||||||
relations: ['device', 'scene'],
|
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum,
|
||||||
});
|
},
|
||||||
if (sceneDevice.uuid) {
|
relations: ['device', 'scene'],
|
||||||
const SceneDetails = await this.sceneService.getSceneByUuid(
|
});
|
||||||
|
|
||||||
|
if (!sceneDevice) {
|
||||||
|
throw new HttpException(
|
||||||
|
`No scene found for device with UUID ${deviceUuid} and switch name ${getSceneFourSceneDeviceDto.switchName}`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sceneDetails = await this.sceneService.getSceneByUuid(
|
||||||
sceneDevice.scene.uuid,
|
sceneDevice.scene.uuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
switchName: sceneDevice.switchName,
|
switchName: sceneDevice.switchName,
|
||||||
createdAt: sceneDevice.createdAt,
|
createdAt: sceneDevice.createdAt,
|
||||||
updatedAt: sceneDevice.updatedAt,
|
updatedAt: sceneDevice.updatedAt,
|
||||||
deviceUuid: sceneDevice.device.uuid,
|
deviceUuid: sceneDevice.device.uuid,
|
||||||
scene: {
|
scene: sceneDetails.data,
|
||||||
...SceneDetails.data,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND);
|
// Query for multiple records if switchName is not provided
|
||||||
}
|
|
||||||
}
|
|
||||||
async getScenesFourSceneDevice(deviceUuid: string): Promise<any[]> {
|
|
||||||
try {
|
|
||||||
const sceneDevices = await this.sceneDeviceRepository.find({
|
const sceneDevices = await this.sceneDeviceRepository.find({
|
||||||
where: { device: { uuid: deviceUuid } },
|
where: { device: { uuid: deviceUuid } },
|
||||||
relations: ['device', 'scene'],
|
relations: ['device', 'scene'],
|
||||||
@ -1358,37 +1363,28 @@ export class DeviceService {
|
|||||||
|
|
||||||
if (!sceneDevices.length) {
|
if (!sceneDevices.length) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'No scenes found for the device',
|
`No scenes found for device with UUID ${deviceUuid}`,
|
||||||
HttpStatus.NOT_FOUND,
|
HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
sceneDevices.map(async (sceneDevice) => {
|
sceneDevices.map(async (sceneDevice) => {
|
||||||
if (!sceneDevice.scene?.uuid) return null;
|
const sceneDetails = await this.sceneService.getSceneByUuid(
|
||||||
|
sceneDevice.scene.uuid,
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
return {
|
||||||
const sceneDetails = await this.sceneService.getSceneByUuid(
|
switchName: sceneDevice.switchName,
|
||||||
sceneDevice.scene.uuid,
|
createdAt: sceneDevice.createdAt,
|
||||||
);
|
updatedAt: sceneDevice.updatedAt,
|
||||||
|
deviceUuid: sceneDevice.device.uuid,
|
||||||
return {
|
scene: sceneDetails.data,
|
||||||
switchName: sceneDevice.switchName,
|
};
|
||||||
createdAt: sceneDevice.createdAt,
|
|
||||||
updatedAt: sceneDevice.updatedAt,
|
|
||||||
deviceUuid: sceneDevice.device.uuid,
|
|
||||||
scene: sceneDetails.data,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Failed to fetch scene details',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return results.filter(Boolean);
|
return results;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Failed to fetch scenes for device',
|
error.message || 'Failed to fetch scenes for device',
|
||||||
|
|||||||
42
src/guards/scene.device.type.guard.ts
Normal file
42
src/guards/scene.device.type.guard.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
BadRequestException,
|
||||||
|
HttpException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { DeviceService } from 'src/device/services';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckFourAndSixSceneDeviceTypeGuard implements CanActivate {
|
||||||
|
constructor(private readonly deviceService: DeviceService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const request = context.switchToHttp().getRequest();
|
||||||
|
const deviceUuid = request.params.deviceUuid;
|
||||||
|
|
||||||
|
if (!deviceUuid) {
|
||||||
|
throw new BadRequestException('Device UUID is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const deviceDetails =
|
||||||
|
await this.deviceService.getDeviceByDeviceUuid(deviceUuid);
|
||||||
|
|
||||||
|
if (
|
||||||
|
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
||||||
|
deviceDetails.productDevice.prodType !== ProductType.SIX_S
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('The device type is not supported');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'An error occurred',
|
||||||
|
error.status || 500,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user