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

TuyaDevice.cs - github.com/ClusterM/tuyanet.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 731f0a9fd2b3f5885b0b05c3ce06828e3a13dc23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using com.clusterrr.SemaphoreLock;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace com.clusterrr.TuyaNet
{
    /// <summary>
    /// Connection with Tuya device.
    /// </summary>
    public class TuyaDevice : IDisposable
    {
        /// <summary>
        /// Creates a new instance of the TuyaDevice class.
        /// </summary>
        /// <param name="ip">IP address of device.</param>
        /// <param name="localKey">Local key of device (obtained via API).</param>
        /// <param name="deviceId">Device ID.</param>
        /// <param name="protocolVersion">Protocol version.</param>
        /// <param name="port">TCP port of device.</param>
        /// <param name="receiveTimeout">Receive timeout (msec).</param>
        public TuyaDevice(string ip, string localKey, string deviceId, TuyaProtocolVersion protocolVersion = TuyaProtocolVersion.V33, int port = 6668, int receiveTimeout = 250)
        {
            IP = ip;
            LocalKey = localKey;
            this.accessId = null;
            this.apiSecret = null;
            DeviceId = deviceId;
            ProtocolVersion = protocolVersion;
            Port = port;
            ReceiveTimeout = receiveTimeout;
        }

        /// <summary>
        /// Creates a new instance of the TuyaDevice class.
        /// </summary>
        /// <param name="ip">IP address of device.</param>
        /// <param name="region">Region to access Cloud API.</param>
        /// <param name="accessId">Access ID to access Cloud API.</param>
        /// <param name="apiSecret">API secret to access Cloud API.</param>
        /// <param name="deviceId">Device ID.</param>
        /// <param name="protocolVersion">Protocol version.</param>
        /// <param name="port">TCP port of device.</param>
        /// <param name="receiveTimeout">Receive timeout (msec).</param> 
        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;
            ReceiveTimeout = receiveTimeout;
        }

        /// <summary>
        /// IP address of device.
        /// </summary>
        public string IP { get; private set; }
        /// <summary>
        /// Local key of device.
        /// </summary>
        public string LocalKey { get; set; }
        /// <summary>
        /// Device ID.
        /// </summary>
        public string DeviceId { get; private set; }
        /// <summary>
        /// TCP port of device.
        /// </summary>
        public int Port { get; private set; } = 6668;
        /// <summary>
        /// Protocol version.
        /// </summary>
        public TuyaProtocolVersion ProtocolVersion { get; set; }
        /// <summary>
        /// Connection timeout.
        /// </summary>
        public int ConnectionTimeout { get; set; } = 500;
        /// <summary>
        /// Receive timeout.
        /// </summary>
        public int ReceiveTimeout { get; set; }
        /// <summary>
        /// Network error retry interval (msec)
        /// </summary>
        public int NetworkErrorRetriesInterval { get; set; } = 100;
        /// <summary>
        /// Empty responce retry interval (msec)
        /// </summary>
        public int NullRetriesInterval { get; set; } = 0;
        /// <summary>
        /// Permanent connection (connect and stay connected).
        /// </summary>
        public bool PermanentConnection { get; set; } = false;

        private TcpClient client = null;
        private TuyaApi.Region region;
        private string accessId;
        private string apiSecret;
        private SemaphoreSlim sem = new SemaphoreSlim(1);

        /// <summary>
        /// Fills JSON string with base fields required by most commands.
        /// </summary>
        /// <param name="json">JSON string</param>
        /// <param name="addGwId">Add "gwId" field with device ID.</param>
        /// <param name="addDevId">Add "devId" field with device ID.</param>
        /// <param name="addUid">Add "uid" field with device ID.</param>
        /// <param name="addTime">Add "time" field with current timestamp.</param>
        /// <returns>JSON string with added fields.</returns>
        public string FillJson(string json, bool addGwId = true, bool addDevId = true, bool addUid = true, bool addTime = true)
        {
            if (string.IsNullOrEmpty(json))
                json = "{}";
            var root = JObject.Parse(json);
            if ((addGwId || addDevId || addUid) && string.IsNullOrWhiteSpace(DeviceId))
                throw new ArgumentNullException("deviceId", "Device ID can't be null.");
            if (addTime && !root.ContainsKey("t"))
                root.AddFirst(new JProperty("t", (DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds.ToString("0")));
            if (addUid && !root.ContainsKey("uid"))
                root.AddFirst(new JProperty("uid", DeviceId));
            if (addDevId && !root.ContainsKey("devId"))
                root.AddFirst(new JProperty("devId", DeviceId));
            if (addGwId && !root.ContainsKey("gwId"))
                root.AddFirst(new JProperty("gwId", DeviceId));
            return root.ToString();
        }

        /// <summary>
        /// Creates encoded and encrypted payload data from JSON string.
        /// </summary>
        /// <param name="command">Tuya command ID.</param>
        /// <param name="json">String with JSON to send.</param>
        /// <returns>Raw data.</returns>
        public byte[] EncodeRequest(TuyaCommand command, string json)
        {
            if (string.IsNullOrEmpty(LocalKey)) 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.
        /// </summary>
        /// <param name="data">Raw data to parse and decrypt.</param>
        /// <returns>Instance of TuyaLocalResponse.</returns>
        public TuyaLocalResponse DecodeResponse(byte[] data)
        {
            if (string.IsNullOrEmpty(LocalKey)) 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.
        /// </summary>
        /// <param name="command">Tuya command ID.</param>
        /// <param name="json">JSON string.</param>
        /// <param name="retries">Number of retries in case of network error (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Parsed and decrypred received data as instance of TuyaLocalResponse.</returns>
        public async Task<TuyaLocalResponse> SendAsync(TuyaCommand command, string json, int retries = 2, int nullRetries = 1, int? overrideRecvTimeout = null, CancellationToken cancellationToken = default)
            => DecodeResponse(await SendAsync(EncodeRequest(command, json), retries, nullRetries, overrideRecvTimeout, cancellationToken));

        /// <summary>
        /// Sends raw data over to device and read response.
        /// </summary>
        /// <param name="data">Raw data to send.</param>
        /// <param name="retries">Number of retries in case of network error (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Received data (raw).</returns>
        public async Task<byte[]> SendAsync(byte[] data, int retries = 2, int nullRetries = 1, int? overrideRecvTimeout = null, CancellationToken cancellationToken = default)
        {
            Exception lastException = null;
            while (retries-- > 0)
            {
                if (!PermanentConnection || (client?.Connected == false))
                {
                    client?.Close();
                    client?.Dispose();
                    client = null;
                }
                try
                {
                    using (await sem.WaitDisposableAsync(cancellationToken))
                    {
                        if (client == null)
                            client = new TcpClient();
                        if (!client.ConnectAsync(IP, Port).Wait(ConnectionTimeout))
                            throw new IOException("Connection timeout");
                        var stream = client.GetStream();
                        await stream.WriteAsync(data, 0, data.Length, cancellationToken).ConfigureAwait(false);
                        return await ReceiveAsync(stream, nullRetries, overrideRecvTimeout, cancellationToken);
                    }
                }
                catch (Exception ex) when (ex is IOException or TimeoutException or SocketException)
                {
                    // sockets sometimes drop the connection unexpectedly, so let's 
                    // retry at least once
                    lastException = ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (!PermanentConnection || (client?.Connected == false) || (lastException != null))
                    {
                        client?.Close();
                        client?.Dispose();
                        client = null;
                    }
                }
                await Task.Delay(NetworkErrorRetriesInterval, cancellationToken);
            }
            throw lastException;
        }

        private async Task<byte[]> ReceiveAsync(NetworkStream stream, int nullRetries = 1, int? overrideRecvTimeout = null, CancellationToken cancellationToken = default)
        {
            byte[] result;
            byte[] buffer = new byte[1024];
            using (var ms = new MemoryStream())
            {
                int length = buffer.Length;
                while ((ms.Length < 16) || ((length = BitConverter.ToInt32(TuyaParser.BigEndian(ms.ToArray().Skip(12).Take(4)).ToArray(), 0) + 16) < ms.Length))
                {
                    var timeoutCancellationTokenSource = new CancellationTokenSource();
                    var readTask = stream.ReadAsync(buffer, 0, length, cancellationToken: cancellationToken);
                    var timeoutTask = Task.Delay(overrideRecvTimeout ?? ReceiveTimeout, cancellationToken: timeoutCancellationTokenSource.Token);
                    var t = await Task.WhenAny(readTask, timeoutTask).ConfigureAwait(false);
                    timeoutCancellationTokenSource.Cancel();
                    int bytes = 0;
                    if (t == timeoutTask)
                    {
                        if (stream.DataAvailable)
                            bytes = await stream.ReadAsync(buffer, 0, length, cancellationToken);
                        else
                            throw new TimeoutException();
                    }
                    else if (t == readTask)
                    {
                        bytes = await readTask;
                    }
                    ms.Write(buffer, 0, bytes);
                }
                result = ms.ToArray();
            }
            if ((result.Length <= 28) && (nullRetries > 0)) // empty response
            {
                await Task.Delay(NullRetriesInterval, cancellationToken);
                result = await ReceiveAsync(stream, nullRetries - 1, overrideRecvTimeout: overrideRecvTimeout, cancellationToken);
            }
            return result;
        }

        /// <summary>
        /// Requests current DPs status.
        /// </summary>
        /// <param name="retries">Number of retries in case of network error (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Dictionary of DP numbers and values.</returns>
        public async Task<Dictionary<int, object>> GetDpsAsync(int retries = 5, int nullRetries = 1, int? overrideRecvTimeout = null, CancellationToken cancellationToken = default)
        {
            var requestJson = FillJson(null);
            var response = await SendAsync(TuyaCommand.DP_QUERY, requestJson, retries, nullRetries, overrideRecvTimeout, cancellationToken);
            if (string.IsNullOrEmpty(response.JSON))
                throw new InvalidDataException("Response is empty");
            var root = JObject.Parse(response.JSON);
            var dps = JsonConvert.DeserializeObject<Dictionary<string, object>>(root.GetValue("dps").ToString());
            return dps.ToDictionary(kv => int.Parse(kv.Key), kv => kv.Value);
        }

        /// <summary>
        /// Sets single DP to specified value.
        /// </summary>
        /// <param name="dp">DP number.</param>
        /// <param name="value">Value.</param>
        /// <param name="retries">Number of retries in case of network error (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="allowEmptyResponse">Do not throw exception on empty Response</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Dictionary of DP numbers and values.</returns>
        public async Task<Dictionary<int, object>> SetDpAsync(int dp, object value, int retries = 2, int nullRetries = 1, int? overrideRecvTimeout = null, bool allowEmptyResponse = false, CancellationToken cancellationToken = default)
            => await SetDpsAsync(new Dictionary<int, object> { { dp, value } }, retries, nullRetries, overrideRecvTimeout, allowEmptyResponse, cancellationToken);

        /// <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 (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="allowEmptyResponse">Do not throw exception on empty Response</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Dictionary of DP numbers and values.</returns>
        public async Task<Dictionary<int, object>> SetDpsAsync(Dictionary<int, object> dps, int retries = 2, int nullRetries = 1, int? overrideRecvTimeout = null, bool allowEmptyResponse = false, CancellationToken cancellationToken = default)
        {
            var cmd = new Dictionary<string, object>
            {
                { "dps",  dps }
            };
            string requestJson = JsonConvert.SerializeObject(cmd);
            requestJson = FillJson(requestJson);
            var response = await SendAsync(TuyaCommand.CONTROL, requestJson, retries, nullRetries, overrideRecvTimeout, cancellationToken);
            if (string.IsNullOrEmpty(response.JSON))
            {
                if (!allowEmptyResponse)
                    throw new InvalidDataException("Response is empty");
                else
                    return null;
            }
            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>
        /// <param name="retries">Number of retries in case of network error (default - 2).</param>
        /// <param name="nullRetries">Number of retries in case of empty answer (default - 1).</param>
        /// <param name="overrideRecvTimeout">Override receive timeout (default - ReceiveTimeout property).</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Dictionary of DP numbers and values.</returns>
        public async Task<Dictionary<int, object>> UpdateDpsAsync(IEnumerable<int> dpIds, int retries = 5, int nullRetries = 1, int? overrideRecvTimeout = null, CancellationToken cancellationToken = default)
        {
            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, overrideRecvTimeout, cancellationToken);
            if (string.IsNullOrEmpty(response.JSON))
                return new Dictionary<int, object>();
            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>
        /// Get current local key from Tuya Cloud API
        /// </summary>
        /// <param name="forceTokenRefresh">Refresh access token even it's not expired.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        public async Task RefreshLocalKeyAsync(bool forceTokenRefresh = false, CancellationToken cancellationToken = default)
        {
            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, forceTokenRefresh: forceTokenRefresh, cancellationToken);
            LocalKey = deviceInfo.LocalKey;
        }

        /// <summary>
        /// Disposes object.
        /// </summary>
        public void Dispose()
        {
            client?.Close();
            client?.Dispose();
            client = null;
        }
    }
}