我们在c#中只有这两种方法可用于azure IoThub。
Device device = await registryManager.GetDeviceAsync(“deviceId”);和
device = await registryManager.GetDevicesAsync(“max count”);…
你感兴趣的价值观是其中的一部分 Azure IoT Hub指标 。基本上你可以获得他们的:
据我所知,
没有直接的方法来实际获得 的 total number of devices 强> 。或者,您可以执行的操作是创建一个List以及每当您使用添加设备时 的 AddDeviceAsync 强> 你应该把对象推到列表中。
total number of devices
AddDeviceAsync
与消息总数相同,您应该创建自己的方法来更新值。
以下代码应该有所帮助。
static async Task startClient(string IoTHub, string IoTDevicePrefix, int deviceNumber, string commonKey, int maxMessages, int messageDelaySeconds) { allClientStarted++; runningDevices++; string connectionString = "HostName=" + IoTHub + ";DeviceId=" + IoTDevicePrefix + deviceNumber + ";SharedAccessKey=" + commonKey; DeviceClient device = DeviceClient.CreateFromConnectionString(connectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt); await device.OpenAsync(); Random rnd = new Random(); int mycounter = 1; Console.WriteLine("Device " + IoTDevicePrefix + deviceNumber + " started"); while (mycounter <= maxMessages) { Thread.Sleep((messageDelaySeconds * 1000) + rnd.Next(1, 100)); string message = "{ \'loadTest\':\'True\', 'sequenceNumber': " + mycounter + ", \'SubmitTime\': \'" + DateTime.UtcNow + "\', \'randomValue\':" + rnd.Next(1, 4096 * 4096) + " }"; Microsoft.Azure.Devices.Client.Message IoTMessage = new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(message)); await device.SendEventAsync(IoTMessage); totalMessageSent++; mycounter++; } await device.CloseAsync(); Console.WriteLine("Device " + IoTDevicePrefix + deviceNumber + " ended"); runningDevices--; } static void createDevices(int number) { for (int i = 1; i <= number; i++) { var registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); Device mydevice = new Device(IoTDevicePrefix + i.ToString()); mydevice.Authentication = new AuthenticationMechanism(); mydevice.Authentication.SymmetricKey.PrimaryKey = commonKey; mydevice.Authentication.SymmetricKey.SecondaryKey = commonKey; try { registryManager.AddDeviceAsync(mydevice).Wait(); Console.WriteLine("Adding device: " + IoTDevicePrefix + i.ToString()); } catch (Exception er) { Console.WriteLine(" Error adding device: " + IoTDevicePrefix + i.ToString() + " error: " + er.InnerException.Message); } } }