//在某产品下申请创建一批设备 public void TestApplyDeviceWithNames() { DefaultAcsClient acsClient = Demo.IotClient.GetClient(); BatchCheckDeviceNamesRequest request = new BatchCheckDeviceNamesRequest(); request.ProductKey = "<productKey>"; List <String> DeviceNames = new List <string>(); for (int i = 1; i < 20; i++) { if (i < 10) { DeviceNames.Add("device_20171206_00" + i); } else { DeviceNames.Add("device_20171206_0" + i); } } request.DeviceNames = DeviceNames; BatchCheckDeviceNamesResponse response = acsClient.GetAcsResponse(request); Console.WriteLine(response.Success); Console.WriteLine(response.ErrorMessage); Console.WriteLine("apply id: " + response.Data.ApplyId); }
//批量注册设备,最多一次创建1000台设备,指定设备名称 public void TestBatchRegisterDeviceWithNames() { DefaultAcsClient acsClient = Demo.IotClient.GetClient(); String productKey = "<productKey>"; BatchCheckDeviceNamesRequest request1 = new BatchCheckDeviceNamesRequest(); request1.ProductKey = productKey; List <string> DeviceNames = new List <string>(); for (int i = 100; i < 150; i += 1) { DeviceNames.Add("device_0821_" + i); } request1.DeviceNames = DeviceNames; //Step1 创建申请单,返回applyId BatchCheckDeviceNamesResponse response1 = acsClient.GetAcsResponse(request1); Console.WriteLine("Batch Check: " + response1.Success); if (!(bool)response1.Success) { Console.WriteLine(response1.Code + ", " + response1.ErrorMessage); } long applyId = (long)response1.Data.ApplyId; Console.WriteLine("ApplyId: " + applyId); String Status = "FAILED"; while (true) { //Step2 轮询申请单的检查进度,如果检查正常通过,则可以量产设备 QueryBatchRegisterDeviceStatusRequest request2 = new QueryBatchRegisterDeviceStatusRequest(); request2.ApplyId = applyId; request2.ProductKey = productKey; QueryBatchRegisterDeviceStatusResponse response2 = acsClient.GetAcsResponse(request2); if (!(bool)response2.Success) { Console.WriteLine(response2.Code + ", " + response2.ErrorMessage); break; } QueryBatchRegisterDeviceStatusResponse.QueryBatchRegisterDeviceStatus_Data data = response2.Data; Status = data.Status; Console.WriteLine("Query Status: " + response2.Success + ", " + Status); if ("CHECK_SUCCESS".Equals(Status)) { break; } else { if ("CHECK_FAILED".Equals(Status)) { List <string> InvalidDevicenameList = response2.Data.InvalidList; for (int j = 0; j < InvalidDevicenameList.Count; j += 1) { String deviceName = InvalidDevicenameList[j]; Console.WriteLine("Invalid DeviceName: " + deviceName); } break; } else { Thread.Sleep(1000); } } } if ("CHECK_SUCCESS".Equals(Status)) { //Step3 开始量产设备,量产设备是异步过程,全部待生产设备完成生产需要一段时间 BatchRegisterDeviceWithApplyIdRequest request3 = new BatchRegisterDeviceWithApplyIdRequest(); request3.ProductKey = productKey; request3.ApplyId = applyId; BatchRegisterDeviceWithApplyIdResponse response3 = acsClient.GetAcsResponse(request3); Console.WriteLine("Batch Register With ApplyId Request: " + response3.Success); if (!(bool)response3.Success) { Console.WriteLine(response3.Code + ", " + response3.ErrorMessage); } } while (true) { //轮询申请单的检查进度,如果申请单是创建完成状态,则表明全部设备已经生产完毕 QueryBatchRegisterDeviceStatusRequest request2 = new QueryBatchRegisterDeviceStatusRequest(); request2.ApplyId = applyId; request2.ProductKey = productKey; QueryBatchRegisterDeviceStatusResponse response2 = acsClient.GetAcsResponse(request2); if (!(bool)response2.Success) { Console.WriteLine(response2.Code + ", " + response2.ErrorMessage); break; } QueryBatchRegisterDeviceStatusResponse.QueryBatchRegisterDeviceStatus_Data data = response2.Data; Status = data.Status; Console.WriteLine("Query Status: " + response2.Success + ", " + Status); if ("CREATE_SUCCESS".Equals(Status)) { break; } else { Thread.Sleep(1000); } } //Step5 根据ApplyId,获取这一批次量产的全部设备 QueryPageByApplyIdRequest request4 = new QueryPageByApplyIdRequest(); request4.ApplyId = applyId; request4.CurrentPage = 1; request4.PageSize = 10; QueryPageByApplyIdResponse response4 = acsClient.GetAcsResponse(request4); Console.WriteLine("Query With ApplyId: " + response4.Success); if (!(bool)response4.Success) { Console.WriteLine(response4.Code + ", " + response4.ErrorMessage); } Console.WriteLine("Page: " + response4.Page); Console.WriteLine("PageSize: " + response4.PageSize); Console.WriteLine("PageCount: " + response4.PageCount); Console.WriteLine("Total: " + response4.Total); }