mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
add automation
This commit is contained in:
@ -16,9 +16,11 @@ import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
Action,
|
||||
AddAutomationInterface,
|
||||
AutomationDetailsResult,
|
||||
AutomationResponseData,
|
||||
Condition,
|
||||
DeleteAutomationInterface,
|
||||
GetAutomationBySpaceInterface,
|
||||
} from '../interface/automation.interface';
|
||||
@ -27,6 +29,7 @@ import {
|
||||
ActionExecutorEnum,
|
||||
EntityTypeEnum,
|
||||
} from '@app/common/constants/automation.enum';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
|
||||
@Injectable()
|
||||
export class AutomationService {
|
||||
@ -35,6 +38,7 @@ export class AutomationService {
|
||||
private readonly configService: ConfigService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -48,77 +52,39 @@ export class AutomationService {
|
||||
|
||||
async addAutomation(addAutomationDto: AddAutomationDto, spaceTuyaId = null) {
|
||||
try {
|
||||
let tuyaSpaceId;
|
||||
if (!spaceTuyaId) {
|
||||
const space = await this.getSpaceByUuid(addAutomationDto.spaceUuid);
|
||||
const { automationName, effectiveTime, decisionExpr } = addAutomationDto;
|
||||
const space = await this.getSpaceByUuid(addAutomationDto.spaceUuid);
|
||||
|
||||
tuyaSpaceId = space.spaceTuyaUuid;
|
||||
if (!space) {
|
||||
throw new BadRequestException(
|
||||
`Invalid space UUID ${addAutomationDto.spaceUuid}`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
tuyaSpaceId = spaceTuyaId;
|
||||
}
|
||||
|
||||
const actions = addAutomationDto.actions.map((action) =>
|
||||
convertKeysToSnakeCase(action),
|
||||
);
|
||||
const conditions = addAutomationDto.conditions.map((condition) =>
|
||||
convertKeysToSnakeCase(condition),
|
||||
const actions = await this.processEntities<Action>(
|
||||
addAutomationDto.actions,
|
||||
'actionExecutor',
|
||||
{ [ActionExecutorEnum.DEVICE_ISSUE]: true },
|
||||
this.deviceService,
|
||||
);
|
||||
|
||||
for (const action of actions) {
|
||||
if (action.action_executor === ActionExecutorEnum.DEVICE_ISSUE) {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
action.entity_id,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
action.entity_id = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
const conditions = await this.processEntities<Condition>(
|
||||
addAutomationDto.conditions,
|
||||
'entityType',
|
||||
{ [EntityTypeEnum.DEVICE_REPORT]: true },
|
||||
this.deviceService,
|
||||
);
|
||||
|
||||
for (const condition of conditions) {
|
||||
if (condition.entity_type === EntityTypeEnum.DEVICE_REPORT) {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
condition.entity_id,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
condition.entity_id = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
const response = (await this.tuyaService.createAutomation(
|
||||
space.spaceTuyaUuid,
|
||||
automationName,
|
||||
effectiveTime,
|
||||
decisionExpr,
|
||||
conditions,
|
||||
actions,
|
||||
)) as AddAutomationInterface;
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule`;
|
||||
const response: AddAutomationInterface = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: {
|
||||
space_id: tuyaSpaceId,
|
||||
name: addAutomationDto.automationName,
|
||||
effective_time: {
|
||||
...addAutomationDto.effectiveTime,
|
||||
timezone_id: 'Asia/Dubai',
|
||||
},
|
||||
type: 'automation',
|
||||
decision_expr: addAutomationDto.decisionExpr,
|
||||
conditions: conditions,
|
||||
actions: actions,
|
||||
},
|
||||
});
|
||||
if (!response.success) {
|
||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return {
|
||||
id: response.result.id,
|
||||
id: response?.result.id,
|
||||
};
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Automation not found',
|
||||
@ -127,6 +93,7 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getSpaceByUuid(spaceUuid: string) {
|
||||
try {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
@ -189,6 +156,7 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getTapToRunSceneDetailsTuya(
|
||||
sceneUuid: string,
|
||||
): Promise<AutomationDetailsResult> {
|
||||
@ -425,4 +393,42 @@ 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;
|
||||
|
||||
// Check if entity needs device UUID lookup
|
||||
const key = processedEntity[lookupKey];
|
||||
if (
|
||||
entityTypeOrExecutorMap[key as ActionExecutorEnum | EntityTypeEnum]
|
||||
) {
|
||||
const device = await deviceService.getDeviceByDeviceUuid(
|
||||
processedEntity.entityId,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
processedEntity.entityId = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
|
||||
return processedEntity;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user