mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
Add new automation actions and repository
This commit is contained in:
@ -2,6 +2,8 @@ export enum ActionExecutorEnum {
|
|||||||
DEVICE_ISSUE = 'device_issue',
|
DEVICE_ISSUE = 'device_issue',
|
||||||
DELAY = 'delay',
|
DELAY = 'delay',
|
||||||
RULE_TRIGGER = 'rule_trigger',
|
RULE_TRIGGER = 'rule_trigger',
|
||||||
|
RULE_DISABLE = 'rule_disable',
|
||||||
|
RULE_ENABLE = 'rule_enable',
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum EntityTypeEnum {
|
export enum EntityTypeEnum {
|
||||||
|
1
libs/common/src/modules/automation/repositories/index.ts
Normal file
1
libs/common/src/modules/automation/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './automation.repository';
|
@ -15,6 +15,7 @@ import {
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||||
@ -30,6 +31,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
SceneDeviceRepository,
|
SceneDeviceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [AutomationService],
|
exports: [AutomationService],
|
||||||
})
|
})
|
||||||
|
@ -60,4 +60,5 @@ export interface AddAutomationParams {
|
|||||||
effectiveTime: EffectiveTime;
|
effectiveTime: EffectiveTime;
|
||||||
decisionExpr: string;
|
decisionExpr: string;
|
||||||
spaceTuyaId: string;
|
spaceTuyaId: string;
|
||||||
|
spaceUuid: string;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import {
|
|||||||
AutomationDetailsResult,
|
AutomationDetailsResult,
|
||||||
AutomationResponseData,
|
AutomationResponseData,
|
||||||
Condition,
|
Condition,
|
||||||
GetAutomationBySpaceInterface,
|
|
||||||
} from '../interface/automation.interface';
|
} from '../interface/automation.interface';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import {
|
import {
|
||||||
@ -35,6 +34,11 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
|
|||||||
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
import { SceneRepository } from '@app/common/modules/scene/repositories';
|
import { SceneRepository } from '@app/common/modules/scene/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
import { AutomationEntity } from '@app/common/modules/automation/entities';
|
||||||
|
import { DeleteTapToRunSceneInterface } from 'src/scene/interface/scene.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AutomationService {
|
export class AutomationService {
|
||||||
@ -46,6 +50,7 @@ export class AutomationService {
|
|||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||||
private readonly sceneRepository: SceneRepository,
|
private readonly sceneRepository: SceneRepository,
|
||||||
|
private readonly automationRepository: AutomationRepository,
|
||||||
) {
|
) {
|
||||||
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');
|
||||||
@ -57,7 +62,9 @@ export class AutomationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addAutomation(addAutomationDto: AddAutomationDto) {
|
async addAutomation(
|
||||||
|
addAutomationDto: AddAutomationDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
automationName,
|
automationName,
|
||||||
@ -65,30 +72,37 @@ export class AutomationService {
|
|||||||
decisionExpr,
|
decisionExpr,
|
||||||
actions,
|
actions,
|
||||||
conditions,
|
conditions,
|
||||||
|
spaceUuid,
|
||||||
} = addAutomationDto;
|
} = addAutomationDto;
|
||||||
const space = await this.getSpaceByUuid(addAutomationDto.spaceUuid);
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
const response = await this.add({
|
const automation = await this.add({
|
||||||
automationName,
|
automationName,
|
||||||
effectiveTime,
|
effectiveTime,
|
||||||
decisionExpr,
|
decisionExpr,
|
||||||
actions,
|
actions,
|
||||||
conditions,
|
conditions,
|
||||||
spaceTuyaId: space.spaceTuyaUuid,
|
spaceTuyaId: space.spaceTuyaUuid,
|
||||||
|
spaceUuid,
|
||||||
|
});
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
message: `Successfully created new automation with uuid ${automation.uuid}`,
|
||||||
|
data: automation,
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
});
|
});
|
||||||
return response;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
console.error(
|
||||||
throw err;
|
`Error in creating automation for space UUID ${addAutomationDto.spaceUuid}:`,
|
||||||
} else {
|
err.message,
|
||||||
throw new HttpException(
|
);
|
||||||
err.message || 'Automation not found',
|
throw err instanceof HttpException
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
? err
|
||||||
);
|
: new HttpException(
|
||||||
}
|
'Failed to create automation',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async createAutomationExternalService(params: AddAutomationParams) {
|
||||||
async add(params: AddAutomationParams) {
|
|
||||||
try {
|
try {
|
||||||
const formattedActions = await this.prepareActions(params.actions);
|
const formattedActions = await this.prepareActions(params.actions);
|
||||||
const formattedCondition = await this.prepareConditions(
|
const formattedCondition = await this.prepareConditions(
|
||||||
@ -104,14 +118,54 @@ export class AutomationService {
|
|||||||
formattedActions,
|
formattedActions,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
if (!response.result?.id) {
|
||||||
id: response?.result.id,
|
throw new HttpException(
|
||||||
};
|
'Failed to create automation in Tuya',
|
||||||
} catch (error) {
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
throw new HttpException(
|
);
|
||||||
error.message || 'Failed to add automation',
|
}
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
return response;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else if (err.message?.includes('tuya')) {
|
||||||
|
throw new HttpException(
|
||||||
|
'API error: Failed to create automation',
|
||||||
|
HttpStatus.BAD_GATEWAY,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
`An Internal error has been occured ${err}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async add(params: AddAutomationParams) {
|
||||||
|
try {
|
||||||
|
const response = await this.createAutomationExternalService(params);
|
||||||
|
|
||||||
|
const automation = await this.automationRepository.save({
|
||||||
|
automationTuyaUuid: response.result.id,
|
||||||
|
space: { uuid: params.spaceUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
return automation;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else if (err.message?.includes('tuya')) {
|
||||||
|
throw new HttpException(
|
||||||
|
'API error: Failed to create automation',
|
||||||
|
HttpStatus.BAD_GATEWAY,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'Database error: Failed to save automation',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,27 +199,42 @@ export class AutomationService {
|
|||||||
async getAutomationBySpace(spaceUuid: string) {
|
async getAutomationBySpace(spaceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const space = await this.getSpaceByUuid(spaceUuid);
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
|
|
||||||
|
const automationData = await this.automationRepository.find({
|
||||||
|
where: {
|
||||||
|
space: { uuid: spaceUuid },
|
||||||
|
disabled: false,
|
||||||
|
},
|
||||||
|
relations: ['space'],
|
||||||
|
});
|
||||||
|
|
||||||
if (!space.spaceTuyaUuid) {
|
if (!space.spaceTuyaUuid) {
|
||||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||||
}
|
}
|
||||||
|
const automations = await Promise.all(
|
||||||
|
automationData.map(async (automation) => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const automationDetails = await this.tuyaService.getSceneRule(
|
||||||
|
automation.automationTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?space_id=${space.spaceTuyaUuid}&type=automation`;
|
|
||||||
const response: GetAutomationBySpaceInterface = await this.tuya.request({
|
|
||||||
method: 'GET',
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.success) {
|
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.result.list
|
|
||||||
.filter((item) => item.name && !item.name.startsWith(AUTO_PREFIX))
|
|
||||||
.map((item) => {
|
|
||||||
return {
|
return {
|
||||||
id: item.id,
|
uuid: automation.uuid,
|
||||||
name: item.name,
|
...automationDetails,
|
||||||
status: item.status,
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return automations
|
||||||
|
.filter(
|
||||||
|
(item: any) =>
|
||||||
|
item.result.name && !item.result.name.startsWith(AUTO_PREFIX),
|
||||||
|
)
|
||||||
|
.map((item: any) => {
|
||||||
|
return {
|
||||||
|
uuid: item.uuid,
|
||||||
|
name: item.result.name,
|
||||||
|
status: item.result.status,
|
||||||
type: AUTOMATION_TYPE,
|
type: AUTOMATION_TYPE,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -180,7 +249,45 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async findAutomationBySpace(spaceUuid: string) {
|
||||||
|
try {
|
||||||
|
await this.getSpaceByUuid(spaceUuid);
|
||||||
|
|
||||||
|
const automationData = await this.automationRepository.find({
|
||||||
|
where: {
|
||||||
|
space: { uuid: spaceUuid },
|
||||||
|
disabled: false,
|
||||||
|
},
|
||||||
|
relations: ['space'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const automations = await Promise.all(
|
||||||
|
automationData.map(async (automation) => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { actions, ...automationDetails } =
|
||||||
|
await this.getAutomation(automation);
|
||||||
|
|
||||||
|
return automationDetails;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return automations;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
`Error fetching Tap-to-Run scenes for space UUID ${spaceUuid}:`,
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'An error occurred while retrieving scenes',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async getTapToRunSceneDetailsTuya(
|
async getTapToRunSceneDetailsTuya(
|
||||||
sceneUuid: string,
|
sceneUuid: string,
|
||||||
): Promise<AutomationDetailsResult> {
|
): Promise<AutomationDetailsResult> {
|
||||||
@ -213,13 +320,34 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getAutomationDetails(automationUuid: string, withSpaceId = false) {
|
async getAutomationDetails(automationUuid: string) {
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/scene/rule/${automationUuid}`;
|
const automation = await this.findAutomation(automationUuid);
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'GET',
|
const automationDetails = await this.getAutomation(automation);
|
||||||
path,
|
|
||||||
});
|
return automationDetails;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`Error fetching automation details for automationUuid ${automationUuid}:`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'An error occurred while retrieving automation details',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getAutomation(automation: AutomationEntity) {
|
||||||
|
try {
|
||||||
|
const response = await this.tuyaService.getSceneRule(
|
||||||
|
automation.automationTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||||
@ -261,6 +389,11 @@ export class AutomationService {
|
|||||||
action.entityId = scene.uuid;
|
action.entityId = scene.uuid;
|
||||||
action.iconUuid = scene.sceneIcon.uuid;
|
action.iconUuid = scene.sceneIcon.uuid;
|
||||||
action.icon = scene.sceneIcon.icon;
|
action.icon = scene.sceneIcon.icon;
|
||||||
|
} else if (sceneDetails.type === ActionTypeEnum.AUTOMATION) {
|
||||||
|
const automation = await this.automationRepository.findOne({
|
||||||
|
where: { automationTuyaUuid: action.entityId },
|
||||||
|
});
|
||||||
|
action.entityId = automation.uuid;
|
||||||
}
|
}
|
||||||
action.name = sceneDetails.name;
|
action.name = sceneDetails.name;
|
||||||
action.type = sceneDetails.type;
|
action.type = sceneDetails.type;
|
||||||
@ -291,91 +424,95 @@ export class AutomationService {
|
|||||||
responseData.effectiveTime || {};
|
responseData.effectiveTime || {};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: responseData.id,
|
uuid: automation.uuid,
|
||||||
name: responseData.name,
|
name: responseData.name,
|
||||||
status: responseData.status,
|
status: responseData.status,
|
||||||
type: 'automation',
|
type: 'automation',
|
||||||
...(() => {
|
...(() => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const { spaceId, runningMode, ...rest } = responseData;
|
const { spaceId, id, runningMode, ...rest } = responseData;
|
||||||
return rest;
|
return rest;
|
||||||
})(),
|
})(),
|
||||||
actions,
|
actions,
|
||||||
conditions,
|
conditions,
|
||||||
effectiveTime: effectiveTimeWithoutTimeZoneId, // Use modified effectiveTime
|
effectiveTime: effectiveTimeWithoutTimeZoneId, // Use modified effectiveTime
|
||||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
throw new HttpException(
|
||||||
|
`An error occurred while retrieving automation details for ${automation.uuid}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async findAutomation(sceneUuid: string): Promise<AutomationEntity> {
|
||||||
|
const automation = await this.automationRepository.findOne({
|
||||||
|
where: { uuid: sceneUuid },
|
||||||
|
relations: ['space'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!automation) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Invalid automation with id ${sceneUuid}`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return automation;
|
||||||
|
}
|
||||||
|
|
||||||
async deleteAutomation(param: AutomationParamDto) {
|
async deleteAutomation(param: AutomationParamDto) {
|
||||||
|
const { automationUuid } = param;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { automationUuid } = param;
|
const automationData = await this.findAutomation(automationUuid);
|
||||||
|
const space = await this.getSpaceByUuid(automationData.space.uuid);
|
||||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
await this.delete(automationData.automationTuyaUuid, space.spaceTuyaUuid);
|
||||||
|
|
||||||
if (!automation && !automation.spaceId) {
|
|
||||||
throw new HttpException(
|
|
||||||
`Invalid automationid ${automationUuid}`,
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||||
where: { automationTuyaUuid: automationUuid },
|
where: { automationTuyaUuid: automationData.automationTuyaUuid },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existingSceneDevice) {
|
if (existingSceneDevice) {
|
||||||
await this.sceneDeviceRepository.delete({
|
await this.sceneDeviceRepository.delete({
|
||||||
automationTuyaUuid: automationUuid,
|
automationTuyaUuid: automationData.automationTuyaUuid,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
await this.automationRepository.update(
|
||||||
const response = this.tuyaService.deleteAutomation(
|
{
|
||||||
automation.spaceId,
|
uuid: automationUuid,
|
||||||
automationUuid,
|
},
|
||||||
|
{ disabled: true },
|
||||||
);
|
);
|
||||||
return response;
|
return new SuccessResponseDto({
|
||||||
|
message: `Automation with ID ${automationUuid} deleted successfully`,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof HttpException) {
|
if (err instanceof HttpException) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Automation not found',
|
err.message || `Automation not found for id ${param.automationUuid}`,
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async delete(tuyaAutomationId: string, tuyaSpaceId: string) {
|
||||||
async delete(tuyaSpaceId: string, automationUuid: string) {
|
|
||||||
try {
|
try {
|
||||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
const response = (await this.tuyaService.deleteSceneRule(
|
||||||
where: { automationTuyaUuid: automationUuid },
|
tuyaAutomationId,
|
||||||
});
|
|
||||||
|
|
||||||
if (existingSceneDevice) {
|
|
||||||
await this.sceneDeviceRepository.delete({
|
|
||||||
automationTuyaUuid: automationUuid,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const response = await this.tuyaService.deleteAutomation(
|
|
||||||
tuyaSpaceId,
|
tuyaSpaceId,
|
||||||
automationUuid,
|
)) as DeleteTapToRunSceneInterface;
|
||||||
);
|
|
||||||
return response;
|
return response;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
if (err instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw err;
|
throw error;
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
err.message || 'Automation not found',
|
'Failed to delete automation rule in Tuya',
|
||||||
err.status || HttpStatus.NOT_FOUND,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -429,17 +566,13 @@ export class AutomationService {
|
|||||||
automationUuid: string,
|
automationUuid: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
const automation = await this.findAutomation(automationUuid);
|
||||||
if (!automation.spaceId) {
|
const space = await this.getSpaceByUuid(automation.space.uuid);
|
||||||
throw new HttpException(
|
|
||||||
"Automation doesn't exist",
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const updateTuyaAutomationResponse =
|
const updateTuyaAutomationResponse =
|
||||||
await this.updateAutomationExternalService(
|
await this.updateAutomationExternalService(
|
||||||
automation.spaceId,
|
space.spaceTuyaUuid,
|
||||||
automation.id,
|
automation.automationTuyaUuid,
|
||||||
updateAutomationDto,
|
updateAutomationDto,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -449,6 +582,16 @@ export class AutomationService {
|
|||||||
HttpStatus.BAD_GATEWAY,
|
HttpStatus.BAD_GATEWAY,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
const updatedScene = await this.automationRepository.update(
|
||||||
|
{ uuid: automationUuid },
|
||||||
|
{
|
||||||
|
space: { uuid: automation.space.uuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: updatedScene,
|
||||||
|
message: `Automation with ID ${automationUuid} updated successfully`,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
@ -466,6 +609,7 @@ export class AutomationService {
|
|||||||
) {
|
) {
|
||||||
const { isEnable, spaceUuid } = updateAutomationStatusDto;
|
const { isEnable, spaceUuid } = updateAutomationStatusDto;
|
||||||
try {
|
try {
|
||||||
|
const automation = await this.findAutomation(automationUuid);
|
||||||
const space = await this.getSpaceByUuid(spaceUuid);
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
if (!space.spaceTuyaUuid) {
|
if (!space.spaceTuyaUuid) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -476,7 +620,7 @@ export class AutomationService {
|
|||||||
|
|
||||||
const response = await this.tuyaService.updateAutomationState(
|
const response = await this.tuyaService.updateAutomationState(
|
||||||
space.spaceTuyaUuid,
|
space.spaceTuyaUuid,
|
||||||
automationUuid,
|
automation.automationTuyaUuid,
|
||||||
isEnable,
|
isEnable,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -521,6 +665,14 @@ export class AutomationService {
|
|||||||
action.entity_id = scene.sceneTuyaUuid;
|
action.entity_id = scene.sceneTuyaUuid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (
|
||||||
|
action.action_executor === ActionExecutorEnum.RULE_DISABLE ||
|
||||||
|
action.action_executor === ActionExecutorEnum.RULE_ENABLE
|
||||||
|
) {
|
||||||
|
if (action.action_type === ActionTypeEnum.AUTOMATION) {
|
||||||
|
const automation = await this.findAutomation(action.entity_id);
|
||||||
|
action.entity_id = automation.automationTuyaUuid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -19,6 +19,7 @@ import {
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule,
|
ConfigModule,
|
||||||
@ -41,6 +42,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
SceneDeviceRepository,
|
SceneDeviceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [DeviceService],
|
exports: [DeviceService],
|
||||||
})
|
})
|
||||||
|
@ -18,6 +18,7 @@ import {
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [DoorLockController],
|
controllers: [DoorLockController],
|
||||||
@ -36,6 +37,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
SceneDeviceRepository,
|
SceneDeviceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [DoorLockService],
|
exports: [DoorLockService],
|
||||||
})
|
})
|
||||||
|
@ -14,6 +14,7 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
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 { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||||
@ -28,6 +29,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
SceneDeviceRepository,
|
SceneDeviceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [SceneService],
|
exports: [SceneService],
|
||||||
})
|
})
|
||||||
|
@ -23,7 +23,10 @@ import {
|
|||||||
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,
|
||||||
|
ActionTypeEnum,
|
||||||
|
} from '@app/common/constants/automation.enum';
|
||||||
import {
|
import {
|
||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
@ -40,6 +43,7 @@ import { HttpStatusCode } from 'axios';
|
|||||||
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SceneService {
|
export class SceneService {
|
||||||
@ -48,6 +52,7 @@ export class SceneService {
|
|||||||
private readonly sceneIconRepository: SceneIconRepository,
|
private readonly sceneIconRepository: SceneIconRepository,
|
||||||
private readonly sceneRepository: SceneRepository,
|
private readonly sceneRepository: SceneRepository,
|
||||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||||
|
private readonly automationRepository: AutomationRepository,
|
||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
@Inject(forwardRef(() => DeviceService))
|
@Inject(forwardRef(() => DeviceService))
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
@ -460,6 +465,12 @@ export class SceneService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (sceneDetails.id) {
|
if (sceneDetails.id) {
|
||||||
|
if (sceneDetails.type === ActionTypeEnum.AUTOMATION) {
|
||||||
|
const automation = await this.automationRepository.findOne({
|
||||||
|
where: { automationTuyaUuid: action.entityId },
|
||||||
|
});
|
||||||
|
action.entityId = automation.uuid;
|
||||||
|
}
|
||||||
action.name = sceneDetails.name;
|
action.name = sceneDetails.name;
|
||||||
action.type = sceneDetails.type;
|
action.type = sceneDetails.type;
|
||||||
}
|
}
|
||||||
@ -568,6 +579,17 @@ export class SceneService {
|
|||||||
if (device) {
|
if (device) {
|
||||||
action.entity_id = device.deviceTuyaUuid;
|
action.entity_id = device.deviceTuyaUuid;
|
||||||
}
|
}
|
||||||
|
} else if (
|
||||||
|
action.action_executor === ActionExecutorEnum.RULE_DISABLE ||
|
||||||
|
action.action_executor === ActionExecutorEnum.RULE_ENABLE
|
||||||
|
) {
|
||||||
|
const automation = await this.automationRepository.findOne({
|
||||||
|
where: { uuid: action.entity_id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (automation) {
|
||||||
|
action.entity_id = automation.automationTuyaUuid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -70,6 +70,7 @@ import {
|
|||||||
InviteUserRepository,
|
InviteUserRepository,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
} from '@app/common/modules/Invite-user/repositiories';
|
} from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
|
|
||||||
export const CommandHandlers = [DisableSpaceHandler];
|
export const CommandHandlers = [DisableSpaceHandler];
|
||||||
|
|
||||||
@ -128,6 +129,7 @@ export const CommandHandlers = [DisableSpaceHandler];
|
|||||||
TimeZoneRepository,
|
TimeZoneRepository,
|
||||||
InviteUserRepository,
|
InviteUserRepository,
|
||||||
InviteUserSpaceRepository,
|
InviteUserSpaceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [SpaceService],
|
exports: [SpaceService],
|
||||||
})
|
})
|
||||||
|
@ -20,6 +20,7 @@ import {
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||||
controllers: [VisitorPasswordController],
|
controllers: [VisitorPasswordController],
|
||||||
@ -39,6 +40,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
SceneDeviceRepository,
|
SceneDeviceRepository,
|
||||||
|
AutomationRepository,
|
||||||
],
|
],
|
||||||
exports: [VisitorPasswordService],
|
exports: [VisitorPasswordService],
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user