예제 #1
0
        public void SaveWateringData(WateringData wateringData)
        {
            bool lockTaken = false;

            Monitor.TryEnter(_lock, new TimeSpan(0, 2, 0), ref lockTaken);

            if (!lockTaken)
            {
                _logger.Warning("SaveWateringData: Could not lock");
                return;
            }

            using (var context = new WateringContext())
            {
                context.Waterings.Add(wateringData);
                context.SaveChanges();
            }
            Monitor.Exit(_lock);

            DateTime now = DateTime.Now;

            now = now.AddDays(-MaxHistoryDays);
            DeleteDataPointsFromDate(now);
            OnPropertyChanged();
        }
예제 #2
0
        public void SaveReadingPoint(Measurement readingPoint)
        {
            bool lockTaken = false;

            Monitor.TryEnter(_lock, new TimeSpan(0, 2, 0), ref lockTaken);

            if (!lockTaken)
            {
                _logger.Warning("SaveReadingData: Could not lock");
                return;
            }

            using (var context = new WateringContext())
            {
                context.Measurements.Add(readingPoint);
                context.SaveChanges();
            }

            Monitor.Exit(_lock);
            OnPropertyChanged(nameof(readingPoint));
        }
예제 #3
0
        private void DeleteDataPointsFromDate(DateTime deleteOlderThan)
        {
            bool lockTaken = false;

            Monitor.TryEnter(_lock, new TimeSpan(0, 2, 0), ref lockTaken);

            if (!lockTaken)
            {
                _logger.Warning("DeleteDataPointsFromDate: Could not lock");
                return;
            }

            using var context = new WateringContext();
            bool wasChanged = false;

            List <Measurement> toDeleteReadingPoints = context.Measurements.Where(x => x.TimeStamp < deleteOlderThan).ToList();

            if (toDeleteReadingPoints.Count > 0)
            {
                context.Measurements.RemoveRange(toDeleteReadingPoints);
                wasChanged = true;
            }

            List <WateringData> toDeleteTraceData = context.Waterings.Where(x => x.TimeStamp < deleteOlderThan).AsNoTracking().ToList();

            if (toDeleteTraceData.Count > 0)
            {
                context.Waterings.RemoveRange(toDeleteTraceData);
                wasChanged = true;
            }

            if (wasChanged)
            {
                context.SaveChanges();
            }

            Monitor.Exit(_lock);
        }