mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 15:57:54 +00:00
Refactor Device Module
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
import { DeviceService } from '../services/device.service';
|
||||
import { Body, Controller, Get, Post, Query, Param } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Query,
|
||||
Param,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import {
|
||||
AddDeviceInGroupDto,
|
||||
@@ -7,11 +17,12 @@ import {
|
||||
} from '../dtos/add.device.dto';
|
||||
import {
|
||||
GetDeviceByGroupIdDto,
|
||||
GetDeviceByRoomIdDto,
|
||||
GetDeviceByRoomUuidDto,
|
||||
} from '../dtos/get.device.dto';
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { AuthGuardWithRoles } from 'src/guards/device.permission.guard';
|
||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||
import { CheckGroupGuard } from 'src/guards/group.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
|
||||
@ApiTags('Device Module')
|
||||
@Controller({
|
||||
@@ -22,19 +33,38 @@ export class DeviceController {
|
||||
constructor(private readonly deviceService: DeviceService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.READ)
|
||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||
@Get('room')
|
||||
async getDevicesByRoomId(
|
||||
@Query() getDeviceByRoomIdDto: GetDeviceByRoomIdDto,
|
||||
@Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
||||
) {
|
||||
try {
|
||||
return await this.deviceService.getDevicesByRoomId(getDeviceByRoomIdDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
return await this.deviceService.getDevicesByRoomId(
|
||||
getDeviceByRoomUuidDto,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||
@Post('room')
|
||||
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
||||
try {
|
||||
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.READ)
|
||||
@UseGuards(JwtAuthGuard, CheckGroupGuard)
|
||||
@Get('group')
|
||||
async getDevicesByGroupId(
|
||||
@Query() getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
||||
@@ -43,68 +73,81 @@ export class DeviceController {
|
||||
return await this.deviceService.getDevicesByGroupId(
|
||||
getDeviceByGroupIdDto,
|
||||
);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.READ)
|
||||
@Get(':deviceId')
|
||||
async getDeviceDetailsByDeviceId(@Param('deviceId') deviceId: string) {
|
||||
try {
|
||||
return await this.deviceService.getDeviceDetailsByDeviceId(deviceId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.READ)
|
||||
@Get(':deviceId/functions')
|
||||
async getDeviceInstructionByDeviceId(@Param('deviceId') deviceId: string) {
|
||||
try {
|
||||
return await this.deviceService.getDeviceInstructionByDeviceId(deviceId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.READ)
|
||||
@Get(':deviceId/functions/status')
|
||||
async getDevicesInstructionStatus(@Param('deviceId') deviceId: string) {
|
||||
try {
|
||||
return await this.deviceService.getDevicesInstructionStatus(deviceId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
|
||||
@Post('room')
|
||||
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
||||
try {
|
||||
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
|
||||
@UseGuards(JwtAuthGuard, CheckGroupGuard)
|
||||
@Post('group')
|
||||
async addDeviceInGroup(@Body() addDeviceInGroupDto: AddDeviceInGroupDto) {
|
||||
try {
|
||||
return await this.deviceService.addDeviceInGroup(addDeviceInGroupDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@AuthGuardWithRoles(PermissionType.CONTROLLABLE)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':deviceUuid')
|
||||
async getDeviceDetailsByDeviceId(@Param('deviceUuid') deviceUuid: string) {
|
||||
try {
|
||||
return await this.deviceService.getDeviceDetailsByDeviceId(deviceUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':deviceUuid/functions')
|
||||
async getDeviceInstructionByDeviceId(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
) {
|
||||
try {
|
||||
return await this.deviceService.getDeviceInstructionByDeviceId(
|
||||
deviceUuid,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':deviceUuid/functions/status')
|
||||
async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) {
|
||||
try {
|
||||
return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('control')
|
||||
async controlDevice(@Body() controlDeviceDto: ControlDeviceDto) {
|
||||
try {
|
||||
return await this.deviceService.controlDevice(controlDeviceDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,15 @@ import {
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||
import { GroupRepository } from '@app/common/modules/group/repositories';
|
||||
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
|
||||
@Module({
|
||||
imports: [ConfigModule, ProductRepositoryModule, DeviceRepositoryModule],
|
||||
imports: [
|
||||
ConfigModule,
|
||||
ProductRepositoryModule,
|
||||
DeviceRepositoryModule,
|
||||
GroupRepositoryModule,
|
||||
],
|
||||
controllers: [DeviceController],
|
||||
providers: [
|
||||
DeviceService,
|
||||
@@ -23,6 +30,7 @@ import { GroupDeviceRepository } from '@app/common/modules/group-device/reposito
|
||||
SpaceRepository,
|
||||
DeviceRepository,
|
||||
GroupDeviceRepository,
|
||||
GroupRepository,
|
||||
],
|
||||
exports: [DeviceService],
|
||||
})
|
||||
|
||||
@@ -1,45 +1,37 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddDeviceInRoomDto {
|
||||
@ApiProperty({
|
||||
description: 'deviceId',
|
||||
description: 'deviceTuyaUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceId: string;
|
||||
public deviceTuyaUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'roomId',
|
||||
description: 'roomUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roomId: string;
|
||||
public roomUuid: string;
|
||||
}
|
||||
export class AddDeviceInGroupDto {
|
||||
@ApiProperty({
|
||||
description: 'deviceId',
|
||||
description: 'deviceUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceId: string;
|
||||
public deviceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'homeId',
|
||||
description: 'groupUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeId: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'groupId',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public groupId: string;
|
||||
public groupUuid: string;
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class ControlDeviceDto {
|
||||
@ApiProperty({
|
||||
description: 'deviceId',
|
||||
description: 'deviceUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceId: string;
|
||||
public deviceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'code',
|
||||
|
||||
@@ -1,44 +1,21 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsNumberString } from 'class-validator';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class GetDeviceByRoomIdDto {
|
||||
export class GetDeviceByRoomUuidDto {
|
||||
@ApiProperty({
|
||||
description: 'roomId',
|
||||
description: 'roomUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roomId: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'pageSize',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public pageSize: number;
|
||||
public roomUuid: string;
|
||||
}
|
||||
export class GetDeviceByGroupIdDto {
|
||||
@ApiProperty({
|
||||
description: 'groupId',
|
||||
description: 'groupUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public groupId: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'pageSize',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public pageSize: number;
|
||||
@ApiProperty({
|
||||
description: 'pageNo',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public pageNo: number;
|
||||
public groupUuid: string;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
export interface GetDeviceDetailsInterface {
|
||||
result: {
|
||||
productId: string;
|
||||
};
|
||||
success: boolean;
|
||||
msg: string;
|
||||
}
|
||||
export interface GetDevicesByRoomIdInterface {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
result: any;
|
||||
}
|
||||
|
||||
export interface GetDevicesByGroupIdInterface {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
result: {
|
||||
count: number;
|
||||
data_list: [];
|
||||
};
|
||||
activeTime: number;
|
||||
assetId: string;
|
||||
category: string;
|
||||
categoryName: string;
|
||||
createTime: number;
|
||||
gatewayId: string;
|
||||
icon: string;
|
||||
id: string;
|
||||
ip: string;
|
||||
lat: string;
|
||||
localKey: string;
|
||||
lon: string;
|
||||
model: string;
|
||||
name: string;
|
||||
nodeId: string;
|
||||
online: boolean;
|
||||
productId?: string;
|
||||
productName?: string;
|
||||
sub: boolean;
|
||||
timeZone: string;
|
||||
updateTime: number;
|
||||
uuid: string;
|
||||
productType: string;
|
||||
productUuid: string;
|
||||
}
|
||||
|
||||
export interface addDeviceInRoomInterface {
|
||||
@@ -44,21 +49,13 @@ export interface GetDeviceDetailsFunctionsStatusInterface {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
}
|
||||
export interface GetProductInterface {
|
||||
productType: string;
|
||||
productId: string;
|
||||
}
|
||||
|
||||
export interface DeviceInstructionResponse {
|
||||
success: boolean;
|
||||
result: {
|
||||
productId: string;
|
||||
productType: string;
|
||||
functions: {
|
||||
code: string;
|
||||
values: any[];
|
||||
dataType: string;
|
||||
}[];
|
||||
};
|
||||
msg: string;
|
||||
productUuid: string;
|
||||
productType: string;
|
||||
functions: {
|
||||
code: string;
|
||||
values: any[];
|
||||
dataType: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
@@ -15,15 +15,11 @@ import {
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
GetDeviceDetailsFunctionsStatusInterface,
|
||||
GetDeviceDetailsInterface,
|
||||
GetDevicesByGroupIdInterface,
|
||||
GetDevicesByRoomIdInterface,
|
||||
GetProductInterface,
|
||||
addDeviceInRoomInterface,
|
||||
controlDeviceInterface,
|
||||
} from '../interfaces/get.device.interface';
|
||||
import {
|
||||
GetDeviceByGroupIdDto,
|
||||
GetDeviceByRoomIdDto,
|
||||
GetDeviceByRoomUuidDto,
|
||||
} from '../dtos/get.device.dto';
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
@@ -51,25 +47,31 @@ export class DeviceService {
|
||||
});
|
||||
}
|
||||
|
||||
async getDevicesByRoomId(getDeviceByRoomIdDto: GetDeviceByRoomIdDto) {
|
||||
async getDevicesByRoomId(
|
||||
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
||||
): Promise<GetDeviceDetailsInterface[]> {
|
||||
try {
|
||||
const findRoom = await this.spaceRepository.findOne({
|
||||
const devices = await this.deviceRepository.find({
|
||||
where: {
|
||||
uuid: getDeviceByRoomIdDto.roomId,
|
||||
spaceDevice: { uuid: getDeviceByRoomUuidDto.roomUuid },
|
||||
},
|
||||
relations: ['spaceDevice', 'productDevice'],
|
||||
});
|
||||
if (!findRoom) {
|
||||
throw new NotFoundException('Room Details Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDevicesByRoomIdTuya(getDeviceByRoomIdDto);
|
||||
|
||||
return {
|
||||
success: response.success,
|
||||
devices: response.result,
|
||||
msg: response.msg,
|
||||
};
|
||||
const devicesData = await Promise.all(
|
||||
devices.map(async (device) => {
|
||||
return {
|
||||
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||
device.deviceTuyaUuid,
|
||||
)),
|
||||
uuid: device.uuid,
|
||||
productUuid: device.productDevice.uuid,
|
||||
productType: device.productDevice.prodType,
|
||||
} as GetDeviceDetailsInterface;
|
||||
}),
|
||||
);
|
||||
return devicesData;
|
||||
} catch (error) {
|
||||
// Handle the error here
|
||||
throw new HttpException(
|
||||
'Error fetching devices by room',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
@@ -77,68 +79,26 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async getDevicesByRoomIdTuya(
|
||||
getDeviceByRoomIdDto: GetDeviceByRoomIdDto,
|
||||
): Promise<GetDevicesByRoomIdInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/space/device`;
|
||||
const getDeviceByRoomId = await this.deviceRepository.find({
|
||||
where: {
|
||||
deviceTuyaUuid: getDeviceByRoomIdDto.roomId,
|
||||
},
|
||||
});
|
||||
const response: any = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
query: {
|
||||
space_ids: getDeviceByRoomIdDto.roomId,
|
||||
page_size: getDeviceByRoomIdDto.pageSize,
|
||||
},
|
||||
});
|
||||
if (!getDeviceByRoomId.length) {
|
||||
throw new NotFoundException('Devices Not Found');
|
||||
}
|
||||
|
||||
const matchingRecords = [];
|
||||
|
||||
getDeviceByRoomId.forEach((item1) => {
|
||||
const matchingItem = response.find(
|
||||
(item2) => item1.deviceTuyaUuid === item2.uuid,
|
||||
);
|
||||
if (matchingItem) {
|
||||
matchingRecords.push({ ...matchingItem });
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: 'Device Tuya Details Fetched successfully',
|
||||
result: matchingRecords,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching devices by room from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDevicesByGroupId(getDeviceByGroupIdDto: GetDeviceByGroupIdDto) {
|
||||
try {
|
||||
const devicesIds: GetDevicesByGroupIdInterface =
|
||||
await this.getDevicesByGroupIdTuya(getDeviceByGroupIdDto);
|
||||
const devicesDetails = await Promise.all(
|
||||
devicesIds.result.data_list.map(async (device: any) => {
|
||||
const deviceData = await this.getDeviceDetailsByDeviceId(
|
||||
device.dev_id,
|
||||
);
|
||||
return deviceData.result;
|
||||
const groupDevices = await this.groupDeviceRepository.find({
|
||||
where: { group: { uuid: getDeviceByGroupIdDto.groupUuid } },
|
||||
relations: ['device'],
|
||||
});
|
||||
const devicesData = await Promise.all(
|
||||
groupDevices.map(async (device) => {
|
||||
return {
|
||||
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||
device.device.deviceTuyaUuid,
|
||||
)),
|
||||
uuid: device.uuid,
|
||||
productUuid: device.device.productDevice.uuid,
|
||||
productType: device.device.productDevice.prodType,
|
||||
} as GetDeviceDetailsInterface;
|
||||
}),
|
||||
);
|
||||
return {
|
||||
success: devicesIds.success,
|
||||
devices: devicesDetails,
|
||||
msg: devicesIds.msg,
|
||||
};
|
||||
|
||||
return devicesData;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching devices by group',
|
||||
@@ -147,149 +107,81 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async getDevicesByGroupIdTuya(
|
||||
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
||||
): Promise<GetDevicesByGroupIdInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/group/${getDeviceByGroupIdDto.groupId}/devices`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
query: {
|
||||
page_size: getDeviceByGroupIdDto.pageSize,
|
||||
page_no: getDeviceByGroupIdDto.pageNo,
|
||||
},
|
||||
});
|
||||
return response as GetDevicesByGroupIdInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching devices by group from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async addDeviceInRoom(addDeviceInRoomDto: AddDeviceInRoomDto) {
|
||||
const [deviceDetails, roomDetails] = await Promise.all([
|
||||
this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: addDeviceInRoomDto.deviceId,
|
||||
},
|
||||
}),
|
||||
this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: addDeviceInRoomDto.roomId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!roomDetails) {
|
||||
throw new NotFoundException('Room Details Not Found');
|
||||
}
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
}
|
||||
const response = await this.addDeviceInRooms(addDeviceInRoomDto);
|
||||
|
||||
if (response.success) {
|
||||
return {
|
||||
success: response.success,
|
||||
result: response.result,
|
||||
msg: response.msg,
|
||||
};
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
async addDeviceInRooms(
|
||||
addDeviceInRoomDto: AddDeviceInRoomDto,
|
||||
): Promise<addDeviceInRoomInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/${addDeviceInRoomDto.deviceId}/transfer`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: {
|
||||
space_id: addDeviceInRoomDto.roomId,
|
||||
},
|
||||
});
|
||||
return response as addDeviceInRoomInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding device in room from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
addDeviceInRoomDto.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
if (!device.productUuid) {
|
||||
throw new Error('Product UUID is missing for the device.');
|
||||
}
|
||||
|
||||
await this.deviceRepository.save({
|
||||
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
|
||||
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
|
||||
productDevice: { uuid: device.productUuid },
|
||||
});
|
||||
return { message: 'device added in room successfully' };
|
||||
} catch (error) {
|
||||
if (error.code === '23505') {
|
||||
throw new Error('Device already exists in the room.');
|
||||
} else {
|
||||
throw new Error('Failed to add device in room');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
||||
const deviceDetails = this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: addDeviceInGroupDto.deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
}
|
||||
|
||||
const response = await this.addDeviceInGroups(addDeviceInGroupDto);
|
||||
|
||||
if (response.success) {
|
||||
return {
|
||||
success: response.success,
|
||||
result: response.result,
|
||||
msg: response.msg,
|
||||
};
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
async addDeviceInGroups(
|
||||
addDeviceInGroupDto: AddDeviceInGroupDto,
|
||||
): Promise<addDeviceInRoomInterface> {
|
||||
try {
|
||||
await this.groupDeviceRepository.create({
|
||||
deviceUuid: addDeviceInGroupDto.deviceId,
|
||||
groupUuid: addDeviceInGroupDto.groupId,
|
||||
await this.groupDeviceRepository.save({
|
||||
device: { uuid: addDeviceInGroupDto.deviceUuid },
|
||||
group: { uuid: addDeviceInGroupDto.groupUuid },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: 'Group is Added to Specific Device',
|
||||
result: true,
|
||||
} as addDeviceInRoomInterface;
|
||||
return { message: 'device added in group successfully' };
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding device in group from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
if (error.code === '23505') {
|
||||
throw new Error('Device already exists in the group.');
|
||||
} else {
|
||||
throw new Error('Failed to add device in group');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async controlDevice(controlDeviceDto: ControlDeviceDto) {
|
||||
const response = await this.controlDeviceTuya(controlDeviceDto);
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: controlDeviceDto.deviceUuid,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||
throw new NotFoundException('Device Not Found');
|
||||
}
|
||||
const response = await this.controlDeviceTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
controlDeviceDto,
|
||||
);
|
||||
|
||||
if (response.success) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
async controlDeviceTuya(
|
||||
deviceUuid: string,
|
||||
controlDeviceDto: ControlDeviceDto,
|
||||
): Promise<controlDeviceInterface> {
|
||||
try {
|
||||
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceId}/commands`;
|
||||
const path = `/v1.0/iot-03/devices/${deviceUuid}/commands`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
@@ -309,29 +201,30 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async getDeviceDetailsByDeviceId(deviceId: string) {
|
||||
async getDeviceDetailsByDeviceId(deviceUuid: string) {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
});
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
throw new NotFoundException('Device Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDeviceDetailsByDeviceIdTuya(deviceId);
|
||||
const response = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
success: response.success,
|
||||
result: response.result,
|
||||
msg: response.msg,
|
||||
...response,
|
||||
uuid: deviceDetails.uuid,
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching device details',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
async getDeviceDetailsByDeviceIdTuya(
|
||||
@@ -343,19 +236,14 @@ export class DeviceService {
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
// Convert keys to camel case
|
||||
const camelCaseResponse = convertKeysToCamelCase(response);
|
||||
const productType: string = await this.getProductTypeByProductId(
|
||||
camelCaseResponse.result.productId,
|
||||
);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { productName, productId, ...rest } = camelCaseResponse.result;
|
||||
return {
|
||||
result: {
|
||||
...camelCaseResponse.result,
|
||||
productType: productType,
|
||||
},
|
||||
success: camelCaseResponse.success,
|
||||
msg: camelCaseResponse.msg,
|
||||
...rest,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@@ -364,93 +252,39 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getProductIdByDeviceId(deviceId: string) {
|
||||
try {
|
||||
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,
|
||||
deviceUuid: string,
|
||||
): Promise<DeviceInstructionResponse> {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
throw new NotFoundException('Device Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDeviceInstructionByDeviceIdTuya(deviceId);
|
||||
|
||||
const productId: string = await this.getProductIdByDeviceId(deviceId);
|
||||
const productType: string =
|
||||
await this.getProductTypeByProductId(productId);
|
||||
const response = await this.getDeviceInstructionByDeviceIdTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
success: response.success,
|
||||
result: {
|
||||
productId: productId,
|
||||
productType: productType,
|
||||
functions: response.result.functions.map((fun: any) => {
|
||||
return {
|
||||
code: fun.code,
|
||||
values: fun.values,
|
||||
dataType: fun.type,
|
||||
};
|
||||
}),
|
||||
},
|
||||
msg: response.msg,
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
functions: response.result.functions.map((fun: any) => {
|
||||
return {
|
||||
code: fun.code,
|
||||
values: fun.values,
|
||||
dataType: fun.type,
|
||||
};
|
||||
}),
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching device functions by device id',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,28 +305,26 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDevicesInstructionStatus(deviceId: string) {
|
||||
async getDevicesInstructionStatus(deviceUuid: string) {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
throw new NotFoundException('Device Not Found');
|
||||
}
|
||||
const deviceStatus = await this.getDevicesInstructionStatusTuya(deviceId);
|
||||
const productId: string = await this.getProductIdByDeviceId(deviceId);
|
||||
const productType: string =
|
||||
await this.getProductTypeByProductId(productId);
|
||||
const deviceStatus = await this.getDevicesInstructionStatusTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
result: {
|
||||
productId: productId,
|
||||
productType: productType,
|
||||
status: deviceStatus.result[0].status,
|
||||
},
|
||||
success: deviceStatus.success,
|
||||
msg: deviceStatus.msg,
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
status: deviceStatus.result[0].status,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@@ -503,7 +335,7 @@ export class DeviceService {
|
||||
}
|
||||
|
||||
async getDevicesInstructionStatusTuya(
|
||||
deviceId: string,
|
||||
deviceUuid: string,
|
||||
): Promise<GetDeviceDetailsFunctionsStatusInterface> {
|
||||
try {
|
||||
const path = `/v1.0/iot-03/devices/status`;
|
||||
@@ -511,10 +343,10 @@ export class DeviceService {
|
||||
method: 'GET',
|
||||
path,
|
||||
query: {
|
||||
device_ids: deviceId,
|
||||
device_ids: deviceUuid,
|
||||
},
|
||||
});
|
||||
return response as unknown as GetDeviceDetailsFunctionsStatusInterface;
|
||||
return response as GetDeviceDetailsFunctionsStatusInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching device functions status from Tuya',
|
||||
|
||||
Reference in New Issue
Block a user