mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:04:53 +00:00
finished control group api
This commit is contained in:
@ -12,6 +12,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||
import { GetGroupDto } from '../dtos/get.group.dto';
|
||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||
|
||||
@ApiTags('Group Module')
|
||||
@Controller({
|
||||
@ -45,4 +46,15 @@ export class GroupController {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Post('control')
|
||||
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
|
||||
try {
|
||||
return await this.groupService.controlGroup(controlGroupDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/group/dtos/control.group.dto.ts
Normal file
20
src/group/dtos/control.group.dto.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsObject } from 'class-validator';
|
||||
|
||||
export class ControlGroupDto {
|
||||
@ApiProperty({
|
||||
description: 'groupId',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public groupId: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'example {"switch_1":true,"add_ele":300}',
|
||||
required: true,
|
||||
})
|
||||
@IsObject()
|
||||
@IsNotEmpty()
|
||||
public properties: object;
|
||||
}
|
||||
@ -18,3 +18,9 @@ export class addGroupInterface {
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class controlGroupInterface {
|
||||
success: boolean;
|
||||
result: boolean;
|
||||
msg: string;
|
||||
}
|
||||
|
||||
@ -1,13 +1,15 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddGroupDto } from '../dtos';
|
||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||
import {
|
||||
GetGroupsInterface,
|
||||
GetRoomDetailsInterface,
|
||||
addGroupInterface,
|
||||
controlGroupInterface,
|
||||
} from '../interfaces/get.group.interface';
|
||||
import { GetGroupDto } from '../dtos/get.group.dto';
|
||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||
|
||||
@Injectable()
|
||||
export class GroupService {
|
||||
@ -121,4 +123,39 @@ export class GroupService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async controlGroup(controlGroupDto: ControlGroupDto) {
|
||||
const response = await this.controlGroupTuya(controlGroupDto);
|
||||
|
||||
if (response.success) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
async controlGroupTuya(
|
||||
controlGroupDto: ControlGroupDto,
|
||||
): Promise<controlGroupInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/group/properties`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: {
|
||||
group_id: controlGroupDto.groupId,
|
||||
properties: controlGroupDto.properties,
|
||||
},
|
||||
});
|
||||
|
||||
return response as controlGroupInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error control group',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user