Add: Get report logs for a device.#17
Open
HmarikBel wants to merge 6 commits into
Open
Conversation
There was a problem hiding this comment.
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(...)toIDeviceManager/DeviceManagerplus DTOs (ReportLogs,ReportLog) to deserialize report log responses. - Add
GetDeviceSpecificationAsync(...)toIDeviceManager/DeviceManagerplus DTOs (DeviceSpecification,Specification) to deserialize device specification responses. - Update
TuyaClient.RequestAsyncto 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 on lines
+67
to
+71
| /// <summary> | ||
| /// Credentials reloaded flag. | ||
| /// </summary> | ||
| private bool сredentialsReloaded; | ||
|
|
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; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.