BaseCommand.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.IO.Abstractions;
  2. using System.Text.Json;
  3. using Quadarax.Foundation.Core.Logging;
  4. using Quadarax.Foundation.Core.QConsole;
  5. using Quadarax.Foundation.Core.QConsole.Argument;
  6. using Quadarax.Foundation.Core.QConsole.Command.Base;
  7. using Quadarax.Foundation.Core.QConsole.Value;
  8. using Quadarax.Foundation.Core.QMonitor.Console.Options;
  9. using Quadarax.Foundation.Core.Value;
  10. namespace Quadarax.Foundation.Core.QMonitor.Console.Commands
  11. {
  12. internal abstract class BaseCommand : AbstractCommand, ILogHandler
  13. {
  14. #region *** Constants ***
  15. protected const string CS_ARG_NAME_DUMP = "dump";
  16. private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
  17. private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";
  18. protected const string CS_ARG_NAME_DEBUG = "dbg";
  19. private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
  20. private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
  21. protected const string CS_ARG_NAME_URIGENERAL = "uri-gen";
  22. private const string CS_ARG_HINT_URIGENERAL = "<general_uri>";
  23. private const string CS_ARG_DESC_URIGENERAL = "Uri listener specification for receiving general metadata. If not set use default.";
  24. protected const string CS_ARG_NAME_URIDATA = "uri-dat";
  25. private const string CS_ARG_HINT_URIDATA = "";
  26. private const string CS_ARG_DESC_URIDATA = "Uri listener specification for receiving data. If not set use default.";
  27. #endregion
  28. #region *** Properties ***
  29. protected Configuration Configuration { get; private set; }
  30. protected bool IsDebug { get; private set; }
  31. protected IFileSystem FileSystem { get; private set; }
  32. protected QMonReceiver Receiver { get; private set; }
  33. #endregion
  34. #region *** Private fields ***
  35. private Uri? _uriGeneral;
  36. private Uri? _uriData;
  37. #endregion
  38. protected BaseCommand(Engine engine) : base(engine)
  39. {
  40. }
  41. protected override void OnInitialize()
  42. {
  43. base.OnInitialize();
  44. Configuration? conf;
  45. if (!FileSystem.File.Exists(Constants.CS_CONFIGURATION_FILE))
  46. {
  47. WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
  48. conf = new Configuration();
  49. }
  50. else
  51. {
  52. conf = JsonSerializer.Deserialize<Configuration>(
  53. FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
  54. if (conf == null)
  55. {
  56. conf = new Configuration();
  57. WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
  58. }
  59. }
  60. Configuration = conf;
  61. }
  62. protected virtual void OnDataReceived(MonData[] data)
  63. {
  64. }
  65. protected virtual void OnGeneralReceived(MonGeneral general)
  66. {
  67. }
  68. protected override void OnValidateArguments()
  69. {
  70. base.OnValidateArguments();
  71. IsDebug = WasArgumentSpecified(CS_ARG_NAME_DEBUG);
  72. if (IsDebug)
  73. DebugConsoleWriter.IsWriteDebugEnabled = true;
  74. _uriGeneral = ContainsArgument(CS_ARG_NAME_URIGENERAL) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIGENERAL)) : Configuration.GeneralUri;
  75. _uriData = ContainsArgument(CS_ARG_NAME_URIDATA) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIDATA)) : Configuration.DataUri;
  76. }
  77. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  78. {
  79. FileSystem = new FileSystem();
  80. var args = base.OnSetupArguments().ToList();
  81. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  82. args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  83. args.Add(new NamedArgument(CS_ARG_NAME_URIGENERAL, CS_ARG_DESC_URIGENERAL, CS_ARG_HINT_URIGENERAL, TypeValuesEnum.String,string.Empty, false));
  84. args.Add(new NamedArgument(CS_ARG_NAME_URIDATA, CS_ARG_DESC_URIDATA, CS_ARG_HINT_URIDATA, TypeValuesEnum.String,string.Empty, false));
  85. return args;
  86. }
  87. public void Log(LogSeverityEnum type, string message, Exception e = null)
  88. {
  89. if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
  90. return;
  91. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  92. }
  93. private Uri? GetUri(string uriAsString)
  94. {
  95. if (string.IsNullOrEmpty(uriAsString))
  96. return null;
  97. if (!Uri.TryCreate(uriAsString, UriKind.Absolute, out var result))
  98. return null;
  99. return result;
  100. }
  101. protected override void BeginExecute()
  102. {
  103. base.BeginExecute();
  104. var rcvCfg = QMonReceiverConfiguration.CreateDefault();
  105. rcvCfg.SourceUriGeneral = _uriGeneral ?? Configuration.GeneralUri;
  106. rcvCfg.SourceUriData = _uriData ?? Configuration.DataUri;
  107. WriteInfo($"Starting listener for general ({rcvCfg.SourceUriGeneral}) and data ({rcvCfg.SourceUriData}) ...");
  108. Receiver = new QMonReceiver(this.FileSystem, rcvCfg,new ConsoleLogger(), OnGeneralReceived, OnDataReceived);
  109. }
  110. protected override Result EndExecute(Result incommingResult)
  111. {
  112. WriteInfo("Stopping listener...");
  113. Receiver?.Dispose();
  114. return base.EndExecute(incommingResult);
  115. }
  116. }
  117. }