Пример #1
0
        private async Task ProcessTelemetryMessageAndWriteToEventHub(
            JObject telemetry,
            EventHubHelper eventHubHelper,
            ILogger log,
            object tenant,
            object deviceId,
            TelemetryTimestamp telemetryTimestamp)
        {
            try
            {
                if (telemetry == null || !telemetry.HasValues)
                {
                    throw new Exception("Telemetry message had no values to write to cosmos");
                }

                /*
                 * telemetry[DeviceTelemetryKeyConstants.DeviceId] = deviceId.ToString();
                 *
                 * telemetry.Add(DeviceTelemetryKeyConstants.DateTimeReceived, telemetryTimestamp.DateTime.ToString());
                 * telemetry.Add(DeviceTelemetryKeyConstants.TimeReceived, telemetryTimestamp.EpochTimestamp);
                 * telemetry.Add(DeviceTelemetryKeyConstants.Schema, DeviceTelemetryKeyConstants.MessageSchema);
                 */

                JObject telemetryData = new JObject();
                telemetryData.Add(DeviceTelemetryKeyConstants.Data, telemetry);
                telemetryData.Add(DeviceTelemetryKeyConstants.DeviceId, deviceId.ToString());
                telemetryData.Add(DeviceTelemetryKeyConstants.DateTimeReceived, telemetryTimestamp.DateTime);
                telemetryData.Add(DeviceTelemetryKeyConstants.TimeReceived, telemetryTimestamp.EpochTimestamp);
                telemetryData.Add(DeviceTelemetryKeyConstants.Schema, DeviceTelemetryKeyConstants.MessageSchema);

                // Save the telemetry message to EventHub for further processing
                var byteMessage = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(telemetryData));
                var eventData   = new Azure.Messaging.EventHubs.EventData(byteMessage);
                eventData.Properties.Add("deviceid", deviceId.ToString());

                await eventHubHelper.SendMessageToEventHub($"{tenant}-telemetry", new Azure.Messaging.EventHubs.EventData[] { eventData });
            }
            catch (Exception ex)
            {
                this.success = false;
                log.LogError($"Error occurrred : {ex.Message}");
            }
        }
Пример #2
0
        private async Task SendToEventHub(List <Alarm> alarms)
        {
            try
            {
                ConcurrentBag <Azure.Messaging.EventHubs.EventData> events = new ConcurrentBag <Azure.Messaging.EventHubs.EventData>();

                Parallel.ForEach(alarms, alarm =>
                {
                    JObject alarmMappingJson = new JObject();
                    alarmMappingJson.Add("id", alarm.Id);
                    alarmMappingJson.Add("created", alarm.DateCreated.ToUnixTimeMilliseconds());
                    alarmMappingJson.Add("modified", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
                    alarmMappingJson.Add("description", alarm.Description);
                    alarmMappingJson.Add("groupId", alarm.GroupId);
                    alarmMappingJson.Add("deviceId", alarm.DeviceId);
                    alarmMappingJson.Add("status", alarm.Status);
                    alarmMappingJson.Add("ruleId", alarm.RuleId);
                    alarmMappingJson.Add("ruleSeverity", alarm.RuleSeverity);
                    alarmMappingJson.Add("ruleDescription", alarm.RuleDescription);
                    alarmMappingJson.Add("isDeleted", alarm.IsDeleted);
                    var byteMessage            = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(alarmMappingJson));
                    var deviceMappingEventData = new Azure.Messaging.EventHubs.EventData(byteMessage);
                    events.Add(deviceMappingEventData);
                });

                try
                {
                    var            eventHubConnString = this.appConfigurationClient.GetValue($"tenant:{this.TenanId}:eventHubConn");
                    EventHubHelper eventHubHelper     = new EventHubHelper(eventHubConnString);

                    await eventHubHelper.SendMessageToEventHub($"{this.TenanId}-alerts", events.ToArray());
                }
                catch (Exception e)
                {
                    throw new Exception($"Unable to Send the alert data models to Entity Hub. TenantId: {this.TenanId}", e);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"Unable to Send the alert data models to Entity Hub. TenantId: {this.TenanId}", e);
            }
        }
 public bool TryAdd(Azure.Messaging.EventHubs.EventData eventData)
 {
     throw null;
 }
 public ProcessEventArgs(Azure.Messaging.EventHubs.Consumer.PartitionContext partition, Azure.Messaging.EventHubs.EventData data, System.Func <System.Threading.CancellationToken, System.Threading.Tasks.Task> updateCheckpointImplementation, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
 {
     throw null;
 }
 public PartitionEvent(Azure.Messaging.EventHubs.Consumer.PartitionContext partition, Azure.Messaging.EventHubs.EventData data)
 {
     throw null;
 }
        /// <summary>
        /// Parse and print Media Services events.
        /// </summary>
        /// <param name="eventData">Event Hub event data.</param>
        private void PrintJobEvent(Azure.Messaging.EventHubs.EventData eventData)
        {
            // data = Encoding.UTF8.GetString(eventData.EventBody);
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };

            var jsonArray = JsonSerializer.Deserialize <Azure.Messaging.EventGrid.EventGridEvent[]>(eventData.EventBody.ToString(), options);

            foreach (Azure.Messaging.EventGrid.EventGridEvent e in jsonArray)
            {
                var    subject         = e.Subject;
                var    topic           = e.Topic;
                var    eventType       = e.EventType;
                var    eventTime       = e.EventTime;
                string eventSourceName = Regex.Replace(subject, @"^.*/", "");


                if (eventSourceName != jobName && eventSourceName != liveEventName)
                {
                    return;
                }

                // Log the time and type of event
                Console.WriteLine($"{eventTime}   EventType: {eventType}");

                switch (eventType)
                {
                // Job state change events
                case "Microsoft.Media.JobStateChange":
                case "Microsoft.Media.JobScheduled":
                case "Microsoft.Media.JobProcessing":
                case "Microsoft.Media.JobCanceling":
                case "Microsoft.Media.JobFinished":
                case "Microsoft.Media.JobCanceled":
                case "Microsoft.Media.JobErrored":
                {
                    MediaJobStateChangeEventData jobEventData = JsonSerializer.Deserialize <MediaJobStateChangeEventData>(e.Data.ToString(), options);

                    Console.WriteLine($"Job state changed for JobId: {eventSourceName} PreviousState: {jobEventData.PreviousState} State: {jobEventData.State}");

                    // For final states, send a message to notify that the job has finished.
                    if (eventType == "Microsoft.Media.JobFinished" || eventType == "Microsoft.Media.JobCanceled" || eventType == "Microsoft.Media.JobErrored")
                    {
                        // Job finished, send a message.
                        if (jobWaitingEvent != null)
                        {
                            jobWaitingEvent.Set();
                        }
                    }
                }
                break;

                // Job output state change events
                case "Microsoft.Media.JobOutputStateChange":
                case "Microsoft.Media.JobOutputScheduled":
                case "Microsoft.Media.JobOutputProcessing":
                case "Microsoft.Media.JobOutputCanceling":
                case "Microsoft.Media.JobOutputFinished":
                case "Microsoft.Media.JobOutputCanceled":
                case "Microsoft.Media.JobOutputErrored":
                {
                    MediaJobOutputStateChangeEventData jobEventData = JsonSerializer.Deserialize <MediaJobOutputStateChangeEventData>(e.Data.ToString(), options);

                    Console.WriteLine($"Job output state changed for JobId: {eventSourceName} PreviousState: {jobEventData.PreviousState} " +
                                      $"State: {jobEventData.Output.State} Progress: {jobEventData.Output.Progress}%");
                }
                break;

                // Job output progress event
                case "Microsoft.Media.JobOutputProgress":
                {
                    MediaJobOutputProgressEventData jobEventData = JsonSerializer.Deserialize <MediaJobOutputProgressEventData>(e.Data.ToString(), options);

                    Console.WriteLine($"Job output progress changed for JobId: {eventSourceName} Progress: {jobEventData.Progress}%");
                }
                break;

                // LiveEvent Stream-level events
                // See the following documentation for updated schemas  - https://docs.microsoft.com/azure/media-services/latest/monitoring/media-services-event-schemas#live-event-types
                case "Microsoft.Media.LiveEventConnectionRejected":
                {
                    MediaLiveEventConnectionRejectedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventConnectionRejectedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent connection rejected. IngestUrl: {liveEventData.IngestUrl} StreamId: {liveEventData.StreamId} " +
                                      $"EncoderIp: {liveEventData.EncoderIp} EncoderPort: {liveEventData.EncoderPort}");
                }
                break;

                case "Microsoft.Media.LiveEventEncoderConnected":
                {
                    MediaLiveEventEncoderConnectedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventEncoderConnectedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent encoder connected. IngestUrl: {liveEventData.IngestUrl} StreamId: {liveEventData.StreamId} " +
                                      $"EncoderIp: {liveEventData.EncoderIp} EncoderPort: {liveEventData.EncoderPort}");
                }
                break;

                case "Microsoft.Media.LiveEventEncoderDisconnected":
                {
                    MediaLiveEventEncoderDisconnectedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventEncoderDisconnectedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent encoder disconnected. IngestUrl: {liveEventData.IngestUrl} StreamId: {liveEventData.StreamId} " +
                                      $"EncoderIp: {liveEventData.EncoderIp} EncoderPort: {liveEventData.EncoderPort}");
                }
                break;

                // LiveEvent Track-level events
                // See the following documentation for updated schemas - https://docs.microsoft.com/azure/media-services/latest/monitoring/media-services-event-schemas#live-event-types
                case "Microsoft.Media.LiveEventIncomingDataChunkDropped":
                {
                    MediaLiveEventIncomingDataChunkDroppedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventIncomingDataChunkDroppedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent data chunk dropped. LiveEventId: {eventSourceName} ResultCode: {liveEventData.ResultCode}");
                }
                break;

                case "Microsoft.Media.LiveEventIncomingStreamReceived":
                {
                    MediaLiveEventIncomingStreamReceivedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventIncomingStreamReceivedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent incoming stream received. IngestUrl: {liveEventData.IngestUrl} EncoderIp: {liveEventData.EncoderIp} " +
                                      $"EncoderPort: {liveEventData.EncoderPort}");
                }
                break;

                case "Microsoft.Media.LiveEventIncomingStreamsOutOfSync":
                {
                    //MediaLiveEventIncomingStreamsOutOfSyncEventData eventData = JsonSerializer.Deserialize<MediaLiveEventIncomingStreamsOutOfSyncEventData>(e.Data.ToString(), options);;
                    Console.WriteLine($"LiveEvent incoming audio and video streams are out of sync. LiveEventId: {eventSourceName}");
                }
                break;

                case "Microsoft.Media.LiveEventIncomingVideoStreamsOutOfSync":
                {
                    //MediaLiveEventIncomingVideoStreamsOutOfSyncEventData eventData =JsonSerializer.Deserialize<MediaLiveEventIncomingVideoStreamsOutOfSyncEventData>(e.Data.ToString(), options);;
                    Console.WriteLine($"LeveEvent incoming video streams are out of sync. LiveEventId: {eventSourceName}");
                }
                break;

                case "Microsoft.Media.LiveEventIngestHeartbeat":
                {
                    MediaLiveEventIngestHeartbeatEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventIngestHeartbeatEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent ingest heart beat. TrackType: {liveEventData.TrackType} State: {liveEventData.State} Healthy: {liveEventData.Healthy}");
                }
                break;

                case "Microsoft.Media.LiveEventTrackDiscontinuityDetected":
                {
                    MediaLiveEventTrackDiscontinuityDetectedEventData liveEventData = JsonSerializer.Deserialize <MediaLiveEventTrackDiscontinuityDetectedEventData>(e.Data.ToString(), options);
                    Console.WriteLine($"LiveEvent discontinuity in the incoming track detected. LiveEventId: {eventSourceName} TrackType: {liveEventData.TrackType} " +
                                      $"Discontinuity gap: {liveEventData.DiscontinuityGap}");
                }
                break;

                case "Microsoft.Media.LiveEventChannelArchiveHeartbeatEvent":
                {
                    Console.WriteLine($"LiveEvent archive heartbeat event detected. LiveEventId: {eventSourceName}");
                    Console.WriteLine(e.Data.ToString());
                }
                break;
                }
            }
        }
Пример #7
0
        public async Task SaveDeviceTwinOperationAsync(string eventData, ILogger log, string tenant, string deviceId, string operationType, TelemetryTimestamp timeStamp, EventHubHelper eventHubHelper)
        {
            try
            {
                string  deviceTwin     = eventData;
                Twin    twin           = null;
                JObject deviceTwinJson = new JObject();
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceId, deviceId.ToString());
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.TimeStamp, timeStamp.DateTime);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.TimeReceived, timeStamp.EpochTimestamp);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.EventOpType, operationType);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.IsDeleted, false);

                if (operationType.ToString().Equals("createDeviceIdentity"))
                {
                    deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, timeStamp.DateTime);

                    try
                    {
                        twin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString());

                        deviceTwin = twin.ToJson();
                    }
                    catch (Exception e)
                    {
                        log.LogError($"Unable to fetch DeviceId: {deviceId} device twin from iothub of tenant: {tenant}.", e);
                    }
                }
                else
                {
                    JObject         previousTwin = null;
                    KustoOperations kustoClient  = await KustoOperations.GetClientAsync();

                    string kustoQuery     = $"DeviceTwin | where DeviceId == \"{deviceId}\" | summarize arg_max(TimeStamp, *) by DeviceId | where IsDeleted == false";
                    var    deviceTwinList = await kustoClient.QueryAsync <DeviceTwinModel>($"IoT-{tenant}", kustoQuery, null);

                    DeviceTwinModel preDeviceTwin = deviceTwinList.FirstOrDefault();

                    if (preDeviceTwin != null)
                    {
                        previousTwin = preDeviceTwin.Twin;
                        deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, preDeviceTwin.DeviceCreatedDate);
                    }
                    else
                    {
                        deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, default(DateTime)); // Set Device Created Date to Default if twin is not present in storage.

                        try
                        {
                            twin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString());

                            if (twin != null)
                            {
                                previousTwin = JObject.Parse(twin.ToJson());
                            }
                        }
                        catch (Exception e)
                        {
                            log.LogError($"Unable to fetch DeviceId: {deviceId} device twin from iothub of tenant: {tenant}.", e);
                        }
                    }

                    switch (operationType)
                    {
                    case "deviceConnected":
                        if (preDeviceTwin != null)
                        {
                            previousTwin["connectionState"]  = "Connected";
                            previousTwin["lastActivityTime"] = timeStamp.DateTime;
                        }

                        break;

                    case "deviceDisconnected":
                        if (preDeviceTwin != null)
                        {
                            previousTwin["connectionState"]  = "Disconnected";
                            previousTwin["lastActivityTime"] = timeStamp.DateTime;
                        }

                        break;

                    case "updateTwin":
                        if (preDeviceTwin != null)
                        {
                            JObject twinFragment = JObject.Parse(eventData);
                            previousTwin = previousTwin.UpdateJson(twinFragment);
                        }
                        else
                        {
                            previousTwin = JObject.Parse(eventData);
                        }

                        break;

                    case "deleteDeviceIdentity":
                        deviceTwinJson[DeviceTelemetryKeyConstants.IsDeleted] = true;
                        break;

                    default:
                        break;
                    }

                    deviceTwin = previousTwin?.ToString();
                }

                deviceTwinJson.Add(DeviceTelemetryKeyConstants.Data, deviceTwin);

                // Save the Device Twin Data to EventHub for further processing
                var byteMessage         = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(deviceTwinJson));
                var eventDeviceTwinData = new Azure.Messaging.EventHubs.EventData(byteMessage);
                eventDeviceTwinData.Properties.Add("deviceid", deviceId.ToString());

                await eventHubHelper.SendMessageToEventHub($"{tenant}-devicetwin", new Azure.Messaging.EventHubs.EventData[] { eventDeviceTwinData });
            }
            catch (Exception exception)
            {
                throw new ApplicationException($"Save Device Twin operation failed: {exception}, operation type: {operationType}, tenant: {tenant}, deviceId {deviceId}");
            }
        }