public async Task <IActionResult> Index(int?measurementId) { if (!User.Identity.IsAuthenticated) { return(View("IndexPublic")); } _dataContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var model = new HomeViewModel(); model.Measurement = await _dataContext.Measurements .Include(m => m.SensorRoles) .Include(m => m.Calculators) .Where(m => measurementId == null || m.Id == measurementId) .OrderByDescending(m => m.IsActive) .FirstOrDefaultAsync(); if (model.Measurement == null) { return(View("IndexEmpty")); } model.Readings = _dataContext.GetReadings(model.Measurement.Id, null, 10) .OrderByDescending(r => r.Key) .ToList(); //model.ChartData = _dataContext.GetReadings(model.Measurement.Id, DateTime.Now.AddHours(-24), 10000); model.ChartData = _dataContext.GetReadings(model.Measurement.Id, null, 10000); var showOnChart = _calcProvider.GetTypes() .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null) .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().ShowOnChart) .Select(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name) .ToList(); showOnChart.AddRange(model.Measurement.SensorRoles.Select(r => r.RoleName)); model.CalculatorsOnChart = showOnChart.ToArray(); var labels = _calcProvider.GetTypes() .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null) .Select(t => new { Label = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().DisplayLabel, Name = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name }) .ToDictionary(k => k.Name, e => e.Label); foreach (var key in labels.Keys.ToList()) { if (string.IsNullOrEmpty(labels[key])) { labels[key] = key; } } model.Labels = labels; model.Calculators = _calcProvider.GetCalculators(); model.Statistics = _dataContext.GetMeasurementStats(model.Measurement.Id); model.Measurements = _dataContext.Measurements .OrderByDescending(m => m.IsActive) .ThenBy(m => m.Name) .Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString(), Selected = (m.Id == model.Measurement.Id) }) .ToList(); return(View(model)); }
public async Task <IActionResult> Report([FromBody] SharedModels.SensorReadings readings) { try { if (!IsDeviceKeyValid()) { return(BadRequest()); } var measurement = await _dataContext.Measurements .Include(m => m.SensorRoles) .ThenInclude(r => r.Sensor) .Include(m => m.Calculators) .FirstOrDefaultAsync(m => m.IsActive); if (measurement == null) { return(NotFound()); } if (measurement.SensorRoles.Count == 0) { return(NoContent()); } foreach (var sensorRole in measurement.SensorRoles) { var readingForSensor = readings.Readings.FirstOrDefault(r => r.SensorId == sensorRole.Sensor.Id); if (readingForSensor == null) { continue; } var reading = new SensorReading(); reading.ReadingTime = readings.ReadingTime; reading.SensorRole = sensorRole; reading.Value = readingForSensor.Reading; reading.Measurement = measurement; _dataContext.Readings.Add(reading); } var calcs = _calculatorProvider.GetCalculators(); foreach (var registeredCalculator in measurement.Calculators) { if (!calcs.ContainsKey(registeredCalculator.Name)) { continue; } var calc = calcs[registeredCalculator.Name]; calc.SetParameters(registeredCalculator.Parameters); if (calc.ReturnsReading) { var reading = new CalculatorReading(); reading.Calculator = registeredCalculator; reading.Measurement = measurement; reading.ReadingTime = readings.ReadingTime; reading.Value = calc.Calculate(readings, measurement); _dataContext.Readings.Add(reading); } else { calc.Calculate(readings, measurement); } } await _dataContext.SaveChangesAsync(); return(NoContent()); } catch (Exception ex) { _log.LogError(ex.ToString()); throw ex; } }
public async Task <IActionResult> Index(int?measurementId) { _pageContext.Title = "Home"; _pageContext.ActiveMenu = "Home"; if (!User.Identity.IsAuthenticated) { return(View("IndexPublic")); } if (!User.IsInRole("Administrator") && !User.IsInRole("PowerUser")) { return(View("IndexGuest")); } var model = new HomeViewModel(); model.Measurement = await _dataContext.Measurements .Include(m => m.SensorRoles) .Include(m => m.Calculators) .Where(m => measurementId == null || m.Id == measurementId) .OrderByDescending(m => m.IsActive) .FirstOrDefaultAsync(); if (model.Measurement == null) { return(View("IndexEmpty")); } var query = new ReadingsQuery { MeasurementId = model.Measurement.Id, PageSize = 10 }; model.Readings = _dataContext.GetReadings(query); query = new ReadingsQuery { MeasurementId = model.Measurement.Id, Ascending = true, PageSize = int.MaxValue }; model.ChartData = _dataContext.GetReadings(query); var showOnChart = _calcProvider.GetTypes() .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null) .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().ShowOnChart) .Select(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name) .ToList(); showOnChart.AddRange(model.Measurement.SensorRoles.Select(r => r.RoleName)); model.CalculatorsOnChart = showOnChart.ToArray(); var labels = _calcProvider.GetTypes() .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null) .Select(t => new { Label = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().DisplayLabel, Name = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name }) .ToDictionary(k => k.Name, e => e.Label ?? e.Name); model.Labels = labels; model.Calculators = _calcProvider.GetCalculators(); model.Statistics = _dataContext.GetMeasurementStats(model.Measurement.Id); model.Measurements = _dataContext.Measurements .OrderByDescending(m => m.IsActive) .ThenBy(m => m.Name) .Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString(), Selected = (m.Id == model.Measurement.Id) }) .ToList(); return(View(model)); }