mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 11:15:14 +00:00
finished update name device api
This commit is contained in:
@ -15,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|||||||
import {
|
import {
|
||||||
AddDeviceDto,
|
AddDeviceDto,
|
||||||
AddSceneToFourSceneDeviceDto,
|
AddSceneToFourSceneDeviceDto,
|
||||||
|
UpdateDeviceDto,
|
||||||
UpdateDeviceInSpaceDto,
|
UpdateDeviceInSpaceDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
||||||
@ -95,6 +96,26 @@ export class DeviceController {
|
|||||||
userUuid,
|
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()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':deviceUuid/functions')
|
@Get(':deviceUuid/functions')
|
||||||
|
@ -60,3 +60,12 @@ export class AddSceneToFourSceneDeviceDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public spaceUuid: string;
|
public spaceUuid: string;
|
||||||
}
|
}
|
||||||
|
export class UpdateDeviceDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'deviceName',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceName: string;
|
||||||
|
}
|
||||||
|
@ -13,6 +13,7 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
import {
|
import {
|
||||||
AddDeviceDto,
|
AddDeviceDto,
|
||||||
AddSceneToFourSceneDeviceDto,
|
AddSceneToFourSceneDeviceDto,
|
||||||
|
UpdateDeviceDto,
|
||||||
UpdateDeviceInSpaceDto,
|
UpdateDeviceInSpaceDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
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) {
|
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid, false);
|
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(
|
async getDeviceDetailsByDeviceIdTuya(
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
): Promise<GetDeviceDetailsInterface> {
|
): Promise<GetDeviceDetailsInterface> {
|
||||||
|
Reference in New Issue
Block a user