/// <summary> /// Download the climate file based on region and climate type /// </summary> /// <param name="region">Name of region to be downloaded</param> /// <param name="climateType">Climate type that needs to be downloaded</param> /// <returns>Download status</returns> public static bool Download(Region region, ClimateType climateType) { string URL = getWebURL(region, climateType); bool isDownloaded = false; string txtData = String.Empty; try { using (var webClient = new WebClient()) { txtData = webClient.DownloadString(URL); } filename = FileDownloadHelper.getDBFileName(climateType, region); File.WriteAllText(filepath + filename, txtData); Log.Information($"{filename} got downloaded"); } catch (Exception e) { Log.Error(e, " : error"); } if (txtData != String.Empty) { isDownloaded = true; } return(isDownloaded); }
/// <summary> /// Reset table WeatherFileDownload to default values. /// </summary> /// <returns>Status of table reset</returns> public bool ResetFileDownloadTable() { bool isReset = false; try { connection.DeleteAll <WeatherFileDownloadEntity>(); foreach (Region region in Enum.GetValues(typeof(Region))) { foreach (ClimateType ct in Enum.GetValues(typeof(ClimateType))) { WeatherFileDownloadEntity wfd = new WeatherFileDownloadEntity() { Region = region, ClimateType = ct, TimeStamp = DateTime.Now.AddDays(-50), Filename = FileDownloadHelper.getDBFileName(ct, region) }; connection.Insert(wfd); } } isReset = true; Log.Information("File download table is reset"); } catch (Exception e) { Log.Error(e, " : error"); } return(isReset); }
/// <summary> /// Reset entire database and Load data /// </summary> public static bool InitialLoad() { bool isLoaded = false; WeatherDB wdb = new WeatherDB(); //Truncate tables wdb.TruncateTable("WeatherFileDownload"); wdb.TruncateTable("WeatherData"); //Reset WeatherFileDownload table wdb.ResetFileDownloadTable(); //Download all data WeatherDownload.DownloadAll(); //Insert or update data foreach (Region region in Enum.GetValues(typeof(Region))) { foreach (ClimateType ct in Enum.GetValues(typeof(ClimateType))) { var filename = FileDownloadHelper.getDBFileName(ct, region); // get filename from table based on region & climate bool isLatestFile = WeatherParser.isLatestFile(filename, wdb); // Verify if downloaded file is latest if (isLatestFile) { var list = WeatherParser.FormatWeatherData(filename, ct, region); // Parse the latest file into WeatherDataEntity Log.Information($"\n Loading : {ct} , {region}"); var count = wdb.BulkInsertOrUpdate(list); //Insert or Update data Log.Information(count["insert"] + " inserted"); Log.Information(count["update"] + " updated"); Log.Information(count["nochange"] + " no change"); wdb.UpdateFileTimeStamp(filename); } } } return(isLoaded); }