mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 09:57:28 +00:00
Compare commits
4 Commits
fix/commun
...
SP-1778-be
Author | SHA1 | Date | |
---|---|---|---|
a6053b3971 | |||
fddd06e06d | |||
3160773c2a | |||
110ed4157a |
@ -24,6 +24,7 @@ import { PowerClampEnergyEnum } from '@app/common/constants/power.clamp.enargy.e
|
|||||||
import { PresenceSensorEnum } from '@app/common/constants/presence.sensor.enum';
|
import { PresenceSensorEnum } from '@app/common/constants/presence.sensor.enum';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { DataSource, QueryRunner } from 'typeorm';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -35,6 +36,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
private readonly occupancyService: OccupancyService,
|
private readonly occupancyService: OccupancyService,
|
||||||
private readonly aqiDataService: AqiDataService,
|
private readonly aqiDataService: AqiDataService,
|
||||||
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||||
|
private readonly dataSource: DataSource,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
@ -79,28 +81,46 @@ export class DeviceStatusFirebaseService {
|
|||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
): Promise<AddDeviceStatusDto | null> {
|
): Promise<AddDeviceStatusDto | null> {
|
||||||
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
await queryRunner.connect();
|
||||||
|
await queryRunner.startTransaction();
|
||||||
try {
|
try {
|
||||||
const device = await this.getDeviceByDeviceTuyaUuid(
|
const device = await this.getDeviceByDeviceTuyaUuid(
|
||||||
addDeviceStatusDto.deviceTuyaUuid,
|
addDeviceStatusDto.deviceTuyaUuid,
|
||||||
|
queryRunner,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (device?.uuid) {
|
if (device?.uuid) {
|
||||||
return await this.createDeviceStatusFirebase({
|
const result = await this.createDeviceStatusFirebase(
|
||||||
deviceUuid: device.uuid,
|
{
|
||||||
...addDeviceStatusDto,
|
deviceUuid: device.uuid,
|
||||||
productType: device.productDevice.prodType,
|
...addDeviceStatusDto,
|
||||||
});
|
productType: device.productDevice.prodType,
|
||||||
|
},
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
// Return null if device not found or no UUID
|
// Return null if device not found or no UUID
|
||||||
|
await queryRunner.rollbackTransaction();
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Handle the error silently, perhaps log it internally or ignore it
|
await queryRunner.rollbackTransaction();
|
||||||
return null;
|
return null;
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getDeviceByDeviceTuyaUuid(
|
||||||
|
deviceTuyaUuid: string,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
) {
|
||||||
|
const repo = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(this.deviceRepository.target)
|
||||||
|
: this.deviceRepository;
|
||||||
|
|
||||||
async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) {
|
return await repo.findOne({
|
||||||
return await this.deviceRepository.findOne({
|
|
||||||
where: {
|
where: {
|
||||||
deviceTuyaUuid,
|
deviceTuyaUuid,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
@ -108,6 +128,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
relations: ['productDevice'],
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDevicesInstructionStatus(deviceUuid: string) {
|
async getDevicesInstructionStatus(deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -153,9 +174,14 @@ export class DeviceStatusFirebaseService {
|
|||||||
}
|
}
|
||||||
async getDeviceByDeviceUuid(
|
async getDeviceByDeviceUuid(
|
||||||
deviceUuid: string,
|
deviceUuid: string,
|
||||||
withProductDevice: boolean = true,
|
withProductDevice = true,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
) {
|
) {
|
||||||
return await this.deviceRepository.findOne({
|
const repo = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(this.deviceRepository.target)
|
||||||
|
: this.deviceRepository;
|
||||||
|
|
||||||
|
return await repo.findOne({
|
||||||
where: {
|
where: {
|
||||||
uuid: deviceUuid,
|
uuid: deviceUuid,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
@ -163,21 +189,20 @@ export class DeviceStatusFirebaseService {
|
|||||||
...(withProductDevice && { relations: ['productDevice'] }),
|
...(withProductDevice && { relations: ['productDevice'] }),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createDeviceStatusFirebase(
|
async createDeviceStatusFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const dataRef = ref(
|
const dataRef = ref(
|
||||||
this.firebaseDb,
|
this.firebaseDb,
|
||||||
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Use a transaction to handle concurrent updates
|
// Step 1: Update Firebase Realtime Database
|
||||||
await runTransaction(dataRef, (existingData) => {
|
await runTransaction(dataRef, (existingData) => {
|
||||||
if (!existingData) {
|
if (!existingData) existingData = {};
|
||||||
existingData = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign default values if fields are not present
|
|
||||||
if (!existingData.deviceTuyaUuid) {
|
if (!existingData.deviceTuyaUuid) {
|
||||||
existingData.deviceTuyaUuid = addDeviceStatusDto.deviceTuyaUuid;
|
existingData.deviceTuyaUuid = addDeviceStatusDto.deviceTuyaUuid;
|
||||||
}
|
}
|
||||||
@ -191,18 +216,15 @@ export class DeviceStatusFirebaseService {
|
|||||||
existingData.status = [];
|
existingData.status = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a map to track existing status codes
|
// Merge incoming status with existing status
|
||||||
const statusMap = new Map(
|
const statusMap = new Map(
|
||||||
existingData.status.map((item) => [item.code, item.value]),
|
existingData.status.map((item) => [item.code, item.value]),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update or add status codes
|
|
||||||
|
|
||||||
for (const statusItem of addDeviceStatusDto.status) {
|
for (const statusItem of addDeviceStatusDto.status) {
|
||||||
statusMap.set(statusItem.code, statusItem.value);
|
statusMap.set(statusItem.code, statusItem.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the map back to an array format
|
|
||||||
existingData.status = Array.from(statusMap, ([code, value]) => ({
|
existingData.status = Array.from(statusMap, ([code, value]) => ({
|
||||||
code,
|
code,
|
||||||
value,
|
value,
|
||||||
@ -211,9 +233,9 @@ export class DeviceStatusFirebaseService {
|
|||||||
return existingData;
|
return existingData;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save logs to your repository
|
// Step 2: Save device status log entries
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
const newLogs = addDeviceStatusDto.log.properties.map((property) =>
|
||||||
return this.deviceStatusLogRepository.create({
|
this.deviceStatusLogRepository.create({
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
deviceId: addDeviceStatusDto.deviceUuid,
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
||||||
productId: addDeviceStatusDto.log.productId,
|
productId: addDeviceStatusDto.log.productId,
|
||||||
@ -222,10 +244,19 @@ export class DeviceStatusFirebaseService {
|
|||||||
value: property.value,
|
value: property.value,
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
eventId: addDeviceStatusDto.log.dataId,
|
||||||
eventTime: new Date(property.time).toISOString(),
|
eventTime: new Date(property.time).toISOString(),
|
||||||
});
|
}),
|
||||||
});
|
);
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
|
if (queryRunner) {
|
||||||
|
const repo = queryRunner.manager.getRepository(
|
||||||
|
this.deviceStatusLogRepository.target,
|
||||||
|
);
|
||||||
|
await repo.save(newLogs);
|
||||||
|
} else {
|
||||||
|
await this.deviceStatusLogRepository.save(newLogs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: Trigger additional data services
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
if (addDeviceStatusDto.productType === ProductType.PC) {
|
||||||
const energyCodes = new Set([
|
const energyCodes = new Set([
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
||||||
@ -269,7 +300,8 @@ export class DeviceStatusFirebaseService {
|
|||||||
addDeviceStatusDto.deviceUuid,
|
addDeviceStatusDto.deviceUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Return the updated data
|
|
||||||
|
// Step 4: Return updated Firebase status
|
||||||
const snapshot: DataSnapshot = await get(dataRef);
|
const snapshot: DataSnapshot = await get(dataRef);
|
||||||
return snapshot.val();
|
return snapshot.val();
|
||||||
}
|
}
|
||||||
|
@ -36,9 +36,18 @@ export class AqiDataService {
|
|||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
await this.dataSource.query(query, params);
|
await queryRunner.connect();
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
try {
|
||||||
|
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||||
|
await queryRunner.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to execute procedure ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -57,9 +57,18 @@ export class OccupancyService {
|
|||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
await this.dataSource.query(query, params);
|
await queryRunner.connect();
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
try {
|
||||||
|
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||||
|
await queryRunner.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to execute procedure ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -46,12 +46,21 @@ export class PowerClampService {
|
|||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
'fact_device_energy_consumed',
|
await queryRunner.connect();
|
||||||
procedureFileName,
|
try {
|
||||||
);
|
const query = this.loadQuery(
|
||||||
await this.dataSource.query(query, params);
|
'fact_device_energy_consumed',
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
procedureFileName,
|
||||||
|
);
|
||||||
|
await queryRunner.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to execute procedure ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
await queryRunner.release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -190,25 +190,26 @@ export class CommunityService {
|
|||||||
.distinct(true);
|
.distinct(true);
|
||||||
|
|
||||||
if (includeSpaces) {
|
if (includeSpaces) {
|
||||||
qb.leftJoinAndSelect('c.spaces', 'space', 'space.disabled = false')
|
qb.leftJoinAndSelect(
|
||||||
|
'c.spaces',
|
||||||
|
'space',
|
||||||
|
'space.disabled = :disabled AND space.spaceName != :orphanSpaceName',
|
||||||
|
{ disabled: false, orphanSpaceName: ORPHAN_SPACE_NAME },
|
||||||
|
)
|
||||||
.leftJoinAndSelect('space.parent', 'parent')
|
.leftJoinAndSelect('space.parent', 'parent')
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
'space.children',
|
'space.children',
|
||||||
'children',
|
'children',
|
||||||
'children.disabled = :disabled',
|
'children.disabled = :disabled',
|
||||||
{ disabled: false },
|
{ disabled: false },
|
||||||
)
|
);
|
||||||
// .leftJoinAndSelect('space.spaceModel', 'spaceModel')
|
// .leftJoinAndSelect('space.spaceModel', 'spaceModel')
|
||||||
.andWhere('space.spaceName != :orphanSpaceName', {
|
|
||||||
orphanSpaceName: ORPHAN_SPACE_NAME,
|
|
||||||
})
|
|
||||||
.andWhere('space.disabled = :disabled', { disabled: false });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
qb.andWhere(
|
qb.andWhere(
|
||||||
`c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`,
|
`c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`,
|
||||||
{ search: `%${search}%` },
|
{ search },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,7 +217,6 @@ export class CommunityService {
|
|||||||
|
|
||||||
const { baseResponseDto, paginationResponseDto } =
|
const { baseResponseDto, paginationResponseDto } =
|
||||||
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
|
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
|
||||||
|
|
||||||
if (includeSpaces) {
|
if (includeSpaces) {
|
||||||
baseResponseDto.data = baseResponseDto.data.map((community) => ({
|
baseResponseDto.data = baseResponseDto.data.map((community) => ({
|
||||||
...community,
|
...community,
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import { DeviceService } from '../services/device.service';
|
|
||||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
import { GetDoorLockDevices, ProjectParam } from '../dtos';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { GetDevicesFilterDto, ProjectParam } from '../dtos';
|
||||||
|
import { DeviceService } from '../services/device.service';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -25,7 +25,7 @@ export class DeviceProjectController {
|
|||||||
})
|
})
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
@Param() param: ProjectParam,
|
@Param() param: ProjectParam,
|
||||||
@Query() query: GetDoorLockDevices,
|
@Query() query: GetDevicesFilterDto,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.getAllDevices(param, query);
|
return await this.deviceService.getAllDevices(param, query);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
|
IsArray,
|
||||||
IsEnum,
|
IsEnum,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -41,16 +42,7 @@ export class GetDeviceLogsDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
public endTime: string;
|
public endTime: string;
|
||||||
}
|
}
|
||||||
export class GetDoorLockDevices {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Device Type',
|
|
||||||
enum: DeviceTypeEnum,
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsEnum(DeviceTypeEnum)
|
|
||||||
@IsOptional()
|
|
||||||
public deviceType: DeviceTypeEnum;
|
|
||||||
}
|
|
||||||
export class GetDevicesBySpaceOrCommunityDto {
|
export class GetDevicesBySpaceOrCommunityDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Device Product Type',
|
description: 'Device Product Type',
|
||||||
@ -72,3 +64,23 @@ export class GetDevicesBySpaceOrCommunityDto {
|
|||||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||||
requireEither?: never; // This ensures at least one of them is provided
|
requireEither?: never; // This ensures at least one of them is provided
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class GetDevicesFilterDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Device Type',
|
||||||
|
enum: DeviceTypeEnum,
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsEnum(DeviceTypeEnum)
|
||||||
|
@IsOptional()
|
||||||
|
public deviceType: DeviceTypeEnum;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of Space IDs to filter devices',
|
||||||
|
required: false,
|
||||||
|
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
|
public spaces?: string[];
|
||||||
|
}
|
||||||
|
@ -53,7 +53,7 @@ import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
|||||||
import {
|
import {
|
||||||
GetDeviceLogsDto,
|
GetDeviceLogsDto,
|
||||||
GetDevicesBySpaceOrCommunityDto,
|
GetDevicesBySpaceOrCommunityDto,
|
||||||
GetDoorLockDevices,
|
GetDevicesFilterDto,
|
||||||
} from '../dtos/get.device.dto';
|
} from '../dtos/get.device.dto';
|
||||||
import {
|
import {
|
||||||
controlDeviceInterface,
|
controlDeviceInterface,
|
||||||
@ -955,19 +955,20 @@ export class DeviceService {
|
|||||||
|
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
param: ProjectParam,
|
param: ProjectParam,
|
||||||
query: GetDoorLockDevices,
|
{ deviceType, spaces }: GetDevicesFilterDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
try {
|
try {
|
||||||
await this.validateProject(param.projectUuid);
|
await this.validateProject(param.projectUuid);
|
||||||
if (query.deviceType === DeviceTypeEnum.DOOR_LOCK) {
|
if (deviceType === DeviceTypeEnum.DOOR_LOCK) {
|
||||||
return await this.getDoorLockDevices(param.projectUuid);
|
return await this.getDoorLockDevices(param.projectUuid, spaces);
|
||||||
} else if (!query.deviceType) {
|
} else if (!deviceType) {
|
||||||
const devices = await this.deviceRepository.find({
|
const devices = await this.deviceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
spaceDevice: {
|
spaceDevice: {
|
||||||
community: { project: { uuid: param.projectUuid } },
|
uuid: spaces && spaces.length ? In(spaces) : undefined,
|
||||||
spaceName: Not(ORPHAN_SPACE_NAME),
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
||||||
|
community: { project: { uuid: param.projectUuid } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
relations: [
|
relations: [
|
||||||
@ -1563,7 +1564,7 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDoorLockDevices(projectUuid: string) {
|
async getDoorLockDevices(projectUuid: string, spaces?: string[]) {
|
||||||
await this.validateProject(projectUuid);
|
await this.validateProject(projectUuid);
|
||||||
|
|
||||||
const devices = await this.deviceRepository.find({
|
const devices = await this.deviceRepository.find({
|
||||||
@ -1573,6 +1574,7 @@ export class DeviceService {
|
|||||||
},
|
},
|
||||||
spaceDevice: {
|
spaceDevice: {
|
||||||
spaceName: Not(ORPHAN_SPACE_NAME),
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
||||||
|
uuid: spaces && spaces.length ? In(spaces) : undefined,
|
||||||
community: {
|
community: {
|
||||||
project: {
|
project: {
|
||||||
uuid: projectUuid,
|
uuid: projectUuid,
|
||||||
|
Reference in New Issue
Block a user