mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
fix: commission device API
This commit is contained in:
@ -323,7 +323,7 @@ export class DeviceService {
|
||||
|
||||
async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) {
|
||||
try {
|
||||
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
const device = await this.getNewDeviceDetailsFromTuya(
|
||||
addDeviceDto.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
@ -349,6 +349,7 @@ export class DeviceService {
|
||||
spaceDevice: { uuid: addDeviceDto.spaceUuid },
|
||||
tag: { uuid: addDeviceDto.tagUuid },
|
||||
name: addDeviceDto.deviceName,
|
||||
deviceTuyaConstUuid: device.uuid,
|
||||
});
|
||||
if (deviceSaved.uuid) {
|
||||
const deviceStatus: BaseResponseDto =
|
||||
@ -752,6 +753,45 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getNewDeviceDetailsFromTuya(
|
||||
deviceId: string,
|
||||
): Promise<GetDeviceDetailsInterface> {
|
||||
console.log('fetching device details from Tuya for deviceId:', deviceId);
|
||||
try {
|
||||
const result = await this.tuyaService.getDeviceDetails(deviceId);
|
||||
|
||||
if (!result) {
|
||||
throw new NotFoundException('Device not found');
|
||||
}
|
||||
// Convert keys to camel case
|
||||
const camelCaseResponse = convertKeysToCamelCase(result);
|
||||
|
||||
const product = await this.productRepository.findOne({
|
||||
where: { prodId: camelCaseResponse.productId },
|
||||
});
|
||||
|
||||
if (!product) {
|
||||
throw new NotFoundException('Product Type is not supported');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { productId, id, productName, ...rest } = camelCaseResponse;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
productUuid: product.uuid,
|
||||
productName: product.name,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
console.log('error', error);
|
||||
|
||||
throw new HttpException(
|
||||
error.message || 'Error fetching device details from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDeviceInstructionByDeviceId(
|
||||
deviceUuid: string,
|
||||
projectUuid: string,
|
||||
|
Reference in New Issue
Block a user