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.UtcNow; var historyItems = new List <AlertHistoryItemModel>(); var locationModels = new List <AlertHistoryLocationModel>(); 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) { LocationJerkListModel locationJerkListModel = await _locationJerkLogic.GetLocationJerkListModel(); //DeviceListLocationsModel locationsModel = _deviceLogic.ExtractLocationsData(devices); if (locationJerkListModel != null) { resultsModel.MaxLatitude = locationJerkListModel.MaximumLatitude; resultsModel.MaxLongitude = locationJerkListModel.MaximumLongitude; resultsModel.MinLatitude = locationJerkListModel.MinimumLatitude; resultsModel.MinLongitude = locationJerkListModel.MinimumLongitude; if (locationJerkListModel.LocationJerkList != null) { //Func<string, DateTime?> getStatusTime = //_deviceTelemetryLogic.ProduceGetLatestDeviceAlertTime(historyItems); foreach (LocationJerkModel locationModel in locationJerkListModel.LocationJerkList) { if ((locationModel == null) || (locationModel.Latitude == null) || (locationModel.Longitude == null) || (locationModel.DeviceList == null)) { continue; } double maxJerk = GetMaximumJerkAtLocation(locationModel); var alertHistoryLocationModel = new AlertHistoryLocationModel() { Latitude = (double)locationModel.Latitude, Longitude = (double)locationModel.Longitude, TotalDeviceCount = locationModel.DeviceList.Count(), Status = locationModel.Status, MaxJerk = maxJerk, HighestJerks = GetHighestJerks(locationModel) }; //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; // } //} locationModels.Add(alertHistoryLocationModel); } } } } } resultsModel.Data = historyItems.Take(DISPLAYED_HISTORY_ITEMS).ToList(); resultsModel.JerkLocations = locationModels; resultsModel.TotalAlertCount = historyItems.Count; resultsModel.TotalFilteredCount = historyItems.Count; return(resultsModel); }; return(await GetServiceResponseAsync <AlertHistoryResultsModel>(loadHistoryItems, false)); }
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); List <dynamic> devices = await LoadAllDevicesAsync(); if (devices != null) { //Michael var deviceStatusBYD = await _bydErrorLogic.GetLastErrorsAsync(); //End 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; } } //Michael var statusDevice = deviceStatusBYD.Where(b => string.Compare(deviceModel.DeviceId, b.DeviceId, true) == 0).FirstOrDefault(); deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Unknown; if (statusDevice != null) { switch (statusDevice.StatusCode) { case 0: deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Error; break; case 1: deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Shutdown; break; case 2: deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Disconnected; break; case 3: deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_StandingBy; break; } } //End 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)); }