mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
feat: add device name field to DeviceEntity and update related DTOs and services
This commit is contained in:
@ -1,21 +1,21 @@
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
||||
import { GetSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
|
||||
import { ValidationService } from './space-validation.service';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceDeviceService {
|
||||
constructor(
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly deviceService: DeviceService,
|
||||
) {}
|
||||
|
||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||
@ -60,7 +60,9 @@ export class SpaceDeviceService {
|
||||
try {
|
||||
// Fetch Tuya details in parallel
|
||||
const [tuyaDetails, tuyaDeviceStatusResponse] = await Promise.all([
|
||||
this.getDeviceDetailsByDeviceIdTuya(device.deviceTuyaUuid),
|
||||
this.deviceService.getDeviceDetailsByDeviceIdTuya(
|
||||
device.deviceTuyaUuid,
|
||||
),
|
||||
this.tuyaService.getDevicesInstructionStatusTuya(device.deviceTuyaUuid),
|
||||
]);
|
||||
|
||||
@ -119,28 +121,4 @@ export class SpaceDeviceService {
|
||||
);
|
||||
return batteryStatus ? batteryStatus.value : null;
|
||||
}
|
||||
|
||||
private async getDeviceDetailsByDeviceIdTuya(
|
||||
deviceId: string,
|
||||
): Promise<GetDeviceDetailsInterface> {
|
||||
try {
|
||||
const tuyaDeviceDetails =
|
||||
await this.tuyaService.getDeviceDetails(deviceId);
|
||||
|
||||
// Convert keys to camel case
|
||||
const camelCaseResponse = convertKeysToCamelCase(tuyaDeviceDetails);
|
||||
|
||||
// Exclude specific keys and add `productUuid`
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { uuid, ...rest } = camelCaseResponse;
|
||||
return {
|
||||
...rest,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`Error fetching device details from Tuya for device id ${deviceId}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,23 +3,19 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { GetDeviceDetailsInterface } from '../../../device/interfaces/get.device.interface';
|
||||
import { ValidationService } from '../space-validation.service';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { TagRepository } from '@app/common/modules/space';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
|
||||
@Injectable()
|
||||
export class SubspaceDeviceService {
|
||||
constructor(
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly tagRepository: TagRepository,
|
||||
) {}
|
||||
@ -39,9 +35,10 @@ export class SubspaceDeviceService {
|
||||
|
||||
const safeFetch = async (device: any) => {
|
||||
try {
|
||||
const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
device.deviceTuyaUuid,
|
||||
);
|
||||
const tuyaDetails =
|
||||
await this.deviceService.getDeviceDetailsByDeviceIdTuya(
|
||||
device.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
uuid: device.uuid,
|
||||
@ -205,35 +202,6 @@ export class SubspaceDeviceService {
|
||||
);
|
||||
}
|
||||
|
||||
private async getDeviceDetailsByDeviceIdTuya(
|
||||
deviceId: string,
|
||||
): Promise<GetDeviceDetailsInterface> {
|
||||
try {
|
||||
const tuyaDeviceDetails =
|
||||
await this.tuyaService.getDeviceDetails(deviceId);
|
||||
|
||||
const camelCaseResponse = convertKeysToCamelCase(tuyaDeviceDetails);
|
||||
|
||||
const product = await this.productRepository.findOne({
|
||||
where: {
|
||||
prodId: camelCaseResponse.productId,
|
||||
},
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { uuid, ...rest } = camelCaseResponse;
|
||||
return {
|
||||
...rest,
|
||||
productUuid: product?.uuid,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`Error fetching device details from Tuya for device uuid ${deviceId}.`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async findNextTag(): Promise<number> {
|
||||
const tags = await this.tagRepository.find({ select: ['tag'] });
|
||||
|
||||
|
Reference in New Issue
Block a user