C# Azure物联网中心运营监控
我有一个Azure IoT集线器,可以将设备连接到该集线器。我希望启用监视以监视连接和断开集线器的设备 我已经为我的物联网中心启用了监控类别中的详细连接: 我的设备连接到我的集线器并在设备资源管理器中显示: 然后,我将Azure函数设置为将我的数据从操作监控记录到Azure SQL db:C# Azure物联网中心运营监控,c#,azure,iot,azure-iot-hub,C#,Azure,Iot,Azure Iot Hub,我有一个Azure IoT集线器,可以将设备连接到该集线器。我希望启用监视以监视连接和断开集线器的设备 我已经为我的物联网中心启用了监控类别中的详细连接: 我的设备连接到我的集线器并在设备资源管理器中显示: 然后,我将Azure函数设置为将我的数据从操作监控记录到Azure SQL db: using System; using System.Configuration; using System.Data.SqlClient; using Newtonsoft.Json; public
using System;
using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;
public static void Run(string myEventHubMessage, TraceWriter log)
{
log.Info(myEventHubMessage);
try
{
var connectionString = ConfigurationManager.ConnectionStrings["iotAzureSQLDb"].ConnectionString;
log.Info(connectionString);
var message = JsonConvert.DeserializeObject<MonitoringMessage>(myEventHubMessage);
using (var connection = new SqlConnection(connectionString))
{
var sqlStatement = "insert into [dbo].[DeviceStatuses] " +
"(DeviceId, ConnectionStatus, ConnectionUpdateTime)" +
"values " +
"(@DeviceId, @ConnectionStatus, @ConnectionUpdateTime)";
using (var dataCommand = new SqlCommand(sqlStatement, connection))
{
dataCommand.Parameters.AddWithValue("@ConnectionStatus", message.operationName);
dataCommand.Parameters.AddWithValue("@DeviceId", message.deviceId);
dataCommand.Parameters.AddWithValue("@ConnectionUpdateTime", message.time);
connection.Open();
dataCommand.ExecuteNonQuery();
connection.Close();
log.Info($"Device {message.deviceId} changed state: {message.operationName}");
}
}
}
catch (Exception ex)
{
log.Info(ex.Message);
}
}
public class MonitoringMessage
{
public string deviceId { get; set; }
public string operationName { get; set; }
public int? durationMs { get; set; }
public string authType { get; set; }
public string protocol { get; set; }
public DateTimeOffset? time { get; set; }
public string category { get; set; }
public string level { get; set; }
public int? statusCode { get; set; }
public int? statusType { get; set; }
public string statusDescription { get; set; }
}
使用系统;
使用系统配置;
使用System.Data.SqlClient;
使用Newtonsoft.Json;
公共静态无效运行(字符串MyEventThubMessage、TraceWriter日志)
{
log.Info(myEventHubMessage);
尝试
{
var connectionString=ConfigurationManager.connectionString[“iotAzureSQLDb”]。connectionString;
log.Info(连接字符串);
var message=JsonConvert.DeserializeObject(myEventHubMessage);
使用(var连接=新的SqlConnection(connectionString))
{
var sqlStatement=“插入[dbo].[DeviceStatus]”+
(设备ID、连接状态、连接更新时间)+
“价值观”+
“(@DeviceId、@ConnectionStatus、@ConnectionUpdateTime)”;
使用(var dataCommand=newsqlcommand(sqlStatement,connection))
{
dataCommand.Parameters.AddWithValue(“@ConnectionStatus”,message.operationName);
dataCommand.Parameters.AddWithValue(“@DeviceId”,message.DeviceId);
dataCommand.Parameters.AddWithValue(“@ConnectionUpdateTime”,message.time);
connection.Open();
dataCommand.ExecuteOnQuery();
connection.Close();
log.Info($“设备{message.deviceId}更改状态:{message.operationName}”);
}
}
}
捕获(例外情况除外)
{
日志信息(例如消息);
}
}
公共类监视消息
{
公共字符串设备ID{get;set;}
公共字符串操作名{get;set;}
公共整数?持续时间{get;set;}
公共字符串authType{get;set;}
公共字符串协议{get;set;}
公共DateTimeOffset?时间{get;set;}
公共字符串类别{get;set;}
公共字符串级别{get;set;}
公共int?状态码{get;set;}
公共int?状态类型{get;set;}
公共字符串statusDescription{get;set;}
}
如果在操作监视中启用设备标识操作,则会记录创建事件。所以我相信函数的输入是正确的。但是,从未为连接创建任何内容
我还可以向连接的设备发送消息。我只是没有看到连接/断开的事件
我还尝试创建一个流分析,并在一段时间内对输入进行采样,我知道我有连接/断开连接,但什么也找不到。我通过在开发环境中强制我的设备通过MQTT连接来解决这个问题,现在我看到它连接和断开连接 不幸的是,我不能在生产中使用MQTT
有人知道连接/断开事件是否仅在MQTT而不是AMQP中可能发生吗?如果您只想获取设备连接状态,请使用REST 另外,这里有一个在线工具来监控设备的连接状态
我们发明了一种数据流来确定“设备状态”。我们创建了一个流分析作业,连接到
操作监控
(这是物联网输入定义中的端点
类型)。我们将流分析查询选择到一个ServiceBus队列中。我们有一个WebJob处理队列并更新一个持久存储(SQL表、Azure表、您的选择),我们在其中记录状态。然后,UI(或WebAPI控制器)可以检查该持久存储
到目前为止,我们已将所有监控类别
(在IOT Hub门户刀片中)设置为Verbose
,但稍后可能会将其调低
我们确实获得了数据。你能吗?这就是我正在使用的另一种方法,问题是你必须每“x”分钟轮询一次,才能获得连接/断开的数据。然而,如果IoT监控工作正常,我会在设备断开连接时接收事件。HTTP呢?HTTP根本不显示连接状态,如果我通过HTTP连接设备,则设备资源管理器始终将其显示为断开连接。HTTP完全不显示连接/连接状态。但AMQP和MQTT对我有效……你们的物联网集线器定价层是什么?您在创建中心时选择了什么位置?中心位于西欧,tier是免费的,因为我们仍处于开发阶段。我的是S1 tier。根据您的评论,我也尝试了免费层,但我甚至无法成功保存监控类别设置。我将向内部相关团队报告此问题。