mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-03-11 07:51:44 +00:00
Merge pull request #163 from SyncrowIOT/fix-update-scene-issue
Fix update scene issue
This commit is contained in:
@ -120,6 +120,36 @@ export class TuyaService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateTapToRunScene(
|
||||
sceneTuyaUuid: string,
|
||||
spaceId: string,
|
||||
sceneName: string,
|
||||
actions: ConvertedAction[],
|
||||
decisionExpr: string,
|
||||
) {
|
||||
const path = `/v2.0/cloud/scene/rule/${sceneTuyaUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
space_id: spaceId,
|
||||
name: sceneName,
|
||||
type: 'scene',
|
||||
decision_expr: decisionExpr,
|
||||
conditions: [],
|
||||
actions: actions,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
`Error fetching scene rule: ${response.msg}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async triggerScene(sceneId: string): Promise<TuyaResponseInterface> {
|
||||
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
|
||||
|
||||
@ -1349,10 +1349,7 @@ export class DeviceService {
|
||||
});
|
||||
|
||||
if (!sceneDevice) {
|
||||
throw new HttpException(
|
||||
`No scene found for device with UUID ${deviceUuid} and switch name ${getSceneFourSceneDeviceDto.switchName}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
return {};
|
||||
}
|
||||
|
||||
const sceneDetails = await this.sceneService.getSceneByUuid(
|
||||
@ -1376,10 +1373,7 @@ export class DeviceService {
|
||||
});
|
||||
|
||||
if (!sceneDevices.length) {
|
||||
throw new HttpException(
|
||||
`No scenes found for device with UUID ${deviceUuid}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
|
||||
@ -128,6 +128,47 @@ export class SceneService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async updateSceneExternalService(
|
||||
spaceTuyaUuid: string,
|
||||
sceneTuyaUuid: string,
|
||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||
) {
|
||||
const { sceneName, decisionExpr, actions } = updateSceneTapToRunDto;
|
||||
try {
|
||||
const formattedActions = await this.prepareActions(actions);
|
||||
|
||||
const response = (await this.tuyaService.updateTapToRunScene(
|
||||
sceneTuyaUuid,
|
||||
spaceTuyaUuid,
|
||||
sceneName,
|
||||
formattedActions,
|
||||
decisionExpr,
|
||||
)) as AddTapToRunSceneInterface;
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException(
|
||||
'Failed to update scene 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 scene',
|
||||
HttpStatus.BAD_GATEWAY,
|
||||
);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
`An Internal error has been occured ${err}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async createSceneExternalService(
|
||||
spaceTuyaUuid: string,
|
||||
@ -285,25 +326,22 @@ export class SceneService {
|
||||
showInHomePage: updateSceneTapToRunDto.showInHomePage,
|
||||
};
|
||||
|
||||
const createdTuyaSceneResponse = await this.createSceneExternalService(
|
||||
const updateTuyaSceneResponse = await this.updateSceneExternalService(
|
||||
space.spaceTuyaUuid,
|
||||
scene.sceneTuyaUuid,
|
||||
addSceneTapToRunDto,
|
||||
);
|
||||
const newSceneTuyaUuid = createdTuyaSceneResponse.result?.id;
|
||||
|
||||
if (!newSceneTuyaUuid) {
|
||||
if (!updateTuyaSceneResponse.success) {
|
||||
throw new HttpException(
|
||||
`Failed to create a external new scene`,
|
||||
`Failed to update a external scene`,
|
||||
HttpStatus.BAD_GATEWAY,
|
||||
);
|
||||
}
|
||||
|
||||
await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid);
|
||||
|
||||
const updatedScene = await this.sceneRepository.update(
|
||||
{ uuid: sceneUuid },
|
||||
{
|
||||
sceneTuyaUuid: newSceneTuyaUuid,
|
||||
showInHomePage: addSceneTapToRunDto.showInHomePage,
|
||||
sceneIcon: {
|
||||
uuid: addSceneTapToRunDto.iconUuid,
|
||||
|
||||
Reference in New Issue
Block a user