mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Add Tuya integration to UnitService
This commit is contained in:
@ -4,6 +4,7 @@ export interface GetUnitByUuidInterface {
|
|||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
|
spaceTuyaUuid: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UnitChildInterface {
|
export interface UnitChildInterface {
|
||||||
@ -29,3 +30,8 @@ export interface GetUnitByUserUuidInterface {
|
|||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}
|
}
|
||||||
|
export interface addTuyaSpaceInterface {
|
||||||
|
success: boolean;
|
||||||
|
result: string;
|
||||||
|
msg: string;
|
||||||
|
}
|
||||||
|
@ -14,6 +14,7 @@ import {
|
|||||||
GetUnitByUuidInterface,
|
GetUnitByUuidInterface,
|
||||||
RenameUnitByUuidInterface,
|
RenameUnitByUuidInterface,
|
||||||
GetUnitByUserUuidInterface,
|
GetUnitByUserUuidInterface,
|
||||||
|
addTuyaSpaceInterface,
|
||||||
} from '../interface/unit.interface';
|
} from '../interface/unit.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
@ -21,15 +22,27 @@ import { UserSpaceRepository } from '@app/common/modules/user-space/repositories
|
|||||||
import { generateRandomString } from '@app/common/helper/randomString';
|
import { generateRandomString } from '@app/common/helper/randomString';
|
||||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnitService {
|
export class UnitService {
|
||||||
|
private tuya: TuyaContext;
|
||||||
constructor(
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||||
private readonly userSpaceRepository: UserSpaceRepository,
|
private readonly userSpaceRepository: UserSpaceRepository,
|
||||||
private readonly userDevicePermissionService: UserDevicePermissionService,
|
private readonly userDevicePermissionService: UserDevicePermissionService,
|
||||||
) {}
|
) {
|
||||||
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
|
this.tuya = new TuyaContext({
|
||||||
|
baseUrl: 'https://openapi.tuyaeu.com',
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async addUnit(addUnitDto: AddUnitDto) {
|
async addUnit(addUnitDto: AddUnitDto) {
|
||||||
try {
|
try {
|
||||||
@ -38,17 +51,41 @@ export class UnitService {
|
|||||||
type: 'unit',
|
type: 'unit',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const tuyaUnit = await this.addUnitTuya(addUnitDto.unitName);
|
||||||
|
if (!tuyaUnit.result) {
|
||||||
|
throw new HttpException('Error creating unit', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
const unit = await this.spaceRepository.save({
|
const unit = await this.spaceRepository.save({
|
||||||
spaceName: addUnitDto.unitName,
|
spaceName: addUnitDto.unitName,
|
||||||
parent: { uuid: addUnitDto.floorUuid },
|
parent: { uuid: addUnitDto.floorUuid },
|
||||||
spaceType: { uuid: spaceType.uuid },
|
spaceType: { uuid: spaceType.uuid },
|
||||||
|
spaceTuyaUuid: tuyaUnit.result,
|
||||||
});
|
});
|
||||||
return unit;
|
return unit;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async addUnitTuya(unitName: string): Promise<addTuyaSpaceInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/space/creation`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'POST',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
name: unitName,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as addTuyaSpaceInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error creating unit from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getUnitByUuid(unitUuid: string): Promise<GetUnitByUuidInterface> {
|
async getUnitByUuid(unitUuid: string): Promise<GetUnitByUuidInterface> {
|
||||||
try {
|
try {
|
||||||
@ -70,6 +107,7 @@ export class UnitService {
|
|||||||
updatedAt: unit.updatedAt,
|
updatedAt: unit.updatedAt,
|
||||||
name: unit.spaceName,
|
name: unit.spaceName,
|
||||||
type: unit.spaceType.type,
|
type: unit.spaceType.type,
|
||||||
|
spaceTuyaUuid: unit.spaceTuyaUuid,
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
|
Reference in New Issue
Block a user