mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +00:00
Add automation module
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { SceneService } from './services/automation.service';
|
import { AutomationService } from './services/automation.service';
|
||||||
import { SceneController } from './controllers/automation.controller';
|
import { AutomationController } from './controllers/automation.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
@ -10,14 +10,14 @@ import { ProductRepository } from '@app/common/modules/product/repositories';
|
|||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule],
|
||||||
controllers: [SceneController],
|
controllers: [AutomationController],
|
||||||
providers: [
|
providers: [
|
||||||
SceneService,
|
AutomationService,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
DeviceService,
|
DeviceService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
ProductRepository,
|
ProductRepository,
|
||||||
],
|
],
|
||||||
exports: [SceneService],
|
exports: [AutomationService],
|
||||||
})
|
})
|
||||||
export class SceneModule {}
|
export class AutomationModule {}
|
||||||
|
@ -14,7 +14,8 @@ import {
|
|||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
AddAutomationDto,
|
AddAutomationDto,
|
||||||
UpdateSceneTapToRunDto,
|
UpdateAutomationDto,
|
||||||
|
UpdateAutomationStatusDto,
|
||||||
} from '../dtos/automation.dto';
|
} from '../dtos/automation.dto';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
|
||||||
@ -31,13 +32,13 @@ export class AutomationController {
|
|||||||
@Post()
|
@Post()
|
||||||
async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
|
async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
|
||||||
try {
|
try {
|
||||||
const sceneAutomation =
|
const automation =
|
||||||
await this.automationService.addAutomation(addAutomationDto);
|
await this.automationService.addAutomation(addAutomationDto);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Automation added successfully',
|
message: 'Automation added successfully',
|
||||||
data: sceneAutomation,
|
data: automation,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -48,12 +49,12 @@ export class AutomationController {
|
|||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('tap-to-run/:unitUuid')
|
@Get(':unitUuid')
|
||||||
async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) {
|
async getAutomationByUnit(@Param('unitUuid') unitUuid: string) {
|
||||||
try {
|
try {
|
||||||
const tapToRunScenes =
|
const automation =
|
||||||
await this.automationService.getTapToRunSceneByUnit(unitUuid);
|
await this.automationService.getAutomationByUnit(unitUuid);
|
||||||
return tapToRunScenes;
|
return automation;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -63,51 +64,12 @@ export class AutomationController {
|
|||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Delete('tap-to-run/:unitUuid/:sceneId')
|
@Get('details/:automationId')
|
||||||
async deleteTapToRunScene(
|
async getAutomationDetails(@Param('automationId') automationId: string) {
|
||||||
@Param('unitUuid') unitUuid: string,
|
|
||||||
@Param('sceneId') sceneId: string,
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
await this.automationService.deleteTapToRunScene(unitUuid, sceneId);
|
const automation =
|
||||||
return {
|
await this.automationService.getAutomationDetails(automationId);
|
||||||
statusCode: HttpStatus.OK,
|
return automation;
|
||||||
message: 'Scene Deleted Successfully',
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Post('tap-to-run/trigger/:sceneId')
|
|
||||||
async triggerTapToRunScene(@Param('sceneId') sceneId: string) {
|
|
||||||
try {
|
|
||||||
await this.automationService.triggerTapToRunScene(sceneId);
|
|
||||||
return {
|
|
||||||
statusCode: HttpStatus.CREATED,
|
|
||||||
success: true,
|
|
||||||
message: 'Scene trigger successfully',
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Get('tap-to-run/details/:sceneId')
|
|
||||||
async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) {
|
|
||||||
try {
|
|
||||||
const tapToRunScenes =
|
|
||||||
await this.automationService.getTapToRunSceneDetails(sceneId);
|
|
||||||
return tapToRunScenes;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -118,21 +80,65 @@ export class AutomationController {
|
|||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('tap-to-run/:sceneId')
|
@Delete(':unitUuid/:automationId')
|
||||||
async updateTapToRunScene(
|
async deleteAutomation(
|
||||||
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
@Param('unitUuid') unitUuid: string,
|
||||||
@Param('sceneId') sceneId: string,
|
@Param('automationId') automationId: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const tapToRunScene = await this.automationService.updateTapToRunScene(
|
await this.automationService.deleteAutomation(unitUuid, automationId);
|
||||||
updateSceneTapToRunDto,
|
return {
|
||||||
sceneId,
|
statusCode: HttpStatus.OK,
|
||||||
|
message: 'Automation Deleted Successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put(':automationId')
|
||||||
|
async updateAutomation(
|
||||||
|
@Body() updateAutomationDto: UpdateAutomationDto,
|
||||||
|
@Param('automationId') automationId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const automation = await this.automationService.updateAutomation(
|
||||||
|
updateAutomationDto,
|
||||||
|
automationId,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Scene updated successfully',
|
message: 'Automation updated successfully',
|
||||||
data: tapToRunScene,
|
data: automation,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put('status/:automationId')
|
||||||
|
async updateAutomationStatus(
|
||||||
|
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||||
|
@Param('automationId') automationId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await this.automationService.updateAutomationStatus(
|
||||||
|
updateAutomationStatusDto,
|
||||||
|
automationId,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Automation status updated successfully',
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
@ -6,9 +6,67 @@ import {
|
|||||||
ValidateNested,
|
ValidateNested,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
|
IsBoolean,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
|
class EffectiveTime {
|
||||||
|
@ApiProperty({ description: 'Start time', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public start: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'End time', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public end: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Loops', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public loops: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Expr {
|
||||||
|
@ApiProperty({ description: 'Status code', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public statusCode: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Comparator', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public comparator: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Status value', required: true })
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public statusValue: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Condition {
|
||||||
|
@ApiProperty({ description: 'Condition code', required: true })
|
||||||
|
@IsNumber()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public code: number;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Entity ID', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public entityId: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Entity type', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public entityType: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Expression', required: true, type: Expr })
|
||||||
|
@ValidateNested()
|
||||||
|
@Type(() => Expr)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public expr: Expr;
|
||||||
|
}
|
||||||
|
|
||||||
class ExecutorProperty {
|
class ExecutorProperty {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Function code (for device issue action)',
|
description: 'Function code (for device issue action)',
|
||||||
@ -35,100 +93,122 @@ class ExecutorProperty {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Action {
|
class Action {
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Entity ID', required: true })
|
||||||
description: 'Entity ID',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public entityId: string;
|
public entityId: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Action executor', required: true })
|
||||||
description: 'Action executor',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public actionExecutor: string;
|
public actionExecutor: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Executor property',
|
description: 'Executor property',
|
||||||
required: false, // Set required to false
|
required: false,
|
||||||
type: ExecutorProperty,
|
type: ExecutorProperty,
|
||||||
})
|
})
|
||||||
@ValidateNested()
|
@ValidateNested()
|
||||||
@Type(() => ExecutorProperty)
|
@Type(() => ExecutorProperty)
|
||||||
@IsOptional() // Make executorProperty optional
|
@IsOptional()
|
||||||
public executorProperty?: ExecutorProperty;
|
public executorProperty?: ExecutorProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AddAutomationDto {
|
export class AddAutomationDto {
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Unit ID', required: true })
|
||||||
description: 'Unit UUID',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public unitUuid: string;
|
public unitUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Automation name', required: true })
|
||||||
description: 'Scene name',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public sceneName: string;
|
public name: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Decision expression', required: true })
|
||||||
description: 'Decision expression',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public decisionExpr: string;
|
public decisionExpr: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Actions',
|
description: 'Effective time',
|
||||||
required: true,
|
required: true,
|
||||||
type: [Action],
|
type: EffectiveTime,
|
||||||
})
|
})
|
||||||
|
@ValidateNested()
|
||||||
|
@Type(() => EffectiveTime)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public effectiveTime: EffectiveTime;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Conditions', required: true, type: [Condition] })
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => Condition)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public conditions: Condition[];
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Actions', required: true, type: [Action] })
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => Action)
|
@Type(() => Action)
|
||||||
|
@IsNotEmpty()
|
||||||
public actions: Action[];
|
public actions: Action[];
|
||||||
|
|
||||||
constructor(dto: Partial<AddAutomationDto>) {
|
constructor(dto: Partial<AddAutomationDto>) {
|
||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class UpdateSceneTapToRunDto {
|
|
||||||
@ApiProperty({
|
export class UpdateAutomationDto {
|
||||||
description: 'Scene name',
|
@ApiProperty({ description: 'Automation name', required: true })
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public sceneName: string;
|
public name: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({ description: 'Decision expression', required: true })
|
||||||
description: 'Decision expression',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public decisionExpr: string;
|
public decisionExpr: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Actions',
|
description: 'Effective time',
|
||||||
required: true,
|
required: true,
|
||||||
type: [Action],
|
type: EffectiveTime,
|
||||||
})
|
})
|
||||||
|
@ValidateNested()
|
||||||
|
@Type(() => EffectiveTime)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public effectiveTime: EffectiveTime;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Conditions', required: true, type: [Condition] })
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => Condition)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public conditions: Condition[];
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Actions', required: true, type: [Action] })
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => Action)
|
@Type(() => Action)
|
||||||
|
@IsNotEmpty()
|
||||||
public actions: Action[];
|
public actions: Action[];
|
||||||
|
constructor(dto: Partial<UpdateAutomationDto>) {
|
||||||
constructor(dto: Partial<UpdateSceneTapToRunDto>) {
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class UpdateAutomationStatusDto {
|
||||||
|
@ApiProperty({ description: 'Unit uuid', required: true })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public unitUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({ description: 'Is enable', required: true })
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public isEnable: boolean;
|
||||||
|
|
||||||
|
constructor(dto: Partial<UpdateAutomationStatusDto>) {
|
||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
export interface AddTapToRunSceneInterface {
|
export interface AddAutomationInterface {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
result: {
|
result: {
|
||||||
id: string;
|
id: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
export interface GetTapToRunSceneByUnitInterface {
|
export interface GetAutomationByUnitInterface {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
result: {
|
result: {
|
||||||
@ -16,8 +16,30 @@ export interface GetTapToRunSceneByUnitInterface {
|
|||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
export interface DeleteTapToRunSceneInterface {
|
export interface DeleteAutomationInterface {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
result: boolean;
|
result: boolean;
|
||||||
}
|
}
|
||||||
|
export interface Action {
|
||||||
|
actionExecutor: string;
|
||||||
|
entityId: string;
|
||||||
|
[key: string]: any; // Allow additional properties
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Condition {
|
||||||
|
entityType: string;
|
||||||
|
entityId: string;
|
||||||
|
[key: string]: any; // Allow additional properties
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AutomationResponseData {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
status: string;
|
||||||
|
spaceId?: string;
|
||||||
|
runningMode?: string;
|
||||||
|
actions: Action[];
|
||||||
|
conditions: Condition[];
|
||||||
|
[key: string]: any; // Allow additional properties
|
||||||
|
}
|
||||||
|
@ -5,16 +5,17 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddAutomationDto, UpdateSceneTapToRunDto } from '../dtos';
|
import { UpdateAutomationDto, UpdateAutomationStatusDto } from '../dtos';
|
||||||
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
import {
|
import {
|
||||||
AddTapToRunSceneInterface,
|
AddAutomationInterface,
|
||||||
DeleteTapToRunSceneInterface,
|
AutomationResponseData,
|
||||||
GetTapToRunSceneByUnitInterface,
|
DeleteAutomationInterface,
|
||||||
|
GetAutomationByUnitInterface,
|
||||||
} from '../interface/automation.interface';
|
} from '../interface/automation.interface';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
|
|
||||||
@ -36,11 +37,12 @@ export class AutomationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addAutomation(addAutomationDto: AddAutomationDto, spaceTuyaId = null) {
|
async addAutomation(addAutomationDto, spaceTuyaId = null) {
|
||||||
try {
|
try {
|
||||||
let unitSpaceTuyaId;
|
let unitSpaceTuyaId;
|
||||||
if (!spaceTuyaId) {
|
if (!spaceTuyaId) {
|
||||||
const unitDetails = await this.getUnitByUuid(addAutomationDto.unitUuid);
|
const unitDetails = await this.getUnitByUuid(addAutomationDto.unitUuid);
|
||||||
|
|
||||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||||
if (!unitDetails) {
|
if (!unitDetails) {
|
||||||
throw new BadRequestException('Invalid unit UUID');
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
@ -48,14 +50,15 @@ export class AutomationService {
|
|||||||
} else {
|
} else {
|
||||||
unitSpaceTuyaId = spaceTuyaId;
|
unitSpaceTuyaId = spaceTuyaId;
|
||||||
}
|
}
|
||||||
const actions = addAutomationDto.actions.map((action) => {
|
|
||||||
return {
|
|
||||||
...action,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const convertedData = convertKeysToSnakeCase(actions);
|
const actions = addAutomationDto.actions.map((action) =>
|
||||||
for (const action of convertedData) {
|
convertKeysToSnakeCase(action),
|
||||||
|
);
|
||||||
|
const conditions = addAutomationDto.conditions.map((condition) =>
|
||||||
|
convertKeysToSnakeCase(condition),
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const action of actions) {
|
||||||
if (action.action_executor === 'device_issue') {
|
if (action.action_executor === 'device_issue') {
|
||||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
action.entity_id,
|
action.entity_id,
|
||||||
@ -66,16 +69,34 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const condition of conditions) {
|
||||||
|
if (condition.entity_type === 'device_report') {
|
||||||
|
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
|
condition.entity_id,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
if (device) {
|
||||||
|
condition.entity_id = device.deviceTuyaUuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule`;
|
const path = `/v2.0/cloud/scene/rule`;
|
||||||
const response: AddTapToRunSceneInterface = await this.tuya.request({
|
const response: AddAutomationInterface = await this.tuya.request({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
path,
|
path,
|
||||||
body: {
|
body: {
|
||||||
space_id: unitSpaceTuyaId,
|
space_id: unitSpaceTuyaId,
|
||||||
name: addAutomationDto.sceneName,
|
name: addAutomationDto.name,
|
||||||
type: 'scene',
|
effective_time: {
|
||||||
|
...addAutomationDto.effectiveTime,
|
||||||
|
timezone_id: 'Asia/Dubai',
|
||||||
|
},
|
||||||
|
type: 'automation',
|
||||||
decision_expr: addAutomationDto.decisionExpr,
|
decision_expr: addAutomationDto.decisionExpr,
|
||||||
actions: convertedData,
|
conditions: conditions,
|
||||||
|
actions: actions,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
@ -89,7 +110,7 @@ export class AutomationService {
|
|||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Scene not found',
|
err.message || 'Automation not found',
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -125,20 +146,18 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getTapToRunSceneByUnit(unitUuid: string) {
|
async getAutomationByUnit(unitUuid: string) {
|
||||||
try {
|
try {
|
||||||
const unit = await this.getUnitByUuid(unitUuid);
|
const unit = await this.getUnitByUuid(unitUuid);
|
||||||
if (!unit.spaceTuyaUuid) {
|
if (!unit.spaceTuyaUuid) {
|
||||||
throw new BadRequestException('Invalid unit UUID');
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=scene`;
|
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=automation`;
|
||||||
const response: GetTapToRunSceneByUnitInterface = await this.tuya.request(
|
const response: GetAutomationByUnitInterface = await this.tuya.request({
|
||||||
{
|
method: 'GET',
|
||||||
method: 'GET',
|
path,
|
||||||
path,
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||||
@ -149,7 +168,7 @@ export class AutomationService {
|
|||||||
id: item.id,
|
id: item.id,
|
||||||
name: item.name,
|
name: item.name,
|
||||||
status: item.status,
|
status: item.status,
|
||||||
type: 'tap_to_run',
|
type: 'automation',
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -157,15 +176,91 @@ export class AutomationService {
|
|||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Scene not found',
|
err.message || 'Automation not found',
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async deleteTapToRunScene(
|
async getAutomationDetails(automationId: string, withSpaceId = false) {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/scene/rule/${automationId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData: AutomationResponseData = convertKeysToCamelCase(
|
||||||
|
response.result,
|
||||||
|
);
|
||||||
|
|
||||||
|
const actions = responseData.actions.map((action) => ({
|
||||||
|
...action,
|
||||||
|
}));
|
||||||
|
|
||||||
|
for (const action of actions) {
|
||||||
|
if (action.actionExecutor === 'device_issue') {
|
||||||
|
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
||||||
|
action.entityId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (device) {
|
||||||
|
action.entityId = device.uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const conditions = responseData.conditions.map((condition) => ({
|
||||||
|
...condition,
|
||||||
|
}));
|
||||||
|
|
||||||
|
for (const condition of conditions) {
|
||||||
|
if (condition.entityType === 'device_report') {
|
||||||
|
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
||||||
|
condition.entityId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (device) {
|
||||||
|
condition.entityId = device.uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { timeZoneId, ...effectiveTimeWithoutTimeZoneId } =
|
||||||
|
responseData.effectiveTime || {};
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: responseData.id,
|
||||||
|
name: responseData.name,
|
||||||
|
status: responseData.status,
|
||||||
|
type: 'automation',
|
||||||
|
...(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { spaceId, runningMode, ...rest } = responseData;
|
||||||
|
return rest;
|
||||||
|
})(),
|
||||||
|
actions,
|
||||||
|
conditions,
|
||||||
|
effectiveTime: effectiveTimeWithoutTimeZoneId, // Use modified effectiveTime
|
||||||
|
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteAutomation(
|
||||||
unitUuid: string,
|
unitUuid: string,
|
||||||
sceneId: string,
|
automationId: string,
|
||||||
spaceTuyaId = null,
|
spaceTuyaId = null,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
@ -180,14 +275,14 @@ export class AutomationService {
|
|||||||
unitSpaceTuyaId = spaceTuyaId;
|
unitSpaceTuyaId = spaceTuyaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${unitSpaceTuyaId}`;
|
const path = `/v2.0/cloud/scene/rule?ids=${automationId}&space_id=${unitSpaceTuyaId}`;
|
||||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@ -196,106 +291,81 @@ export class AutomationService {
|
|||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Scene not found',
|
err.message || 'Automation not found',
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async triggerTapToRunScene(sceneId: string) {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
|
|
||||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
|
||||||
method: 'POST',
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
if (!response.success) {
|
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof BadRequestException) {
|
|
||||||
throw err; // Re-throw BadRequestException
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
err.message || 'Scene not found',
|
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async getTapToRunSceneDetails(sceneId: string, withSpaceId = false) {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/scene/rule/${sceneId}`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'GET',
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
if (!response.success) {
|
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
const responseData = convertKeysToCamelCase(response.result);
|
|
||||||
const actions = responseData.actions.map((action) => {
|
|
||||||
return {
|
|
||||||
...action,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const action of actions) {
|
async updateAutomation(
|
||||||
if (action.actionExecutor === 'device_issue') {
|
updateAutomationDto: UpdateAutomationDto,
|
||||||
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
automationId: string,
|
||||||
action.entityId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (device) {
|
|
||||||
action.entityId = device.uuid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: responseData.id,
|
|
||||||
name: responseData.name,
|
|
||||||
status: responseData.status,
|
|
||||||
type: 'tap_to_run',
|
|
||||||
actions: actions,
|
|
||||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof BadRequestException) {
|
|
||||||
throw err; // Re-throw BadRequestException
|
|
||||||
} else {
|
|
||||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async updateTapToRunScene(
|
|
||||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
|
||||||
sceneId: string,
|
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneId, true);
|
const spaceTuyaId = await this.getAutomationDetails(automationId, true);
|
||||||
if (!spaceTuyaId.spaceId) {
|
if (!spaceTuyaId.spaceId) {
|
||||||
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
throw new HttpException(
|
||||||
|
"Automation doesn't exist",
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const addSceneTapToRunDto: AddAutomationDto = {
|
const addAutomation = {
|
||||||
...updateSceneTapToRunDto,
|
...updateAutomationDto,
|
||||||
unitUuid: null,
|
unitUuid: null,
|
||||||
};
|
};
|
||||||
const newTapToRunScene = await this.addAutomation(
|
const newAutomation = await this.addAutomation(
|
||||||
addSceneTapToRunDto,
|
addAutomation,
|
||||||
spaceTuyaId.spaceId,
|
spaceTuyaId.spaceId,
|
||||||
);
|
);
|
||||||
if (newTapToRunScene.id) {
|
if (newAutomation.id) {
|
||||||
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
await this.deleteAutomation(null, automationId, spaceTuyaId.spaceId);
|
||||||
return newTapToRunScene;
|
return newAutomation;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Scene not found',
|
err.message || 'Automation not found',
|
||||||
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async updateAutomationStatus(
|
||||||
|
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||||
|
automationId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const unitDetails = await this.getUnitByUuid(
|
||||||
|
updateAutomationStatusDto.unitUuid,
|
||||||
|
);
|
||||||
|
if (!unitDetails.spaceTuyaUuid) {
|
||||||
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = `/v2.0/cloud/scene/rule/state?space_id=${unitDetails.spaceTuyaUuid}`;
|
||||||
|
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||||
|
method: 'PUT',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
ids: automationId,
|
||||||
|
is_enable: updateAutomationStatusDto.isEnable,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || 'Automation not found',
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user