mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Merge pull request #89 from SyncrowIOT/SP-435-be-create-endpoint-for-device-logs
Add Device Logs Retrieval Endpoint and Service
This commit is contained in:
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,3 +10,12 @@ export class GetDeviceByRoomUuidDto {
|
||||
@IsNotEmpty()
|
||||
public roomUuid: string;
|
||||
}
|
||||
export class GetDeviceLogsDto {
|
||||
@ApiProperty({
|
||||
description: 'code',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public code: string;
|
||||
}
|
||||
|
@ -64,3 +64,9 @@ export interface updateDeviceFirmwareInterface {
|
||||
result: boolean;
|
||||
msg: string;
|
||||
}
|
||||
export interface getDeviceLogsInterface {
|
||||
data: [];
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
deviceUuid?: string;
|
||||
}
|
||||
|
@ -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<getDeviceLogsInterface> {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user