mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Add updateAutomation method and update external service handling
This commit is contained in:
@ -218,6 +218,42 @@ export class TuyaService {
|
|||||||
|
|
||||||
return response;
|
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(
|
async updateAutomationState(
|
||||||
spaceId: string,
|
spaceId: string,
|
||||||
|
@ -256,8 +256,11 @@ export class AutomationService {
|
|||||||
if (sceneDetails.type === ActionTypeEnum.SCENE) {
|
if (sceneDetails.type === ActionTypeEnum.SCENE) {
|
||||||
const scene = await this.sceneRepository.findOne({
|
const scene = await this.sceneRepository.findOne({
|
||||||
where: { sceneTuyaUuid: action.entityId },
|
where: { sceneTuyaUuid: action.entityId },
|
||||||
|
relations: ['sceneIcon'],
|
||||||
});
|
});
|
||||||
action.entityId = scene.uuid;
|
action.entityId = scene.uuid;
|
||||||
|
action.iconUuid = scene.sceneIcon.uuid;
|
||||||
|
action.icon = scene.sceneIcon.icon;
|
||||||
}
|
}
|
||||||
action.name = sceneDetails.name;
|
action.name = sceneDetails.name;
|
||||||
action.type = sceneDetails.type;
|
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(
|
async updateAutomation(
|
||||||
updateAutomationDto: UpdateAutomationDto,
|
updateAutomationDto: UpdateAutomationDto,
|
||||||
automationUuid: string,
|
automationUuid: string,
|
||||||
) {
|
) {
|
||||||
const { actions, conditions, automationName, effectiveTime, decisionExpr } =
|
|
||||||
updateAutomationDto;
|
|
||||||
try {
|
try {
|
||||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
const automation = await this.getAutomationDetails(automationUuid, true);
|
||||||
if (!automation.spaceId) {
|
if (!automation.spaceId) {
|
||||||
@ -392,19 +436,18 @@ export class AutomationService {
|
|||||||
HttpStatus.NOT_FOUND,
|
HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
const updateTuyaAutomationResponse =
|
||||||
|
await this.updateAutomationExternalService(
|
||||||
|
automation.spaceId,
|
||||||
|
automation.id,
|
||||||
|
updateAutomationDto,
|
||||||
|
);
|
||||||
|
|
||||||
const newAutomation = await this.add({
|
if (!updateTuyaAutomationResponse.success) {
|
||||||
actions,
|
throw new HttpException(
|
||||||
conditions,
|
`Failed to update a external automation`,
|
||||||
automationName,
|
HttpStatus.BAD_GATEWAY,
|
||||||
effectiveTime,
|
);
|
||||||
decisionExpr,
|
|
||||||
spaceTuyaId: automation.spaceId,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (newAutomation.id) {
|
|
||||||
await this.delete(automation.spaceId, automationUuid);
|
|
||||||
return newAutomation;
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
|
Reference in New Issue
Block a user