mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
Device Related API changes are done
This commit is contained in:
@ -14,6 +14,18 @@ export class GroupDeviceEntity extends AbstractEntity<GroupDeviceDto> {
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
type: 'string',
|
||||
nullable: false,
|
||||
})
|
||||
deviceUuid: string;
|
||||
|
||||
@Column({
|
||||
type: 'string',
|
||||
nullable: false,
|
||||
})
|
||||
groupUuid: string;
|
||||
|
||||
@ManyToOne(() => DeviceEntity, (device) => device.userGroupDevices, {
|
||||
nullable: false,
|
||||
})
|
||||
|
@ -5,12 +5,22 @@ import { ConfigModule } from '@nestjs/config';
|
||||
import { ProductRepositoryModule } from '@app/common/modules/product/product.repository.module';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||
import { DeviceUserTypeRepository } from '@app/common/modules/device/repositories';
|
||||
import { DeviceRepository, DeviceUserTypeRepository } from '@app/common/modules/device/repositories';
|
||||
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';
|
||||
@Module({
|
||||
imports: [ConfigModule, ProductRepositoryModule, DeviceRepositoryModule],
|
||||
controllers: [DeviceController],
|
||||
providers: [DeviceService, ProductRepository,DeviceUserTypeRepository,PermissionTypeRepository],
|
||||
providers: [
|
||||
DeviceService,
|
||||
ProductRepository,
|
||||
DeviceUserTypeRepository,
|
||||
PermissionTypeRepository,
|
||||
SpaceRepository,
|
||||
DeviceRepository,
|
||||
GroupDeviceRepository
|
||||
],
|
||||
exports: [DeviceService],
|
||||
})
|
||||
export class DeviceModule {}
|
||||
|
@ -1,4 +1,9 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
@ -23,6 +28,9 @@ import {
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -30,6 +38,9 @@ export class DeviceService {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly groupDeviceRepository: GroupDeviceRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -42,6 +53,15 @@ export class DeviceService {
|
||||
|
||||
async getDevicesByRoomId(getDeviceByRoomIdDto: GetDeviceByRoomIdDto) {
|
||||
try {
|
||||
const findRoom = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: getDeviceByRoomIdDto.roomId,
|
||||
},
|
||||
});
|
||||
if (!findRoom) {
|
||||
throw new NotFoundException('Room Details Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDevicesByRoomIdTuya(getDeviceByRoomIdDto);
|
||||
|
||||
return {
|
||||
@ -126,7 +146,27 @@ export class DeviceService {
|
||||
}
|
||||
|
||||
async addDeviceInRoom(addDeviceInRoomDto: AddDeviceInRoomDto) {
|
||||
const response = await this.addDeviceInRoomTuya(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 {
|
||||
@ -141,7 +181,7 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async addDeviceInRoomTuya(
|
||||
async addDeviceInRooms(
|
||||
addDeviceInRoomDto: AddDeviceInRoomDto,
|
||||
): Promise<addDeviceInRoomInterface> {
|
||||
try {
|
||||
@ -153,7 +193,6 @@ export class DeviceService {
|
||||
space_id: addDeviceInRoomDto.roomId,
|
||||
},
|
||||
});
|
||||
|
||||
return response as addDeviceInRoomInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@ -163,7 +202,17 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
||||
const response = await this.addDeviceInGroupTuya(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 {
|
||||
@ -178,21 +227,21 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async addDeviceInGroupTuya(
|
||||
async addDeviceInGroups(
|
||||
addDeviceInGroupDto: AddDeviceInGroupDto,
|
||||
): Promise<addDeviceInRoomInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/group/${addDeviceInGroupDto.groupId}/device`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
space_id: addDeviceInGroupDto.homeId,
|
||||
device_ids: addDeviceInGroupDto.deviceId,
|
||||
},
|
||||
await this.groupDeviceRepository.create({
|
||||
deviceUuid: addDeviceInGroupDto.deviceId,
|
||||
groupUuid: addDeviceInGroupDto.groupId,
|
||||
});
|
||||
|
||||
return response as addDeviceInRoomInterface;
|
||||
return {
|
||||
success: true,
|
||||
msg: 'Group is Added to Specific Device',
|
||||
result: true,
|
||||
} as addDeviceInRoomInterface;
|
||||
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding device in group from Tuya',
|
||||
@ -239,6 +288,15 @@ export class DeviceService {
|
||||
|
||||
async getDeviceDetailsByDeviceId(deviceId: string) {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
},
|
||||
});
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDeviceDetailsByDeviceIdTuya(deviceId);
|
||||
|
||||
return {
|
||||
@ -335,6 +393,15 @@ export class DeviceService {
|
||||
deviceId: string,
|
||||
): Promise<DeviceInstructionResponse> {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
},
|
||||
});
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
}
|
||||
|
||||
const response = await this.getDeviceInstructionByDeviceIdTuya(deviceId);
|
||||
|
||||
const productId: string = await this.getProductIdByDeviceId(deviceId);
|
||||
@ -383,6 +450,14 @@ export class DeviceService {
|
||||
}
|
||||
async getDevicesInstructionStatus(deviceId: string) {
|
||||
try {
|
||||
const deviceDetails = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceId,
|
||||
},
|
||||
});
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Details Not Found');
|
||||
}
|
||||
const deviceStatus = await this.getDevicesInstructionStatusTuya(deviceId);
|
||||
const productId: string = await this.getProductIdByDeviceId(deviceId);
|
||||
const productType: string =
|
||||
|
Reference in New Issue
Block a user