-
Notifications
You must be signed in to change notification settings - Fork 7
/
BridgeTest.cs
569 lines (500 loc) · 21.5 KB
/
BridgeTest.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
/*
* Copyright (c) 2022-2022 Huawei Cloud Computing Technology Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using IoT.SDK.Bridge.Clent;
using IoT.SDK.Bridge.Listener;
using IoT.SDK.Bridge.Request;
using IoT.SDK.Device.Client;
using IoT.SDK.Device.Client.Requests;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
namespace IoT.Device.Demo.HubDevice.Bridge
{
class BridgeTest : LoginListener
, BridgeCommandListener
, BridgeDeviceMessageListener
, LogoutListener
, ResetDeviceSecretListener
, BridgeDeviceDisConnListener
, BridgePropertyListener
, BridgeEventListener
, BridgeShadowListener, BridgeRawDeviceMessageListener
{
private static readonly Logger LOG = LogManager.GetCurrentClassLogger();
private BridgeClient client;
private string rootDeviceId;
private string rootDeviceSecret;
private string subDeviceId;
private string newDeviceSecret;
private readonly RequestIdCache<object> requestIdCache = new RequestIdCache<object>();
private const string PSEUDO_REQUEST_ID = "aiminabb";
private const string PSEUDO_EVENT_REQUEST_ID = "event";
private const string PSEUDO_MESSAGE_DOWN_REQUEST_ID = "message";
private const string PSEUDO_COMMAND_DOWN_REQUEST_ID = "commad";
private const string PSEUDO_PROPERTY_SET_REQUEST_ID = "property_set";
private const string PSEUDO_PROPERTY_GET_REQUEST_ID = "property_get";
private const string TEST_SERVICE_ID = "smokeDetector";
private class DownLinkRequest
{
public string RequestId { get; set; }
public object Payload { get; set; }
}
private void FinishByRequestId(string requestId, object result)
{
var future = requestIdCache.GetFuture(requestId);
future?.SetResult(result);
}
private void StartOneTest(string name, Func<string> doAction, Func<object, bool> checkResult)
{
LOG.Info(" start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", name);
string requestId = doAction.Invoke() ?? PSEUDO_REQUEST_ID;
StartOneTestCommon(name, requestId, checkResult, TimeSpan.FromSeconds(5));
LOG.Info("end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< {}", name);
}
private void StartOneTest(string name, Action preSet, Func<string> doAction, Func<object, bool> checkResult, int retryTime)
{
LOG.Info(" start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", name);
preSet.Invoke();
for (int i = 0; i < retryTime; i++)
{
try
{
string requestId = doAction.Invoke() ?? PSEUDO_REQUEST_ID;
StartOneTestCommon(name, requestId, checkResult, TimeSpan.FromSeconds(5));
break;
}
catch (VerificationException)
{
if (i + 1 == retryTime)
{
throw;
}
}
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}
LOG.Info("end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< {}", name);
}
private void StartOneManualTest(string name, string requestId, Func<object, bool> checkResult)
{
LOG.Info(" start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", name);
StartOneTestCommon(name, requestId, checkResult, TimeSpan.FromMinutes(20));
LOG.Info("end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< {}", name);
}
private void StartOneTestCommon(string name, string requestId, Func<object, bool> checkResult,
TimeSpan waitTime)
{
var future = new TaskCompletionSource<object>();
requestIdCache.SetRequestId2Cache(requestId, future);
if (!future.Task.Wait(waitTime))
{
throw new Exception(name + " failed, timeout");
}
if (!checkResult.Invoke(future.Task.Result))
{
LOG.Warn("verify failed: {}", future.Task.Result);
throw new VerificationException(name + " result check failed");
}
}
public void OnLogin(string deviceId, string requestId, int resultCode)
{
LOG.Info("OnLogin deviceId:{}, requestId:{}, resultCode:{}", deviceId, requestId,
resultCode);
FinishByRequestId(requestId, new Dictionary<string, object>
{
{ "deviceId", deviceId },
{ "resultCode", resultCode },
});
}
public void OnCommand(string deviceId, string requestId, BridgeCommand bridgeCommand)
{
LOG.Info("OnCommand deviceId:{}, requestId:{}, bridgeCommand:{}", deviceId, requestId,
JsonConvert.SerializeObject(bridgeCommand));
FinishByRequestId(PSEUDO_COMMAND_DOWN_REQUEST_ID, new DownLinkRequest
{
RequestId = requestId,
Payload = bridgeCommand
});
}
public void OnDeviceMessage(string deviceId, DeviceMessage deviceMessage)
{
LOG.Info("OnDeviceMessage device id:{}, deviceMessage:{}", deviceId,
JsonConvert.SerializeObject(deviceMessage));
FinishByRequestId(PSEUDO_MESSAGE_DOWN_REQUEST_ID, deviceMessage);
}
public void OnRawDeviceMessage(string deviceId, RawDeviceMessage deviceMessage)
{
FinishByRequestId(PSEUDO_MESSAGE_DOWN_REQUEST_ID, deviceMessage);
}
public void OnLogout(string deviceId, string requestId, Dictionary<string, object> map)
{
FinishByRequestId(requestId, new Dictionary<string, object>
{
{ "deviceId", deviceId },
{ "resultCode", Convert.ToInt32(map["result_code"]) },
});
}
public void OnResetDeviceSecret(string deviceId, string requestId, int resultCode, string newSecret)
{
LOG.Info("OnResetDeviceSecret deviceId:{}, requestId:{}, resultCode:{}, newSecret:{}",
deviceId, requestId,
resultCode, newSecret);
FinishByRequestId(requestId, new Dictionary<string, object>
{
{ "deviceId", deviceId },
{ "resultCode", resultCode },
{ "newSecret", newSecret },
});
}
public void OnDisConnect(string deviceId)
{
FinishByRequestId(PSEUDO_REQUEST_ID, deviceId);
}
public void OnPropertiesSet(string deviceId, string requestId, List<ServiceProperty> services)
{
FinishByRequestId(PSEUDO_PROPERTY_SET_REQUEST_ID, new DownLinkRequest
{
RequestId = requestId,
Payload = services
});
}
public void OnPropertiesGet(string deviceId, string requestId, string serviceId)
{
FinishByRequestId(PSEUDO_PROPERTY_GET_REQUEST_ID, new DownLinkRequest
{
RequestId = requestId,
Payload = serviceId
});
}
public void OnEvent(string deviceId, DeviceEvents deviceEvents)
{
FinishByRequestId(PSEUDO_EVENT_REQUEST_ID, deviceEvents);
}
public void OnShadowDown(string deviceId, string requestId, DeviceShadowResponse shadows)
{
FinishByRequestId(requestId, shadows);
}
private bool TestAsyncLoginLogoutResultCheck(object reply)
{
var receivedPayload = (Dictionary<string, object>)reply;
return !((string)receivedPayload["deviceId"] != rootDeviceId ||
(int)receivedPayload["resultCode"] != 0);
}
private void TestAsyncLoginLogout()
{
client.loginListener = this;
client.logoutListener = this;
StartOneTest("async login",
() => client.LoginAsync(rootDeviceId, rootDeviceSecret),
TestAsyncLoginLogoutResultCheck);
StartOneTest("async logout",
() => client.LoginAsync(rootDeviceId, rootDeviceSecret),
TestAsyncLoginLogoutResultCheck);
client.loginListener = null;
client.logoutListener = null;
}
private void TestSyncLoginLogout()
{
if (client.LoginSync(rootDeviceId, rootDeviceSecret, 5000) != 0)
{
throw new Exception("login failed");
}
if (client.LogoutSync(rootDeviceId, 5000) != 0)
{
throw new Exception("logout failed");
}
}
private static ServiceProperty GenerateTestServices()
{
var r = new Random();
var properties = new Dictionary<string, object>
{
{ "alarm", r.Next() % 2 },
{ "smokeConcentration", r.NextDouble() % 100 },
{ "temperature", r.NextDouble() % 100 },
{ "humidity", r.Next() % 100 },
};
return new ServiceProperty
{
serviceId = TEST_SERVICE_ID,
properties = properties
};
}
private void TestReportProperties()
{
var service = GenerateTestServices();
StartOneTest("report properties and verify through getting shadow",
() =>
{
client.ReportProperties(rootDeviceId, new List<ServiceProperty>
{
service
});
},
() =>
{
return client.GetShadow(rootDeviceId, new DeviceShadowRequest
{ ServiceId = service.serviceId, DeviceId = rootDeviceId });
// wait for shadow to refresh
},
(reply) =>
{
var resp = (DeviceShadowResponse)reply;
Debug.Assert(resp.Shadow.Count == 1);
Debug.Assert(resp.Shadow[0].ServiceId == service.serviceId);
var reported = JObject.FromObject(resp.Shadow[0].Reported.Properties);
var propertiesJ = JObject.FromObject(service.properties);
return JToken.DeepEquals(reported, propertiesJ);
}, 10);
}
private void TestReportGatewaySubDeviceProperties()
{
var service = GenerateTestServices();
var deviceProperties = new BridgeDeviceProperties
{
DeviceId = subDeviceId,
Services = new List<ServiceProperty>
{
service
}
};
StartOneTest("report gateway sub device properties and verify through getting shadow",
() =>
{
client.ReportGatewaySubDeviceProperties(rootDeviceId, new List<BridgeDeviceProperties>
{
deviceProperties
});
},
() =>
{
return client.GetShadow(rootDeviceId,
new DeviceShadowRequest { ServiceId = service.serviceId, DeviceId = subDeviceId });
},
reply =>
{
var resp = (DeviceShadowResponse)reply;
Debug.Assert(resp.Shadow.Count == 1);
Debug.Assert(resp.Shadow[0].ServiceId == service.serviceId);
var reported = JObject.FromObject(resp.Shadow[0].Reported.Properties);
var propertiesJ = JObject.FromObject(service.properties);
return JToken.DeepEquals(reported, propertiesJ);
}, 10);
}
private void TestResetSecret()
{
StartOneTest("reset password",
() =>
{
var requestId = Guid.NewGuid().ToString();
client.ResetSecret(rootDeviceId, requestId,
new DeviceSecret(rootDeviceSecret, newDeviceSecret));
return requestId;
},
reply =>
{
var expectedReply = new JObject
{
{ "deviceId", rootDeviceId },
{ "resultCode", 0 },
{ "newSecret", newDeviceSecret },
};
return JToken.DeepEquals(JObject.FromObject(reply), expectedReply);
});
StartOneTest("reset password again",
() =>
{
var requestId = Guid.NewGuid().ToString();
client.ResetSecret(rootDeviceId, requestId,
new DeviceSecret(newDeviceSecret, rootDeviceSecret));
return requestId;
},
reply =>
{
var expectedReply = new JObject
{
{ "deviceId", rootDeviceId },
{ "resultCode", 0 },
{ "newSecret", rootDeviceSecret },
};
return JToken.DeepEquals(JObject.FromObject(reply), expectedReply);
});
}
private void TestReportEvent()
{
StartOneTest("report event",
() =>
{
client.ReportEvent(rootDeviceId, new DeviceEvents
{
services = new List<DeviceEvent>{
new DeviceEvent {
serviceId = "$sub_device_manager",
eventType = "sub_device_sync_request",
paras = new Dictionary<string, object>
{
{ "version", 0 }
}
}
}
});
return PSEUDO_EVENT_REQUEST_ID;
},
reply =>
{
var r = (DeviceEvents)reply;
Debug.Assert(r.services.Count == 1);
var service = r.services[0];
Debug.Assert(service.serviceId == "$sub_device_manager");
JArray devices = (JArray)service.paras["devices"];
var found = devices.Any(v => ((JObject)v)["device_id"].ToString() == subDeviceId);
return found;
});
}
private void TesMessageDownThenReport()
{
client.bridgeDeviceMessageListener = this;
StartOneManualTest("test message down then report",
PSEUDO_MESSAGE_DOWN_REQUEST_ID,
(reply) =>
{
var r = (DeviceMessage)reply;
r.id = Guid.NewGuid().ToString();
client.ReportDeviceMessage(rootDeviceId, r);
return true;
});
client.bridgeDeviceMessageListener = null;
client.BridgeRawDeviceMessageListener = this;
StartOneManualTest("test raw message down then report",
PSEUDO_MESSAGE_DOWN_REQUEST_ID,
(reply) =>
{
var r = (RawDeviceMessage)reply;
client.ReportRawDeviceMessage(rootDeviceId, r);
return true;
});
}
private void TestCommandDown()
{
StartOneManualTest("test command down",
PSEUDO_COMMAND_DOWN_REQUEST_ID,
(reply) =>
{
var r = (DownLinkRequest)reply;
var requestId = r.RequestId;
var bridgeCommand = (BridgeCommand)r.Payload;
Debug.Assert(bridgeCommand.command.serviceId == TEST_SERVICE_ID);
Debug.Assert(bridgeCommand.command.paras.ContainsKey("duration"));
var rsp = new CommandRsp(CommandRsp.SUCCESS,
new Dictionary<string, object>
{
{ "set", true }
})
{
responseName = "ringAlarm",
};
client.RespondCommand(bridgeCommand.deviceId, requestId, rsp);
return true;
});
}
private void TestPropertyGet()
{
StartOneManualTest("test property get",
PSEUDO_PROPERTY_GET_REQUEST_ID,
(reply) =>
{
var r = (DownLinkRequest)reply;
var requestId = r.RequestId;
var serviceId = (string)r.Payload;
var service = GenerateTestServices();
client.RespondPropsGet(rootDeviceId, requestId, new List<ServiceProperty>
{
service
});
return serviceId == TEST_SERVICE_ID;
});
}
private void TestPropertySet()
{
StartOneManualTest("test property set",
PSEUDO_PROPERTY_SET_REQUEST_ID,
(reply) =>
{
var r = (DownLinkRequest)reply;
var requestId = r.RequestId;
var services = (List<ServiceProperty>)r.Payload;
var result = services.Count == 1 && services[0].serviceId == TEST_SERVICE_ID
&& services[0].properties.ContainsKey("alarm")
&& services[0].properties.ContainsKey("smokeConcentration")
&& services[0].properties.ContainsKey("temperature")
&& services[0].properties.ContainsKey("humidity");
client.RespondPropsSet(rootDeviceId, requestId, new IotResult(result ? 0 : 1, "aitest"));
return result;
});
}
public void Start(BridgeClient bridgeClient)
{
// 启动时初始化网桥连接
client = bridgeClient;
rootDeviceId = Environment.GetEnvironmentVariable("ENV_ROOT_DEVICE_ID");
rootDeviceSecret = Environment.GetEnvironmentVariable("ENV_ROOT_DEVICE_SECRET");
subDeviceId = Environment.GetEnvironmentVariable("ENV_SUB_DEVICE_ID");
newDeviceSecret = Environment.GetEnvironmentVariable("ENV_ROOT_DEV_NEW_PW");
client.bridgeCommandListener = this;
client.resetDeviceSecretListener = this;
client.bridgeDeviceDisConnListener = this;
client.bridgePropertyListener = this;
client.BridgeEventListener = this;
client.BridgeShadowListener = this;
TestAsyncLoginLogout();
TestSyncLoginLogout();
client.LoginSync(rootDeviceId, rootDeviceSecret, 5000);
TestReportProperties();
TestReportGatewaySubDeviceProperties();
TestResetSecret();
TestReportEvent();
var parallelManualTests = new List<Task>
{
Task.Run(TesMessageDownThenReport),
Task.Run(TestCommandDown),
Task.Run(TestPropertyGet),
Task.Run(TestPropertySet),
};
Task.WaitAll(parallelManualTests.ToArray());
client.LogoutSync(rootDeviceId, 5000);
}
}
}