mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Add Tuya integration to UnitService
This commit is contained in:
@ -4,6 +4,7 @@ export interface GetUnitByUuidInterface {
|
||||
updatedAt: Date;
|
||||
name: string;
|
||||
type: string;
|
||||
spaceTuyaUuid: string;
|
||||
}
|
||||
|
||||
export interface UnitChildInterface {
|
||||
@ -29,3 +30,8 @@ export interface GetUnitByUserUuidInterface {
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
export interface addTuyaSpaceInterface {
|
||||
success: boolean;
|
||||
result: string;
|
||||
msg: string;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import {
|
||||
GetUnitByUuidInterface,
|
||||
RenameUnitByUuidInterface,
|
||||
GetUnitByUserUuidInterface,
|
||||
addTuyaSpaceInterface,
|
||||
} from '../interface/unit.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
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 { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class UnitService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
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) {
|
||||
try {
|
||||
@ -38,17 +51,41 @@ export class UnitService {
|
||||
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({
|
||||
spaceName: addUnitDto.unitName,
|
||||
parent: { uuid: addUnitDto.floorUuid },
|
||||
spaceType: { uuid: spaceType.uuid },
|
||||
spaceTuyaUuid: tuyaUnit.result,
|
||||
});
|
||||
return unit;
|
||||
} catch (err) {
|
||||
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> {
|
||||
try {
|
||||
@ -70,6 +107,7 @@ export class UnitService {
|
||||
updatedAt: unit.updatedAt,
|
||||
name: unit.spaceName,
|
||||
type: unit.spaceType.type,
|
||||
spaceTuyaUuid: unit.spaceTuyaUuid,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
|
Reference in New Issue
Block a user