18 using Newtonsoft.Json;
20 using System.Collections.Generic;
30 private readonly Queue<ICommand> _commands =
new();
31 private const string _commandFilePattern =
"command*.json";
32 private const string _resultFileBaseName =
"result-command";
48 var currentDirectory =
new DirectoryInfo(Directory.GetCurrentDirectory());
49 var filesFromPattern = currentDirectory.GetFiles(_commandFilePattern);
50 return filesFromPattern.OrderBy(file => file.Name);
62 ReadCommandFile(file.FullName);
64 while (_commands.Count != 0)
66 yield
return _commands.Dequeue();
78 if (
string.IsNullOrEmpty(command.
Id))
80 Log.
Error($
"FileCommandHandler.Acknowledge(): {Messages.FileCommandHandler.NullOrEmptyCommandId}");
83 var resultFilePath = $
"{_resultFileBaseName}-{command.Id}.json";
84 File.WriteAllText(resultFilePath, JsonConvert.SerializeObject(commandResultPacket));
90 private void ReadCommandFile(
string commandFilePath)
92 Log.
Trace($
"FileCommandHandler.ReadCommandFile(): {Messages.FileCommandHandler.ReadingCommandFile(commandFilePath)}");
96 if (!File.Exists(commandFilePath))
98 Log.
Error($
"FileCommandHandler.ReadCommandFile(): {Messages.FileCommandHandler.CommandFileDoesNotExist(commandFilePath)}");
101 var contents = File.ReadAllText(commandFilePath);
102 deserialized = JsonConvert.DeserializeObject(contents,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
104 catch (Exception err)
111 File.Delete(commandFilePath);
114 var enumerable = deserialized as IEnumerable<ICommand>;
115 if (enumerable !=
null)
117 foreach (var command
in enumerable)
119 _commands.Enqueue(command);
125 var item = deserialized as ICommand;
128 _commands.Enqueue(item);