Add Device Logs Retrieval Endpoint and Service

This commit is contained in:
faris Aljohari
2024-08-26 14:41:02 +03:00
parent 176c2af877
commit 807dc84501
4 changed files with 90 additions and 4 deletions

View File

@ -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,
);
}
}
}