mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
Fixed automation bugs
This commit is contained in:
@ -7,7 +7,7 @@ import {
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
AddAutomationDto,
|
||||
DeleteAutomationParamDto,
|
||||
AutomationParamDto,
|
||||
UpdateAutomationDto,
|
||||
UpdateAutomationStatusDto,
|
||||
} from '../dtos';
|
||||
@ -17,11 +17,10 @@ import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
Action,
|
||||
AddAutomationInterface,
|
||||
AddAutomationParams,
|
||||
AutomationDetailsResult,
|
||||
AutomationResponseData,
|
||||
Condition,
|
||||
DeleteAutomationInterface,
|
||||
GetAutomationBySpaceInterface,
|
||||
} from '../interface/automation.interface';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
@ -30,6 +29,7 @@ import {
|
||||
EntityTypeEnum,
|
||||
} from '@app/common/constants/automation.enum';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class AutomationService {
|
||||
@ -50,39 +50,26 @@ export class AutomationService {
|
||||
});
|
||||
}
|
||||
|
||||
async addAutomation(addAutomationDto: AddAutomationDto, spaceTuyaId = null) {
|
||||
async addAutomation(addAutomationDto: AddAutomationDto) {
|
||||
try {
|
||||
const { automationName, effectiveTime, decisionExpr } = addAutomationDto;
|
||||
const space = await this.getSpaceByUuid(addAutomationDto.spaceUuid);
|
||||
|
||||
const actions = await this.processEntities<Action>(
|
||||
addAutomationDto.actions,
|
||||
'actionExecutor',
|
||||
{ [ActionExecutorEnum.DEVICE_ISSUE]: true },
|
||||
this.deviceService,
|
||||
);
|
||||
|
||||
const conditions = await this.processEntities<Condition>(
|
||||
addAutomationDto.conditions,
|
||||
'entityType',
|
||||
{ [EntityTypeEnum.DEVICE_REPORT]: true },
|
||||
this.deviceService,
|
||||
);
|
||||
|
||||
const response = (await this.tuyaService.createAutomation(
|
||||
space.spaceTuyaUuid,
|
||||
const {
|
||||
automationName,
|
||||
effectiveTime,
|
||||
decisionExpr,
|
||||
conditions,
|
||||
actions,
|
||||
)) as AddAutomationInterface;
|
||||
|
||||
return {
|
||||
id: response?.result.id,
|
||||
};
|
||||
conditions,
|
||||
} = addAutomationDto;
|
||||
const space = await this.getSpaceByUuid(addAutomationDto.spaceUuid);
|
||||
const response = await this.add({
|
||||
automationName,
|
||||
effectiveTime,
|
||||
decisionExpr,
|
||||
actions,
|
||||
conditions,
|
||||
spaceTuyaId: space.spaceTuyaUuid,
|
||||
});
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err;
|
||||
} else {
|
||||
@ -94,6 +81,33 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
|
||||
async add(params: AddAutomationParams) {
|
||||
try {
|
||||
const formattedActions = await this.prepareActions(params.actions);
|
||||
const formattedCondition = await this.prepareConditions(
|
||||
params.conditions,
|
||||
);
|
||||
|
||||
const response = await this.tuyaService.createAutomation(
|
||||
params.spaceTuyaId,
|
||||
params.automationName,
|
||||
params.effectiveTime,
|
||||
params.decisionExpr,
|
||||
formattedCondition,
|
||||
formattedActions,
|
||||
);
|
||||
|
||||
return {
|
||||
id: response?.result.id,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to add automation',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getSpaceByUuid(spaceUuid: string) {
|
||||
try {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
@ -120,6 +134,7 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getAutomationBySpace(spaceUuid: string) {
|
||||
try {
|
||||
const space = await this.getSpaceByUuid(spaceUuid);
|
||||
@ -277,34 +292,45 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAutomation(param: DeleteAutomationParamDto, spaceTuyaId = null) {
|
||||
async deleteAutomation(param: AutomationParamDto) {
|
||||
try {
|
||||
const { automationUuid, spaceUuid } = param;
|
||||
let tuyaSpaceId;
|
||||
if (!spaceTuyaId) {
|
||||
const space = await this.getSpaceByUuid(spaceUuid);
|
||||
tuyaSpaceId = space.spaceTuyaUuid;
|
||||
if (!tuyaSpaceId) {
|
||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||
}
|
||||
} else {
|
||||
tuyaSpaceId = spaceTuyaId;
|
||||
const { automationUuid } = param;
|
||||
|
||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
||||
|
||||
if (!automation && !automation.spaceId) {
|
||||
throw new HttpException(
|
||||
`Invalid automationid ${automationUuid}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationUuid}&space_id=${tuyaSpaceId}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'DELETE',
|
||||
path,
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
const response = this.tuyaService.deleteAutomation(
|
||||
automation.spaceId,
|
||||
automationUuid,
|
||||
);
|
||||
return response;
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
if (err instanceof HttpException) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async delete(tuyaSpaceId: string, automationUuid: string) {
|
||||
try {
|
||||
const response = await this.tuyaService.deleteAutomation(
|
||||
tuyaSpaceId,
|
||||
automationUuid,
|
||||
);
|
||||
return response;
|
||||
} catch (err) {
|
||||
if (err instanceof HttpException) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Automation not found',
|
||||
@ -318,28 +344,28 @@ export class AutomationService {
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
automationUuid: string,
|
||||
) {
|
||||
const { actions, conditions, automationName, effectiveTime, decisionExpr } =
|
||||
updateAutomationDto;
|
||||
try {
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationUuid, true);
|
||||
if (!spaceTuyaId.spaceId) {
|
||||
const automation = await this.getAutomationDetails(automationUuid, true);
|
||||
if (!automation.spaceId) {
|
||||
throw new HttpException(
|
||||
"Automation doesn't exist",
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const addAutomation = {
|
||||
...updateAutomationDto,
|
||||
spaceUuid: null,
|
||||
};
|
||||
const newAutomation = await this.addAutomation(
|
||||
addAutomation,
|
||||
spaceTuyaId.spaceId,
|
||||
);
|
||||
const params: DeleteAutomationParamDto = {
|
||||
spaceUuid: spaceTuyaId.spaceId,
|
||||
automationUuid: automationUuid,
|
||||
};
|
||||
|
||||
const newAutomation = await this.add({
|
||||
actions,
|
||||
conditions,
|
||||
automationName,
|
||||
effectiveTime,
|
||||
decisionExpr,
|
||||
spaceTuyaId: automation.spaceId,
|
||||
});
|
||||
|
||||
if (newAutomation.id) {
|
||||
await this.deleteAutomation(null, params);
|
||||
await this.delete(automation.spaceId, automationUuid);
|
||||
return newAutomation;
|
||||
}
|
||||
} catch (err) {
|
||||
@ -357,29 +383,21 @@ export class AutomationService {
|
||||
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||
automationUuid: string,
|
||||
) {
|
||||
const { isEnable, spaceUuid } = updateAutomationStatusDto;
|
||||
try {
|
||||
const space = await this.getSpaceByUuid(
|
||||
updateAutomationStatusDto.spaceUuid,
|
||||
);
|
||||
const space = await this.getSpaceByUuid(spaceUuid);
|
||||
if (!space.spaceTuyaUuid) {
|
||||
throw new BadRequestException(
|
||||
`Invalid space UUID ${updateAutomationStatusDto.spaceUuid}`,
|
||||
throw new HttpException(
|
||||
`Invalid space UUID ${spaceUuid}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule/state?space_id=${space.spaceTuyaUuid}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
ids: automationUuid,
|
||||
is_enable: updateAutomationStatusDto.isEnable,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
const response = await this.tuyaService.updateAutomationState(
|
||||
space.spaceTuyaUuid,
|
||||
automationUuid,
|
||||
isEnable,
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (err) {
|
||||
@ -394,41 +412,41 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
|
||||
async processEntities<T extends Action | Condition>(
|
||||
entities: T[], // Accepts either Action[] or Condition[]
|
||||
lookupKey: keyof T, // The key to look up, specific to T
|
||||
entityTypeOrExecutorMap: {
|
||||
[key in ActionExecutorEnum | EntityTypeEnum]?: boolean;
|
||||
},
|
||||
deviceService: {
|
||||
getDeviceByDeviceUuid: (
|
||||
id: string,
|
||||
flag: boolean,
|
||||
) => Promise<{ deviceTuyaUuid: string } | null>;
|
||||
},
|
||||
): Promise<T[]> {
|
||||
// Returns the same type as provided in the input
|
||||
return Promise.all(
|
||||
entities.map(async (entity) => {
|
||||
// Convert keys to snake case (assuming a utility function exists)
|
||||
const processedEntity = convertKeysToSnakeCase(entity) as T;
|
||||
private async prepareActions(actions: Action[]): Promise<ConvertedAction[]> {
|
||||
const convertedData = convertKeysToSnakeCase(actions) as ConvertedAction[];
|
||||
|
||||
// Check if entity needs device UUID lookup
|
||||
const key = processedEntity[lookupKey];
|
||||
if (
|
||||
entityTypeOrExecutorMap[key as ActionExecutorEnum | EntityTypeEnum]
|
||||
) {
|
||||
const device = await deviceService.getDeviceByDeviceUuid(
|
||||
processedEntity.entityId,
|
||||
await Promise.all(
|
||||
convertedData.map(async (action) => {
|
||||
if (action.action_executor === ActionExecutorEnum.DEVICE_ISSUE) {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
action.entity_id,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
processedEntity.entityId = device.deviceTuyaUuid;
|
||||
action.entity_id = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
|
||||
return processedEntity;
|
||||
}),
|
||||
);
|
||||
|
||||
return convertedData;
|
||||
}
|
||||
|
||||
private async prepareConditions(conditions: Condition[]) {
|
||||
const convertedData = convertKeysToSnakeCase(conditions);
|
||||
await Promise.all(
|
||||
convertedData.map(async (condition) => {
|
||||
if (condition.entity_type === EntityTypeEnum.DEVICE_REPORT) {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
condition.entity_id,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
condition.entity_id = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
return convertedData;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user