diff --git a/libs/common/src/constants/product-type.enum.ts b/libs/common/src/constants/product-type.enum.ts index 6442f87..2f518fb 100644 --- a/libs/common/src/constants/product-type.enum.ts +++ b/libs/common/src/constants/product-type.enum.ts @@ -15,4 +15,5 @@ export enum ProductType { WL = 'WL', GD = 'GD', CUR = 'CUR', + PC = 'PC', } diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 5f1a7ae..d0ebbc9 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -191,4 +191,14 @@ export class DeviceController { batchFactoryResetDevicesDto, ); } + @ApiBearerAuth() + // @UseGuards(JwtAuthGuard) + @Get(':powerClampUuid/power-clamp/status') + async getPowerClampInstructionStatus( + @Param('powerClampUuid') powerClampUuid: string, + ) { + return await this.deviceService.getPowerClampInstructionStatus( + powerClampUuid, + ); + } } diff --git a/src/device/interfaces/get.device.interface.ts b/src/device/interfaces/get.device.interface.ts index 7422ed2..93438b9 100644 --- a/src/device/interfaces/get.device.interface.ts +++ b/src/device/interfaces/get.device.interface.ts @@ -70,3 +70,10 @@ export interface getDeviceLogsInterface { endTime: string; deviceUuid?: string; } +export interface GetPowerClampFunctionsStatusInterface { + result: { + properties: []; + }; + success: boolean; + msg: string; +} diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 2b37df8..db1cfb4 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -14,6 +14,7 @@ import { GetDeviceDetailsFunctionsInterface, GetDeviceDetailsFunctionsStatusInterface, GetDeviceDetailsInterface, + GetPowerClampFunctionsStatusInterface, controlDeviceInterface, getDeviceLogsInterface, updateDeviceFirmwareInterface, @@ -965,4 +966,49 @@ 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, + ); + + return { + productUuid: deviceDetails.productDevice.uuid, + productType: deviceDetails.productDevice.prodType, + status: deviceStatus.result.properties, + }; + } catch (error) { + throw new HttpException( + error.message || 'Error fetching power clamp functions status', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + async getPowerClampInstructionStatusTuya( + deviceUuid: string, + ): Promise { + 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, + ); + } + } }