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-12-12 09:11:01 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2021-12-12 09:11:01 +0300
commit2486dd4f45f2977656c15471d56468677dd64ef9 (patch)
treec4a54e7de15b2482da9352c1342adfd3da56e0aa
parent646a4b966c991986ba71b471da3aaa46c196dcc1 (diff)
TuyaDevice can request local key on it's own now
-rw-r--r--TuyaApi.cs2
-rw-r--r--TuyaDevice.cs42
2 files changed, 40 insertions, 4 deletions
diff --git a/TuyaApi.cs b/TuyaApi.cs
index f1694fb..1e25073 100644
--- a/TuyaApi.cs
+++ b/TuyaApi.cs
@@ -12,7 +12,7 @@ using System.Threading.Tasks;
namespace com.clusterrr.TuyaNet
{
/// <summary>
- /// Provides access to Tuya Web API.
+ /// Provides access to Tuya Cloud API.
/// </summary>
public class TuyaApi
{
diff --git a/TuyaDevice.cs b/TuyaDevice.cs
index deed2a7..41331e6 100644
--- a/TuyaDevice.cs
+++ b/TuyaDevice.cs
@@ -29,6 +29,21 @@ namespace com.clusterrr.TuyaNet
{
IP = ip;
LocalKey = localKey;
+ this.accessId = null;
+ this.apiSecret = null;
+ DeviceId = deviceId;
+ ProtocolVersion = protocolVersion;
+ Port = port;
+ ReceiveTimeout = receiveTimeout;
+ }
+
+ public TuyaDevice(string ip, TuyaApi.Region region, string accessId, string apiSecret, string deviceId, TuyaProtocolVersion protocolVersion = TuyaProtocolVersion.V33, int port = 6668, int receiveTimeout = 250)
+ {
+ IP = ip;
+ LocalKey = null;
+ this.region = region;
+ this.accessId = accessId;
+ this.apiSecret = apiSecret;
DeviceId = deviceId;
ProtocolVersion = protocolVersion;
Port = port;
@@ -42,7 +57,7 @@ namespace com.clusterrr.TuyaNet
/// <summary>
/// Local key of device.
/// </summary>
- public string LocalKey { get; private set; }
+ public string LocalKey { get; set; }
/// <summary>
/// Device ID.
/// </summary>
@@ -65,6 +80,9 @@ namespace com.clusterrr.TuyaNet
public bool PermanentConnection { get; set; } = false;
private TcpClient client = null;
+ private TuyaApi.Region region;
+ private string accessId;
+ private string apiSecret;
/// <summary>
/// Fills JSON string with base fields required by most commands.
@@ -100,7 +118,10 @@ namespace com.clusterrr.TuyaNet
/// <param name="json">String with JSON to send.</param>
/// <returns>Raw data.</returns>
public byte[] EncodeRequest(TuyaCommand command, string json)
- => TuyaParser.EncodeRequest(command, json, Encoding.UTF8.GetBytes(LocalKey), ProtocolVersion);
+ {
+ if (string.IsNullOrEmpty(accessId)) throw new ArgumentException("LocalKey is not specified", "LocalKey");
+ return TuyaParser.EncodeRequest(command, json, Encoding.UTF8.GetBytes(LocalKey), ProtocolVersion);
+ }
/// <summary>
/// Parses and decrypts payload data from received bytes.
@@ -108,7 +129,10 @@ namespace com.clusterrr.TuyaNet
/// <param name="data">Raw data to parse and decrypt.</param>
/// <returns>Instance of TuyaLocalResponse.</returns>
public TuyaLocalResponse DecodeResponse(byte[] data)
- => TuyaParser.DecodeResponse(data, Encoding.UTF8.GetBytes(LocalKey), ProtocolVersion);
+ {
+ if (string.IsNullOrEmpty(accessId)) throw new ArgumentException("LocalKey is not specified", "LocalKey");
+ return TuyaParser.DecodeResponse(data, Encoding.UTF8.GetBytes(LocalKey), ProtocolVersion);
+ }
/// <summary>
/// Sends JSON string to device and reads response.
@@ -303,6 +327,18 @@ namespace com.clusterrr.TuyaNet
}
/// <summary>
+ /// Get current local key from Tuya Cloud API
+ /// </summary>
+ public async Task RefreshLocalKeyAsync()
+ {
+ if (string.IsNullOrEmpty(accessId)) throw new ArgumentException("Access ID is not specified", "accessId");
+ if (string.IsNullOrEmpty(apiSecret)) throw new ArgumentException("API secret is not specified", "apiSecret");
+ var api = new TuyaApi(region, accessId, apiSecret);
+ var deviceInfo = await api.GetDeviceInfoAsync(DeviceId);
+ LocalKey = deviceInfo.LocalKey;
+ }
+
+ /// <summary>
/// Disposes object.
/// </summary>
public void Dispose()