private static DailyUsage GetDailyUsageImproved(DateTime yesterday) { yesterday = yesterday.Date; var today = yesterday.AddDays(1); var todayRecords = GetP1RecordsForDay(today); var yesterdayRecords = GetP1RecordsForDay(yesterday); var dayBeforeYesterdayRecords = GetP1RecordsForDay(yesterday.AddDays(-1)); var yesterdayLocal = yesterday.AddHours(12).ToLocalTime().Date; var todayLocal = yesterdayLocal.AddDays(1); var records = dayBeforeYesterdayRecords.Concat(yesterdayRecords).Concat(todayRecords); var improved = P1Improved.FromP1Records(records).ToArray(); var firstRecord = improved.First(p1I => p1I.DateTime >= yesterdayLocal); var lastRecord = improved.First(p1I => p1I.DateTime >= todayLocal); var usageTotal = Usage.FromRecords(firstRecord, lastRecord); var range = Enumerable.Range(0, 25).ToArray(); var hours = range.Select(i => yesterdayLocal.AddHours(i)).ToArray(); var hourRecords = hours.Select(h => improved.First(p1I => p1I.DateTime >= h)).ToArray(); var hourly = range.Skip(1).Select(r => new Usage { kWh = hourRecords[r].kWhTotal - hourRecords[r - 1].kWhTotal, M3 = hourRecords[r].M3Total - hourRecords[r - 1].M3Total, }).ToArray(); var dailyUsage = new DailyUsage { Total = usageTotal, Day = yesterday, Hourly = hourly }; return(dailyUsage); }
public void TestWithStartNoEnd() { var refTime = DateTime.UtcNow; var prev = new P1Record { ActiveMeter = 1, DateTime = refTime.AddHours(-3), kWh1 = 999, kW = 1 }; var first1k = new P1Record { ActiveMeter = 1, DateTime = refTime.AddHours(-2), kWh1 = 1000, kW = 1 }; var next1k = new P1Record { ActiveMeter = 1, DateTime = refTime.AddHours(-1), kWh1 = 1000, kW = 0.4 }; var first1k1 = new P1Record { ActiveMeter = 1, DateTime = refTime.AddHours(0), kWh1 = 1000, kW = 0.4 }; var standen = new[] { prev, first1k, next1k, first1k1 }; var improvedStanden = P1Improved.FromP1Records(standen).ToArray(); Assert.Equal(1000.4, improvedStanden[2].kWhTotal, 2); }
public static Usage FromRecords(P1Improved first, P1Improved last) { return(new Usage { kWh = last.kWhTotal - first.kWhTotal, M3 = last.M3Total - first.M3Total }); }