Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/tuyanet.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2021-11-13 12:04:59 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2021-11-13 12:04:59 +0300
commite866ddca850aa49652c795666920653c958ce142 (patch)
tree878966a2c080f15a8e15af53a76c4121b67f832d
parentf02ea6d5655d9f5f7f3a3b855d6803c5b3eab987 (diff)
UpdateDps methos, some refactoring, SetDps() renamed to SetDp()
-rw-r--r--README.md11
-rw-r--r--TuyaDevice.cs73
-rw-r--r--TuyaNet.csproj2
3 files changed, 73 insertions, 13 deletions
diff --git a/README.md b/README.md
index ae7cae1..d788b92 100644
--- a/README.md
+++ b/README.md
@@ -220,7 +220,16 @@ Dictionary<int, object> dps = await device.GetDps();
// Change multiple values at once
Dictionary<int, object> newDps = await device.SetDps(new Dictionary<int, object> { { 1, false }, { 2, true } });
// Change single value
-newDps = await device.SetDps(1, true);
+newDps = await device.SetDp(1, true);
+```
+
+Some devices may have dynamically changed values, such as timers. They need to be updated before reading:
+```C#
+newDps = await device.UpdateDps(new int[] { 9, 10 });
+```
+Or just:
+```C#
+newDps = await device.UpdateDps(9, 10);
```
## Credits
diff --git a/TuyaDevice.cs b/TuyaDevice.cs
index bd84ec1..403d93f 100644
--- a/TuyaDevice.cs
+++ b/TuyaDevice.cs
@@ -209,13 +209,15 @@ namespace com.clusterrr.TuyaNet
}
/// <summary>
- /// Requests current DPS status.
+ /// Requests current DPs status.
/// </summary>
- /// <returns>Dictionary of DPS numbers and values.</returns>
- public async Task<Dictionary<int, object>> GetDps()
+ /// <param name="retries">Number of retries in case of network error.</param>
+ /// <param name="nullRetries">Number of retries in case of empty answer.</param>
+ /// <returns>Dictionary of DP numbers and values.</returns>
+ public async Task<Dictionary<int, object>> GetDps(int retries = 5, int nullRetries = 1)
{
var requestJson = FillJson(null);
- var response = await SendAsync(TuyaCommand.DP_QUERY, requestJson, retries: 2, nullRetries: 1);
+ var response = await SendAsync(TuyaCommand.DP_QUERY, requestJson, retries, nullRetries);
if (string.IsNullOrEmpty(response.JSON))
throw new InvalidDataException("Response is empty");
var root = JObject.Parse(response.JSON);
@@ -223,21 +225,37 @@ namespace com.clusterrr.TuyaNet
return dps.ToDictionary(kv => int.Parse(kv.Key), kv => kv.Value);
}
+ [Obsolete("SetDps() is renamed to SetDp(), use SetDp()")]
/// <summary>
- /// Sets DPS to specified value.
+ /// Sets single DP to specified value.
/// </summary>
- /// <param name="dpsNumber">DPS number.</param>
+ /// <param name="dp">DP number.</param>
/// <param name="value">Value.</param>
+ /// <param name="retries">Number of retries in case of network error.</param>
+ /// <param name="nullRetries">Number of retries in case of empty answer.</param>
/// <returns></returns>
- public async Task<Dictionary<int, object>> SetDps(int dpsNumber, object value)
- => await SetDps(new Dictionary<int, object> { { dpsNumber, value } });
+ public async Task<Dictionary<int, object>> SetDps(int dp, object value, int retries = 2, int nullRetries = 1)
+ => await SetDps(new Dictionary<int, object> { { dp, value } }, retries, nullRetries);
/// <summary>
- /// Sets DPS to specified value.
+ /// Sets single DP to specified value.
/// </summary>
- /// <param name="dps">Dictionary of DPS numbers and values to set.</param>
+ /// <param name="dp">DP number.</param>
+ /// <param name="value">Value.</param>
+ /// <param name="retries">Number of retries in case of network error.</param>
+ /// <param name="nullRetries">Number of retries in case of empty answer.</param>
/// <returns></returns>
- public async Task<Dictionary<int, object>> SetDps(Dictionary<int, object> dps)
+ public async Task<Dictionary<int, object>> SetDp(int dp, object value, int retries = 2, int nullRetries = 1)
+ => await SetDps(new Dictionary<int, object> { { dp, value } }, retries, nullRetries);
+
+ /// <summary>
+ /// Sets DPs to specified value.
+ /// </summary>
+ /// <param name="dps">Dictionary of DP numbers and values to set.</param>
+ /// <param name="retries">Number of retries in case of network error.</param>
+ /// <param name="nullRetries">Number of retries in case of empty answer.</param>
+ /// <returns></returns>
+ public async Task<Dictionary<int, object>> SetDps(Dictionary<int, object> dps, int retries = 2, int nullRetries = 1)
{
var cmd = new Dictionary<string, object>
{
@@ -245,7 +263,38 @@ namespace com.clusterrr.TuyaNet
};
string requestJson = JsonConvert.SerializeObject(cmd);
requestJson = FillJson(requestJson);
- var response = await SendAsync(TuyaCommand.CONTROL, requestJson, retries: 2, nullRetries: 1);
+ var response = await SendAsync(TuyaCommand.CONTROL, requestJson, retries, nullRetries);
+ if (string.IsNullOrEmpty(response.JSON))
+ throw new InvalidDataException("Response is empty");
+ var root = JObject.Parse(response.JSON);
+ var newDps = JsonConvert.DeserializeObject<Dictionary<string, object>>(root.GetValue("dps").ToString());
+ return newDps.ToDictionary(kv => int.Parse(kv.Key), kv => kv.Value);
+ }
+
+ /// <summary>
+ /// Update DP values.
+ /// </summary>
+ /// <param name="dpIds">DP identificators to update (can be empty for some devices).</param>
+ /// <returns>Dictionary of DP numbers and values.</returns>
+ public async Task<Dictionary<int, object>> UpdateDps(params int[] dpIds)
+ => await UpdateDps(dpIds, retries: 5, nullRetries: 1);
+
+ /// <summary>
+ /// Update DP values.
+ /// </summary>
+ /// <param name="dpIds">DP identificators to update (can be empty for some devices).</param>
+ /// <param name="retries">Number of retries in case of network error.</param>
+ /// <param name="nullRetries">Number of retries in case of empty answer.</param>
+ /// <returns>Dictionary of DP numbers and values.</returns>
+ public async Task<Dictionary<int, object>> UpdateDps(IEnumerable<int> dpIds, int retries = 5, int nullRetries = 1)
+ {
+ var cmd = new Dictionary<string, object>
+ {
+ { "dpId", dpIds.ToArray() }
+ };
+ string requestJson = JsonConvert.SerializeObject(cmd);
+ requestJson = FillJson(requestJson);
+ var response = await SendAsync(TuyaCommand.UPDATE_DPS, requestJson, retries, nullRetries);
if (string.IsNullOrEmpty(response.JSON))
throw new InvalidDataException("Response is empty");
var root = JObject.Parse(response.JSON);
diff --git a/TuyaNet.csproj b/TuyaNet.csproj
index 9884672..413af72 100644
--- a/TuyaNet.csproj
+++ b/TuyaNet.csproj
@@ -15,6 +15,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<LangVersion>9.0</LangVersion>
+ <AssemblyVersion>1.0.1.0</AssemblyVersion>
+ <FileVersion>1.0.1.0</FileVersion>
</PropertyGroup>
<ItemGroup>