From 4a51c9170b5f120d4f3b5797b30145ac827e9152 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:53:39 -0600 Subject: [PATCH] add constants values --- libs/common/src/constants/automation.enum.ts | 10 +++++++++ src/device/services/device.service.ts | 22 ++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/libs/common/src/constants/automation.enum.ts b/libs/common/src/constants/automation.enum.ts index 7e431eb..e9bb6a8 100644 --- a/libs/common/src/constants/automation.enum.ts +++ b/libs/common/src/constants/automation.enum.ts @@ -7,3 +7,13 @@ export enum ActionExecutorEnum { export enum EntityTypeEnum { DEVICE_REPORT = 'device_report', } +export const AUTOMATION_CONFIG = { + DEFAULT_START_TIME: '00:00', + DEFAULT_END_TIME: '23:59', + DEFAULT_LOOPS: '1111111', + DECISION_EXPR: 'and', + CONDITION_TYPE: 'device_report', + ACTION_EXECUTOR: 'rule_trigger', + COMPARATOR: '==', + SCENE_STATUS_VALUE: 'scene', +}; diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 1e44c6d..4d45264 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -54,6 +54,7 @@ import { AddAutomationDto } from 'src/automation/dtos'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories'; import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum'; +import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum'; @Injectable() export class DeviceService { @@ -1220,6 +1221,7 @@ export class DeviceService { return descendants; } + async addSceneToSceneDevice( deviceUuid: string, addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto, @@ -1237,7 +1239,6 @@ export class DeviceService { this.getDeviceByDeviceUuid(deviceUuid), ]); - // Generate a shorter automation name (e.g., "Auto_ABC123_169") const shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds const automationName = `Auto_${shortUuid}_${timestamp}`; @@ -1245,33 +1246,32 @@ export class DeviceService { const addAutomationData: AddAutomationDto = { spaceUuid: spaceData.spaceTuyaUuid, automationName, - decisionExpr: 'and', + decisionExpr: AUTOMATION_CONFIG.DECISION_EXPR, effectiveTime: { - start: '00:00', - end: '23:59', - loops: '1111111', + start: AUTOMATION_CONFIG.DEFAULT_START_TIME, + end: AUTOMATION_CONFIG.DEFAULT_END_TIME, + loops: AUTOMATION_CONFIG.DEFAULT_LOOPS, }, conditions: [ { code: 1, entityId: deviceData.deviceTuyaUuid, - entityType: 'device_report', + entityType: AUTOMATION_CONFIG.CONDITION_TYPE, expr: { - comparator: '==', + comparator: AUTOMATION_CONFIG.COMPARATOR, statusCode: switchName, - statusValue: 'scene', + statusValue: AUTOMATION_CONFIG.SCENE_STATUS_VALUE, }, }, ], actions: [ { - actionExecutor: 'rule_trigger', + actionExecutor: AUTOMATION_CONFIG.ACTION_EXECUTOR, entityId: sceneData.sceneTuyaUuid, }, ], }; - // Create automation const automation = await this.tuyaService.createAutomation( addAutomationData.spaceUuid, addAutomationData.automationName, @@ -1328,7 +1328,7 @@ export class DeviceService { where: { device: { uuid: deviceUuid }, switchName: - getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum, // Cast the string to the enum + getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum, }, relations: ['device', 'scene'], });