mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
Add automation module
This commit is contained in:
@ -5,16 +5,17 @@ import {
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { AddAutomationDto, UpdateSceneTapToRunDto } from '../dtos';
|
||||
import { UpdateAutomationDto, UpdateAutomationStatusDto } from '../dtos';
|
||||
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
AddTapToRunSceneInterface,
|
||||
DeleteTapToRunSceneInterface,
|
||||
GetTapToRunSceneByUnitInterface,
|
||||
AddAutomationInterface,
|
||||
AutomationResponseData,
|
||||
DeleteAutomationInterface,
|
||||
GetAutomationByUnitInterface,
|
||||
} from '../interface/automation.interface';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
|
||||
@ -36,11 +37,12 @@ export class AutomationService {
|
||||
});
|
||||
}
|
||||
|
||||
async addAutomation(addAutomationDto: AddAutomationDto, spaceTuyaId = null) {
|
||||
async addAutomation(addAutomationDto, spaceTuyaId = null) {
|
||||
try {
|
||||
let unitSpaceTuyaId;
|
||||
if (!spaceTuyaId) {
|
||||
const unitDetails = await this.getUnitByUuid(addAutomationDto.unitUuid);
|
||||
|
||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||
if (!unitDetails) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
@ -48,14 +50,15 @@ export class AutomationService {
|
||||
} else {
|
||||
unitSpaceTuyaId = spaceTuyaId;
|
||||
}
|
||||
const actions = addAutomationDto.actions.map((action) => {
|
||||
return {
|
||||
...action,
|
||||
};
|
||||
});
|
||||
|
||||
const convertedData = convertKeysToSnakeCase(actions);
|
||||
for (const action of convertedData) {
|
||||
const actions = addAutomationDto.actions.map((action) =>
|
||||
convertKeysToSnakeCase(action),
|
||||
);
|
||||
const conditions = addAutomationDto.conditions.map((condition) =>
|
||||
convertKeysToSnakeCase(condition),
|
||||
);
|
||||
|
||||
for (const action of actions) {
|
||||
if (action.action_executor === 'device_issue') {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
action.entity_id,
|
||||
@ -66,16 +69,34 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const condition of conditions) {
|
||||
if (condition.entity_type === 'device_report') {
|
||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||
condition.entity_id,
|
||||
false,
|
||||
);
|
||||
if (device) {
|
||||
condition.entity_id = device.deviceTuyaUuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule`;
|
||||
const response: AddTapToRunSceneInterface = await this.tuya.request({
|
||||
const response: AddAutomationInterface = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: {
|
||||
space_id: unitSpaceTuyaId,
|
||||
name: addAutomationDto.sceneName,
|
||||
type: 'scene',
|
||||
name: addAutomationDto.name,
|
||||
effective_time: {
|
||||
...addAutomationDto.effectiveTime,
|
||||
timezone_id: 'Asia/Dubai',
|
||||
},
|
||||
type: 'automation',
|
||||
decision_expr: addAutomationDto.decisionExpr,
|
||||
actions: convertedData,
|
||||
conditions: conditions,
|
||||
actions: actions,
|
||||
},
|
||||
});
|
||||
if (!response.success) {
|
||||
@ -89,7 +110,7 @@ export class AutomationService {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Scene not found',
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
@ -125,20 +146,18 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async getTapToRunSceneByUnit(unitUuid: string) {
|
||||
async getAutomationByUnit(unitUuid: string) {
|
||||
try {
|
||||
const unit = await this.getUnitByUuid(unitUuid);
|
||||
if (!unit.spaceTuyaUuid) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=scene`;
|
||||
const response: GetTapToRunSceneByUnitInterface = await this.tuya.request(
|
||||
{
|
||||
method: 'GET',
|
||||
path,
|
||||
},
|
||||
);
|
||||
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=automation`;
|
||||
const response: GetAutomationByUnitInterface = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||
@ -149,7 +168,7 @@ export class AutomationService {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
status: item.status,
|
||||
type: 'tap_to_run',
|
||||
type: 'automation',
|
||||
};
|
||||
});
|
||||
} catch (err) {
|
||||
@ -157,15 +176,91 @@ export class AutomationService {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Scene not found',
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async deleteTapToRunScene(
|
||||
async getAutomationDetails(automationId: string, withSpaceId = false) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${automationId}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
const responseData: AutomationResponseData = convertKeysToCamelCase(
|
||||
response.result,
|
||||
);
|
||||
|
||||
const actions = responseData.actions.map((action) => ({
|
||||
...action,
|
||||
}));
|
||||
|
||||
for (const action of actions) {
|
||||
if (action.actionExecutor === 'device_issue') {
|
||||
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
||||
action.entityId,
|
||||
);
|
||||
|
||||
if (device) {
|
||||
action.entityId = device.uuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const conditions = responseData.conditions.map((condition) => ({
|
||||
...condition,
|
||||
}));
|
||||
|
||||
for (const condition of conditions) {
|
||||
if (condition.entityType === 'device_report') {
|
||||
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
||||
condition.entityId,
|
||||
);
|
||||
|
||||
if (device) {
|
||||
condition.entityId = device.uuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { timeZoneId, ...effectiveTimeWithoutTimeZoneId } =
|
||||
responseData.effectiveTime || {};
|
||||
|
||||
return {
|
||||
id: responseData.id,
|
||||
name: responseData.name,
|
||||
status: responseData.status,
|
||||
type: 'automation',
|
||||
...(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { spaceId, runningMode, ...rest } = responseData;
|
||||
return rest;
|
||||
})(),
|
||||
actions,
|
||||
conditions,
|
||||
effectiveTime: effectiveTimeWithoutTimeZoneId, // Use modified effectiveTime
|
||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAutomation(
|
||||
unitUuid: string,
|
||||
sceneId: string,
|
||||
automationId: string,
|
||||
spaceTuyaId = null,
|
||||
) {
|
||||
try {
|
||||
@ -180,14 +275,14 @@ export class AutomationService {
|
||||
unitSpaceTuyaId = spaceTuyaId;
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${unitSpaceTuyaId}`;
|
||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationId}&space_id=${unitSpaceTuyaId}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'DELETE',
|
||||
path,
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return response;
|
||||
@ -196,106 +291,81 @@ export class AutomationService {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Scene not found',
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async triggerTapToRunScene(sceneId: string) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
|
||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
});
|
||||
if (!response.success) {
|
||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return response;
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Scene not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async getTapToRunSceneDetails(sceneId: string, withSpaceId = false) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${sceneId}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
if (!response.success) {
|
||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const responseData = convertKeysToCamelCase(response.result);
|
||||
const actions = responseData.actions.map((action) => {
|
||||
return {
|
||||
...action,
|
||||
};
|
||||
});
|
||||
|
||||
for (const action of actions) {
|
||||
if (action.actionExecutor === 'device_issue') {
|
||||
const device = await this.deviceService.getDeviceByDeviceTuyaUuid(
|
||||
action.entityId,
|
||||
);
|
||||
|
||||
if (device) {
|
||||
action.entityId = device.uuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: responseData.id,
|
||||
name: responseData.name,
|
||||
status: responseData.status,
|
||||
type: 'tap_to_run',
|
||||
actions: actions,
|
||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
async updateTapToRunScene(
|
||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||
sceneId: string,
|
||||
async updateAutomation(
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
automationId: string,
|
||||
) {
|
||||
try {
|
||||
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneId, true);
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationId, true);
|
||||
if (!spaceTuyaId.spaceId) {
|
||||
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
||||
throw new HttpException(
|
||||
"Automation doesn't exist",
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const addSceneTapToRunDto: AddAutomationDto = {
|
||||
...updateSceneTapToRunDto,
|
||||
const addAutomation = {
|
||||
...updateAutomationDto,
|
||||
unitUuid: null,
|
||||
};
|
||||
const newTapToRunScene = await this.addAutomation(
|
||||
addSceneTapToRunDto,
|
||||
const newAutomation = await this.addAutomation(
|
||||
addAutomation,
|
||||
spaceTuyaId.spaceId,
|
||||
);
|
||||
if (newTapToRunScene.id) {
|
||||
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
||||
return newTapToRunScene;
|
||||
if (newAutomation.id) {
|
||||
await this.deleteAutomation(null, automationId, spaceTuyaId.spaceId);
|
||||
return newAutomation;
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Scene not found',
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async updateAutomationStatus(
|
||||
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||
automationId: string,
|
||||
) {
|
||||
try {
|
||||
const unitDetails = await this.getUnitByUuid(
|
||||
updateAutomationStatusDto.unitUuid,
|
||||
);
|
||||
if (!unitDetails.spaceTuyaUuid) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule/state?space_id=${unitDetails.spaceTuyaUuid}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
ids: automationId,
|
||||
is_enable: updateAutomationStatusDto.isEnable,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new HttpException('Automation not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException(
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user