18 using Newtonsoft.Json;
20 using System.Collections.Generic;
22 using Newtonsoft.Json.Linq;
31 private readonly Queue<ICommand> _commands =
new();
32 private const string _commandFilePattern =
"command*.json";
33 private const string _resultFileBaseName =
"result-command";
49 var currentDirectory =
new DirectoryInfo(Directory.GetCurrentDirectory());
50 var filesFromPattern = currentDirectory.GetFiles(_commandFilePattern);
51 return filesFromPattern.OrderBy(file => file.Name);
63 ReadCommandFile(file.FullName);
65 while (_commands.Count != 0)
67 yield
return _commands.Dequeue();
79 if (
string.IsNullOrEmpty(command.
Id))
81 Log.
Error($
"FileCommandHandler.Acknowledge(): {Messages.FileCommandHandler.NullOrEmptyCommandId}");
84 var resultFilePath = $
"{_resultFileBaseName}-{command.Id}.json";
85 File.WriteAllText(resultFilePath, JsonConvert.SerializeObject(commandResultPacket));
91 private void ReadCommandFile(
string commandFilePath)
93 Log.
Trace($
"FileCommandHandler.ReadCommandFile(): {Messages.FileCommandHandler.ReadingCommandFile(commandFilePath)}");
94 string contents =
null;
95 Exception exception =
null;
96 object deserialized =
null;
99 if (!File.Exists(commandFilePath))
101 Log.
Error($
"FileCommandHandler.ReadCommandFile(): {Messages.FileCommandHandler.CommandFileDoesNotExist(commandFilePath)}");
104 contents = File.ReadAllText(commandFilePath);
105 deserialized = JsonConvert.DeserializeObject(contents,
Settings);
107 catch (Exception err)
113 File.Delete(commandFilePath);
116 var enumerable = deserialized as IEnumerable<ICommand>;
117 if (enumerable !=
null)
119 foreach (var command
in enumerable)
121 _commands.Enqueue(command);
127 var item = deserialized as ICommand;
130 _commands.Enqueue(item);
135 if (callbackCommand !=
null)
137 _commands.Enqueue(callbackCommand);
141 if (exception !=
null)