mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 09:54:55 +00:00
finished 4scene configration endpoints
This commit is contained in:
@ -5,10 +5,16 @@ import {
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
forwardRef,
|
||||
Inject,
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddSceneToFourSceneDeviceDto,
|
||||
UpdateDeviceInSpaceDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import {
|
||||
DeviceInstructionResponse,
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
@ -28,6 +34,7 @@ import {
|
||||
BatchFactoryResetDevicesDto,
|
||||
BatchStatusDevicesDto,
|
||||
ControlDeviceDto,
|
||||
GetSceneFourSceneDeviceDto,
|
||||
} from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
@ -40,6 +47,11 @@ import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
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 { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -47,9 +59,13 @@ export class DeviceService {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
@Inject(forwardRef(() => SceneService))
|
||||
private readonly sceneService: SceneService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -630,7 +646,6 @@ export class DeviceService {
|
||||
status: deviceStatus.result[0].status,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw new HttpException(
|
||||
'Error fetching device functions status',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
@ -1136,4 +1151,134 @@ export class DeviceService {
|
||||
|
||||
return descendants;
|
||||
}
|
||||
async addSceneToFourSceneDevice(
|
||||
deviceUuid: string,
|
||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
) {
|
||||
try {
|
||||
const { spaceUuid, sceneUuid, switchName } = addSceneToFourSceneDeviceDto;
|
||||
|
||||
if (!spaceUuid || !sceneUuid || !switchName) {
|
||||
throw new BadRequestException('Missing required fields in DTO');
|
||||
}
|
||||
|
||||
const [sceneData, spaceData, deviceData] = await Promise.all([
|
||||
this.sceneService.findScene(sceneUuid),
|
||||
this.sceneService.getSpaceByUuid(spaceUuid),
|
||||
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}`;
|
||||
|
||||
const addAutomationData: AddAutomationDto = {
|
||||
spaceUuid: spaceData.spaceTuyaUuid,
|
||||
automationName,
|
||||
decisionExpr: 'and',
|
||||
effectiveTime: {
|
||||
start: '00:00',
|
||||
end: '23:59',
|
||||
loops: '1111111',
|
||||
},
|
||||
conditions: [
|
||||
{
|
||||
code: 1,
|
||||
entityId: deviceData.deviceTuyaUuid,
|
||||
entityType: 'device_report',
|
||||
expr: {
|
||||
comparator: '==',
|
||||
statusCode: switchName,
|
||||
statusValue: 'scene',
|
||||
},
|
||||
},
|
||||
],
|
||||
actions: [
|
||||
{
|
||||
actionExecutor: 'rule_trigger',
|
||||
entityId: sceneData.sceneTuyaUuid,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Create automation
|
||||
const automation = await this.tuyaService.createAutomation(
|
||||
addAutomationData.spaceUuid,
|
||||
addAutomationData.automationName,
|
||||
addAutomationData.effectiveTime,
|
||||
addAutomationData.decisionExpr,
|
||||
addAutomationData.conditions,
|
||||
addAutomationData.actions,
|
||||
);
|
||||
|
||||
if (automation.success) {
|
||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||
where: {
|
||||
device: { uuid: deviceUuid },
|
||||
scene: { uuid: sceneUuid },
|
||||
switchName: switchName,
|
||||
},
|
||||
relations: ['scene', 'device'],
|
||||
});
|
||||
|
||||
if (existingSceneDevice) {
|
||||
await this.tuyaService.deleteAutomation(
|
||||
spaceData.spaceTuyaUuid,
|
||||
existingSceneDevice.automationTuyaUuid,
|
||||
);
|
||||
|
||||
existingSceneDevice.automationTuyaUuid = automation.result.id;
|
||||
existingSceneDevice.scene = sceneData;
|
||||
existingSceneDevice.device = deviceData;
|
||||
existingSceneDevice.switchName = switchName;
|
||||
|
||||
return await this.sceneDeviceRepository.save(existingSceneDevice);
|
||||
} else {
|
||||
const sceneDevice = await this.sceneDeviceRepository.save({
|
||||
scene: sceneData,
|
||||
device: deviceData,
|
||||
automationTuyaUuid: automation.result.id,
|
||||
switchName: switchName,
|
||||
});
|
||||
return sceneDevice;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
const errorMessage = err.message || 'Error creating automation';
|
||||
const errorStatus = err.status || HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
throw new HttpException(errorMessage, errorStatus);
|
||||
}
|
||||
}
|
||||
async getSceneFourSceneDevice(
|
||||
deviceUuid: string,
|
||||
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||
) {
|
||||
try {
|
||||
const sceneDevice = await this.sceneDeviceRepository.findOne({
|
||||
where: {
|
||||
device: { uuid: deviceUuid },
|
||||
switchName:
|
||||
getSceneFourSceneDeviceDto.switchName as FourSceneSwitchesEnum, // Cast the string to the enum
|
||||
},
|
||||
relations: ['device', 'scene'],
|
||||
});
|
||||
if (sceneDevice.uuid) {
|
||||
const SceneDetails = await this.sceneService.getSceneByUuid(
|
||||
sceneDevice.scene.uuid,
|
||||
);
|
||||
return {
|
||||
switchName: sceneDevice.switchName,
|
||||
createdAt: sceneDevice.createdAt,
|
||||
updatedAt: sceneDevice.updatedAt,
|
||||
deviceUuid: sceneDevice.device.uuid,
|
||||
scene: {
|
||||
...SceneDetails.data,
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user