diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 15bf622..cf977d2 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -15,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { AddDeviceDto, AddSceneToFourSceneDeviceDto, + UpdateDeviceDto, UpdateDeviceInSpaceDto, } from '../dtos/add.device.dto'; import { GetDeviceLogsDto } from '../dtos/get.device.dto'; @@ -95,6 +96,26 @@ export class DeviceController { userUuid, ); } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put(':deviceUuid') + async updateDevice( + @Param('deviceUuid') deviceUuid: string, + @Body() updateDeviceDto: UpdateDeviceDto, + ) { + const device = await this.deviceService.updateDevice( + deviceUuid, + updateDeviceDto, + ); + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'device updated successfully', + data: device, + }; + } + @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get(':deviceUuid/functions') diff --git a/src/device/dtos/add.device.dto.ts b/src/device/dtos/add.device.dto.ts index b5884a8..bdc76f6 100644 --- a/src/device/dtos/add.device.dto.ts +++ b/src/device/dtos/add.device.dto.ts @@ -60,3 +60,12 @@ export class AddSceneToFourSceneDeviceDto { @IsNotEmpty() public spaceUuid: string; } +export class UpdateDeviceDto { + @ApiProperty({ + description: 'deviceName', + required: true, + }) + @IsString() + @IsNotEmpty() + public deviceName: string; +} diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 81ae501..0216cc7 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -13,6 +13,7 @@ import { ConfigService } from '@nestjs/config'; import { AddDeviceDto, AddSceneToFourSceneDeviceDto, + UpdateDeviceDto, UpdateDeviceInSpaceDto, } from '../dtos/add.device.dto'; import { @@ -291,6 +292,26 @@ export class DeviceService { ); } } + async updateDeviceNameTuya( + deviceId: string, + deviceName: string, + ): Promise { + try { + const path = `/v2.0/cloud/thing/${deviceId}/attribute`; + const response = await this.tuya.request({ + method: 'POST', + path, + body: { type: 1, data: deviceName }, + }); + + return response as controlDeviceInterface; + } catch (error) { + throw new HttpException( + 'Error updating device name from Tuya', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) { try { const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid, false); @@ -555,6 +576,27 @@ export class DeviceService { ); } } + async updateDevice(deviceUuid: string, updateDeviceDto: UpdateDeviceDto) { + try { + const device = await this.getDeviceByDeviceUuid(deviceUuid); + if (device.deviceTuyaUuid) { + await this.updateDeviceNameTuya( + device.deviceTuyaUuid, + updateDeviceDto.deviceName, + ); + } + + return { + uuid: device.uuid, + deviceName: updateDeviceDto.deviceName, + }; + } catch (error) { + throw new HttpException( + 'Error updating device', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } async getDeviceDetailsByDeviceIdTuya( deviceId: string, ): Promise {