Esempio n. 1
0
        public void LoadFile()
        {
            bool done  = false;
            int  count = 0;

            using (TextFieldParser parser = new TextFieldParser(pathName))
            {
                parser.Delimiters = new string[] { "," };
                while (!done)
                {
                    string[] parts = parser.ReadFields();
                    count++;
                    if (parts == null)
                    {
                        done = true;
                    }
                    else if (parts.Length != 3)
                    {
                        // TODO. Put this line into an error collection and report
                        done = true;
                    }
                    else if (!done && count > 1)
                    {
                        FoodEntity ee = new FoodEntity(Convert.ToInt32(parts[0]), parts[1].ToLower(), Convert.ToInt32(parts[2]));
                        FoodValues.Add(ee.Name, ee);
                    }
                }
            }

            return;
        }
        static void Main(string[] args)
        {
            LoadFood  loadFood   = new LoadFood(@"~\..\..\..\AppData\FoodDB.csv");
            CsvLoader foodLoader = new CsvLoader(loadFood);

            foodLoader.LoadCsvFile();

            LoadExercises exercises      = new LoadExercises(@"~\..\..\..\AppData\ExerciseDB.csv");
            CsvLoader     exerciseLoader = new CsvLoader(exercises);

            exerciseLoader.LoadCsvFile();

            DateTime start = new DateTime(2018, 05, 08);
            DateTime end   = new DateTime(2018, 05, 09);

            // Go with this hardcoded interval in seconds.
            int        interval   = 60 * 30;
            TimeSeries ts         = new TimeSeries(start, end, interval);
            FoodEntity foodEntity = GetFood(loadFood);

            // Hack
            foodEntity.GlycemicIndex = 200;
            ts.InsertEvent(foodEntity, start.AddHours(1), interval);

            ExerciseEntity exerciseEntity = GetExercise(exercises);

            // Hack
            exerciseEntity.ExerciseIndex = 200;
            ts.InsertEvent(exerciseEntity, start.AddHours(7), interval);

            Console.WriteLine("Start Output");
            PrintRange(start, end, ts);

            return;
        }
        public void InsertEvent(FoodEntity foodEvent, DateTime time, int interval)
        {
            DateTime start = time;
            DateTime end   = start;

            end = end.AddHours(2);

            DateTime EndOfDay = time.AddDays(1);

            EndOfDay = EndOfDay.AddHours(-EndOfDay.Hour);
            EndOfDay = EndOfDay.AddMinutes(-EndOfDay.Minute);
            EndOfDay = EndOfDay.AddSeconds(-EndOfDay.Second);

            RegisterEvent(time, EventType.Food);

            int i = (120 * 60) / interval;

            double increments = (double)((double)foodEvent.GlycemicIndex / i);
            int    counter    = 0;

            double Total = 0;

            while (start < end && start <= EndOfDay)
            {
                counter++;
                start = start.AddSeconds(Interval);
                string key = GetKeyValue(start);

                if (glycemicEvents.ContainsKey(key))
                {
                    Total += increments;
                    double currentValue = glycemicEvents[key];
                    // Keep adding for food.
                    currentValue       += Total;
                    glycemicEvents[key] = currentValue;
                }
                else
                {
                    throw new ApplicationException("Operation attempted on uninitialized data");
                }
            }
            // TODO. Assert Counter and TWOHOURS ARE EQUAL

            string endstr      = GetKeyValue(end);
            string endofdaystr = GetKeyValue(EndOfDay);

            // Apply the Total to the rest of the day. Store in temp.
            Dictionary <string, double> tmpDict = new Dictionary <string, double>();

            foreach (KeyValuePair <string, double> kvp in glycemicEvents)
            {
                if (kvp.Key.CompareTo(endstr) > 0 && kvp.Key.CompareTo(endofdaystr) < 0)
                {
                    double currentValue = kvp.Value;
                    currentValue    += Total;
                    tmpDict[kvp.Key] = currentValue;
                }
            }

            // Get from temp and update.
            foreach (KeyValuePair <string, double> kvp in tmpDict)
            {
                glycemicEvents[kvp.Key] = kvp.Value;
            }

            return;
        }