Skip to content

Add: Get report logs for a device.#17

Open
HmarikBel wants to merge 6 commits into
thatbrainiac:masterfrom
HmarikBel:master
Open

Add: Get report logs for a device.#17
HmarikBel wants to merge 6 commits into
thatbrainiac:masterfrom
HmarikBel:master

Conversation

@HmarikBel

Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings June 20, 2026 10:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands the Tuya.Net client with new IoT device APIs, primarily adding support for fetching device report logs (and additionally device specification data), and updates authenticated request handling to refresh access tokens when they become invalid.

Changes:

  • Add GetReportLogsAsync(...) to IDeviceManager/DeviceManager plus DTOs (ReportLogs, ReportLog) to deserialize report log responses.
  • Add GetDeviceSpecificationAsync(...) to IDeviceManager/DeviceManager plus DTOs (DeviceSpecification, Specification) to deserialize device specification responses.
  • Update TuyaClient.RequestAsync to retry once after refreshing the access token on token-invalid responses; bump package version.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
Tuya.Net/TuyaClient.cs Adds token-invalid retry/refresh behavior for authenticated requests.
Tuya.Net/Tuya.Net.csproj Bumps package version to 0.2.2-alpha.
Tuya.Net/IoT/IDeviceManager.cs Adds new public API methods for report logs and device specifications.
Tuya.Net/IoT/DeviceManager.cs Implements the new device report log/specification calls.
Tuya.Net/Data/Specification.cs Adds DTO for device function/status specification entries.
Tuya.Net/Data/DeviceSpecification.cs Adds DTO for device specification response.
Tuya.Net/Data/ReportLogs.cs Adds DTO for report log list response (including v2.0/v2.1 field aliases).
Tuya.Net/Data/ReportLog.cs Adds DTO for individual report log entries (including v2.0/v2.1 field aliases).
Tuya.Net.Tests/TuyaClientTests.cs Adds integration-style tests for report logs and device specifications.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Tuya.Net/TuyaClient.cs
Comment on lines +67 to +71
/// <summary>
/// Credentials reloaded flag.
/// </summary>
private bool сredentialsReloaded;

Comment thread Tuya.Net/TuyaClient.cs
Comment on lines +92 to +108
try
{
var requestResult = await LowLevel.SendRequestAsync<T>(httpMethod, path, tuyaAccessToken, payload, cancellationToken);
сredentialsReloaded = false;
return requestResult;
}
catch (TuyaResponseException ex) when (ex.Code == "1010" || ex.Message.Contains("token invalid"))
{
if (!сredentialsReloaded)
{
сredentialsReloaded = true;
tuyaAccessToken = await GetAccessTokenInfoAsync(ct: cancellationToken);
return await RequestAsync<T>(httpMethod, path, payload, cancellationToken);
}

throw;
}
Comment on lines +91 to +96
{
var startUnix = new DateTimeOffset(start).ToUnixTimeMilliseconds();
var endUnix = new DateTimeOffset(end).ToUnixTimeMilliseconds();
logger?.LogInformation("Getting device instructions for device: {deviceId}", deviceId);
return await client.RequestAsync<ReportLogs>(HttpMethod.Get, $"/{apiVersion}/cloud/thing/{deviceId}/report-logs?codes={codes}&end_time={endUnix}&size={size}&start_time={startUnix}", cancellationToken: ct);
}
Comment on lines +3 to +4
namespace Tuya.Net.Data;

Comment on lines +13 to +18
[JsonProperty("deviceId", NullValueHandling = NullValueHandling.Ignore)]
public string DeviceIdV21
{
set => DeviceId = value;
get => DeviceId;
}
Comment on lines +131 to +143
/// <summary>
/// Test obtaining a device.
/// </summary>
[Test]
public void Test_GetDeviceStatisticsAsync()
{
Assert.DoesNotThrowAsync(async () =>
{
var testDeviceId = config["TestDeviceId"];
AssertInconclusiveIfNullOrEmpty(testDeviceId);
//var reportLogs = await client.DeviceManager.GetReportLogsAsync(testDeviceId, "v2.1", "add_ele", DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, 10);
//var reportLogs = await client.DeviceManager.GetReportLogsAsync(testDeviceId, "v2.0","va_battery", DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, 10);
var reportLogs = await client.DeviceManager.GetReportLogsAsync(testDeviceId, "v2.1", "stat", DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, 10);
Comment on lines +109 to +113
/// <param name="apiVersion">Api version. (Try v2.0, v2.1)</param>
/// <param name="codes">Report codes like add_ele, cur_power. Check device debugging page.</param>
/// <param name="start">Start date.</param>
/// <param name="end">End date</param>
/// <param name="size">Batch size.</param>
Comment on lines +71 to +86
/// <summary>
/// Get the specification (functions and status) of a device.
/// </summary>
/// <param name="deviceId">Tuya device ID.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A <see cref="DeviceSpecification"/> instance describing the device's functions and status.</returns>
public Task<DeviceSpecification?> GetDeviceSpecificationAsync(string deviceId, CancellationToken ct = default);

/// <summary>
/// Get the specification (functions and status) of a device.
/// </summary>
/// <param name="device">Tuya device.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A <see cref="DeviceSpecification"/> instance describing the device's functions and status.</returns>
public Task<DeviceSpecification?> GetDeviceSpecificationAsync(DeviceInfo device, CancellationToken ct = default);

Comment on lines +19 to +20
[JsonProperty("dp_id", NullValueHandling = NullValueHandling.Ignore)]
public int DpId { get; set; }
Comment on lines +33 to +34
[JsonProperty("total", NullValueHandling = NullValueHandling.Ignore)]
public int Total { get; set; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants