C# 如何获取发送到azure IoT hub c的设备总数和消息总数#
我们只有这两种方法可用于c#中的azure IoThub 及 但就我所知,如何使用c#?获取所有可用设备计数或活动设备计数以及消息计数 没有直接的方法来实际获取设备总数。或者,您可以创建一个列表,无论何时使用C# 如何获取发送到azure IoT hub c的设备总数和消息总数#,c#,azure,c#-4.0,iot,azure-iot-hub,C#,Azure,C# 4.0,Iot,Azure Iot Hub,我们只有这两种方法可用于c#中的azure IoThub 及 但就我所知,如何使用c#?获取所有可用设备计数或活动设备计数以及消息计数 没有直接的方法来实际获取设备总数。或者,您可以创建一个列表,无论何时使用AddDeviceAsync添加设备,都应该将对象推送到列表中 与消息总数相同,您应该创建自己的方法来保持值的更新 下面的代码应该会有所帮助 static async Task startClient(string IoTHub, string IoTDevicePrefix, int de
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);
}
}
}
static async Task startClient(字符串IoTHub、字符串IoTDevicePrefix、int deviceNumber、字符串commonKey、int maxMessages、int messageDelaySeconds)
{
AllClientStart++;
运行设备++;
字符串连接string=“HostName=”+IoTHub+“DeviceId=”+IoDevicePrefix+deviceNumber+“SharedAccessKey=”+commonKey;
DeviceClient device=DeviceClient.CreateFromConnectionString(connectionString,Microsoft.Azure.Devices.Client.TransportType.Mqtt);
等待设备。OpenAsync();
随机rnd=新随机();
int-mycounter=1;
Console.WriteLine(“设备”+IoTDevicePrefix+deviceNumber+“已启动”);
而(mycounter据我所知
没有直接的方法可以实际获取设备总数。或者,您可以创建一个列表,并且无论何时使用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);
}
}
}
static async Task startClient(字符串IoTHub、字符串IoTDevicePrefix、int deviceNumber、字符串commonKey、int maxMessages、int messageDelaySeconds)
{
AllClientStart++;
运行设备++;
字符串连接string=“HostName=”+IoTHub+“DeviceId=”+IoDevicePrefix+deviceNumber+“SharedAccessKey=”+commonKey;
DeviceClient device=DeviceClient.CreateFromConnectionString(connectionString,Microsoft.Azure.Devices.Client.TransportType.Mqtt);
等待设备。OpenAsync();
随机rnd=新随机();
int-mycounter=1;
Console.WriteLine(“设备”+IoTDevicePrefix+deviceNumber+“已启动”);
而(mycounter您感兴趣的值是。基本上您可以获得它们的:
- 使用和
- 为Azure IoT Hub添加诊断设置,并为AllMetrics选择以下目标之一:
- 要在事件驱动存储中存档。使用订阅服务器(如带有输入blob绑定的EventGridTrigger函数),可以在函数体中查询度量
- 或者通过事件中心将度量推送到流媒体管道,并使用流分析作业查询度量数据
您感兴趣的值是测试的一部分。基本上,您可以获得它们的:
- 使用和
- 为Azure IoT Hub添加诊断设置,并为AllMetrics选择以下目标之一:
- 要在事件驱动存储中存档。使用订阅服务器(如带有输入blob绑定的EventGridTrigger函数),可以在函数体中查询度量
- 或者通过事件中心将度量推送到流媒体管道,并使用流分析作业查询度量数据
可以通过使用,例如:
选择COUNT()作为c中的numberOfDevices
返回如下内容:
[
{
“设备数量”:123
}
]
要检索消息数,您需要连接到与事件中心兼容的端点,连接到每个底层分区并查看每个分区信息(最后的序列号和序列号)。但是,这涉及到一些数据保留,因此除非您对此添加更多逻辑,否则您将得到一个数字,该数字表示集线器中当前存在的邮件数,而不是创建后的总数,也不是要处理的总数
更新:以下代码显示了获取设备数量的两种方法:
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Newtonsoft.Json;
namespace Test
{
class Program
{
static async Task Main()
{
string connString = "HostName=_______.azure-devices.net;SharedAccessKeyName=_______;SharedAccessKey=_______";
RegistryManager registry = RegistryManager.CreateFromConnectionString(connString);
// Method 1: using Device Twin
string queryString = "SELECT COUNT() AS numberOfDevices FROM devices";
IQuery query = registry.CreateQuery(queryString, 1);
string json = (await query.GetNextAsJsonAsync()).FirstOrDefault();
Dictionary<string, long> data = JsonConvert.DeserializeObject<Dictionary<string, long>>(json);
long count1 = data["numberOfDevices"];
// Method 2: using Device Registry
RegistryStatistics stats = await registry.GetRegistryStatisticsAsync();
long count2 = stats.TotalDeviceCount;
}
}
}
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.Azure.Devices;
使用Newtonsoft.Json;
名称空间测试
{
班级计划
{
静态异步任务Main()
{
string connString=“主机名=\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
RegistryManager注册表=RegistryManager.CreateFromConnectionString(connString);
//方法1:使用双设备
string queryString=“选择COUNT()作为设备中的numberOfDevices”;
IQuery=registry.CreateQuery(queryString,1);
字符串json=(wait query.GetNextAsJsonAsync()).FirstOrDefault();
Dictionary data=JsonConvert.DeserializeObject(json);
长计数1=数据[“设备数”];
//方法2:使用设备注册表
RegistryStatistics=await registry.GetRegistryStatisticsAsync();
long count2=统计总数设备计数;
}
}
}
可以通过使用,例如:
选择COUNT()作为c中的numberOfDevices
返回如下内容:
[
{
“设备数量”:123
}
]
要检索消息数,您需要连接到与事件中心兼容的端点,连接到每个底层分区并查看
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Newtonsoft.Json;
namespace Test
{
class Program
{
static async Task Main()
{
string connString = "HostName=_______.azure-devices.net;SharedAccessKeyName=_______;SharedAccessKey=_______";
RegistryManager registry = RegistryManager.CreateFromConnectionString(connString);
// Method 1: using Device Twin
string queryString = "SELECT COUNT() AS numberOfDevices FROM devices";
IQuery query = registry.CreateQuery(queryString, 1);
string json = (await query.GetNextAsJsonAsync()).FirstOrDefault();
Dictionary<string, long> data = JsonConvert.DeserializeObject<Dictionary<string, long>>(json);
long count1 = data["numberOfDevices"];
// Method 2: using Device Registry
RegistryStatistics stats = await registry.GetRegistryStatisticsAsync();
long count2 = stats.TotalDeviceCount;
}
}
}