-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLink.cs
864 lines (716 loc) · 32 KB
/
Link.cs
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
namespace EWeLink.Api
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EWeLink.Api.Models;
using EWeLink.Api.Models.Devices;
using EWeLink.Api.Models.EventParameters;
using EWeLink.Api.Models.LightThemes;
using EWeLink.Api.Models.Parameters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using ColorLightParameters = EWeLink.Api.Models.Parameters.ColorLightParameters;
public class Link : ILink, ILinkAuthorization
{
private const int MaxCallCount = 300;
private const string DefaultAppId = "4s1FXKC9FaGfoqXhmXSJneb3qcm1gOak";
private const string DefaultAppSecret = "oKvCM06gvwkRbfetd6qWRrbC3rFrbIpV";
private static readonly ConcurrentDictionary<string, string> TokenToApiCache = new ConcurrentDictionary<string, string>();
private static readonly TimeSpan MaxCallCountOverTimePeriod = TimeSpan.FromMinutes(5);
private static readonly TimeSpan DelayBetweenRequests = TimeSpan.FromMilliseconds(500);
private readonly ILinkConfiguration configuration;
private readonly IDeviceCache deviceCache;
private readonly ILinkWebSocket linkWebSocket;
private readonly ILinkLanControl lanControl;
private readonly IHttpClientFactory httpClientFactory;
private readonly Semaphore requestLock;
private readonly Semaphore tokenLock;
private DateTimeOffset? lastRequestTime;
private int callCountForPeriod = 0;
private DateTimeOffset? callCountPeriodStart;
private string? region = "us";
private string? email;
private string? phoneNumber;
private string? password;
private string? at;
private string? apiKey;
public Link(ILinkConfiguration configuration, IDeviceCache deviceCache, ILinkWebSocket linkWebSocket, ILinkLanControl lanControl, IHttpClientFactory httpClientFactory)
{
this.configuration = configuration;
this.deviceCache = deviceCache;
this.linkWebSocket = linkWebSocket;
this.lanControl = lanControl;
this.httpClientFactory = httpClientFactory;
this.lanControl.ParametersUpdated += e => LanParametersUpdated?.Invoke(e);
this.lanControl.DiscoveredDevice += d => DiscoveredLanControlDevice?.Invoke(d);
this.requestLock = new Semaphore(1, 1, configuration.AppId);
this.tokenLock = new Semaphore(1, 1, $"{configuration.AppId}_token");
this.region = configuration.Region;
this.phoneNumber = configuration.PhoneNumber;
this.email = configuration.Email;
this.password = configuration.Password;
this.at = configuration.At;
this.apiKey = configuration.ApiKey;
}
public event Action<ILinkEvent<IEventParameters>>? LanParametersUpdated;
public event Action<IDevice>? DiscoveredLanControlDevice;
public event Action<OAuhToken>? OAuthTokenUpdated;
public Uri ApiUri => new Uri($"https://{this.region}-apia.coolkit.cc");
public Uri OtaUri => new Uri($"https://{this.region}-ota.coolkit.cc:8080/otaother");
public Uri ApiWebSocketUri => new Uri($"wss://{this.region}-pconnect3.coolkit.cc:8080/api/ws");
private string AppId => this.configuration.AppId ?? DefaultAppId;
private string AppSecret => this.configuration.AppSecret ?? DefaultAppSecret;
private OAuhToken? AuhToken => this.configuration.AuhToken;
public async Task<SensorData?> GetDeviceCurrentSensorData(string deviceId)
{
var device = await this.GetDevice(deviceId, true);
if (device is IThermostatDevice thermostatDevice)
{
var parameters = thermostatDevice.Parameters;
if (!parameters.Temperature.HasValue && !parameters.Humidity.HasValue)
{
return null;
}
if (parameters.Temperature.HasValue)
{
return new SensorData(deviceId, SensorType.Temperature, parameters.Temperature.Value);
}
if (parameters.Humidity.HasValue)
{
return new SensorData(deviceId, SensorType.Humidity, parameters.Humidity.Value);
}
}
return null;
}
public async Task<(string? Email, string? Region)> GetRegion()
{
if (string.IsNullOrWhiteSpace(this.email))
{
throw new ArgumentNullException(nameof(this.email));
}
if (string.IsNullOrWhiteSpace(this.password))
{
throw new ArgumentNullException(nameof(this.password));
}
var credentials = await this.GetCredentials();
return (credentials.User?.Email, credentials.Region);
}
public Task<IDevice?> GetDevice(string deviceId) => this.GetDevice(deviceId, false);
public int GetDeviceChannelCountByUuid(int uuid)
{
var deviceType = this.GetDeviceTypeByUiid(uuid);
return DeviceData.DeviceChannelCount[deviceType];
}
public Task ToggleDevice(string deviceId, ChannelId channel = ChannelId.One)
{
return this.SetDevicePowerState(deviceId, SwitchStateChange.Toggle, channel);
}
public async Task<int> GetDeviceChannelCount(string deviceId)
{
var device = await this.GetDevice(deviceId);
if (device?.Extra is null)
{
throw new ArgumentNullException(nameof(device.Extra));
}
var uiid = device.Extra.Uiid;
var switchesAmount = this.GetDeviceChannelCountByUuid(uiid);
return switchesAmount;
}
public async Task SetDevicePowerState(string deviceId, SwitchStateChange state, ChannelId channel = ChannelId.One)
{
var device = await this.GetDevice(deviceId);
if (device == null)
{
throw new KeyNotFoundException("The device id was not found");
}
if (device.Extra is null)
{
throw new ArgumentNullException(nameof(device.Extra));
}
var uiid = device.Extra.Uiid;
var switchesAmount = this.GetDeviceChannelCountByUuid(uiid);
if (switchesAmount > 0 && switchesAmount < (int)channel)
{
throw new Exception(NiceError.Custom[CustomErrors.Ch404]);
}
SwitchState status;
switch (device)
{
case ISingleSwitchDevice singleSwitch:
status = singleSwitch.Parameters.Switch;
break;
case IOneSwitchDevice oneSwitch:
status = oneSwitch.Parameters.One.Switch;
break;
case ITwoSwitchDevice twoSwitch:
{
status = channel switch
{
ChannelId.One => twoSwitch.Parameters.One.Switch,
ChannelId.Two => twoSwitch.Parameters.Two.Switch,
_ => throw new ArgumentOutOfRangeException(nameof(channel))
};
}
break;
case IThreeSwitchDevice threeSwitchDevice:
{
status = channel switch
{
ChannelId.One => threeSwitchDevice.Parameters.One.Switch,
ChannelId.Two => threeSwitchDevice.Parameters.Two.Switch,
ChannelId.Three => threeSwitchDevice.Parameters.Three.Switch,
_ => throw new ArgumentOutOfRangeException(nameof(channel))
};
}
break;
case IFourSwitchDevice fourSwitchDevice:
{
status = channel switch
{
ChannelId.One => fourSwitchDevice.Parameters.One.Switch,
ChannelId.Two => fourSwitchDevice.Parameters.Two.Switch,
ChannelId.Three => fourSwitchDevice.Parameters.Three.Switch,
ChannelId.Four => fourSwitchDevice.Parameters.Four.Switch,
_ => throw new ArgumentOutOfRangeException(nameof(channel))
};
}
break;
default:
throw new NotSupportedException("Device is not a switch");
}
var deviceParameters = ((IDevice<Parameters>)device).Parameters;
var parameters = deviceParameters.CreateParameters();
var stateToSwitch = state switch
{
SwitchStateChange.Toggle => status == SwitchState.On ? SwitchState.Off : SwitchState.On,
SwitchStateChange.On => SwitchState.On,
SwitchStateChange.Off => SwitchState.Off,
_ => throw new ArgumentOutOfRangeException(nameof(state))
};
if (parameters is MultiSwitchParameters multiSwitchParameters)
{
var linkSwitch = multiSwitchParameters.Switches.First(x => x.Outlet == (int)channel);
linkSwitch.Switch = stateToSwitch;
// Reduce the switches to only include the channel we want changed
multiSwitchParameters.Switches = new[] { linkSwitch };
}
else if (parameters is SwitchParameters switchParameters)
{
switchParameters.Switch = stateToSwitch;
}
else
{
throw new ArgumentException(nameof(parameters));
}
if (device.HasLanControl)
{
var handled = await this.lanControl.SendSwitchRequest(device, parameters);
if (handled.HasValue)
{
return;
}
}
await this.MakeRequest(
"v2/device/thing/status",
body: new
{
type = 1,
id = deviceId,
@params = parameters,
},
method: HttpMethod.Post);
deviceParameters.Update(parameters);
}
public async Task SetLightColor(string deviceId, LightBrightness value)
{
var device = await this.GetDevice(deviceId);
if (device == null)
{
throw new KeyNotFoundException("The device id was not found");
}
if (!(device is Device<ColorLightParameters> colorLight))
{
throw new NotSupportedException("The given device id is not a color light");
}
var deviceParameters = (ColorLightParameters)colorLight.Parameters.CreateParameters();
if (value is Color colorValue)
{
deviceParameters.LightType = LightType.Color;
deviceParameters.Color = colorValue;
}
else if (value is White whiteValue)
{
deviceParameters.LightType = LightType.White;
deviceParameters.White = whiteValue;
}
await this.MakeRequest(
"v2/device/thing/status",
body: new
{
type = 1,
id = deviceId,
@params = deviceParameters,
},
method: HttpMethod.Post);
colorLight.Parameters.Update(deviceParameters);
}
public async Task<ILinkWebSocket> OpenWebSocket(CancellationToken cancellationToken = default)
{
var wssLoginPayload = JsonConvert.SerializeObject(new
{
action = "userOnline",
this.at,
apikey = this.apiKey,
appid = AppId,
nonce = Utilities.Nonce,
ts = Utilities.Timestamp,
userAgent = "ewelink-api",
sequence = 1,
version = 8,
});
await this.linkWebSocket.Open(wssLoginPayload, ApiWebSocketUri, cancellationToken);
return this.linkWebSocket;
}
public void EnableLanControl()
{
this.lanControl.Start();
}
public async Task<List<IDevice>> GetDevices()
{
dynamic response = await this.MakeRequest(
"v2/device/thing",
query: new
{
lang = "en",
num = 0,
});
JToken thingList = response.thingList;
var deviceList = thingList.ToObject<List<Thing>>()?.Select(x => x.ItemData).ToList();
if (deviceList != null)
{
this.deviceCache.UpdateCache(deviceList);
}
return deviceList ?? new List<IDevice>();
}
public async Task<List<UpdateCheckResult>> CheckAllDeviceUpdates()
{
var devices = await this.GetDevices();
var result = await this.CheckDeviceUpdates(devices);
var genericDevices = devices.Cast<Device<Parameters>>()
.Where(x => x.Parameters is LinkParameters)
.Cast<Device<LinkParameters>>()
.ToDictionary(x => x.DeviceId);
return result.Select(r => new UpdateCheckResult(r.Version != genericDevices[r.DeviceId].Parameters.FirmWareVersion, r))
.ToList();
}
public async Task<List<UpgradeInfo>> CheckDeviceUpdates(IEnumerable<IDevice> devices)
{
var genericDevices = devices.Cast<Device<Parameters>>()
.Where(x => x.Parameters is LinkParameters)
.Cast<Device<LinkParameters>>();
var deviceInfoList = GetFirmwareUpdateInfo(genericDevices);
dynamic response = await this.MakeRequest(
"v2/device/ota/query",
body: new { deviceInfoList = deviceInfoList },
method: HttpMethod.Post);
JToken token = response.otaInfoList;
return token.ToObject<List<UpgradeInfo>>() ?? new List<UpgradeInfo>();
}
public async Task<UpdateCheckResult> CheckDeviceUpdate(string deviceId)
{
var device = await this.GetDevice(deviceId);
var genericDevice = device as Device<LinkParameters>;
var result = await this.CheckDeviceUpdates(new[] { device });
var info = result.FirstOrDefault();
return new UpdateCheckResult(info!.Version != genericDevice!.Parameters.FirmWareVersion, info);
}
public async Task<string?> GetFirmwareVersion(string deviceId)
{
var device = await this.GetDevice(deviceId);
var genericDevice = device as Device<LinkParameters>;
return genericDevice!.Parameters.FirmWareVersion;
}
public async Task<Credentials> GetCredentials()
{
var body = new
{
password,
countryCode = "+86",
this.email,
};
var data = JsonConvert.SerializeObject(body);
var uri = new Uri($"{this.ApiUri}v2/user/login");
var httpMessage = new HttpRequestMessage(HttpMethod.Post, uri);
httpMessage.Headers.Add("Authorization", $"Sign {this.MakeAuthorizationSign(data)}");
httpMessage.Headers.Add("X-CK-Appid", AppId);
httpMessage.Content = new StringContent(data, Encoding.UTF8, "application/json");
var httpClient = httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpMessage);
response.EnsureSuccessStatusCode();
var jsonString = await response.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject<dynamic>(jsonString) !;
JToken token = GetResponseData(json);
var credentials = token.ToObject<Credentials>() ?? new Credentials();
this.apiKey = credentials.User?.Apikey;
this.at = credentials.At;
return credentials;
}
public (string Signature, long Seq, string AppId) GetAuthorization()
{
var seq = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var authorization = MakeAuthorizationSign($"{AppId}_{seq}");
return (authorization, seq, AppId);
}
public async Task<OAuhToken> GetAccessToken(string code, string redirectUri)
{
var body = new
{
code,
redirectUrl = redirectUri,
grantType = "authorization_code",
};
var uri = new Uri($"{this.ApiUri}v2/user/oauth/token");
var httpMessage = new HttpRequestMessage(HttpMethod.Post, uri);
httpMessage.Headers.Add("Authorization", $"Sign {this.MakeAuthorizationSign(JsonConvert.SerializeObject(body))}");
httpMessage.Headers.Add("X-CK-Appid", AppId);
var data = JsonConvert.SerializeObject(body, new StringEnumConverter());
httpMessage.Content = new StringContent(data, Encoding.UTF8, "application/json");
var httpClient = httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpMessage);
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(responseData) !;
JToken tokenData = GetResponseData(json);
return tokenData.ToObject<OAuhToken>() !;
}
public async Task ReloadAccessToken()
{
if (this.AuhToken is null)
{
return;
}
await ValidateAuthToken();
}
public async Task<IDevice?> GetDevice(string deviceId, bool noCacheLoad)
{
if (!noCacheLoad && this.deviceCache.TryGetDevice(deviceId, out IDevice? device))
{
return device!;
}
dynamic response = await this.MakeRequest(
$"v2/device/thing",
body: new
{
thingList = new[]
{
new
{
itemType = 2,
id = deviceId,
},
},
}, method: HttpMethod.Post);
JToken token = response.thingList;
if (token == null)
{
throw new KeyNotFoundException("The device id was not found");
}
device = token.ToObject<List<Thing>>()?.Select(x => x.ItemData).FirstOrDefault();
if (device != null)
{
return this.deviceCache.UpdateCache(device);
}
return device;
}
private async Task ValidateAuthToken()
{
tokenLock.WaitOne();
try
{
var auhToken = this.AuhToken;
if (auhToken is null)
{
throw new ArgumentNullException(nameof(auhToken), "ValidateAuthToken called without a access token");
}
var accessExpiry = DateTimeOffset.FromUnixTimeMilliseconds(auhToken.AccessTokenExpiredTime);
if (accessExpiry - TimeSpan.FromDays(2) > DateTimeOffset.Now)
{
if (this.at != auhToken.AccessToken)
{
this.at = auhToken.AccessToken;
this.apiKey = null;
if (TokenToApiCache.TryGetValue(this.at, out var key))
{
this.apiKey = key;
}
}
}
else
{
var refreshExpiry = DateTimeOffset.FromUnixTimeMilliseconds(auhToken.RefreshTokenExpiredTime);
if (refreshExpiry < DateTimeOffset.Now)
{
throw new ArgumentOutOfRangeException(nameof(auhToken.RefreshToken), "Refresh Token expired");
}
TokenToApiCache.TryRemove(auhToken.AccessToken, out _);
var body = new { rt = auhToken.RefreshToken, };
var uri = new Uri($"{this.ApiUri}v2/user/refresh");
var httpMessage = new HttpRequestMessage(HttpMethod.Post, uri);
httpMessage.Headers.Add("Authorization", $"Sign {this.MakeAuthorizationSign(JsonConvert.SerializeObject(body))}");
httpMessage.Headers.Add("X-CK-Appid", AppId);
var data = JsonConvert.SerializeObject(body, new StringEnumConverter());
httpMessage.Content = new StringContent(data, Encoding.UTF8, "application/json");
var httpClient = httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpMessage);
var refreshTime = DateTimeOffset.Now;
response.EnsureSuccessStatusCode();
var rawJson = await response.Content.ReadAsStringAsync();
var json = JsonConvert.DeserializeObject<dynamic>(rawJson) !;
var refreshTokenData = GetResponseData(json);
string accessToken = refreshTokenData.at;
long accessTokenExpiredTime = refreshTime.AddDays(30).ToUnixTimeMilliseconds();
string refreshToken = refreshTokenData.rt;
long refreshTokenExpiredTime = refreshTime.AddDays(60).ToUnixTimeMilliseconds();
var authToken = new OAuhToken
{
AccessToken = accessToken, AccessTokenExpiredTime = accessTokenExpiredTime,
RefreshToken = refreshToken,
RefreshTokenExpiredTime = refreshTokenExpiredTime,
};
auhToken = authToken;
this.at = auhToken.AccessToken;
this.apiKey = null;
this.OAuthTokenUpdated?.Invoke(authToken);
}
if (this.apiKey is null && this.at != null)
{
await LoadApiKey();
}
}
finally
{
this.tokenLock.Release(1);
}
}
private async Task LoadApiKey()
{
// Get api key from the family
var uriBuilder = new UriBuilder($"{this.ApiUri}v2/family")
{
Query = this.ToQueryString(new { lang = "en" }),
};
var uri = uriBuilder.Uri;
var httpMessage = new HttpRequestMessage(HttpMethod.Get, uri);
httpMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.at);
httpMessage.Headers.Add("X-CK-Appid", AppId);
var homeList = await SendHttpMessage(httpMessage);
string currentId = homeList.currentFamilyId;
int count = homeList.familyList.Count;
for (var i = 0; i < count && this.apiKey is null; i++)
{
dynamic home = homeList.familyList[i];
string id = home.id;
if (id == currentId)
{
this.apiKey = home.apikey;
if (this.at != null)
{
TokenToApiCache.AddOrUpdate(this.at, this.apiKey, (_, _) => this.apiKey);
}
}
}
}
private async Task<dynamic> MakeRequest(string path, Uri? baseUri = null, object? body = null, object? query = null, HttpMethod? method = null)
{
if (method == null)
{
method = HttpMethod.Get;
}
if (this.AuhToken != null)
{
await ValidateAuthToken();
}
if (string.IsNullOrWhiteSpace(this.at) && this.AuhToken is null)
{
await this.GetCredentials();
}
if (baseUri == null)
{
baseUri = this.ApiUri;
}
var uriBuilder = new UriBuilder($"{baseUri}{path}")
{
Query = query != null ? this.ToQueryString(query) : string.Empty,
};
var uri = uriBuilder.Uri;
var httpMessage = new HttpRequestMessage(method, uri);
httpMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.at);
httpMessage.Headers.Add("X-CK-Appid", AppId);
if (body != null)
{
var data = JsonConvert.SerializeObject(body, new StringEnumConverter());
httpMessage.Content = new StringContent(data, Encoding.UTF8, "application/json");
}
return await SendHttpMessage(httpMessage);
}
private async Task<dynamic> SendHttpMessage(HttpRequestMessage httpMessage)
{
this.requestLock.WaitOne();
try
{
if (this.lastRequestTime.HasValue)
{
var timeBetweenRequests = DateTimeOffset.UtcNow - this.lastRequestTime.Value;
var timeLeftBetweenRequests = DelayBetweenRequests - timeBetweenRequests;
if (timeLeftBetweenRequests > TimeSpan.Zero)
{
await Task.Delay(timeLeftBetweenRequests);
}
}
if (this.callCountPeriodStart.HasValue)
{
var timeSinceReset = DateTimeOffset.UtcNow - this.callCountPeriodStart.Value;
var timeLeftToReset = MaxCallCountOverTimePeriod - timeSinceReset;
if (timeLeftToReset <= TimeSpan.Zero)
{
this.callCountForPeriod = 0;
}
if (this.callCountForPeriod >= MaxCallCount)
{
await Task.Delay(timeLeftToReset);
this.callCountForPeriod = 0;
this.callCountPeriodStart = null;
}
}
var httpClient = this.httpClientFactory.CreateClient();
this.lastRequestTime = DateTimeOffset.UtcNow;
this.callCountForPeriod++;
if (!this.callCountPeriodStart.HasValue)
{
this.callCountPeriodStart = this.lastRequestTime;
}
var response = await httpClient.SendAsync(httpMessage);
response.EnsureSuccessStatusCode();
var jsonString = await response.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(jsonString) !;
return GetResponseData(json);
}
finally
{
this.requestLock.Release(1);
}
}
private bool CheckLoginParameters(
string? email,
string? phoneNumber,
string? password,
string? at)
{
if (email != null && phoneNumber != null)
{
return false;
}
if ((email != null && password != null) || (phoneNumber != null && password != null) || at != null)
{
return true;
}
return false;
}
private string ToQueryString(object? qs)
{
if (qs is null)
{
return string.Empty;
}
var properties = from p in qs.GetType().GetProperties()
where p.GetValue(qs, null) != null
select p.Name + "=" + System.Web.HttpUtility.UrlEncode(p.GetValue(qs, null).ToString());
return string.Join("&", properties.ToArray());
}
private string MakeAuthorizationSign(string data)
{
var crypto = HMAC.Create("HmacSHA256");
crypto.Key = Encoding.UTF8.GetBytes(AppSecret);
var hash = crypto.ComputeHash(Encoding.UTF8.GetBytes(data));
return Convert.ToBase64String(hash);
}
private string GetDeviceTypeByUiid(int uiid)
{
if (DeviceData.DeviceTypeUuid.TryGetValue(uiid, out var type))
{
return type;
}
return string.Empty;
}
private dynamic GetResponseData(dynamic json)
{
int error = json.error;
if (error != 0)
{
if (error == 412)
{
this.callCountForPeriod = MaxCallCount;
}
string message = json.msg;
throw new HttpRequestException($"{error}: {message}");
}
return json.data;
}
private List<FirmwareUpdateInfo> GetFirmwareUpdateInfo(IEnumerable<Device<LinkParameters>> devices)
{
return devices.Select(
d => new FirmwareUpdateInfo
{
Model = d.Extra?.Model,
Version = d.Parameters.Version,
DeviceId = d.DeviceId,
})
.ToList();
}
private class PayLoad
{
public PayLoad(string appId, string? email, string? phoneNumber, string? password, long timestamp, int version, string nonce)
{
this.AppId = appId;
this.Email = email;
this.PhoneNumber = phoneNumber;
this.Password = password;
this.Timestamp = timestamp;
this.Version = version;
this.Nonce = nonce;
}
[JsonProperty("appid")]
public string? AppId { get; }
[JsonProperty("email")]
public string? Email { get; }
[JsonProperty("phoneNumber")]
public string? PhoneNumber { get; }
[JsonProperty("password")]
public string? Password { get; }
[JsonProperty("ts")]
public long Timestamp { get; }
[JsonProperty("version")]
public int Version { get; }
[JsonProperty("nonce")]
public string Nonce { get; }
}
private class FirmwareUpdateInfo
{
[JsonProperty("model")]
public string? Model { get; set; }
[JsonProperty("version")]
public string? Version { get; set; }
[JsonProperty("deviceid")]
public string? DeviceId { get; set; }
}
}
}