Exemplo n.º 1
0
        public static Dictionary <string, List <ReadingBase> > GetDailySummary(WeatherValueType valueType, int deviceId, DateTime startDate, DateTime endDate)
        {
            var summaryList = new Dictionary <string, List <ReadingBase> >();

            summaryList["Average"] = new List <ReadingBase>();
            summaryList["Minimum"] = new List <ReadingBase>();
            summaryList["Maximum"] = new List <ReadingBase>();

            for (var year = startDate.Year; year <= endDate.Year; year++)
            {
                using (var archiveData = new WeatherArchiveData(year))
                {
                    var groupList = archiveData.Readings
                                    .Where(r => r.ReadTime >= startDate &&
                                           r.ReadTime <= endDate &&
                                           r.DeviceId == deviceId &&
                                           r.Type == (int)valueType)
                                    .GroupBy(r => DbFunctions.TruncateTime(r.ReadTime))
                                    .Select(g => new
                    {
                        ReadTime = g.Key,
                        Readings = g.Select(r => r.Value)
                    }).ToList();

                    summaryList["Average"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Average())));
                    summaryList["Minimum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Min())));
                    summaryList["Maximum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Max())));
                }
            }

            return(summaryList);
        }
Exemplo n.º 2
0
        private static List <ReadingBase> LoadHistory(WeatherValueType valueType, int deviceId, DateTimeOffset start, DateTimeOffset end)
        {
            var history = new List <ReadingBase>();

            for (var year = start.Year; year <= end.Year; year++)
            {
                using (var archiveData = new WeatherArchiveData(year))
                {
                    var readings = archiveData.Readings;

                    var yearlyHistory = readings.Where(r => r.DeviceId == deviceId && r.Type == (int)valueType && r.ReadTime >= start && r.ReadTime <= end).ToList();

                    history.AddRange(yearlyHistory.Select(r => ReadingBase.CreateReading(valueType, r.ReadTime.DateTime, r.Value)));
                }
            }

            return(history);
        }
Exemplo n.º 3
0
        internal void SetValue(double value, DateTime timeStamp, bool save)
        {
            // Set the current value
            Current.SetValue(value, timeStamp);

            if (save)
            {
                // Save the reading
                using (var weatherArchiveData = new WeatherArchiveData(timeStamp.Year))
                {
                    var reading = new ReadingData
                    {
                        DeviceId = _ownerDevice.Id,
                        Type     = (int)ValueType,
                        Value    = value,
                        ReadTime = timeStamp
                    };

                    weatherArchiveData.Readings.Add(reading);
                    weatherArchiveData.SaveChanges();
                }
            }
        }