Skip to content

Commit

Permalink
Added new logic for loading application settings
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Mar 20, 2020
1 parent f77aca9 commit c06f86d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions TFU002/TFU002.Interfaces/Services/IDirectoryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IDirectoryProvider
{
string AssemblyDirectory { get; }
string DefaultPath { get; }
}
}
1 change: 1 addition & 0 deletions TFU002/TFU002.Logic/Services/DirectoryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public DirectoryProvider(ILogger<DirectoryProvider> logger)
}

public string AssemblyDirectory { get; }
public string DefaultPath => @"C:\TwinCAT\Functions\Unofficial\BeckhoffS7Client";
}
}
27 changes: 21 additions & 6 deletions TFU002/TFU002.Logic/Services/SettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SettingsProvider : ISettingsProvider
public ApplicationSettings Settings { get; private set; }
private void LoadOrCreateSettings()
{
var settingsPath = Path.Combine(directoryProvider.AssemblyDirectory, "TFU002.settings.json");
var settingsPath = Path.Combine(directoryProvider.DefaultPath, "TFU002.settings.json");
logger.LogInformation($"Looking for application settings into {settingsPath}");
try
{
Expand All @@ -28,11 +28,26 @@ private void LoadOrCreateSettings()
}
else
{
logger.LogInformation("Application settings does'n not exist: creating new default settings");
Settings = new ApplicationSettings();
Settings.ExtenalPlcSettings.Add(new ExtenalPlcSetting());
var json = JsonConvert.SerializeObject(Settings, Formatting.Indented);
File.WriteAllText(settingsPath, json);
logger.LogInformation("Application settings do not exist...");
var fallbackSettingsPath = Path.Combine(directoryProvider.AssemblyDirectory, "TFU002.settings.json");
logger.LogInformation($"looking for settings into {fallbackSettingsPath}...");
if (File.Exists(fallbackSettingsPath))
{
Settings = JsonConvert.DeserializeObject<ApplicationSettings>(File.ReadAllText(fallbackSettingsPath));
logger.LogInformation("Application settings successfully loaded!");
}
else
{
logger.LogInformation("Application settings not found also in fallback path...");
Settings = new ApplicationSettings();
Settings.ExtenalPlcSettings.Add(new ExtenalPlcSetting());
var json = JsonConvert.SerializeObject(Settings, Formatting.Indented);
if (Directory.Exists(directoryProvider.DefaultPath))
{
logger.LogInformation($"Writing default settings into {settingsPath}");
File.WriteAllText(settingsPath, json);
}
}
}
}
catch (Exception e)
Expand Down

0 comments on commit c06f86d

Please sign in to comment.