mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Refactor device service methods for better error handling and readability
This commit is contained in:
@ -6,11 +6,13 @@ import {
|
|||||||
AddDeviceInRoomDto,
|
AddDeviceInRoomDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
import {
|
||||||
|
DeviceInstructionResponse,
|
||||||
GetDeviceDetailsFunctionsInterface,
|
GetDeviceDetailsFunctionsInterface,
|
||||||
GetDeviceDetailsFunctionsStatusInterface,
|
GetDeviceDetailsFunctionsStatusInterface,
|
||||||
GetDeviceDetailsInterface,
|
GetDeviceDetailsInterface,
|
||||||
GetDevicesByGroupIdInterface,
|
GetDevicesByGroupIdInterface,
|
||||||
GetDevicesByRoomIdInterface,
|
GetDevicesByRoomIdInterface,
|
||||||
|
GetProductInterface,
|
||||||
addDeviceInRoomInterface,
|
addDeviceInRoomInterface,
|
||||||
controlDeviceInterface,
|
controlDeviceInterface,
|
||||||
} from '../interfaces/get.device.interface';
|
} from '../interfaces/get.device.interface';
|
||||||
@ -19,11 +21,16 @@ import {
|
|||||||
GetDeviceByRoomIdDto,
|
GetDeviceByRoomIdDto,
|
||||||
} from '../dtos/get.device.dto';
|
} from '../dtos/get.device.dto';
|
||||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||||
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
private readonly productRepository: ProductRepository,
|
||||||
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
||||||
@ -45,7 +52,7 @@ export class DeviceService {
|
|||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching devices',
|
'Error fetching devices by room',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -67,7 +74,7 @@ export class DeviceService {
|
|||||||
return response as GetDevicesByRoomIdInterface;
|
return response as GetDevicesByRoomIdInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching devices ',
|
'Error fetching devices by room from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -86,7 +93,7 @@ export class DeviceService {
|
|||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching devices',
|
'Error fetching devices by group',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -109,7 +116,7 @@ export class DeviceService {
|
|||||||
return response as GetDevicesByGroupIdInterface;
|
return response as GetDevicesByGroupIdInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching devices ',
|
'Error fetching devices by group from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -147,7 +154,7 @@ export class DeviceService {
|
|||||||
return response as addDeviceInRoomInterface;
|
return response as addDeviceInRoomInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error adding device',
|
'Error adding device in room from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -185,7 +192,7 @@ export class DeviceService {
|
|||||||
return response as addDeviceInRoomInterface;
|
return response as addDeviceInRoomInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error adding device',
|
'Error adding device in group from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -221,15 +228,15 @@ export class DeviceService {
|
|||||||
return response as controlDeviceInterface;
|
return response as controlDeviceInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error control device',
|
'Error control device from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDevicesByDeviceId(deviceId: string) {
|
async getDeviceDetailsByDeviceId(deviceId: string) {
|
||||||
try {
|
try {
|
||||||
const response = await this.getDevicesByDeviceIdTuya(deviceId);
|
const response = await this.getDeviceDetailsByDeviceIdTuya(deviceId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: response.success,
|
success: response.success,
|
||||||
@ -238,13 +245,12 @@ export class DeviceService {
|
|||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device',
|
'Error fetching device details',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getDeviceDetailsByDeviceIdTuya(
|
||||||
async getDevicesByDeviceIdTuya(
|
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
): Promise<GetDeviceDetailsInterface> {
|
): Promise<GetDeviceDetailsInterface> {
|
||||||
try {
|
try {
|
||||||
@ -253,35 +259,110 @@ export class DeviceService {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
return response as GetDeviceDetailsInterface;
|
|
||||||
|
// Convert keys to camel case
|
||||||
|
const camelCaseResponse = convertKeysToCamelCase(response);
|
||||||
|
const productType: string = await this.getProductTypeByProductId(
|
||||||
|
camelCaseResponse.result.productId,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
result: {
|
||||||
|
...camelCaseResponse.result,
|
||||||
|
productType: productType,
|
||||||
|
},
|
||||||
|
success: camelCaseResponse.success,
|
||||||
|
msg: camelCaseResponse.msg,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device ',
|
'Error fetching device details from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getDevicesInstructionByDeviceId(deviceId: string) {
|
async getProductIdByDeviceId(deviceId: string) {
|
||||||
try {
|
try {
|
||||||
const response = await this.getDevicesInstructionByDeviceIdTuya(deviceId);
|
const deviceDetails: GetDeviceDetailsInterface =
|
||||||
|
await this.getDeviceDetailsByDeviceId(deviceId);
|
||||||
|
|
||||||
|
return deviceDetails.result.productId;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching product id by device id',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getProductByProductId(productId: string): Promise<GetProductInterface> {
|
||||||
|
try {
|
||||||
|
const product = await this.productRepository
|
||||||
|
.createQueryBuilder('product')
|
||||||
|
.where('product.prodId = :productId', { productId })
|
||||||
|
.select(['product.prodId', 'product.prodType'])
|
||||||
|
.getOne();
|
||||||
|
|
||||||
|
if (product) {
|
||||||
|
return {
|
||||||
|
productType: product.prodType,
|
||||||
|
productId: product.prodId,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Product not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching product by product id from db',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getProductTypeByProductId(productId: string) {
|
||||||
|
try {
|
||||||
|
const product = await this.getProductByProductId(productId);
|
||||||
|
return product.productType;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error getting product type by product id',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getDeviceInstructionByDeviceId(
|
||||||
|
deviceId: string,
|
||||||
|
): Promise<DeviceInstructionResponse> {
|
||||||
|
try {
|
||||||
|
const response = await this.getDeviceInstructionByDeviceIdTuya(deviceId);
|
||||||
|
|
||||||
|
const productId: string = await this.getProductIdByDeviceId(deviceId);
|
||||||
|
const productType: string =
|
||||||
|
await this.getProductTypeByProductId(productId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: response.success,
|
success: response.success,
|
||||||
result: {
|
result: {
|
||||||
category: response.result.category,
|
productId: productId,
|
||||||
function: response.result.functions,
|
productType: productType,
|
||||||
|
functions: response.result.functions.map((fun: any) => {
|
||||||
|
return {
|
||||||
|
code: fun.code,
|
||||||
|
values: fun.values,
|
||||||
|
dataType: fun.type,
|
||||||
|
};
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
msg: response.msg,
|
msg: response.msg,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device functions',
|
'Error fetching device functions by device id',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDevicesInstructionByDeviceIdTuya(
|
async getDeviceInstructionByDeviceIdTuya(
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
): Promise<GetDeviceDetailsFunctionsInterface> {
|
): Promise<GetDeviceDetailsFunctionsInterface> {
|
||||||
try {
|
try {
|
||||||
@ -293,7 +374,7 @@ export class DeviceService {
|
|||||||
return response as GetDeviceDetailsFunctionsInterface;
|
return response as GetDeviceDetailsFunctionsInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device functions',
|
'Error fetching device functions from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -309,7 +390,7 @@ export class DeviceService {
|
|||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device functions',
|
'Error fetching device functions status',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -330,7 +411,7 @@ export class DeviceService {
|
|||||||
return response as unknown as GetDeviceDetailsFunctionsStatusInterface;
|
return response as unknown as GetDeviceDetailsFunctionsStatusInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error fetching device functions',
|
'Error fetching device functions status from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user