问题描述
我有一个将设备连接到的Azure IoT中心.我想启用监视功能以监视与集线器连接和断开连接的设备.
I have an Azure IoT hub that I connect devices to. I want to enable monitoring to monitor devices connecting and disconnecting from the hub.
我已为Iot集线器的监视类别中的连接上启用详细":
I've enabled Verbose on Connections in the monitoring categories for my Iot hub:
我的设备连接到我的集线器并显示在设备资源管理器中:
My devices connect to my Hub and show in Device Explorer:
然后,我设置了一个Azure函数,以将我的数据从操作监控日志记录到Azure SQL数据库:
I then have an Azure Function set to log my data from the Operations Monitoring to an Azure SQL db:
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; }
}
如果在操作监控中启用设备标识操作,则会记录创建事件.因此,我确信该功能的输入是正确的.但是,为连接什么都没有创建??
If I enable Device Identity Operations in Operations Monitoring, I get create events being logged. So I'm confident the inputs to the function is correct. However, nothing is ever created for Connections???
我还可以向已连接的设备发送消息.我只是没有看到任何有关连接/断开连接的事件.
I can also send messages to my connected devices fine. I'm just seeing no events for connections / disconnections.
我还尝试过创建流分析并在一段时间内对输入进行采样,直到我知道自己有连接/断开连接,却一无所获.
I've also tried creating a Stream Analytics and sampling the input for a period where I know I have connections / disconnections and nothing is being found.
推荐答案
我已通过强制设备在开发环境中通过MQTT连接来解决此问题,现在我看到它可以连接和断开连接.
I have resolved this by forcing my device to connect over MQTT in the dev environment and now I see it connecting and disconnecting.
不幸的是,我无法在生产中使用MQTT.
Unfortunately I can't use MQTT in production.
有人知道是否只有在MQTT而非AMQP中才可能发生连接/断开事件吗?
Does anyone know if Connection / Disconnection events are only possible in MQTT as opposed to AMQP?
这篇关于Azure IoT中心操作监控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!