mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:04:53 +00:00
116 lines
3.2 KiB
TypeScript
116 lines
3.2 KiB
TypeScript
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { AddRoomDto } from '../dtos';
|
|
import {
|
|
GetRoomDetailsInterface,
|
|
GetRoomsIdsInterface,
|
|
} from '../interfaces/get.room.interface';
|
|
|
|
@Injectable()
|
|
export class RoomService {
|
|
private tuya: TuyaContext;
|
|
constructor(private readonly configService: ConfigService) {
|
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
|
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
|
this.tuya = new TuyaContext({
|
|
baseUrl: 'https://openapi.tuyaeu.com',
|
|
accessKey,
|
|
secretKey,
|
|
});
|
|
}
|
|
|
|
async getRoomsByHomeId(homeId: string) {
|
|
try {
|
|
const roomsIds = await this.getRoomsIds(homeId);
|
|
|
|
const roomsDetails = await Promise.all(
|
|
roomsIds.result.data.map(async (roomId) => {
|
|
const roomData = await this.getRoomDetails(roomId);
|
|
return {
|
|
roomId: roomData?.result?.id,
|
|
roomName: roomData ? roomData.result.name : null,
|
|
};
|
|
}),
|
|
);
|
|
|
|
return roomsDetails;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'Error fetching rooms',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
async getRoomsIds(homeId: string): Promise<GetRoomsIdsInterface> {
|
|
try {
|
|
const path = `/v2.0/cloud/space/child`;
|
|
const response = await this.tuya.request({
|
|
method: 'GET',
|
|
path,
|
|
query: { space_id: homeId },
|
|
});
|
|
return response as GetRoomsIdsInterface;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'Error fetching rooms ids',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
async getRoomDetails(roomId: string): Promise<GetRoomDetailsInterface> {
|
|
// Added return type
|
|
try {
|
|
const path = `/v2.0/cloud/space/${roomId}`;
|
|
const response = await this.tuya.request({
|
|
method: 'GET',
|
|
path,
|
|
});
|
|
|
|
return response as GetRoomDetailsInterface; // Cast response to RoomData
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'Error fetching rooms details',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
async addRoom(addRoomDto: AddRoomDto) {
|
|
try {
|
|
const path = `/v2.0/cloud/space/creation`;
|
|
const data = await this.tuya.request({
|
|
method: 'POST',
|
|
path,
|
|
body: { name: addRoomDto.roomName, parent_id: addRoomDto.homeId },
|
|
});
|
|
|
|
return {
|
|
success: data.success,
|
|
roomId: data.result,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'Error adding room',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
async getRoomsByRoomId(roomId: string) {
|
|
try {
|
|
const response = await this.getRoomDetails(roomId);
|
|
|
|
return {
|
|
homeId: response.result.root_id,
|
|
roomId: response.result.id,
|
|
roomName: response.result.name,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'Error fetching rooms',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|