mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
automation service
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
AddAutomationDto,
|
||||
DeleteAutomationParamDto,
|
||||
UpdateAutomationDto,
|
||||
UpdateAutomationStatusDto,
|
||||
} from '../dtos';
|
||||
@ -217,9 +218,9 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async getAutomationDetails(automationId: string, withSpaceId = false) {
|
||||
async getAutomationDetails(automationUuid: string, withSpaceId = false) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${automationId}`;
|
||||
const path = `/v2.0/cloud/scene/rule/${automationUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
@ -305,15 +306,12 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAutomation(
|
||||
unitUuid: string,
|
||||
automationId: string,
|
||||
spaceTuyaId = null,
|
||||
) {
|
||||
async deleteAutomation(param: DeleteAutomationParamDto, spaceTuyaId = null) {
|
||||
try {
|
||||
const { automationUuid, spaceUuid } = param;
|
||||
let unitSpaceTuyaId;
|
||||
if (!spaceTuyaId) {
|
||||
const unitDetails = await this.getUnitByUuid(unitUuid);
|
||||
const unitDetails = await this.getUnitByUuid(spaceUuid);
|
||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||
if (!unitSpaceTuyaId) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
@ -322,7 +320,7 @@ export class AutomationService {
|
||||
unitSpaceTuyaId = spaceTuyaId;
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationId}&space_id=${unitSpaceTuyaId}`;
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationUuid}&space_id=${unitSpaceTuyaId}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'DELETE',
|
||||
path,
|
||||
@ -347,10 +345,10 @@ export class AutomationService {
|
||||
|
||||
async updateAutomation(
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
automationId: string,
|
||||
automationUuid: string,
|
||||
) {
|
||||
try {
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationId, true);
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationUuid, true);
|
||||
if (!spaceTuyaId.spaceId) {
|
||||
throw new HttpException(
|
||||
"Automation doesn't exist",
|
||||
@ -365,8 +363,12 @@ export class AutomationService {
|
||||
addAutomation,
|
||||
spaceTuyaId.spaceId,
|
||||
);
|
||||
const params: DeleteAutomationParamDto = {
|
||||
spaceUuid: spaceTuyaId.spaceId,
|
||||
automationUuid: automationUuid,
|
||||
};
|
||||
if (newAutomation.id) {
|
||||
await this.deleteAutomation(null, automationId, spaceTuyaId.spaceId);
|
||||
await this.deleteAutomation(null, params);
|
||||
return newAutomation;
|
||||
}
|
||||
} catch (err) {
|
||||
@ -382,7 +384,7 @@ export class AutomationService {
|
||||
}
|
||||
async updateAutomationStatus(
|
||||
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||
automationId: string,
|
||||
automationUuid: string,
|
||||
) {
|
||||
try {
|
||||
const unitDetails = await this.getUnitByUuid(
|
||||
@ -397,7 +399,7 @@ export class AutomationService {
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
ids: automationId,
|
||||
ids: automationUuid,
|
||||
is_enable: updateAutomationStatusDto.isEnable,
|
||||
},
|
||||
});
|
||||
|
Reference in New Issue
Block a user