public IEnumerable<TemperatureAggregate> GetForBrew(int brewId, DateTime @from, DateTime to)
        {
            var lowerBoundary = @from.Round(TimeSpan.FromMinutes(10));
            var upperBoundary = to.Round(TimeSpan.FromMinutes(10));

            return TemperatureAggregateRepository.GetAll().Where(a => a.BrewId == brewId && a.When >= lowerBoundary && a.When <= upperBoundary).OrderBy(a => a.When);
        }
Example #2
0
        public static DataTable GenerateDataTable(IEnumerable<Sensor> sensorsToGenerateFrom, DateTime startTime, DateTime endTime)
        {
            var sensors = sensorsToGenerateFrom.Distinct(new SensorNameEqualityComparer()).OrderBy(x => x.SortIndex).ToArray();

            if (sensors.Length == 0)
                return new DataTable();

            if (startTime < sensors[0].Owner.StartTimeStamp)
                startTime = sensors[0].Owner.StartTimeStamp;

            if (endTime > sensors[0].Owner.EndTimeStamp)
                endTime = sensors[0].Owner.EndTimeStamp;

            var table = new DataTable();

            table.Columns.Add(new DataColumn("Timestamp", typeof(FormattedDateTime)));

            foreach (var sensor in sensors)
            {
                table.Columns.Add(new DataColumn(sensor.Name.Replace(".", ""), typeof(string)));
            }

            for (var j = startTime.Round(new TimeSpan(0, 0, sensors[0].Owner.DataInterval, 0)); j <= endTime; j = j.AddMinutes(sensors[0].Owner.DataInterval))
            {
                var row = table.NewRow();
                row[0] = new FormattedDateTime(j);
                for (var i = 0; i < sensors.Length; i++)
                {
                    row[i + 1] = "";

                    var raw = string.Empty;
                    if (sensors[i].RawData.Values.ContainsKey(j))
                        raw = string.Format("{0}", sensors[i].RawData.Values[j]);

                    var current = string.Empty;
                    if (sensors[i].CurrentState.Values.ContainsKey(j))
                        current = string.Format("{0}", sensors[i].CurrentState.Values[j]);

                    if (String.CompareOrdinal(raw, current) == 0)
                        row[i + 1] = current;
                    else
                        row[i + 1] = current == string.Empty ? string.Format("[{0}]", raw) : string.Format("[{0}] {1}", raw, current);
                }
                table.Rows.Add(row);
            }

            return table;
        }
        public void AggregateData(string sensorId, DateTime when, double value)
        {
            // Find linked brew
            var brew = BrewRepository.GetAll().FirstOrDefault(b => b.SensorId == sensorId);
            if (brew != null)
            {
                // Find previous aggregate
                var brewId = brew.Id;
                var lowerBoundary = when.Round(TimeSpan.FromMinutes(10));
                var upperBoundary = lowerBoundary.AddMinutes(1);

                var aggregate = TemperatureAggregateRepository.GetAll().FirstOrDefault(a => a.BrewId == brewId && a.When >= lowerBoundary && a.When <= upperBoundary);
                if (aggregate == null)
                {
                    TemperatureAggregateRepository.InsertOnCommit(new TemperatureAggregate { BrewId = brewId, Value = value, When = lowerBoundary });
                }
                else
                {
                    aggregate.Value = (aggregate.Value + value) / 2;
                }
                TemperatureAggregateRepository.CommitChanges();
            }
        }
 private DateTime CutTimeTo(DateTime dt, AggregationInterval intl)
 {
     switch (intl.Value)
     {
         case AggregationIntervalValue.Day:
             return new DateTime(dt.Year, dt.Month, dt.Day);
         case AggregationIntervalValue.Week:
             return dt.Round(TimeSpan.FromDays(7));
         case AggregationIntervalValue.Month:
             return new DateTime(dt.Year, dt.Month, 1);
         case AggregationIntervalValue.Quarter:
             return new DateTime(dt.Year, ((int)(Math.Ceiling((double)dt.Month / 3) * 3 - 2)), 1);
         case AggregationIntervalValue.Year:
             return new DateTime(dt.Year, 1, 1);
     }
     return new DateTime();
 }
Example #5
0
        public void Round_WhenExecuted_ReturnsRoundedValue()
        {
            DateTime date = new DateTime(2012, 4, 26, 12, 13, 14, 951);
            TimeSpan span = new TimeSpan((long)100000);
            DateTime expected = new DateTime(2012, 4, 26, 12, 13, 14, 950);

            DateTime actual = date.Round(span);

            Assert.AreEqual(expected, actual);
        }
Example #6
0
        private DateTime setTimeSlotDateTime(DateTime timeNow, Resolution adjustmentResolution)
        {
            DateTime dateTimeToReturn = timeNow;
            switch (adjustmentResolution)
            {
                case Resolution.Minute:
                    dateTimeToReturn = timeNow.Round(TimeSpan.FromMinutes(1));
                    break;
                case Resolution.FiveMinute:
                    dateTimeToReturn = timeNow.Round(TimeSpan.FromMinutes(5));
                    break;
                case Resolution.FifteenMinute:
                    dateTimeToReturn = timeNow.Round(TimeSpan.FromMinutes(15));
                    break;
                case Resolution.ThirtyMinute:
                    dateTimeToReturn = timeNow.Round(TimeSpan.FromMinutes(30));
                    break;
                case Resolution.Hour:
                    dateTimeToReturn = timeNow.Round(TimeSpan.FromHours(1));
                    break;
                case Resolution.Day:
                    dateTimeToReturn = DateTime.SpecifyKind((new DateTime(timeNow.Year, timeNow.Month, timeNow.Day) + Configurator.Configuration.DayTotalTZOffset),DateTimeKind.Utc);
                    switch (Configurator.Configuration.GrapheneRoundingMethod)
                    {
                        case TimespanRoundingMethod.Start:
                            if (dateTimeToReturn > timeNow)
                             {
                                 dateTimeToReturn = timeNow.AddDays(-1);
                             }
                            break;
                        case TimespanRoundingMethod.End:
                            if (dateTimeToReturn < timeNow)
                             {
                                 dateTimeToReturn = timeNow.AddDays(1);
                             }
                            break;
                        case TimespanRoundingMethod.MidPoint:
                            if (dateTimeToReturn > timeNow.AddMinutes(-30))
                             {
                                 dateTimeToReturn = timeNow.AddDays(-1);
                             }
                            break;
                    }
                    break;
                case Resolution.Month:
                    dateTimeToReturn = new DateTime(timeNow.Year, timeNow.Month, 1) +
                                            Configurator.Configuration.DayTotalTZOffset;
                    switch (Configurator.Configuration.GrapheneRoundingMethod)
                    {
                        case TimespanRoundingMethod.Start:
                            if (dateTimeToReturn > timeNow)
                             {
                                 dateTimeToReturn = timeNow.AddMonths(-1);
                             }
                            break;
                        case TimespanRoundingMethod.End:
                            if (dateTimeToReturn < timeNow)
                             {
                                 dateTimeToReturn = timeNow.AddMonths(1);
                             }
                            break;
                        case TimespanRoundingMethod.MidPoint:
                            if (dateTimeToReturn > timeNow.AddMinutes(-30))
                             {
                                 dateTimeToReturn = timeNow.AddMonths(-1);
                             }
                            break;
                    }
                    break;
            }

            return dateTimeToReturn;
        }