Use SettingsData in TaskManager

This commit is contained in:
glax 2023-05-21 15:05:53 +02:00
parent 168bf5a358
commit 715cf1f4f3
2 changed files with 36 additions and 29 deletions

View File

@ -14,16 +14,19 @@ public static class Tranga_Cli
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null, string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tranga");
Path.Join(Directory.GetCurrentDirectory(), $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt")); string logMessageFolderPath = Path.Join(applicationFolderPath, "logs");
string settingsFilePath = Path.Join(applicationFolderPath, "data.json");
logger.WriteLine("Tranga_CLI", "Loading Settings."); Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null,
Path.Join(logMessageFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"));
logger.WriteLine("Tranga_CLI", "Loading Taskmanager.");
TaskManager.SettingsData settings; TaskManager.SettingsData settings;
string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "data.json"); if (File.Exists(settingsFilePath))
if (File.Exists(settingsPath)) settings = TaskManager.LoadData(settingsFilePath);
settings = TaskManager.LoadData(Directory.GetCurrentDirectory());
else else
settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), null, new HashSet<TrangaTask>()); settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), settingsFilePath, null, new HashSet<TrangaTask>());
logger.WriteLine("Tranga_CLI", "User Input"); logger.WriteLine("Tranga_CLI", "User Input");
@ -76,7 +79,7 @@ public static class Tranga_Cli
{ {
TaskManager taskManager = new (settings, logger); TaskManager taskManager = new (settings, logger);
ConsoleKey selection = ConsoleKey.EraseEndOfFile; ConsoleKey selection = ConsoleKey.EraseEndOfFile;
PrintMenu(taskManager, settings.downloadLocation, logger); PrintMenu(taskManager, taskManager.settings.downloadLocation, logger);
while (selection != ConsoleKey.Q) while (selection != ConsoleKey.Q)
{ {
int taskCount = taskManager.GetAllTasks().Length; int taskCount = taskManager.GetAllTasks().Length;
@ -97,7 +100,7 @@ public static class Tranga_Cli
Console.ReadKey(); Console.ReadKey();
break; break;
case ConsoleKey.C: case ConsoleKey.C:
CreateTask(taskManager, settings, logger); CreateTask(taskManager, taskManager.settings, logger);
Console.WriteLine("Press any key."); Console.WriteLine("Press any key.");
Console.ReadKey(); Console.ReadKey();
break; break;
@ -145,7 +148,7 @@ public static class Tranga_Cli
Console.ReadKey(); Console.ReadKey();
break; break;
} }
PrintMenu(taskManager, settings.downloadLocation, logger); PrintMenu(taskManager, taskManager.settings.downloadLocation, logger);
} }
Thread.Sleep(1000); Thread.Sleep(1000);
} }

View File

@ -15,27 +15,31 @@ public class TaskManager
private bool _continueRunning = true; private bool _continueRunning = true;
private readonly Connector[] _connectors; private readonly Connector[] _connectors;
private readonly Dictionary<Connector, List<TrangaTask>> _taskQueue = new(); private readonly Dictionary<Connector, List<TrangaTask>> _taskQueue = new();
private string downloadLocation { get; } public SettingsData settings { get; }
private Logger? logger { get; } private Logger? logger { get; }
public Komga? komga { get; } public Komga? komga { get; }
/// <param name="folderPath">Local path to save data (Manga) to</param> /// <param name="downloadFolderPath">Local path to save data (Manga) to</param>
/// <param name="settingsFilePath">Path to the settings file (data.json)</param>
/// <param name="komgaBaseUrl">The Url of the Komga-instance that you want to update</param> /// <param name="komgaBaseUrl">The Url of the Komga-instance that you want to update</param>
/// <param name="komgaUsername">The Komga username</param> /// <param name="komgaUsername">The Komga username</param>
/// <param name="komgaPassword">The Komga password</param> /// <param name="komgaPassword">The Komga password</param>
/// <param name="logger"></param> /// <param name="logger"></param>
public TaskManager(string folderPath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) public TaskManager(string downloadFolderPath, string? settingsFilePath = null, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null)
{ {
this.logger = logger; this.logger = logger;
this.downloadLocation = folderPath; _allTasks = new HashSet<TrangaTask>();
if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null)
this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger);
this.settings = new SettingsData(downloadFolderPath, settingsFilePath, this.komga, this._allTasks);
if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null) if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null)
this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger);
this._connectors = new Connector[]{ new MangaDex(folderPath, logger) }; this._connectors = new Connector[]{ new MangaDex(downloadFolderPath, logger) };
foreach(Connector cConnector in this._connectors) foreach(Connector cConnector in this._connectors)
_taskQueue.Add(cConnector, new List<TrangaTask>()); _taskQueue.Add(cConnector, new List<TrangaTask>());
_allTasks = new HashSet<TrangaTask>();
Thread taskChecker = new(TaskCheckerThread); Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start(); taskChecker.Start();
@ -45,9 +49,9 @@ public class TaskManager
{ {
this.logger = logger; this.logger = logger;
this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) }; this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) };
this.settings = settings;
foreach(Connector cConnector in this._connectors) foreach(Connector cConnector in this._connectors)
_taskQueue.Add(cConnector, new List<TrangaTask>()); _taskQueue.Add(cConnector, new List<TrangaTask>());
this.downloadLocation = settings.downloadLocation;
this.komga = settings.komga; this.komga = settings.komga;
_allTasks = settings.allTasks; _allTasks = settings.allTasks;
Thread taskChecker = new(TaskCheckerThread); Thread taskChecker = new(TaskCheckerThread);
@ -67,7 +71,7 @@ public class TaskManager
foreach (KeyValuePair<Connector, List<TrangaTask>> connectorTaskQueue in _taskQueue) foreach (KeyValuePair<Connector, List<TrangaTask>> connectorTaskQueue in _taskQueue)
{ {
if(connectorTaskQueue.Value.RemoveAll(task => task.state == TrangaTask.ExecutionState.Waiting) > 0) if(connectorTaskQueue.Value.RemoveAll(task => task.state == TrangaTask.ExecutionState.Waiting) > 0)
ExportData(Directory.GetCurrentDirectory()); ExportData();
if (connectorTaskQueue.Value.Count > 0 && connectorTaskQueue.Value.All(task => task.state is TrangaTask.ExecutionState.Enqueued)) if (connectorTaskQueue.Value.Count > 0 && connectorTaskQueue.Value.All(task => task.state is TrangaTask.ExecutionState.Enqueued))
ExecuteTaskNow(connectorTaskQueue.Value.First()); ExecuteTaskNow(connectorTaskQueue.Value.First());
@ -154,7 +158,7 @@ public class TaskManager
} }
} }
logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask.ToString()}"); logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask.ToString()}");
ExportData(Directory.GetCurrentDirectory()); ExportData();
return newTask; return newTask;
} }
@ -184,7 +188,7 @@ public class TaskManager
else else
logger?.WriteLine(this.GetType().ToString(), $"No Task {task} {publication?.sortName} {publication?.downloadUrl} could be found."); logger?.WriteLine(this.GetType().ToString(), $"No Task {task} {publication?.sortName} {publication?.downloadUrl} could be found.");
} }
ExportData(Directory.GetCurrentDirectory()); ExportData();
} }
/// <summary> /// <summary>
@ -252,7 +256,7 @@ public class TaskManager
{ {
logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})"); logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})");
_continueRunning = false; _continueRunning = false;
ExportData(Directory.GetCurrentDirectory()); ExportData();
if(force) if(force)
Environment.Exit(_allTasks.Count(task => task.state is TrangaTask.ExecutionState.Enqueued or TrangaTask.ExecutionState.Running)); Environment.Exit(_allTasks.Count(task => task.state is TrangaTask.ExecutionState.Enqueued or TrangaTask.ExecutionState.Running));
@ -272,7 +276,7 @@ public class TaskManager
{ {
string importPath = Path.Join(importFolderPath, "data.json"); string importPath = Path.Join(importFolderPath, "data.json");
if (!File.Exists(importPath)) if (!File.Exists(importPath))
return new SettingsData("", null, new HashSet<TrangaTask>()); return new SettingsData(Directory.GetCurrentDirectory(), null, null, new HashSet<TrangaTask>());
string toRead = File.ReadAllText(importPath); string toRead = File.ReadAllText(importPath);
SettingsData data = JsonConvert.DeserializeObject<SettingsData>(toRead)!; SettingsData data = JsonConvert.DeserializeObject<SettingsData>(toRead)!;
@ -283,26 +287,26 @@ public class TaskManager
/// <summary> /// <summary>
/// Exports data (settings, tasks) to file /// Exports data (settings, tasks) to file
/// </summary> /// </summary>
/// <param name="exportFolderPath">Folder path, filename will be data.json</param> private void ExportData()
private void ExportData(string exportFolderPath)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Exporting data to data.json"); logger?.WriteLine(this.GetType().ToString(), $"Exporting data to data.json");
SettingsData data = new SettingsData(this.downloadLocation, this.komga, this._allTasks);
string exportPath = Path.Join(exportFolderPath, "data.json"); string serializedData = JsonConvert.SerializeObject(settings);
string serializedData = JsonConvert.SerializeObject(data); File.WriteAllText(settings.settingsFilePath, serializedData);
File.Delete(exportPath);
File.WriteAllText(exportPath, serializedData);
} }
public class SettingsData public class SettingsData
{ {
public string downloadLocation { get; set; } public string downloadLocation { get; set; }
public string settingsFilePath { get; }
public Komga? komga { get; set; } public Komga? komga { get; set; }
public HashSet<TrangaTask> allTasks { get; } public HashSet<TrangaTask> allTasks { get; }
public SettingsData(string downloadLocation, Komga? komga, HashSet<TrangaTask> allTasks) public SettingsData(string downloadLocation, string? settingsFilePath, Komga? komga, HashSet<TrangaTask> allTasks)
{ {
this.settingsFilePath = settingsFilePath ??
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Tranga", "data.json");
this.downloadLocation = downloadLocation; this.downloadLocation = downloadLocation;
this.komga = komga; this.komga = komga;
this.allTasks = allTasks; this.allTasks = allTasks;