示例#1
0
        public IActionResult Dashboard(Guid deviceId, [FromRoute] DateSelectionViewModel dateSelectionViewModel)
        {
            var device = _context.Device.AsNoTracking().SingleOrDefault(r => r.DeviceId == deviceId);

            if (device == null)
            {
                return(View("Index", new DeviceSearchViewModel
                {
                    ErrorMessage = string.Format("Device: {0} niet gevonden.", deviceId),
                    DeviceId = deviceId
                }));
            }

            //Quick null checks. also makes it so the default period is 24hrs
            var fromDate = dateSelectionViewModel.FromDate != null
                ? EpochTimeHelper.EpochToDateTime(dateSelectionViewModel.FromDate.Value)
                : DateTime.Now;

            var toDate = dateSelectionViewModel.ToDate != null
                ? EpochTimeHelper.EpochToDateTime(dateSelectionViewModel.ToDate.Value)
                : DateTime.Now;


            var updatesForDevice = _context.WeatherUpdate.AsNoTracking()
                                   .Where(r => r.DeviceId == deviceId && r.TimeStamp < toDate)
                                   .OrderByDescending(r => r.TimeStamp)
                                   .Take(20)                            //Only take 20 max.
                                   .OrderBy(r => r.TimeStamp).ToList(); // order back for chartjs

            var mostRecentUpdate = _context.WeatherUpdate.AsNoTracking().Where(r => r.DeviceId == deviceId)
                                   .OrderByDescending(r => r.TimeStamp).FirstOrDefault();

            //Construct viewmodel
            var dashboardViewModel = new DeviceDashboardViewModel
            {
                Device        = device,
                RecentUpdates = updatesForDevice,
                LastUpdate    = mostRecentUpdate
            };

            return(View("Dashboard", dashboardViewModel));
        }
        public async Task <IActionResult> GetUpdateFromDevice([FromRoute] Guid id, [FromQuery] DateSelectionViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            bool deviceExists = _context.Device.Any(d => d.DeviceId == id);

            if (!deviceExists)
            {
                return(NotFound());
            }

            //Quick null checks. also makes it so the default period is 24hrs
            var fromDate = model.FromDate != null
                ? EpochTimeHelper.EpochToDateTime(model.FromDate.Value)
                : DateTime.Now.Date;

            var toDate = model.ToDate != null
                ? EpochTimeHelper.EpochToDateTime(model.ToDate.Value)
                : DateTime.Now.Date;


            if ((toDate - fromDate).TotalHours > 72)
            {
                return(BadRequest());
            }

            //Truncating time in the query.
            var updatesFromDevice = await _context.WeatherUpdate
                                    .Where(u => u.DeviceId == id && fromDate.Date <= u.TimeStamp.Date && toDate.Date >= u.TimeStamp.Date)
                                    .OrderByDescending(u => u.TimeStamp)
                                    .ToListAsync();

            return(Ok(updatesFromDevice));
        }
        public async Task <IActionResult> GetWeatherUpdate([FromQuery] DateSelectionViewModel model)
        {
            //Quick null checks. also makes it so the default period is 24hrs
            var fromDate = model.FromDate != null
                ? EpochTimeHelper.EpochToDateTime(model.FromDate.Value)
                : DateTime.Now.Date;

            var toDate = model.ToDate != null
                ? EpochTimeHelper.EpochToDateTime(model.ToDate.Value)
                : DateTime.Now.Date;

            if ((toDate - fromDate).TotalHours > 24)
            {
                return(BadRequest());
            }

            //Truncating time in the query.
            var updatesWithingTimeSpan = await _context.WeatherUpdate
                                         .Where(u => fromDate.Date <= u.TimeStamp.Date && toDate.Date >= u.TimeStamp.Date)
                                         .OrderByDescending(u => u.TimeStamp)
                                         .ToListAsync();

            return(Ok(updatesWithingTimeSpan));
        }