finished get power clamp status

This commit is contained in:
faris Aljohari
2024-10-17 00:41:02 -05:00
parent a157444897
commit f0666fab82
4 changed files with 64 additions and 0 deletions

View File

@ -15,4 +15,5 @@ export enum ProductType {
WL = 'WL', WL = 'WL',
GD = 'GD', GD = 'GD',
CUR = 'CUR', CUR = 'CUR',
PC = 'PC',
} }

View File

@ -191,4 +191,14 @@ export class DeviceController {
batchFactoryResetDevicesDto, batchFactoryResetDevicesDto,
); );
} }
@ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Get(':powerClampUuid/power-clamp/status')
async getPowerClampInstructionStatus(
@Param('powerClampUuid') powerClampUuid: string,
) {
return await this.deviceService.getPowerClampInstructionStatus(
powerClampUuid,
);
}
} }

View File

@ -70,3 +70,10 @@ export interface getDeviceLogsInterface {
endTime: string; endTime: string;
deviceUuid?: string; deviceUuid?: string;
} }
export interface GetPowerClampFunctionsStatusInterface {
result: {
properties: [];
};
success: boolean;
msg: string;
}

View File

@ -14,6 +14,7 @@ import {
GetDeviceDetailsFunctionsInterface, GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface, GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface, GetDeviceDetailsInterface,
GetPowerClampFunctionsStatusInterface,
controlDeviceInterface, controlDeviceInterface,
getDeviceLogsInterface, getDeviceLogsInterface,
updateDeviceFirmwareInterface, 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<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,
);
}
}
} }