public string test() { var reading = new PowerAvg() { P1 = 120, //model.Power1, P2 = 120, //model.Power2, P3 = 120, //model.Power3, primarySourceId = 5, //model.PrimarySourceId, secondrySourceId = null, // model.SecondarySourceId, readingDate = DateTime.Now //model.TimeStamp }; Context.PowerAvg.Add(reading); Context.SaveChanges(); return($"Ok,"); }
public void peakPower(SourceReading model) { var count = Context.PowerAvg.ToList(); if (count.Count() != 0) { PowerAvg firstModel = Context.PowerAvg.First(); if (firstModel != null && firstModel.readingDate.AddHours(1) < model.TimeStamp) { var primaries = Context.PrimarySource.ToList(); var secondries = Context.secondarySource.ToList(); var avgs = Context.PowerAvg.ToList(); foreach (var primary in primaries) { decimal totalp1 = 0; decimal totalp2 = 0; decimal totalp3 = 0; decimal avgp1 = 0; decimal avgp2 = 0; decimal avgp3 = 0; var pavg = count.Where(a => a.primarySourceId == Convert.ToInt32(primary.Code)).ToList(); if (pavg.Count != 0) { foreach (var avg in pavg) { totalp1 += avg.P1; totalp2 += avg.P2; totalp3 += avg.P3; } avgp1 = (totalp1) / (pavg.Count); avgp2 = (totalp2) / (pavg.Count); avgp3 = (totalp3) / (pavg.Count); var peak = Context.powerPeak.DefaultIfEmpty().FirstOrDefault(p => p.primarySourceId == Convert.ToInt32(primary.Code)); if (peak != null) { if (peak.peakP1 < avgp1 || peak.peakP1 == avgp1) { peak.peakP1 = avgp1; peak.dateP1 = firstModel.readingDate; } if (peak.peakP2 < avgp2 || peak.peakP2 == avgp2) { peak.peakP2 = avgp2; peak.dateP2 = firstModel.readingDate; } if (peak.peakP3 < avgp3 || peak.peakP3 == avgp3) { peak.peakP3 = avgp3; peak.dateP3 = firstModel.readingDate; } Context.powerPeak.Update(peak); Context.SaveChanges(); } } } foreach (var secondry in secondries) { decimal totalp1 = 0; decimal totalp2 = 0; decimal totalp3 = 0; decimal avgp1 = 0; decimal avgp2 = 0; decimal avgp3 = 0; var savg = avgs.Where(a => a.secondrySourceId == Convert.ToInt32(secondry.Code)).ToList(); if (savg.Count != 0) { foreach (var avg in savg) { totalp1 += avg.P1; totalp2 += avg.P2; totalp3 += avg.P3; } avgp1 = (totalp1) / (savg.Count); avgp2 = (totalp2) / (savg.Count); avgp3 = (totalp3) / (savg.Count); var peak = Context.powerPeak.FirstOrDefault(p => p.secondrySourceId == Convert.ToInt32(secondry.Code)); if (peak.peakP1 < avgp1 || peak.peakP1 == avgp1) { peak.peakP1 = avgp1; peak.dateP1 = firstModel.readingDate; } if (peak.peakP2 < avgp2 || peak.peakP2 == avgp2) { peak.peakP2 = avgp2; peak.dateP2 = firstModel.readingDate; } if (peak.peakP3 < avgp3 || peak.peakP3 == avgp3) { peak.peakP3 = avgp3; peak.dateP3 = firstModel.readingDate; } Context.powerPeak.Update(peak); Context.SaveChanges(); } } // clear table Context.Database.ExecuteSqlCommand("Delete From PowerAvg"); var reading = new PowerAvg() { P1 = model.Power1, P2 = model.Power2, P3 = model.Power3, primarySourceId = model.PrimarySourceId, secondrySourceId = model.SecondarySourceId, readingDate = model.TimeStamp }; Context.PowerAvg.Add(reading); Context.SaveChanges(); } else { var reading = new PowerAvg() { P1 = model.Power1, P2 = model.Power2, P3 = model.Power3, primarySourceId = model.PrimarySourceId, secondrySourceId = model.SecondarySourceId, readingDate = model.TimeStamp }; Context.PowerAvg.Add(reading); Context.SaveChanges(); } } else { var reading = new PowerAvg() { P1 = model.Power1, P2 = model.Power2, P3 = model.Power3, primarySourceId = model.PrimarySourceId, secondrySourceId = model.SecondarySourceId, readingDate = model.TimeStamp }; Context.PowerAvg.Add(reading); Context.SaveChanges(); } // return $"Ok," + DateTimeOffset.UtcNow.ToUnixTimeSeconds(); }