Files
backend/src/device/services/device.service.ts
2025-07-09 16:05:31 +03:00

1947 lines
60 KiB
TypeScript

import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
import { ProductType } from '@app/common/constants/product-type.enum';
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
import {
BadRequestException,
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { AddAutomationDto } from 'src/automation/dtos';
import { SceneService } from 'src/scene/services';
import { In, Not, QueryRunner } from 'typeorm';
import { ProjectParam } from '../dtos';
import {
AddDeviceDto,
AddSceneToFourSceneDeviceDto,
AssignDeviceToSpaceDto,
UpdateDeviceDto,
} from '../dtos/add.device.dto';
import {
BatchControlDevicesDto,
BatchFactoryResetDevicesDto,
BatchStatusDevicesDto,
ControlDeviceDto,
GetSceneFourSceneDeviceDto,
} from '../dtos/control.device.dto';
import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto';
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
import {
GetDeviceLogsDto,
GetDevicesBySpaceOrCommunityDto,
GetDevicesFilterDto,
} from '../dtos/get.device.dto';
import {
controlDeviceInterface,
DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface,
getDeviceLogsInterface,
GetMacAddressInterface,
GetPowerClampFunctionsStatusInterface,
updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from './../../../libs/common/src/constants/orphan-constant';
import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository';
@Injectable()
export class DeviceService {
private tuya: TuyaContext;
constructor(
private readonly configService: ConfigService,
private readonly deviceRepository: DeviceRepository,
private readonly sceneDeviceRepository: SceneDeviceRepository,
private readonly productRepository: ProductRepository,
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
private readonly spaceRepository: SpaceRepository,
@Inject(forwardRef(() => SceneService))
private readonly sceneService: SceneService,
private readonly tuyaService: TuyaService,
private readonly projectRepository: ProjectRepository,
private readonly communityRepository: CommunityRepository,
) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
const tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
this.tuya = new TuyaContext({
baseUrl: tuyaEuUrl,
accessKey,
secretKey,
});
}
async getAllDevices(
param: ProjectParam,
{ deviceType, spaces, communities }: GetDevicesFilterDto,
): Promise<BaseResponseDto> {
try {
await this.validateProject(param.projectUuid);
if (deviceType === DeviceTypeEnum.DOOR_LOCK) {
return await this.getDoorLockDevices(param.projectUuid, {
spaces,
communities,
});
} else if (!deviceType) {
const devices = await this.deviceRepository.find({
where: {
isActive: true,
spaceDevice: {
uuid: spaces && spaces.length ? In(spaces) : undefined,
spaceName: Not(ORPHAN_SPACE_NAME),
community: {
uuid:
communities && communities.length
? In(communities)
: undefined,
project: { uuid: param.projectUuid },
},
},
},
relations: [
'spaceDevice.parent',
'spaceDevice.community',
'productDevice',
'permission',
'permission.permissionType',
'subspace',
],
});
const devicesData = await Promise.allSettled(
devices.map(async (device) => {
let battery = null;
// Check if the device is a door lock (DL)
if (device.productDevice.prodType === ProductType.DL) {
const doorLockInstructionsStatus: BaseResponseDto =
await this.getDevicesInstructionStatus(
device.uuid,
param.projectUuid,
);
const batteryStatus: any =
doorLockInstructionsStatus.data.status.find(
(status: any) =>
status.code === BatteryStatus.RESIDUAL_ELECTRICITY,
);
if (batteryStatus) {
battery = batteryStatus.value;
}
}
// Check if the device is a door sensor (DS)
if (device.productDevice.prodType === ProductType.DS) {
const doorSensorInstructionsStatus: BaseResponseDto =
await this.getDevicesInstructionStatus(
device.uuid,
param.projectUuid,
);
const batteryStatus: any =
doorSensorInstructionsStatus.data.status.find(
(status: any) =>
status.code === BatteryStatus.BATTERY_PERCENTAGE,
);
if (batteryStatus) {
battery = batteryStatus.value;
}
}
// Check if the device is a water leak sensor (WL)
if (device.productDevice.prodType === ProductType.WL) {
const doorSensorInstructionsStatus: BaseResponseDto =
await this.getDevicesInstructionStatus(
device.uuid,
param.projectUuid,
);
const batteryStatus: any =
doorSensorInstructionsStatus.data.status.find(
(status: any) =>
status.code === BatteryStatus.BATTERY_PERCENTAGE,
);
if (batteryStatus) {
battery = batteryStatus.value;
}
}
const spaceHierarchy = await this.getParentHierarchy(
device?.spaceDevice,
);
const orderedHierarchy = [
device?.spaceDevice,
...spaceHierarchy.reverse(),
];
return {
spaces: orderedHierarchy.map((space) => ({
uuid: space.uuid,
spaceName: space.spaceName,
})),
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
community: {
uuid: device.spaceDevice.community.uuid,
name: device.spaceDevice.community.name,
},
subspace: device.subspace,
// permissionType: device.permission[0].permissionType.type,
...(await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
)),
uuid: device.uuid,
...(battery && { battery }),
} as GetDeviceDetailsInterface;
}),
);
// Filter out rejected promises and extract the fulfilled values
const fulfilledDevices = devicesData
.filter((result) => result.status === DeviceStatuses.FULLFILLED)
.map(
(result) =>
(result as PromiseFulfilledResult<GetDeviceDetailsInterface>)
.value,
);
return new SuccessResponseDto({
message: `Devices fetched successfully`,
data: fulfilledDevices,
statusCode: HttpStatus.OK,
});
}
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceLogs(
deviceUuid: string,
query: GetDeviceLogsDto,
projectUuid: string,
): Promise<BaseResponseDto> {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
}
const response = await this.getDeviceLogsTuya(
deviceDetails.deviceTuyaUuid,
query.code,
query.startTime,
query.endTime,
);
return new SuccessResponseDto({
message: `Device logs fetched successfully`,
data: {
deviceUuid,
...response,
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
HttpStatus.NOT_FOUND,
);
}
}
async getDeviceByDeviceUuid(
deviceUuid: string,
withProductDevice: boolean = true,
projectUuid: string,
) {
const relations = ['subspace'];
if (withProductDevice) {
relations.push('productDevice');
}
return this.deviceRepository.findOne({
where: {
uuid: deviceUuid,
spaceDevice: {
community: { project: { uuid: projectUuid } },
spaceName: Not(ORPHAN_SPACE_NAME),
},
},
relations,
});
}
async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) {
return await this.deviceRepository.findOne({
where: {
deviceTuyaUuid,
},
relations: ['productDevice'],
});
}
async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) {
try {
const device = await this.getNewDeviceDetailsFromTuya(
addDeviceDto.deviceTuyaUuid,
);
if (!device.productUuid) {
throw new Error('Product UUID is missing for the device.');
}
const existingConflictingDevice = await this.deviceRepository.exists({
where: {
spaceDevice: { uuid: addDeviceDto.spaceUuid },
productDevice: { uuid: device.productUuid },
tag: { uuid: addDeviceDto.tagUuid },
},
});
if (existingConflictingDevice) {
throw new HttpException(
'Device with the same product type and tag already exists in this space',
HttpStatus.BAD_REQUEST,
);
}
const deviceSaved = await this.deviceRepository.save({
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
productDevice: { uuid: device.productUuid },
spaceDevice: { uuid: addDeviceDto.spaceUuid },
tag: { uuid: addDeviceDto.tagUuid },
name: addDeviceDto.deviceName,
deviceTuyaConstUuid: device.uuid,
});
if (deviceSaved.uuid) {
const deviceStatus: BaseResponseDto =
await this.getDevicesInstructionStatus(deviceSaved.uuid, projectUuid);
if (deviceStatus.data.productUuid) {
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
deviceUuid: deviceSaved.uuid,
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
status: deviceStatus.data.status,
productUuid: deviceStatus.data.productUuid,
productType: deviceStatus.data.productType,
});
}
}
return new SuccessResponseDto({
message: `Device added successfully`,
data: deviceSaved,
statusCode: HttpStatus.CREATED,
});
} catch (error) {
if (error.code === CommonErrorCodes.DUPLICATE_ENTITY) {
throw new HttpException(
'Device already exists',
HttpStatus.BAD_REQUEST,
);
} else {
throw new HttpException(
error.message || 'Failed to add device in space',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
async deleteDevice(
devices: DeviceEntity[],
orphanSpace: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
try {
const deviceIds = devices.map((device) => device.uuid);
if (deviceIds.length > 0) {
await queryRunner.manager
.createQueryBuilder()
.update(DeviceEntity)
.set({ spaceDevice: orphanSpace })
.whereInIds(deviceIds)
.execute();
}
} catch (error) {
throw new HttpException(
`Failed to update devices to orphan space: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async transferDeviceInSpaces(
assignDeviceToSpaceDto: AssignDeviceToSpaceDto,
projectUuid: string,
) {
try {
await this.deviceRepository.update(
{
uuid: assignDeviceToSpaceDto.deviceUuid,
spaceDevice: {
community: { project: { uuid: projectUuid } },
spaceName: Not(ORPHAN_SPACE_NAME),
},
},
{
spaceDevice: { uuid: assignDeviceToSpaceDto.spaceUuid },
},
);
const device = await this.deviceRepository.findOne({
where: {
uuid: assignDeviceToSpaceDto.deviceUuid,
},
relations: ['spaceDevice', 'spaceDevice.parent'],
});
if (device.spaceDevice.parent.spaceTuyaUuid) {
await this.transferDeviceInSpacesTuya(
device.deviceTuyaUuid,
device.spaceDevice.parent.spaceTuyaUuid,
);
}
return new SuccessResponseDto({
message: `Device transferred successfully to spaceUuid: ${assignDeviceToSpaceDto.spaceUuid}`,
data: {
uuid: device.uuid,
spaceUuid: device.spaceDevice.uuid,
},
statusCode: HttpStatus.CREATED,
});
} catch (error) {
throw new HttpException(
'Failed to add device in space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async transferDeviceInSpacesTuya(
deviceId: string,
spaceId: string,
): Promise<controlDeviceInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceId}/transfer`;
const response = await this.tuya.request({
method: 'POST',
path,
body: { space_id: spaceId },
});
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error transferring device in spaces from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async controlDevice(
controlDeviceDto: ControlDeviceDto,
deviceUuid: string,
projectUuid: string,
) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
false,
projectUuid,
);
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
throw new NotFoundException('Device Not Found');
}
const response = await this.controlDeviceTuya(
deviceDetails.deviceTuyaUuid,
controlDeviceDto,
);
if (response.success) {
return new SuccessResponseDto({
message: `Device controlled successfully`,
data: response,
statusCode: HttpStatus.CREATED,
});
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
async factoryResetDeviceTuya(
deviceUuid: string,
): Promise<controlDeviceInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceUuid}/reset`;
const response = await this.tuya.request({
method: 'POST',
path,
});
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error factory resetting device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async batchControlDevices(
batchControlDevicesDto: BatchControlDevicesDto,
projectUuid: string,
) {
const { devicesUuid, operationType } = batchControlDevicesDto;
if (operationType === BatchDeviceTypeEnum.RESET) {
return await this.batchFactoryResetDevices(
batchControlDevicesDto,
projectUuid,
);
} else if (operationType === BatchDeviceTypeEnum.COMMAND) {
try {
// Check if all devices have the same product UUID
await this.checkAllDevicesHaveSameProductUuid(devicesUuid);
// Perform all operations concurrently
const results = await Promise.allSettled(
devicesUuid.map(async (deviceUuid) => {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
const result = await this.controlDeviceTuya(
deviceDetails.deviceTuyaUuid,
batchControlDevicesDto,
);
return { deviceUuid, result };
}),
);
// Separate successful and failed operations
const successResults = [];
const failedResults = [];
for (const result of results) {
if (result.status === DeviceStatuses.FULLFILLED) {
const { deviceUuid, result: operationResult } = result.value;
if (operationResult.success) {
// Add to success results if operationResult.success is true
successResults.push({ deviceUuid, result: operationResult });
} else {
// Add to failed results if operationResult.success is false
failedResults.push({ deviceUuid, error: operationResult.msg });
}
} else {
// Add to failed results if promise is rejected
failedResults.push({
deviceUuid: devicesUuid[results.indexOf(result)],
error: result.reason.message,
});
}
}
return new SuccessResponseDto({
message: `Devices controlled successfully`,
data: { successResults, failedResults },
statusCode: HttpStatus.CREATED,
});
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
}
async batchStatusDevices(
batchStatusDevicesDto: BatchStatusDevicesDto,
projectUuid: string,
) {
const { devicesUuid } = batchStatusDevicesDto;
const devicesUuidArray = devicesUuid.split(',');
try {
await this.checkAllDevicesHaveSameProductUuid(devicesUuidArray);
const statuses = await Promise.all(
devicesUuidArray.map(async (deviceUuid) => {
const result = await this.getDevicesInstructionStatus(
deviceUuid,
projectUuid,
);
return { deviceUuid, result };
}),
);
return new SuccessResponseDto({
message: `Devices status fetched successfully`,
data: {
status: statuses[0].result.data,
devices: statuses,
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
async getDeviceDetailsByDeviceId(deviceUuid: string, projectUuid: string) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
}
const response = await this.getDeviceDetailsByDeviceIdTuya(
deviceDetails.deviceTuyaUuid,
);
const macAddress = await this.getMacAddressByDeviceIdTuya(
deviceDetails.deviceTuyaUuid,
);
return new SuccessResponseDto({
message: `Device details fetched successfully`,
data: {
...response,
uuid: deviceDetails.uuid,
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
macAddress: macAddress.mac,
subspace: deviceDetails.subspace ? deviceDetails.subspace : {},
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
HttpStatus.NOT_FOUND,
);
}
}
async updateDevice(
deviceUuid: string,
updateDeviceDto: UpdateDeviceDto,
projectUuid: string,
) {
try {
const device = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (device.uuid) {
await this.updateDeviceName(deviceUuid, updateDeviceDto.deviceName);
}
return new SuccessResponseDto({
message: `Device updated successfully`,
data: {
uuid: device.uuid,
deviceName: updateDeviceDto.deviceName,
},
statusCode: HttpStatus.CREATED,
});
} catch (error) {
throw new HttpException(
'Error updating device',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceDetailsByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {
console.log('fetching device details from Tuya for deviceId:', deviceId);
try {
const deviceDetails = await this.deviceRepository.findOne({
where: {
deviceTuyaUuid: deviceId,
},
relations: ['productDevice'],
});
if (!deviceDetails) {
throw new NotFoundException('Device not found');
}
let result = await this.tuyaService.getDeviceDetails(deviceId);
if (!result) {
const updatedDeviceTuyaUuid = (
await this.updateDeviceTuyaUuidFromTuya(deviceDetails)
).deviceTuyaUuid;
// Retry with the updated deviceTuyaUuid
result = await this.tuyaService.getDeviceDetails(updatedDeviceTuyaUuid);
}
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(result);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { productId, id, productName, uuid, name, ...rest } =
camelCaseResponse;
return {
...rest,
productUuid: deviceDetails.productDevice.uuid,
name: deviceDetails?.name,
productName: deviceDetails.productDevice.name,
} as GetDeviceDetailsInterface;
} catch (error) {
console.log('error', error);
throw new HttpException(
'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getNewDeviceDetailsFromTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {
console.log('fetching device details from Tuya for deviceId:', deviceId);
try {
const result = await this.tuyaService.getDeviceDetails(deviceId);
if (!result) {
throw new NotFoundException('Device not found');
}
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(result);
const product = await this.productRepository.findOne({
where: { prodId: camelCaseResponse.productId },
});
if (!product) {
throw new NotFoundException('Product Type is not supported');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { productId, id, productName, ...rest } = camelCaseResponse;
return {
...rest,
productUuid: product.uuid,
productName: product.name,
} as GetDeviceDetailsInterface;
} catch (error) {
console.log('error', error);
throw new HttpException(
error.message || 'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceInstructionByDeviceId(
deviceUuid: string,
projectUuid: string,
): Promise<DeviceInstructionResponse> {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
}
try {
const response = await this.getDeviceInstructionByDeviceIdTuya(
deviceDetails.deviceTuyaUuid,
);
return new SuccessResponseDto({
message: `Device instructions fetched successfully`,
data: {
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
functions: response.result.functions.map((fun: any) => {
return {
code: fun.code,
values: fun.values,
dataType: fun.type,
};
}),
},
statusCode: HttpStatus.CREATED,
});
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async getDevicesStatus(deviceUuid: string, projectUuid: string) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
}
if (deviceDetails.productDevice.prodType === ProductType.PC) {
return await this.getPowerClampInstructionStatus(deviceDetails);
} else {
return await this.getDevicesInstructionStatus(deviceUuid, projectUuid);
}
} catch (error) {
throw new HttpException(
'Error fetching device functions status',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDevicesInGateway(gatewayUuid: string, projectUuid: string) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
gatewayUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
} else if (deviceDetails.productDevice.prodType !== ProductType.GW) {
throw new BadRequestException('This is not a gateway device');
}
const response = await this.getDevicesInGatewayTuya(
deviceDetails.deviceTuyaUuid,
);
const devices = await Promise.all(
response.map(async (device: any) => {
try {
const deviceDetails = await this.getDeviceByDeviceTuyaUuid(
device.id,
);
if (deviceDetails.deviceTuyaUuid) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { id, ...rest } = device;
return {
...rest,
tuyaUuid: deviceDetails.deviceTuyaUuid,
uuid: deviceDetails.uuid,
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
};
}
return null;
} catch (error) {
return null;
}
}),
);
return new SuccessResponseDto({
message: `Devices fetched successfully`,
data: {
uuid: deviceDetails.uuid,
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
devices: devices.filter((device) => device !== null),
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async updateDeviceFirmware(
deviceUuid: string,
firmwareVersion: number,
projectUuid: string,
) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
false,
projectUuid,
);
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
throw new NotFoundException('Device Not Found');
}
const response = await this.updateDeviceFirmwareTuya(
deviceDetails.deviceTuyaUuid,
firmwareVersion,
);
if (response.success) {
return new SuccessResponseDto({
message: `Device firmware updated successfully`,
data: response,
statusCode: HttpStatus.CREATED,
});
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async addSceneToSceneDevice(
deviceUuid: string,
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
projectUuid: string,
) {
try {
const { spaceUuid, sceneUuid, switchName } = addSceneToFourSceneDeviceDto;
if (!spaceUuid || !sceneUuid || !switchName) {
throw new BadRequestException('Missing required fields in DTO');
}
const [sceneData, spaceData, deviceData] = await Promise.all([
this.sceneService.findScene(sceneUuid),
this.sceneService.getSpaceByUuid(spaceUuid),
this.getDeviceByDeviceUuid(deviceUuid, true, projectUuid),
]);
const shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
const automationName = `Auto_${shortUuid}_${timestamp}`;
const addAutomationData: AddAutomationDto = {
spaceUuid: spaceData.spaceTuyaUuid,
automationName,
decisionExpr: AUTOMATION_CONFIG.DECISION_EXPR,
effectiveTime: {
start: AUTOMATION_CONFIG.DEFAULT_START_TIME,
end: AUTOMATION_CONFIG.DEFAULT_END_TIME,
loops: AUTOMATION_CONFIG.DEFAULT_LOOPS,
},
conditions: [
{
code: 1,
entityId: deviceData.deviceTuyaUuid,
entityType: AUTOMATION_CONFIG.CONDITION_TYPE,
expr: {
comparator: AUTOMATION_CONFIG.COMPARATOR,
statusCode: switchName,
statusValue: AUTOMATION_CONFIG.SCENE_STATUS_VALUE,
},
},
],
actions: [
{
actionExecutor: AUTOMATION_CONFIG.ACTION_EXECUTOR,
entityId: sceneData.sceneTuyaUuid,
},
],
};
const automation = await this.tuyaService.createAutomation(
addAutomationData.spaceUuid,
addAutomationData.automationName,
addAutomationData.effectiveTime,
addAutomationData.decisionExpr,
addAutomationData.conditions,
addAutomationData.actions,
);
if (automation.success) {
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
where: {
device: { uuid: deviceUuid },
switchName: switchName,
},
relations: ['scene', 'device'],
});
if (existingSceneDevice) {
await this.tuyaService.deleteAutomation(
spaceData.spaceTuyaUuid,
existingSceneDevice.automationTuyaUuid,
);
existingSceneDevice.automationTuyaUuid = automation.result.id;
existingSceneDevice.scene = sceneData;
existingSceneDevice.device = deviceData;
existingSceneDevice.switchName = switchName;
const updatedSceneDevice =
await this.sceneDeviceRepository.save(existingSceneDevice);
return new SuccessResponseDto({
message: `Successfully updated scene device with uuid ${updatedSceneDevice.uuid}`,
data: updatedSceneDevice,
statusCode: HttpStatus.CREATED,
});
} else {
const sceneDevice = await this.sceneDeviceRepository.save({
scene: sceneData,
device: deviceData,
automationTuyaUuid: automation.result.id,
switchName: switchName,
});
return new SuccessResponseDto({
message: `Successfully created scene device with uuid ${sceneDevice.uuid}`,
data: sceneDevice,
statusCode: HttpStatus.CREATED,
});
}
}
} catch (err) {
const errorMessage = err.message || 'Error creating automation';
const errorStatus = err.status || HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(errorMessage, errorStatus);
}
}
async getScenesBySceneDevice(
deviceUuid: string,
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
projectUuid: string,
): Promise<any> {
try {
if (getSceneFourSceneDeviceDto.switchName) {
// Query for a single record directly when switchName is provided
const sceneDevice = await this.sceneDeviceRepository.findOne({
where: {
device: {
uuid: deviceUuid,
spaceDevice: {
spaceName: Not(ORPHAN_SPACE_NAME),
community: {
project: {
uuid: projectUuid,
},
},
},
},
switchName:
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum,
},
relations: ['device', 'scene'],
});
if (!sceneDevice) {
return {};
}
const sceneDetails = await this.sceneService.getSceneByUuid(
sceneDevice.scene.uuid,
);
return {
switchSceneUuid: sceneDevice.uuid,
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
updatedAt: sceneDevice.updatedAt,
deviceUuid: sceneDevice.device.uuid,
scene: sceneDetails.data,
};
}
// Query for multiple records if switchName is not provided
const sceneDevices = await this.sceneDeviceRepository.find({
where: { device: { uuid: deviceUuid } },
relations: ['device', 'scene'],
});
if (!sceneDevices.length) {
return [];
}
const results = await Promise.all(
sceneDevices.map(async (sceneDevice) => {
const sceneDetails = await this.sceneService.getSceneByUuid(
sceneDevice.scene.uuid,
);
return {
switchSceneUuid: sceneDevice.uuid,
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
updatedAt: sceneDevice.updatedAt,
deviceUuid: sceneDevice.device.uuid,
scene: sceneDetails.data,
};
}),
);
return new SuccessResponseDto({
message: `Successfully fetched scenes for device with uuid ${deviceUuid}`,
data: results,
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
error.message || 'Failed to fetch scenes for device',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteSceneFromSceneDevice(
params: DeviceSceneParamDto,
query: DeleteSceneFromSceneDeviceDto,
projectUuid: string,
): Promise<BaseResponseDto> {
const { deviceUuid } = params;
const { switchName } = query;
try {
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
where: {
device: {
uuid: deviceUuid,
spaceDevice: {
spaceName: Not(ORPHAN_SPACE_NAME),
community: { project: { uuid: projectUuid } },
},
},
switchName: switchName as SceneSwitchesTypeEnum,
},
relations: ['scene.space.community'],
});
if (!existingSceneDevice) {
throw new HttpException(
`No scene found for device with UUID ${deviceUuid} and switch name ${switchName}`,
HttpStatus.NOT_FOUND,
);
}
const deleteResult = await this.sceneDeviceRepository.delete({
device: { uuid: deviceUuid },
switchName: switchName as SceneSwitchesTypeEnum,
});
if (deleteResult.affected === 0) {
throw new HttpException(
`Failed to delete Switch Scene with device ID ${deviceUuid} and switch name ${switchName}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const tuyaAutomationResult = await this.tuyaService.deleteAutomation(
existingSceneDevice.scene.space.community.externalId,
existingSceneDevice.automationTuyaUuid,
);
if (!tuyaAutomationResult.success) {
throw new HttpException(
`Failed to delete Tuya automation for Switch Scene with ID ${existingSceneDevice.automationTuyaUuid}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
return new SuccessResponseDto({
message: `Switch Scene with device ID ${deviceUuid} and switch name ${switchName} deleted successfully`,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getParentHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
try {
const targetSpace = await this.spaceRepository.findOne({
where: { uuid: space.uuid },
relations: ['parent'],
});
if (!targetSpace) {
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
}
const ancestors = await this.fetchAncestors(targetSpace);
return ancestors.map((parentSpace) => ({
uuid: parentSpace.uuid,
spaceName: parentSpace.spaceName,
}));
} catch (error) {
console.error('Error fetching parent hierarchy:', error.message);
throw new HttpException(
'Error fetching parent hierarchy',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async addDevicesToOrphanSpace(
space: SpaceEntity,
project: ProjectEntity,
queryRunner: QueryRunner,
) {
const spaceRepository = queryRunner.manager.getRepository(SpaceEntity);
const deviceRepository = queryRunner.manager.getRepository(DeviceEntity);
try {
const orphanSpace = await spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
},
spaceName: ORPHAN_SPACE_NAME,
},
});
if (!orphanSpace) {
throw new HttpException(
`Orphan space not found in community ${project.name}`,
HttpStatus.NOT_FOUND,
);
}
await deviceRepository.update(
{ uuid: In(space.devices.map((device) => device.uuid)) },
{ spaceDevice: orphanSpace },
);
} catch (error) {
throw new Error(
`Failed to add devices to orphan spaces: ${error.message}`,
);
}
}
async addTuyaConstUuidToDevices() {
const devices = await this.deviceRepository.find();
const updatedDevices = [];
for (const device of devices) {
if (!device.deviceTuyaConstUuid) {
const path = `/v1.1/iot-03/devices/${device.deviceTuyaUuid}`;
const response = await this.tuya.request({
method: 'GET',
path,
});
if (!response?.success) {
console.error(
`Failed to fetch Tuya constant UUID for device ${device.deviceTuyaUuid}`,
);
continue;
}
const camelCaseResponse = convertKeysToCamelCase(response);
const tuyaConstUuid = camelCaseResponse?.result.uuid;
if (tuyaConstUuid) {
device.deviceTuyaConstUuid = tuyaConstUuid;
updatedDevices.push(device);
}
}
}
await this.deviceRepository.save(updatedDevices);
}
private async getDoorLockDevices(
projectUuid: string,
{ communities, spaces }: { spaces?: string[]; communities?: string[] },
) {
await this.validateProject(projectUuid);
const devices = await this.deviceRepository.find({
where: {
productDevice: {
prodType: ProductType.DL,
},
spaceDevice: {
spaceName: Not(ORPHAN_SPACE_NAME),
uuid: spaces && spaces.length ? In(spaces) : undefined,
community: {
uuid:
communities && communities.length ? In(communities) : undefined,
project: {
uuid: projectUuid,
},
},
},
isActive: true,
},
relations: ['productDevice', 'spaceDevice'],
});
const devicesData = await Promise.all(
devices?.map(async (device) => {
try {
const deviceDetails = await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
);
return {
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
...deviceDetails,
uuid: device.uuid,
spaceName: device.spaceDevice.spaceName,
} as GetDeviceDetailsInterface;
} catch (error) {
console.error(
`Error fetching details for device ${device.deviceTuyaUuid}:`,
error,
);
// Return null to indicate the error
return null;
}
}),
);
// Filter out null values to only include successful device data
const filteredDevicesData = devicesData.filter(
(deviceData) => deviceData !== null,
);
return new SuccessResponseDto({
message: 'Successfully retrieved all pass devices',
data: filteredDevicesData,
statusCode: HttpStatus.OK,
});
}
private async controlDeviceTuya(
deviceUuid: string,
controlDeviceDto: ControlDeviceDto,
): Promise<controlDeviceInterface> {
try {
const path = `/v1.0/iot-03/devices/${deviceUuid}/commands`;
const response = await this.tuya.request({
method: 'POST',
path,
body: {
commands: [
{ code: controlDeviceDto.code, value: controlDeviceDto.value },
],
},
});
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error control device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async updateDeviceName(deviceUuid: string, deviceName: string) {
try {
await this.deviceRepository.update(
{ uuid: deviceUuid },
{ name: deviceName },
);
} catch (error) {
throw new HttpException(
'Error updating device name',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
const firstDevice = await this.deviceRepository.findOne({
where: { uuid: deviceUuids[0], isActive: true },
relations: ['productDevice'],
});
if (!firstDevice) {
throw new BadRequestException('First device not found');
}
const firstProductType = firstDevice.productDevice.prodType;
for (let i = 1; i < deviceUuids.length; i++) {
const device = await this.deviceRepository.findOne({
where: { uuid: deviceUuids[i], isActive: true },
relations: ['productDevice'],
});
if (!device) {
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
}
if (device.productDevice.prodType !== firstProductType) {
throw new BadRequestException(`Devices have different product types`);
}
}
}
private async batchFactoryResetDevices(
batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto,
projectUuid: string,
) {
const { devicesUuid } = batchFactoryResetDevicesDto;
try {
// Check if all devices have the same product UUID
await this.checkAllDevicesHaveSameProductUuid(devicesUuid);
// Perform all operations concurrently
const results = await Promise.allSettled(
devicesUuid.map(async (deviceUuid) => {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
const result = await this.factoryResetDeviceTuya(
deviceDetails.deviceTuyaUuid,
);
return { deviceUuid, result };
}),
);
// Separate successful and failed operations
const successResults = [];
const failedResults = [];
for (const result of results) {
if (result.status === DeviceStatuses.FULLFILLED) {
const { deviceUuid, result: operationResult } = result.value;
if (operationResult.success) {
// Add to success results if operationResult.success is true
successResults.push({ deviceUuid, result: operationResult });
// Update isActive to false in the repository for the successfully reset device
await this.deviceRepository.update(
{ uuid: deviceUuid },
{ isActive: false },
);
} else {
// Add to failed results if operationResult.success is false
failedResults.push({ deviceUuid, error: operationResult.msg });
}
} else {
// Add to failed results if promise is rejected
failedResults.push({
deviceUuid: devicesUuid[results.indexOf(result)],
error: result.reason.message,
});
}
}
return { successResults, failedResults };
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || 'Device Not Found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
private async updateDeviceTuyaUuidFromTuya(device: DeviceEntity) {
console.log(`looking for device with Tuya UUID: ${device.deviceTuyaUuid}`);
try {
let last_id = null;
let deviceFound = false;
while (!deviceFound) {
const path = `/v2.0/cloud/thing/device`;
const response = await this.tuya.request({
method: 'GET',
path,
query: {
product_ids: device.productDevice.prodId,
page_size: 20, // maximum allowed by Tuya
last_id,
},
});
if (
!response.success ||
!response.result ||
!(response.result as any[]).length
) {
throw new NotFoundException('Device not found in Tuya');
}
const devicesTuya = response.result as any[];
for (const dev of devicesTuya) {
if (dev.uuid == device.deviceTuyaConstUuid) {
deviceFound = true;
device.deviceTuyaUuid = dev.id;
break;
}
}
if (!deviceFound) {
last_id = devicesTuya[devicesTuya.length - 1].id;
}
}
console.log(`found device with Tuya UUID: ${device.deviceTuyaUuid}`);
return this.deviceRepository.save(device);
} catch (error) {
console.log(error);
throw new HttpException(
'Error fetching device by product ID from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getMacAddressByDeviceIdTuya(
deviceId: string,
): Promise<GetMacAddressInterface> {
try {
const path = `/v1.0/devices/factory-infos?device_ids=${deviceId}`;
const response = await this.tuya.request({
method: 'GET',
path,
});
return response.result[0];
} catch (error) {
throw new HttpException(
'Error fetching mac address device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getDeviceInstructionByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsFunctionsInterface> {
try {
const path = `/v1.0/iot-03/devices/${deviceId}/functions`;
const response = await this.tuya.request({
method: 'GET',
path,
});
return response as GetDeviceDetailsFunctionsInterface;
} catch (error) {
throw new HttpException(
'Error fetching device functions from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getDevicesInstructionStatus(
deviceUuid: string,
projectUuid: string,
) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(
deviceUuid,
true,
projectUuid,
);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
}
const deviceStatus = await this.getDevicesInstructionStatusTuya(
deviceDetails.deviceTuyaUuid,
);
return new SuccessResponseDto({
message: `Device instructions status fetched successfully`,
data: {
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
status: deviceStatus.result[0].status,
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
'Error fetching device functions status',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getDevicesInstructionStatusTuya(
deviceUuid: string,
): Promise<GetDeviceDetailsFunctionsStatusInterface> {
try {
const path = `/v1.0/iot-03/devices/status`;
const response = await this.tuya.request({
method: 'GET',
path,
query: {
device_ids: deviceUuid,
},
});
return response as GetDeviceDetailsFunctionsStatusInterface;
} catch (error) {
throw new HttpException(
'Error fetching device functions status from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getDevicesInGatewayTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface[]> {
try {
const path = `/v1.0/devices/${deviceId}/sub-devices`;
const response: any = await this.tuya.request({
method: 'GET',
path,
});
const camelCaseResponse = response.result.map((device: any) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { product_id, category, ...rest } = device;
const camelCaseDevice = convertKeysToCamelCase({ ...rest });
return camelCaseDevice as GetDeviceDetailsInterface[];
});
return camelCaseResponse;
} catch (error) {
throw new HttpException(
'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async updateDeviceFirmwareTuya(
deviceUuid: string,
firmwareVersion: number,
): Promise<updateDeviceFirmwareInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceUuid}/firmware/${firmwareVersion}`;
const response = await this.tuya.request({
method: 'POST',
path,
});
return response as updateDeviceFirmwareInterface;
} catch (error) {
throw new HttpException(
'Error updating device firmware from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getDeviceLogsTuya(
deviceId: string,
code: string,
startTime: string = (Date.now() - 1 * 60 * 60 * 1000).toString(),
endTime: string = Date.now().toString(),
): Promise<getDeviceLogsInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceId}/report-logs?start_time=${startTime}&end_time=${endTime}&codes=${code}&size=50`;
const response = await this.tuya.request({
method: 'GET',
path,
});
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(response);
const logs = camelCaseResponse.result.logs ?? [];
return {
startTime,
endTime,
data: logs,
} as getDeviceLogsInterface;
} catch (error) {
throw new HttpException(
'Error fetching device logs from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getPowerClampInstructionStatus(deviceDetails: any) {
try {
const deviceStatus = await this.getPowerClampInstructionStatusTuya(
deviceDetails.deviceTuyaUuid,
);
const statusList = deviceStatus.result.properties as {
code: string;
value: any;
}[];
const groupedStatus = statusList.reduce(
(acc, currentStatus) => {
const { code } = currentStatus;
if (code.endsWith('A')) {
acc.phaseA.push(currentStatus);
} else if (code.endsWith('B')) {
acc.phaseB.push(currentStatus);
} else if (code.endsWith('C')) {
acc.phaseC.push(currentStatus);
} else {
acc.general.push(currentStatus);
}
return acc;
},
{
phaseA: [] as { code: string; value: any }[],
phaseB: [] as { code: string; value: any }[],
phaseC: [] as { code: string; value: any }[],
general: [] as { code: string; value: any }[],
},
);
return new SuccessResponseDto({
message: `Power clamp functions status fetched successfully`,
data: {
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
status: {
phaseA: groupedStatus.phaseA,
phaseB: groupedStatus.phaseB,
phaseC: groupedStatus.phaseC,
general: groupedStatus.general,
},
},
statusCode: HttpStatus.OK,
});
} catch (error) {
throw new HttpException(
error.message || 'Error fetching power clamp functions status',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getPowerClampInstructionStatusTuya(
deviceUuid: string,
): Promise<GetPowerClampFunctionsStatusInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceUuid}/shadow/properties`;
const response = await this.tuya.request({
method: 'GET',
path,
query: {
device_ids: deviceUuid,
},
});
const camelCaseResponse = convertKeysToCamelCase(response);
return camelCaseResponse as GetPowerClampFunctionsStatusInterface;
} catch (error) {
throw new HttpException(
'Error fetching power clamp functions status from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async fetchAncestors(space: SpaceEntity): Promise<SpaceEntity[]> {
const ancestors: SpaceEntity[] = [];
let currentSpace = space;
while (currentSpace && currentSpace.parent) {
// Fetch the parent space
const parent = await this.spaceRepository.findOne({
where: { uuid: currentSpace.parent.uuid },
relations: ['parent'], // To continue fetching upwards
});
if (parent) {
ancestors.push(parent);
currentSpace = parent;
} else {
currentSpace = null;
}
}
// Return the ancestors in reverse order to have the root at the start
return ancestors.reverse();
}
private async validateProject(uuid: string) {
const project = await this.projectRepository.findOne({
where: { uuid },
});
if (!project) {
throw new HttpException(
`A project with the uuid '${uuid}' doesn't exists.`,
HttpStatus.BAD_REQUEST,
);
}
return project;
}
async getAllDevicesBySpaceOrCommunityWithChild(
query: GetDevicesBySpaceOrCommunityDto,
): Promise<BaseResponseDto> {
try {
const { spaceUuid, communityUuid, productType } = query;
if (!spaceUuid && !communityUuid) {
throw new BadRequestException(
'Either spaceUuid or communityUuid must be provided',
);
}
// Get devices based on space or community
const devices = spaceUuid
? await this.getAllDevicesBySpace(spaceUuid)
: await this.getAllDevicesByCommunity(communityUuid);
if (!devices?.length) {
return new SuccessResponseDto({
message: `No devices found for ${spaceUuid ? 'space' : 'community'}`,
data: [],
statusCode: HttpStatus.CREATED,
});
}
const devicesFilterd = devices.filter(
(device) => device.productDevice?.prodType === productType,
);
if (devicesFilterd.length === 0) {
return new SuccessResponseDto({
message: `No ${productType} devices found for ${spaceUuid ? 'space' : 'community'}`,
data: [],
statusCode: HttpStatus.CREATED,
});
}
return new SuccessResponseDto({
message: `Devices fetched successfully`,
data: devicesFilterd,
statusCode: HttpStatus.OK,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getAllDevicesBySpace(
spaceUuid: string,
): Promise<DeviceEntity[]> {
const space = await this.spaceRepository.findOne({
where: { uuid: spaceUuid },
relations: ['children', 'devices', 'devices.productDevice'],
});
if (!space) {
throw new NotFoundException('Space not found');
}
const allDevices: DeviceEntity[] = [];
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
// Recursive fetch function
const fetchChildren = async (parentSpace: SpaceEntity) => {
const children = await this.spaceRepository.find({
where: { parent: { uuid: parentSpace.uuid } },
relations: ['children', 'devices', 'devices.productDevice'],
});
for (const child of children) {
allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid));
if (child.children.length > 0) {
await fetchChildren(child);
}
}
};
// Start recursive fetch
await fetchChildren(space);
return allDevices;
}
private async getAllDevicesByCommunity(
communityUuid: string,
): Promise<DeviceEntity[]> {
const community = await this.communityRepository.findOne({
where: { uuid: communityUuid },
relations: [
'spaces',
'spaces.children',
'spaces.devices',
'spaces.devices.productDevice',
],
});
if (!community) {
throw new NotFoundException('Community not found');
}
const allDevices: DeviceEntity[] = [];
const visitedSpaceUuids = new Set<string>();
// Recursive fetch function with visited check
const fetchSpaceDevices = async (space: SpaceEntity) => {
if (visitedSpaceUuids.has(space.uuid)) return;
visitedSpaceUuids.add(space.uuid);
if (space.devices?.length) {
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
}
if (space.children?.length) {
for (const child of space.children) {
const fullChild = await this.spaceRepository.findOne({
where: { uuid: child.uuid },
relations: ['children', 'devices', 'devices.productDevice'],
});
if (fullChild) {
await fetchSpaceDevices(fullChild);
}
}
}
};
for (const space of community.spaces) {
await fetchSpaceDevices(space);
}
return allDevices;
}
}