2017-04-19 5 views
0

장치를 연결하는 Azure IoT 허브가 있습니다. 허브에 연결하고 연결을 끊는 장치를 모니터링하도록 모니터링 할 수있게하려고합니다.Azure IoT 허브 작동 모니터링

내 여잔 허브에 대한 모니터링 범주에 연결에 대한 자세한 정보를 설정 한 :

enter image description here

: 장치 탐색기에서 enter image description here

내 장치

내 허브에 연결하고 쇼 그런 다음 작업 모니터링에서 Azure SQL 데이터베이스로 데이터를 기록하도록 Azure 기능을 설정했습니다.

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; } 
} 

Operations Monitoring에서 장치 ID 작업을 사용하도록 설정하면 만들기 이벤트가 기록됩니다. 그래서 함수에 대한 입력이 정확하다고 확신합니다. 그러나, 아무것도 연결을 위해 이제까지 창조된다?

연결된 장치로 메시지를 보낼 수도 있습니다. 연결/연결 해제 이벤트가 표시되지 않습니다.

또한 스트림 애널리틱스를 만들고 연결/연결 해제가 있고 아무 것도 발견되지 않는 기간 동안 입력을 샘플링했습니다.

+1

[iothub-explorer 도구를 사용하여 연결/연결 해제 이벤트를 캡처 할 수 있습니까] (https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-operations-monitoring#view-events))? –

+0

그게 제가 대안을 사용하고있는 문제입니다. 연결/연결 해제를 위해 매 x 분마다 폴링해야합니다.반면 IoT 모니터링이 제대로 작동하면 장치가 분리 될 때 이벤트가 수신됩니다. – Stuart

답변

0

dev 디바이스 환경에서 MQTT를 통해 디바이스를 강제 연결하면이 문제가 해결되어 이제 연결 및 연결 해제를 볼 수 있습니다.

아쉽게도 프로덕션 환경에서는 MQTT를 사용할 수 없습니다.

AMQP가 아닌 MQTT에서만 Connection/Disconnection 이벤트가 가능하다는 것을 아는 사람이 있습니까?

+0

HTTP는 무엇입니까? –

+0

HTTP는 연결 상태를 전혀 표시하지 않습니다. HTTP 장치를 통해 장치를 연결하면 장치 탐색기는 항상 연결이 끊어진 것으로 표시합니다. – Stuart

+0

HTTP에는 연결/연결 상태가 전혀 표시되지 않습니다. 그러나 AMQP와 MQTT는 나를 위해 작동합니다 ... 어떤 종류의 IoT 허브 가격 책정 단계입니까? 허브를 만들 때 어떤 위치를 선택 했습니까? –

0

https://iotdevtool.com/registry/

우리는 "장치 상태"를 결정하기 위해 데이터 흐름을 발명했다. 우리는 Operations monitoring에 연결된 유선 분석 작업을 만들었습니다 (IOT 입력 정의에서 Endpoint 유형). Stream Analytics 쿼리 SELECT INTO이 ServiceBus 큐에 있습니다. 상태를 적어 둔 영구 저장소 (SQL Table, Azure Table, pick)를 대기열에 넣고 업데이트하는 WebJob 처리가 있습니다. 그런 다음 UI (또는 WebAPI 컨트롤러)에서 영구 저장소를 검사 할 수 있습니다.

(IOT 허브 포털 블레이드의) Monitoring categories은 지금까지 Verbose으로 설정되었지만 나중에 다이얼링 할 수 있습니다.

데이터를 얻습니다.