/* * Input: String file name, SolarContext database * Output: None * Desc: Reads weather sensor data from file_name into database */ static void ReadInWeatherSensorData(string file_in, SolarContext context) { try { using (var reader = new StreamReader("Data/" + file_in)) { string line; bool firstRun = true; System.Diagnostics.Debug.WriteLine("Seeding Database with " + file_in); reader.ReadLine(); //First line is human stuff while ((line = reader.ReadLine()) != null) { var values = line.Split(','); if (firstRun) { AddPlantNumber(values[1], context); firstRun = false; } /* * WeatherReading { * PK int WeatherReadingID, auto increment * DateTime DateAndTime, * double AmbientTemp, * double ModuleTemp, * double Irradiation } */ DateTime dateTime = DateTime.Parse(values[0]); int plantNum = int.Parse(values[1]); double aTemp = double.Parse(values[3]); double mTemp = double.Parse(values[4]); double irrid = double.Parse(values[5]); WeatherReading r = new WeatherReading() { DateAndTime = dateTime, PlantNumber = plantNum, AmbientTemp = aTemp, ModuleTemp = mTemp, Irradiation = irrid }; context.WeatherReadings.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); } }
/* * Input: SolarContext database * Output: None * Desc: Scans all weather readings in the database looking for missing records based on date */ static void addMissingWeather(SolarContext context) { int plant1 = 4135001; int plant2 = 4136001; DateTime firstDate = DateTime.Parse("05-15-2020 00:00"); DateTime lastDate = DateTime.Parse("06-17-2020 23:45"); int recordsAdded = 0; //Search for missing date values in weather table List <WeatherReading> readings5001 = context.WeatherReadings.Where(wr => wr.PlantNumber == 4135001).OrderBy(wr => wr.DateAndTime).ToList(); List <WeatherReading> readings6001 = context.WeatherReadings.Where(wr => wr.PlantNumber == 4136001).OrderBy(wr => wr.DateAndTime).ToList(); System.Diagnostics.Debug.WriteLine((readings5001.Count + readings6001.Count) + " weather records found. Scanning for missing records."); DateTime currDate = firstDate; int index5001 = 0; int index6001 = 0; while (currDate != lastDate) { if (index5001 > readings5001.Count) //List doesnt include readings to end of date { WeatherReading r = new WeatherReading() { DateAndTime = currDate, PlantNumber = plant1, AmbientTemp = -1, ModuleTemp = -1, Irradiation = -1 }; context.WeatherReadings.Add(r); recordsAdded++; } else if (currDate != readings5001[index5001].DateAndTime) //List missing readings in middle { WeatherReading r = new WeatherReading() { DateAndTime = currDate, PlantNumber = plant1, AmbientTemp = -1, ModuleTemp = -1, Irradiation = -1 }; context.WeatherReadings.Add(r); recordsAdded++; } else { index5001++; //Continue } if (index6001 > readings6001.Count) //List doesnt include readings to end of date { WeatherReading r = new WeatherReading() { DateAndTime = currDate, PlantNumber = plant2, AmbientTemp = -1, ModuleTemp = -1, Irradiation = -1 }; context.WeatherReadings.Add(r); recordsAdded++; } else if (currDate != readings6001[index6001].DateAndTime) //List missing readings in middle { WeatherReading r = new WeatherReading() { DateAndTime = currDate, PlantNumber = plant2, AmbientTemp = -1, ModuleTemp = -1, Irradiation = -1 }; context.WeatherReadings.Add(r); recordsAdded++; } else { index6001++; //Continue } currDate = currDate.AddMinutes(15); //I don't know why this has to return a DateTime } //Search for missing date values in weather table context.SaveChanges(); int count = context.WeatherReadings.Count(); System.Diagnostics.Debug.WriteLine(recordsAdded + " records were added. " + count + " total records"); }