From 0bb3b36e0de37ee8bddd57a4abbda5092f75e544 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Wed, 1 Apr 2015 18:36:10 +0200
Subject: [PATCH] new search method to look up auxiliary power demand

---
 .../Data/AuxiliariesDemandAdapter.cs          | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Data/AuxiliariesDemandAdapter.cs b/VectoCore/Models/SimulationComponent/Data/AuxiliariesDemandAdapter.cs
index 8d1711ba83..e529511241 100644
--- a/VectoCore/Models/SimulationComponent/Data/AuxiliariesDemandAdapter.cs
+++ b/VectoCore/Models/SimulationComponent/Data/AuxiliariesDemandAdapter.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Security.Cryptography.X509Certificates;
 using Common.Logging;
@@ -10,14 +11,21 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
 	public class AuxiliariesDemandAdapter
 	{
 		private readonly DrivingCycleData _drivingCycle;
+		private IEnumerator<DrivingCycleData.DrivingCycleEntry> _nextCycleEntry; 
 		private readonly string _auxiliaryId;
 
+		protected DrivingCycleData.DrivingCycleEntry CurrentCycleEntry { get; set; }
+		//protected DrivingCycleData.DrivingCycleEntry NextCycleEntry { get { return _nextCycleEntry.Current; } }
+
 		private ILog Log;
 
 		public AuxiliariesDemandAdapter(DrivingCycleData inputData, string column = null)
 		{
 			Log = LogManager.GetLogger(this.GetType());
 			_drivingCycle = inputData;
+			_nextCycleEntry = _drivingCycle.Entries.GetEnumerator();
+			_nextCycleEntry.MoveNext();
+			CurrentCycleEntry = _drivingCycle.Entries.First();
 			_auxiliaryId = column;
 			if (_auxiliaryId != null && !_drivingCycle.Entries.First().AuxiliarySupplyPower.ContainsKey(_auxiliaryId)) {
 				Log.ErrorFormat("driving cycle data does not contain column {0}", column);
@@ -27,12 +35,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
 
 		public Watt GetPowerDemand(TimeSpan absTime, TimeSpan dt)
 		{
-			//var entry = _drivingCycle.Entries.FindLastIndex(x => x.Time <= absTime.TotalSeconds);
-/*			if (entry == -1) {
-				Log.ErrorFormat("time: {0} could not find power demand for auxiliary {1}", absTime.TotalSeconds, _auxiliaryId);
-				throw new VectoSimulationException(String.Format("time: {0} could not find power demand for auxiliary {1}", absTime.TotalSeconds, _auxiliaryId));
-			} */
-			return 0.SI<Watt>(); //_auxiliaryId == null ? _drivingCycle.Entries[entry].AdditionalAuxPowerDemand : _drivingCycle.Entries[entry].AuxiliarySupplyPower[_auxiliaryId];
+			if (_nextCycleEntry.Current.Time <= absTime.TotalSeconds) {
+				CurrentCycleEntry = _nextCycleEntry.Current;
+				_nextCycleEntry.MoveNext();
+			}
+			return String.IsNullOrEmpty(_auxiliaryId) ? CurrentCycleEntry.AdditionalAuxPowerDemand : CurrentCycleEntry.AuxiliarySupplyPower[_auxiliaryId];
 		}
 
 	}
-- 
GitLab