mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:44:55 +00:00
add constants values
This commit is contained in:
@ -7,3 +7,13 @@ export enum ActionExecutorEnum {
|
|||||||
export enum EntityTypeEnum {
|
export enum EntityTypeEnum {
|
||||||
DEVICE_REPORT = 'device_report',
|
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',
|
||||||
|
};
|
||||||
|
|||||||
@ -54,6 +54,7 @@ import { AddAutomationDto } from 'src/automation/dtos';
|
|||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
|
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
@ -1220,6 +1221,7 @@ export class DeviceService {
|
|||||||
|
|
||||||
return descendants;
|
return descendants;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addSceneToSceneDevice(
|
async addSceneToSceneDevice(
|
||||||
deviceUuid: string,
|
deviceUuid: string,
|
||||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||||
@ -1237,7 +1239,6 @@ export class DeviceService {
|
|||||||
this.getDeviceByDeviceUuid(deviceUuid),
|
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 shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID
|
||||||
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
|
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
|
||||||
const automationName = `Auto_${shortUuid}_${timestamp}`;
|
const automationName = `Auto_${shortUuid}_${timestamp}`;
|
||||||
@ -1245,33 +1246,32 @@ export class DeviceService {
|
|||||||
const addAutomationData: AddAutomationDto = {
|
const addAutomationData: AddAutomationDto = {
|
||||||
spaceUuid: spaceData.spaceTuyaUuid,
|
spaceUuid: spaceData.spaceTuyaUuid,
|
||||||
automationName,
|
automationName,
|
||||||
decisionExpr: 'and',
|
decisionExpr: AUTOMATION_CONFIG.DECISION_EXPR,
|
||||||
effectiveTime: {
|
effectiveTime: {
|
||||||
start: '00:00',
|
start: AUTOMATION_CONFIG.DEFAULT_START_TIME,
|
||||||
end: '23:59',
|
end: AUTOMATION_CONFIG.DEFAULT_END_TIME,
|
||||||
loops: '1111111',
|
loops: AUTOMATION_CONFIG.DEFAULT_LOOPS,
|
||||||
},
|
},
|
||||||
conditions: [
|
conditions: [
|
||||||
{
|
{
|
||||||
code: 1,
|
code: 1,
|
||||||
entityId: deviceData.deviceTuyaUuid,
|
entityId: deviceData.deviceTuyaUuid,
|
||||||
entityType: 'device_report',
|
entityType: AUTOMATION_CONFIG.CONDITION_TYPE,
|
||||||
expr: {
|
expr: {
|
||||||
comparator: '==',
|
comparator: AUTOMATION_CONFIG.COMPARATOR,
|
||||||
statusCode: switchName,
|
statusCode: switchName,
|
||||||
statusValue: 'scene',
|
statusValue: AUTOMATION_CONFIG.SCENE_STATUS_VALUE,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
actionExecutor: 'rule_trigger',
|
actionExecutor: AUTOMATION_CONFIG.ACTION_EXECUTOR,
|
||||||
entityId: sceneData.sceneTuyaUuid,
|
entityId: sceneData.sceneTuyaUuid,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create automation
|
|
||||||
const automation = await this.tuyaService.createAutomation(
|
const automation = await this.tuyaService.createAutomation(
|
||||||
addAutomationData.spaceUuid,
|
addAutomationData.spaceUuid,
|
||||||
addAutomationData.automationName,
|
addAutomationData.automationName,
|
||||||
@ -1328,7 +1328,7 @@ export class DeviceService {
|
|||||||
where: {
|
where: {
|
||||||
device: { uuid: deviceUuid },
|
device: { uuid: deviceUuid },
|
||||||
switchName:
|
switchName:
|
||||||
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum, // Cast the string to the enum
|
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum,
|
||||||
},
|
},
|
||||||
relations: ['device', 'scene'],
|
relations: ['device', 'scene'],
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user