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