Exemplo n.º 1
0
 public static Observations GetMaxTemperatureObservation(int deviceId)
 {
     using (VaderContext ctx = new VaderContext())
     {
         return ctx.Observations.Where(o => o.DeviceId == deviceId).OrderByDescending(t => t.TemperatureInCelsius).Take(1).SingleOrDefault();
     }
 }
Exemplo n.º 2
0
        public void ImportObservations()
        {
            var fileName = @"D:\Development\Observations.csv";

            using (StreamReader reader = new StreamReader(File.OpenRead(fileName)))
            {
                using (VaderContext ctx = new VaderContext())
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        var parts = line.Split(';');

                        var deviceId = int.Parse(parts[1].Replace("\"", string.Empty));
                        var datetime = DateTime.Parse(parts[2].Replace("\"", string.Empty));
                        var temperature = decimal.Parse(parts[3].Replace("\"", string.Empty).Replace(".", ","));

                        var observation = new Observations
                        {
                            DeviceId = deviceId,
                            ObservationTime = datetime,
                            TemperatureInCelsius = temperature
                        };

                        ctx.Observations.Add(observation);
                        ctx.SaveChanges();
                    }
                }
            }
        }
Exemplo n.º 3
0
 public static ObservationRange GetLastDayObservations(int deviceId)
 {
     using (VaderContext ctx = new VaderContext())
     {
         var compareDate = DateTime.UtcNow.AddDays(-1);
         return ObservationRange.BuildObject(ctx.Observations.Where(t => t.DeviceId == deviceId && t.ObservationTime >= compareDate).ToList());
     }
 }
Exemplo n.º 4
0
        public static Dictionary<double, decimal?> GetAverageTemperatures(int deviceId, DateTime start, DateTime end)
        {
            using (VaderContext ctx = new VaderContext())
            {
                var sevenDaysBackFromStart = start.AddDays(-7);
                var allObservationsInRange = ctx.Observations.Where(d => d.DeviceId == deviceId && d.ObservationTime > sevenDaysBackFromStart && d.ObservationTime < end).ToList();

                var perHour = new Dictionary<double, decimal?>();

                for (var t = start; t <= end; t = t.AddHours(6))
                {
                    var sevenDaysBack = t.AddDays(-7);

                    var observationsSevenDaysBack = allObservationsInRange.Where(o => o.ObservationTime >= sevenDaysBack && o.ObservationTime < t).ToList();

                    var averageTemperature = GetAverageTemperature(observationsSevenDaysBack);

                    perHour.Add(HighChartsDateTimeHelper.GetUnixTime(t), averageTemperature);
                }

                return perHour;
            }
        }
Exemplo n.º 5
0
        public static void AddTemperatureObservation(int deviceId, decimal temperature)
        {
            using (VaderContext ctx = new VaderContext())
            {
                var observation = new Observations()
                {
                    DeviceId = deviceId,
                    ObservationTime = DateTime.UtcNow,
                    TemperatureInCelsius = temperature
                };

                ctx.Observations.Add(observation);
                ctx.SaveChanges();
            }
        }
Exemplo n.º 6
0
 public static ObservationRange GetAllObservations(int deviceId)
 {
     using (VaderContext ctx = new VaderContext())
     {
         return ObservationRange.BuildObject(ctx.Observations.Where(t => t.DeviceId == deviceId).OrderBy(t => t.ObservationTime).ToList());
     }
 }