mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 05:37:55 +00:00
feat: add device name field to DeviceEntity and update related DTOs and services
This commit is contained in:
@@ -17,6 +17,14 @@ export class AddDeviceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public spaceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'deviceName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceName: string;
|
||||
}
|
||||
export class AssignDeviceToSpaceDto {
|
||||
@ApiProperty({
|
||||
|
||||
@@ -159,6 +159,7 @@ export class DeviceService {
|
||||
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
|
||||
productDevice: { uuid: device.productUuid },
|
||||
spaceDevice: { uuid: addDeviceDto.spaceUuid },
|
||||
name: addDeviceDto.deviceName,
|
||||
});
|
||||
if (deviceSaved.uuid) {
|
||||
const deviceStatus: BaseResponseDto =
|
||||
@@ -299,22 +300,15 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateDeviceNameTuya(
|
||||
deviceId: string,
|
||||
deviceName: string,
|
||||
): Promise<controlDeviceInterface> {
|
||||
async updateDeviceName(deviceUuid: string, deviceName: string) {
|
||||
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;
|
||||
await this.deviceRepository.update(
|
||||
{ uuid: deviceUuid },
|
||||
{ name: deviceName },
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error updating device name from Tuya',
|
||||
'Error updating device name',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
@@ -643,11 +637,8 @@ export class DeviceService {
|
||||
true,
|
||||
projectUuid,
|
||||
);
|
||||
if (device.deviceTuyaUuid) {
|
||||
await this.updateDeviceNameTuya(
|
||||
device.deviceTuyaUuid,
|
||||
updateDeviceDto.deviceName,
|
||||
);
|
||||
if (device.uuid) {
|
||||
await this.updateDeviceName(deviceUuid, updateDeviceDto.deviceName);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
@@ -682,15 +673,22 @@ export class DeviceService {
|
||||
prodId: camelCaseResponse.result.productId,
|
||||
},
|
||||
});
|
||||
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
deviceTuyaUuid: deviceId,
|
||||
},
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { productId, id, ...rest } = camelCaseResponse.result;
|
||||
const { productId, id, name, ...rest } = camelCaseResponse.result;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
productUuid: product.uuid,
|
||||
name: deviceDetails.name,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
console.log('error', error);
|
||||
|
||||
throw new HttpException(
|
||||
'Error fetching device details from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
|
||||
Reference in New Issue
Block a user