From 807dc8450194cd0b956eb3cfe2f237511afe6817 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:41:02 +0300 Subject: [PATCH] Add Device Logs Retrieval Endpoint and Service --- src/device/controllers/device.controller.ts | 21 ++++++- src/device/dtos/get.device.dto.ts | 9 +++ src/device/interfaces/get.device.interface.ts | 6 ++ src/device/services/device.service.ts | 58 ++++++++++++++++++- 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 6e834eb..bc399b6 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -14,7 +14,10 @@ import { } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto'; -import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto'; +import { + GetDeviceByRoomUuidDto, + GetDeviceLogsDto, +} from '../dtos/get.device.dto'; import { ControlDeviceDto } from '../dtos/control.device.dto'; import { CheckRoomGuard } from 'src/guards/room.guard'; import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard'; @@ -236,4 +239,20 @@ export class DeviceController { ); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('report-logs/:deviceUuid') + async getBuildingChildByUuid( + @Param('deviceUuid') deviceUuid: string, + @Query() query: GetDeviceLogsDto, + ) { + try { + return await this.deviceService.getDeviceLogs(deviceUuid, query); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/device/dtos/get.device.dto.ts b/src/device/dtos/get.device.dto.ts index 26002bb..99c0dad 100644 --- a/src/device/dtos/get.device.dto.ts +++ b/src/device/dtos/get.device.dto.ts @@ -10,3 +10,12 @@ export class GetDeviceByRoomUuidDto { @IsNotEmpty() public roomUuid: string; } +export class GetDeviceLogsDto { + @ApiProperty({ + description: 'code', + required: true, + }) + @IsString() + @IsNotEmpty() + public code: string; +} diff --git a/src/device/interfaces/get.device.interface.ts b/src/device/interfaces/get.device.interface.ts index 526c199..7422ed2 100644 --- a/src/device/interfaces/get.device.interface.ts +++ b/src/device/interfaces/get.device.interface.ts @@ -64,3 +64,9 @@ export interface updateDeviceFirmwareInterface { result: boolean; msg: string; } +export interface getDeviceLogsInterface { + data: []; + startTime: string; + endTime: string; + deviceUuid?: string; +} diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index db1b521..128ea73 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -15,9 +15,13 @@ import { GetDeviceDetailsFunctionsStatusInterface, GetDeviceDetailsInterface, controlDeviceInterface, + getDeviceLogsInterface, updateDeviceFirmwareInterface, } from '../interfaces/get.device.interface'; -import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto'; +import { + GetDeviceByRoomUuidDto, + GetDeviceLogsDto, +} from '../dtos/get.device.dto'; import { ControlDeviceDto } from '../dtos/control.device.dto'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; import { DeviceRepository } from '@app/common/modules/device/repositories'; @@ -636,8 +640,6 @@ export class DeviceService { // Check if the device is a door lock (DL) if (device.productDevice.prodType === ProductType.DL) { - console.log('device', device.deviceTuyaUuid); - const doorLockInstructionsStatus = await this.getDevicesInstructionStatus(device.uuid); @@ -689,4 +691,54 @@ export class DeviceService { ); } } + async getDeviceLogs(deviceUuid: string, query: GetDeviceLogsDto) { + try { + const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid); + + if (!deviceDetails) { + throw new NotFoundException('Device Not Found'); + } + + const response = await this.getDeviceLogsTuya( + deviceDetails.deviceTuyaUuid, + query.code, + ); + + return { + deviceUuid, + ...response, + }; + } catch (error) { + throw new HttpException( + error.message || 'Device Not Found', + HttpStatus.NOT_FOUND, + ); + } + } + async getDeviceLogsTuya( + deviceId: string, + code: string, + ): Promise { + try { + const now = Date.now(); + const twoWeeksAgo = now - 14 * 24 * 60 * 60 * 1000; + const path = `/v2.0/cloud/thing/${deviceId}/report-logs?start_time=${twoWeeksAgo}&end_time=${now}&codes=${code}&size=50`; + const response = await this.tuya.request({ + method: 'GET', + path, + }); + // Convert keys to camel case + const camelCaseResponse = convertKeysToCamelCase(response); + return { + startTime: twoWeeksAgo.toString(), + endTime: now.toString(), + data: camelCaseResponse.result.logs, + } as getDeviceLogsInterface; + } catch (error) { + throw new HttpException( + 'Error fetching device logs from Tuya', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }