/* * Input: DateTime, DateTime, KeyValuePair<string, List>, SolarContext * DateTime fd - firstDate in CSV files * DateTime ld - lastDate in CSV files * KVP<string, List> - List to scan * SolarContext - database * Output: int - Number of missing records found * Desc: Scans a List found in KVP<string, list>, adds missing values to database */ static int addMissingArray(DateTime fd, DateTime ld, KeyValuePair <string, List <PowerReading> > s, SolarContext context) { DateTime currDate = fd; int index = 0; int records = 0; while (currDate != ld) { if (index > s.Value.Count) //List doesnt include readings to end of date { PowerReading r = new PowerReading() { DateAndTime = currDate, SourceKey = s.Key, DC_Power = -1.0, AC_Power = -1.0, DailyYield = -1.0, TotalYield = -1.0 }; context.PowerReadings.Add(r); records++; } else if (currDate != s.Value[index].DateAndTime) //List missing readings in middle { PowerReading r = new PowerReading() { DateAndTime = currDate, SourceKey = s.Key, DC_Power = -1.0, AC_Power = -1.0, DailyYield = -1.0, TotalYield = -1.0 }; context.PowerReadings.Add(r); records++; } else { index++; //Continue } currDate = currDate.AddMinutes(15); } //Search for missing date values in power table context.SaveChanges(); System.Diagnostics.Debug.WriteLine("Finished power array " + s.Key); return(records); }
/* * Input: String file_name, SolarContext database * Output: None * Desc: Reads power reading data from file into database */ static void ReadInGenerationData(string file_in, SolarContext context) { try { using (var reader = new StreamReader("Data/" + file_in)) { string line; System.Diagnostics.Debug.WriteLine("Seeding Database with " + file_in); reader.ReadLine(); //First line is human stuff //Quick lookup for already entered Source_Key's List <string> sourceKeys = new List <string>(); while ((line = reader.ReadLine()) != null) { var values = line.Split(','); /* * PowerSource { * PK int PowerSourceID, auto increment * string SourceKey, * int PlantNumber } * * PowerReading { * PK int PowerReadingID, auto increment * DateTime DateAndTime, * string SourceKey, * double DC_Power, * double AC_Power, * double Irradiation } */ DateTime dateTime = DateTime.Parse(values[0]); int plantNum = int.Parse(values[1]); string sourceKey = values[2]; double dc_power = double.Parse(values[3]); double ac_power = double.Parse(values[4]); double daily_y = double.Parse(values[5]); double total_y = double.Parse(values[6]); //Add a new PowerSource to database if it doesnt exist already if (sourceKeys.FirstOrDefault(s => s.Equals(sourceKey)) == null) { PowerSource s = new PowerSource() { SourceKey = sourceKey, PlantNumber = plantNum }; context.PowerSources.Add(s); sourceKeys.Add(sourceKey); } PowerReading r = new PowerReading() { DateAndTime = dateTime, SourceKey = sourceKey, DC_Power = dc_power, AC_Power = ac_power, DailyYield = daily_y, TotalYield = total_y }; context.PowerReadings.Add(r); } context.SaveChanges(); System.Diagnostics.Debug.WriteLine("Finished successfully."); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error reading from file '" + file_in + "': " + e.Message); } }