public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("FeedbackProcessor: In ProcessEventsAsync");
            IEnumerable <LocationJerkModel> locationJerks = await _locationJerkLogic.LoadLatestLocationJerkInfoAsync();

            foreach (EventData message in messages)
            {
                try
                {
                    Trace.TraceInformation("FeedbackProcessor: {0} - Partition {1}", message.Offset, context.Lease.PartitionId);
                    this.LastMessageOffset = message.Offset;

                    string jsonString            = Encoding.UTF8.GetString(message.GetBytes());
                    IoTRawTelemetryModel rawData = null;
                    try
                    {
                        rawData = JsonConvert.DeserializeObject <IoTRawTelemetryModel>(jsonString);
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }

                    if (rawData != null)
                    {
                        CurrentDeviceLocationModel item = new CurrentDeviceLocationModel()
                        {
                            DeviceId   = rawData.DeviceId,
                            Latitude   = rawData.Latitude,
                            Longitude  = rawData.Longitude,
                            Altitude   = rawData.Altitude,
                            Heading    = rawData.Heading,
                            Speed      = rawData.Speed,
                            MapIsBound = rawData.MapIsBound
                        };

                        if (item.MapIsBound)
                        {
                            var feedbackObject = new FeedbackModel(item);

                            var userLocation = new GeoCoordinate()
                            {
                                Latitude  = item.Latitude,
                                Longitude = item.Longitude,
                                Altitude  = item.Altitude,
                                Speed     = item.Speed
                            };

                            feedbackObject.NearestJerks = GetNearestJerks(locationJerks, userLocation);

                            var     feedbackString = JsonConvert.SerializeObject(feedbackObject);
                            Message msg            = new Message(Encoding.ASCII.GetBytes(feedbackString));
                            try
                            {
                                await _serviceClient.SendAsync(item.DeviceId, msg);
                            }
                            catch (Exception ex)
                            {
                                if (!String.IsNullOrWhiteSpace(item.DeviceId))
                                {
                                    Trace.TraceError("FeedbackProcessor: Error in ProcessEventAsync -- Device: " + item.DeviceId + " -- " + ex.ToString());
                                }
                            }
                        }
                    }

                    ++_totalMessages;
                }
                catch (Exception e)
                {
                    Trace.TraceError("FeedbackProcessor: Error in ProcessEventAsync -- " + e.ToString());
                }
            }

            try
            {
                await context.CheckpointAsync();
            }
            catch (Exception ex)
            {
                Trace.TraceError(
                    "{0}{0}*** CheckpointAsync Exception - FeedbackProcessor.ProcessEventsAsync ***{0}{0}{1}{0}{0}",
                    Console.Out.NewLine,
                    ex);
            }

            if (this.IsClosed)
            {
                this.IsReceivedMessageAfterClose = true;
            }
        }
Exemple #2
0
        public async Task <HttpResponseMessage> GetDashboardDevicePaneDataAsync(string deviceId)
        {
            ValidateArgumentNotNullOrWhitespace("deviceId", deviceId);

            DashboardDevicePaneDataModel result = new DashboardDevicePaneDataModel()
            {
                DeviceId = deviceId
            };

            Func <Task <DashboardDevicePaneDataModel> > getTelemetry =
                async() =>
            {
                DeviceModel device = await _deviceLogic.GetDeviceAsync(deviceId);

                IEnumerable <LocationJerkModel> locationJerks = await _locationJerkLogic.LoadLatestLocationJerkInfoAsync();


                IList <DeviceTelemetryFieldModel> telemetryFields = null;

                try
                {
                    telemetryFields = _deviceLogic.ExtractTelemetry(device);
                    result.DeviceTelemetryFields = telemetryFields != null?
                                                   telemetryFields.ToArray() : null;
                }
                catch
                {
                    HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                    message.Content = new StringContent(
                        string.Format(Strings.InvalidDeviceTelemetryFormat, deviceId));
                    throw new HttpResponseException(message);
                }

                // Get Telemetry Summary
                DeviceTelemetrySummaryModel summaryModel;

                result.DeviceTelemetrySummaryModel = summaryModel =
                    await _deviceTelemetryLogic.LoadLatestDeviceTelemetrySummaryAsync(
                        deviceId, DateTime.Now.AddMinutes(-MAX_DEVICE_SUMMARY_AGE_MINUTES));

                if (summaryModel == null)
                {
                    result.DeviceTelemetrySummaryModel =
                        new DeviceTelemetrySummaryModel();
                }

                // Get Telemetry History
                IEnumerable <DeviceTelemetryModel> telemetryModels;
                DateTime minTime = DateTime.Now.AddMinutes(-MAX_DEVICE_SUMMARY_AGE_MINUTES);
                telemetryModels = await _deviceTelemetryLogic.LoadLatestDeviceTelemetryAsync(deviceId, telemetryFields, minTime);

                if (telemetryModels == null)
                {
                    result.DeviceTelemetryModels = new DeviceTelemetryModel[0];
                }
                else
                {
                    result.DeviceTelemetryModels =
                        telemetryModels.OrderBy(t => t.Timestamp).ToArray();
                }

                return(result);
            };

            return(await GetServiceResponseAsync <DashboardDevicePaneDataModel>(
                       getTelemetry,
                       false));
        }
Exemple #3
0
 public async Task <HttpResponseMessage> GetLocationsAsync()
 {
     return(await GetServiceResponseAsync(async() => (await _locationJerkLogic.LoadLatestLocationJerkInfoAsync())));
 }