public DeviceListLocationsModel ExtractLocationsData(List <dynamic> devices) { var result = new DeviceListLocationsModel(); // Initialize defaults to opposite extremes to ensure mins and maxes are beyond any actual values double minLat = double.MaxValue; double maxLat = double.MinValue; double minLong = double.MaxValue; double maxLong = double.MinValue; var locationList = new List <DeviceLocationModel>(); foreach (dynamic device in devices) { dynamic props = DeviceSchemaHelper.GetDeviceProperties(device); if (props.Longitude == null || props.Latitude == null) { continue; } double latitude; double longitude; try { latitude = DeviceSchemaHelper.GetDeviceProperties(device).Latitude; longitude = DeviceSchemaHelper.GetDeviceProperties(device).Longitude; } catch (FormatException) { continue; } var location = new DeviceLocationModel() { DeviceId = DeviceSchemaHelper.GetDeviceID(device), Longitude = longitude, Latitude = latitude }; locationList.Add(location); if (longitude < minLong) { minLong = longitude; } if (longitude > maxLong) { maxLong = longitude; } if (latitude < minLat) { minLat = latitude; } if (latitude > maxLat) { maxLat = latitude; } } if (locationList.Count == 0) { // reinitialize bounds to center on Seattle area if no devices minLat = 17.431978; maxLat = 17.431978; minLong = 78.343696; maxLong = 78.343696; } double offset = 0.05; result.DeviceLocationList = locationList; result.MinimumLatitude = minLat - offset; result.MaximumLatitude = maxLat + offset; result.MinimumLongitude = minLong - offset; result.MaximumLongitude = maxLong + offset; return(result); }
public async Task <HttpResponseMessage> GetLatestAlertHistoryAsync() { Func <Task <AlertHistoryResultsModel> > loadHistoryItems = async() => { // Dates are stored internally as UTC and marked as such. // When parsed, they'll be made relative to the server's // time zone. This is only in an issue on servers machines, // not set to GMT. DateTime currentTime = DateTime.Now; var historyItems = new List <AlertHistoryItemModel>(); var deviceModels = new List <AlertHistoryDeviceModel>(); var resultsModel = new AlertHistoryResultsModel(); IEnumerable <AlertHistoryItemModel> data = await _alertsLogic.LoadLatestAlertHistoryAsync( currentTime.Subtract(CautionAlertMaxDelta), DISPLAYED_HISTORY_ITEMS); if (data != null) { historyItems.AddRange(data); //get alert history List <DeviceModel> devices = await this.LoadAllDevicesAsync(); if (devices != null) { DeviceListLocationsModel locationsModel = _deviceLogic.ExtractLocationsData(devices); if (locationsModel != null) { resultsModel.MaxLatitude = locationsModel.MaximumLatitude; resultsModel.MaxLongitude = locationsModel.MaximumLongitude; resultsModel.MinLatitude = locationsModel.MinimumLatitude; resultsModel.MinLongitude = locationsModel.MinimumLongitude; if (locationsModel.DeviceLocationList != null) { Func <string, DateTime?> getStatusTime = _deviceTelemetryLogic.ProduceGetLatestDeviceAlertTime(historyItems); foreach (DeviceLocationModel locationModel in locationsModel.DeviceLocationList) { if ((locationModel == null) || string.IsNullOrWhiteSpace(locationModel.DeviceId)) { continue; } var deviceModel = new AlertHistoryDeviceModel() { DeviceId = locationModel.DeviceId, Latitude = locationModel.Latitude, Longitude = locationModel.Longitude }; DateTime?lastStatusTime = getStatusTime(locationModel.DeviceId); if (lastStatusTime.HasValue) { TimeSpan deltaTime = currentTime - lastStatusTime.Value; if (deltaTime < CriticalAlertMaxDelta) { deviceModel.Status = AlertHistoryDeviceStatus.Critical; } else if (deltaTime < CautionAlertMaxDelta) { deviceModel.Status = AlertHistoryDeviceStatus.Caution; } } deviceModels.Add(deviceModel); } } } } } resultsModel.Data = historyItems.Take(DISPLAYED_HISTORY_ITEMS).ToList(); resultsModel.Devices = deviceModels; resultsModel.TotalAlertCount = historyItems.Count; resultsModel.TotalFilteredCount = historyItems.Count; return(resultsModel); }; return(await GetServiceResponseAsync <AlertHistoryResultsModel>(loadHistoryItems, false)); }