mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
Add updateAutomation method and update external service handling
This commit is contained in:
@ -218,6 +218,42 @@ export class TuyaService {
|
||||
|
||||
return response;
|
||||
}
|
||||
async updateAutomation(
|
||||
automationUuid: string,
|
||||
spaceId: string,
|
||||
automationName: string,
|
||||
actions: ConvertedAction[],
|
||||
conditions: any[],
|
||||
decisionExpr: string,
|
||||
effectiveTime: any,
|
||||
) {
|
||||
const path = `/v2.0/cloud/scene/rule/${automationUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
space_id: spaceId,
|
||||
name: automationName,
|
||||
effective_time: {
|
||||
...effectiveTime,
|
||||
timezone_id: 'Asia/Dubai',
|
||||
},
|
||||
type: 'automation',
|
||||
decision_expr: decisionExpr,
|
||||
conditions: conditions,
|
||||
actions: actions,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
`Error fetching automation rule: ${response.msg}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async updateAutomationState(
|
||||
spaceId: string,
|
||||
|
@ -256,8 +256,11 @@ export class AutomationService {
|
||||
if (sceneDetails.type === ActionTypeEnum.SCENE) {
|
||||
const scene = await this.sceneRepository.findOne({
|
||||
where: { sceneTuyaUuid: action.entityId },
|
||||
relations: ['sceneIcon'],
|
||||
});
|
||||
action.entityId = scene.uuid;
|
||||
action.iconUuid = scene.sceneIcon.uuid;
|
||||
action.icon = scene.sceneIcon.icon;
|
||||
}
|
||||
action.name = sceneDetails.name;
|
||||
action.type = sceneDetails.type;
|
||||
@ -377,13 +380,54 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async updateAutomationExternalService(
|
||||
spaceTuyaUuid: string,
|
||||
automationUuid: string,
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
) {
|
||||
const { automationName, decisionExpr, actions, conditions, effectiveTime } =
|
||||
updateAutomationDto;
|
||||
try {
|
||||
const formattedActions = await this.prepareActions(actions);
|
||||
const formattedCondition = await this.prepareConditions(conditions);
|
||||
const response = await this.tuyaService.updateAutomation(
|
||||
automationUuid,
|
||||
spaceTuyaUuid,
|
||||
automationName,
|
||||
formattedActions,
|
||||
formattedCondition,
|
||||
decisionExpr,
|
||||
effectiveTime,
|
||||
);
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException(
|
||||
'Failed to update automation in Tuya',
|
||||
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 update automation',
|
||||
HttpStatus.BAD_GATEWAY,
|
||||
);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
`An Internal error has been occured ${err}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async updateAutomation(
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
automationUuid: string,
|
||||
) {
|
||||
const { actions, conditions, automationName, effectiveTime, decisionExpr } =
|
||||
updateAutomationDto;
|
||||
try {
|
||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
||||
if (!automation.spaceId) {
|
||||
@ -392,19 +436,18 @@ export class AutomationService {
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const updateTuyaAutomationResponse =
|
||||
await this.updateAutomationExternalService(
|
||||
automation.spaceId,
|
||||
automation.id,
|
||||
updateAutomationDto,
|
||||
);
|
||||
|
||||
const newAutomation = await this.add({
|
||||
actions,
|
||||
conditions,
|
||||
automationName,
|
||||
effectiveTime,
|
||||
decisionExpr,
|
||||
spaceTuyaId: automation.spaceId,
|
||||
});
|
||||
|
||||
if (newAutomation.id) {
|
||||
await this.delete(automation.spaceId, automationUuid);
|
||||
return newAutomation;
|
||||
if (!updateTuyaAutomationResponse.success) {
|
||||
throw new HttpException(
|
||||
`Failed to update a external automation`,
|
||||
HttpStatus.BAD_GATEWAY,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
|
Reference in New Issue
Block a user