Skip to content

Commit

Permalink
Add: Add config class
Browse files Browse the repository at this point in the history
  • Loading branch information
ms committed Feb 28, 2021
1 parent e1eb4df commit aab191d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 44 deletions.
68 changes: 68 additions & 0 deletions RcloneFileWatcherCore/Logic/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using RcloneFileWatcherCore.Logic.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using RcloneFileWatcherCore.DTO;

namespace RcloneFileWatcherCore.Logic
{

class Config
{
private readonly string _configFileName;
private readonly ILogger _logger;
public Config(string configFileName, ILogger logger)
{
_configFileName = configFileName;
_logger = logger;
}

internal List<PathDTO> LoadConfig()
{
try
{
List<PathDTO> pathDTOs = new List<PathDTO>();
if (!File.Exists(_configFileName))
{
_logger.WriteAlways("Config file is missing");
return null;
}
string[] parameters = File.ReadAllLines(_configFileName);
foreach (var param in parameters)
{
if (param.ToUpper().Contains("CONSOLEWRITER.OFF"))
{
_logger.Enable = false;
}
else if (param.ToUpper().Contains("CONSOLEWRITER.ON"))
{
_logger.Enable = true;
}
else
{
var item = param.Split(',');
if (item.Length == 3)
{
PathDTO pathDTO = new PathDTO();
pathDTO.WatchingPath = item[0];
pathDTO.RcloneFilesFromPath = item[1];
pathDTO.RcloneBatch = item[2];
pathDTOs.Add(pathDTO);
}
else
{
_logger.WriteAlways("Error in config file.");
return null;
}
}
}
return pathDTOs;
}
catch (Exception ex)
{
_logger.WriteAlways(ex.ToString());
return null;
}
}
}
}
10 changes: 7 additions & 3 deletions RcloneFileWatcherCore/Logic/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

namespace RcloneFileWatcherCore.Logic
{
class ConsoleLogger:ILogger
class ConsoleLogger : ILogger
{
public bool Enable { get; set; } = true;
public bool Enable { get; set; }

public void Write(string text)
{
if (Enable)
{
Console.WriteLine($@"{DateTime.Now} {text}");
WriteAlways(text);
}
}
public void WriteAlways(string text)
{
Console.WriteLine($@"{DateTime.Now} {text}");
}
}
}
47 changes: 11 additions & 36 deletions RcloneFileWatcherCore/Logic/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,26 @@ class Controller
private ConcurrentDictionary<string, FileDTO> _fileDTOs;
private List<PathDTO> _pathDTOs;
private Watcher _watcher;
private Config _config;
private readonly ProcessRunner _processRunner;
private ILogger _logger;
private readonly Scheduler _scheduler;
public Controller()
private const string _configFileName = "RcloneFileWatcherCoreConfig.txt";
private const int _exitCodeConfigError = 2;
internal Controller()
{
_fileDTOs = new ConcurrentDictionary<string, FileDTO>();
_pathDTOs = new List<PathDTO>();
_logger = new ConsoleLogger();
FilePrepare _filePrepare = new FilePrepare(_logger, _pathDTOs, _fileDTOs);
_watcher = new Watcher(_logger, _fileDTOs, _pathDTOs);
if (!File.Exists("RcloneFileWatcherCoreConfig.txt"))
{
_logger.Write("Config file missing");
Environment.Exit(0);
}
string[] parameters = File.ReadAllLines("RcloneFileWatcherCoreConfig.txt");
foreach (var param in parameters)
_logger = new ConsoleLogger();
_config = new Config(_configFileName, _logger);
_pathDTOs = _config.LoadConfig();
if (_pathDTOs==null || _pathDTOs.Count==0)
{
if (param.ToUpper().Contains("CONSOLEWRITER.OFF"))
{
_logger.Enable = false;
}
else if (param.ToUpper().Contains("CONSOLEWRITER.ON"))
{
_logger.Enable = true;
}
else
{
var item = param.Split(',');
if (item.Length == 3)
{
PathDTO _pathDTO = new PathDTO();
_pathDTO.WatchingPath = item[0];
_pathDTO.RcloneFilesFromPath = item[1];
_pathDTO.RcloneBatch = item[2];
_pathDTOs.Add(_pathDTO);
}
else
{
_logger.Write("Errors in config file. Chceck it.");
}
}
Environment.Exit(_exitCodeConfigError);
}
FilePrepare _filePrepare = new FilePrepare(_logger, _pathDTOs, _fileDTOs);
_processRunner = new ProcessRunner(_logger, _filePrepare, _fileDTOs);
_scheduler = new Scheduler(_logger, _processRunner);
_watcher = new Watcher(_logger, _fileDTOs, _pathDTOs);
_watcher.Start();
_scheduler.SetTimer();
_logger.Write("Started");
Expand Down
1 change: 1 addition & 0 deletions RcloneFileWatcherCore/Logic/Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ interface ILogger
{
bool Enable { get; set; }
void Write(string text);
void WriteAlways(string text);
}
}
9 changes: 4 additions & 5 deletions RcloneFileWatcherCore/Logic/Watcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ namespace RcloneFileWatcherCore.Logic
class Watcher
{
private readonly ILogger _logger;
private readonly ConcurrentDictionary<string, FileDTO> _fileList;
private readonly ConcurrentDictionary<string, FileDTO> _fileDTOs;
private readonly List<PathDTO> _FilePathDTO;
private List<FileSystemWatcher> _fileWatcherList = new List<FileSystemWatcher>();
public Watcher(ILogger logger, ConcurrentDictionary<string, FileDTO> fileList, List<PathDTO> FilePathDTO)
public Watcher(ILogger logger, ConcurrentDictionary<string, FileDTO> fileDTOs, List<PathDTO> FilePathDTO)
{
_logger = logger;
_fileList = fileList;
_fileDTOs = fileDTOs;
_FilePathDTO = FilePathDTO;
Start();
}

public void Start()
Expand Down Expand Up @@ -57,7 +56,7 @@ private void OnChanged(object source, FileSystemEventArgs e)
{
var sourceFileWatcher = (FileSystemWatcher)source;
//ConsoleWriter($" File {sourceFileWatcher.NotifyFilter}: {e.FullPath.Substring(sourceFileWatcher.Path.Length)} {e.ChangeType}");
_fileList.TryAdd($@"{sourceFileWatcher.Path};{e.FullPath.Substring(sourceFileWatcher.Path.Length)}",
_fileDTOs.TryAdd($@"{sourceFileWatcher.Path};{e.FullPath.Substring(sourceFileWatcher.Path.Length)}",
new FileDTO
{
SourcePath = sourceFileWatcher.Path,
Expand Down

0 comments on commit aab191d

Please sign in to comment.