finished update name device api

This commit is contained in:
faris Aljohari
2024-11-16 23:36:10 -06:00
parent c7a6986292
commit 6d73968631
3 changed files with 72 additions and 0 deletions

View File

@ -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')

View File

@ -60,3 +60,12 @@ export class AddSceneToFourSceneDeviceDto {
@IsNotEmpty()
public spaceUuid: string;
}
export class UpdateDeviceDto {
@ApiProperty({
description: 'deviceName',
required: true,
})
@IsString()
@IsNotEmpty()
public deviceName: string;
}

View File

@ -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<controlDeviceInterface> {
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<GetDeviceDetailsInterface> {