示例#1
0
        private static async Task PrepareTestAsync()
        {
            kRegistryManager      = AZD.RegistryManager.CreateFromConnectionString(kIoTHubConnectionString);
            kDeviceConnectionList = new List <string>();
            kDevicesList          = new List <AZD.Device>();

            do
            {
                var devicesList = (await kRegistryManager.GetDevicesAsync(kDevicePageSize)).ToList();
                kDevicesList.AddRange(devicesList);

                if (kDevicesList.Count == kTotalDevices)
                {
                    break;
                }
            } while (true);

            for (int idx = 0; idx < kDevicesList.Count; ++idx)
            {
                try
                {
                    var device     = kDevicesList[idx];
                    var connString = device.Authentication.SymmetricKey.PrimaryKey;
                    connString = string.Format(kDeviceConnectionString, device.Id, connString);
                    Console.WriteLine(connString);

                    kDeviceConnectionList.Add(connString);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public void GetDeviceListFromAzure()
        {
            registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal);
            List <Device> localDevices = new List <Device>();
            IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100);
            Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; });

            task.Wait();
            Device currentDevice;

            try
            {
                foreach (var x in azureDevice)
                {
                    currentDevice = new Device()
                    {
                        Id                    = x.Id,
                        Status                = (AzureIOT.Models.Status)x.Status,
                        LastActive            = x.ConnectionStateUpdatedTime,
                        CloudToDeviceMessages = x.CloudToDeviceMessageCount,
                        //AuthType = (AzureIOT.Models.AuthTypes)(x. ?? x.AuthenticationType),
                        PrimaryThumbprint   = x.Authentication != null ? x.Authentication.X509Thumbprint.PrimaryThumbprint : null,
                        SecondaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.SecondaryThumbprint : null,
                    };
                    localDevices.Add(currentDevice);
                    currentDevice = null;
                }
                this.deviceResponse = new Response <List <Device> >()
                {
                    Success        = true,
                    ResponseObject = localDevices
                };
            }
            catch (Exception ex)
            {
                List <AzureException> azureException = new List <AzureException>()
                {
                    new AzureException()
                    {
                        Exception = ex, Message = ex.Message
                    }
                };
                this.deviceResponse = new Response <List <Device> >()
                {
                    Success    = false,
                    Exceptions = azureException.ToArray()
                };
            }
        }
        public Device DeviceDetail(string DeviceId)
        {
            registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal);
            List <Device> localDevices = new List <Device>();
            IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100);
            Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; });

            task.Wait();

            try
            {
                Device currentDevice = new Device();
                foreach (var x in azureDevice)
                {
                    if (x.Id != DeviceId)
                    {
                        continue;
                    }
                    currentDevice = new Device()
                    {
                        Id                    = x.Id,
                        Status                = (AzureIOT.Models.Status)x.Status,
                        LastActive            = x.LastActivityTime,
                        ConnectionStatus      = (AzureIOT.Models.ConnectStatus)x.ConnectionState,
                        CloudToDeviceMessages = x.CloudToDeviceMessageCount,
                        //AuthType = (AzureIOT.Models.AuthTypes)(x. ?? x.AuthenticationType),
                        PrimaryThumbprint   = x.Authentication != null ? x.Authentication.X509Thumbprint.PrimaryThumbprint : null,
                        SecondaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.SecondaryThumbprint : null,
                    };
                }
                return(currentDevice);
            }
            catch (DeviceAlreadyExistsException ex)
            {
                List <AzureException> exception = new List <AzureException>()
                {
                    new AzureException()
                    {
                        Exception = ex, Message = ex.Message
                    }
                };
                return(new Device()
                {
                });
            }
        }
        public DashboardFacts GetDashboardFacts()
        {
            DashboardFacts facts = new DashboardFacts();

            registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal);
            List <Device> localDevices = new List <Device>();
            IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100);
            Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; });

            task.Wait();

            try
            {
                facts.TotalDevices = azureDevice.Count();
                foreach (var x in azureDevice)
                {
                    if (x.ConnectionState == DeviceConnectionState.Connected)
                    {
                        facts.ConnectedDevices++;
                    }
                    else
                    {
                        facts.OfflineDevices++;
                    }
                    facts.TotalCloudToDeviceMessages = x.CloudToDeviceMessageCount;
                }
                return(facts);
            }
            catch (DeviceAlreadyExistsException ex)
            {
                List <AzureException> exception = new List <AzureException>()
                {
                    new AzureException()
                    {
                        Exception = ex, Message = ex.Message
                    }
                };
                return(facts);
            }
        }
示例#5
0
        private static async Task RemoveDevicesAsync()
        {
            kRegistryManager = AZD.RegistryManager.CreateFromConnectionString(kIoTHubConnectionString);

            do
            {
                var devicesList = (await kRegistryManager.GetDevicesAsync(kRemoveMaxCount)).ToList();
                if (devicesList?.Count == 0)
                {
                    break;
                }

                try
                {
                    await kRegistryManager.RemoveDevices2Async(devicesList);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }while (true);
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter IoT Hub connection string: ");
            connectionString = Console.ReadLine();

            Console.WriteLine("Enter Service Fabric cluster address where your IoT project is deployed (or blank for local): ");
            clusterAddress = Console.ReadLine();

            registryManager = RegistryManager.CreateFromConnectionString(connectionString);
            fabricClient = String.IsNullOrEmpty(clusterAddress)
                ? new FabricClient()
                : new FabricClient(clusterAddress);

            Task.Run(
                async () =>
                {
                    while (true)
                    {
                        try
                        {
                            devices = await registryManager.GetDevicesAsync(Int32.MaxValue);
                            tenants = (await fabricClient.QueryManager.GetApplicationListAsync())
                                .Where(x => x.ApplicationTypeName == Names.TenantApplicationTypeName)
                                .Select(x => x.ApplicationName.ToString().Replace(Names.TenantApplicationNamePrefix + "/", ""));

                            Console.WriteLine();
                            Console.WriteLine("Devices IDs: ");
                            foreach (Device device in devices)
                            {
                                Console.WriteLine(device.Id);
                            }

                            Console.WriteLine();
                            Console.WriteLine("Tenants: ");
                            foreach (string tenant in tenants)
                            {
                                Console.WriteLine(tenant);
                            }

                            Console.WriteLine();
                            Console.WriteLine("Commands:");
                            Console.WriteLine("1: Register a device");
                            Console.WriteLine("2: Register random devices");
                            Console.WriteLine("3: Send data from a device");
                            Console.WriteLine("4: Send data from all devices");
                            Console.WriteLine("5: Exit");

                            string command = Console.ReadLine();

                            switch (command)
                            {
                                case "1":
                                    Console.WriteLine("Make up a device ID: ");
                                    string deviceId = Console.ReadLine();
                                    await AddDeviceAsync(deviceId);
                                    break;
                                case "2":
                                    Console.WriteLine("How many devices? ");
                                    int num = Int32.Parse(Console.ReadLine());
                                    await AddRandomDevicesAsync(num);
                                    break;
                                case "3":
                                    Console.WriteLine("Tenant: ");
                                    string tenant = Console.ReadLine();
                                    Console.WriteLine("Device id: ");
                                    string deviceKey = Console.ReadLine();
                                    await SendDeviceToCloudMessagesAsync(deviceKey, tenant);
                                    break;
                                case "4":
                                    Console.WriteLine("Tenant: ");
                                    string tenantName = Console.ReadLine();
                                    Console.WriteLine("Iterations: ");
                                    int iterations = Int32.Parse(Console.ReadLine());
                                    await SendAllDevices(tenantName, iterations);
                                    break;
                                case "5":
                                    return;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Oops, {0}", ex.Message);
                        }
                    }
                })
                .GetAwaiter().GetResult();
        }