Merge branch 'dev' of https://github.com/SyncrowIOT/backend into feature/space-management

This commit is contained in:
hannathkadher
2024-10-30 23:08:51 +04:00
35 changed files with 611 additions and 42 deletions

View File

@ -14,6 +14,7 @@ import {
GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface,
GetPowerClampFunctionsStatusInterface,
controlDeviceInterface,
getDeviceLogsInterface,
updateDeviceFirmwareInterface,
@ -985,7 +986,6 @@ export class DeviceService {
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
try {
// Fetch only the relevant spaces, starting with the target space
const targetSpace = await this.spaceRepository.findOne({
where: { uuid: space.uuid },
@ -1015,6 +1015,84 @@ export class DeviceService {
}
}
async getPowerClampInstructionStatus(powerClampUuid: string) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(powerClampUuid);
if (!deviceDetails) {
throw new NotFoundException('Device Not Found');
} else if (deviceDetails.productDevice.prodType !== ProductType.PC) {
throw new BadRequestException('This is not a power clamp device');
}
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 {
productUuid: deviceDetails.productDevice.uuid,
productType: deviceDetails.productDevice.prodType,
status: {
phaseA: groupedStatus.phaseA,
phaseB: groupedStatus.phaseB,
phaseC: groupedStatus.phaseC,
general: groupedStatus.general,
},
};
} catch (error) {
throw new HttpException(
error.message || 'Error fetching power clamp functions status',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
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[] = [];