BaseCommand.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. protected const string CS_ARG_NAME_PURGE = "purge";
  28. private const string CS_ARG_HINT_PURGE = "<purge_cached_data>";
  29. private const string CS_ARG_DESC_PURGE = "Purges chached data before start monitoring.";
  30. #endregion
  31. #region *** Properties ***
  32. protected Configuration Configuration { get; private set; }
  33. protected bool IsDebug { get; private set; }
  34. protected IFileSystem FileSystem { get; private set; }
  35. protected QMonReceiver Receiver { get; private set; }
  36. #endregion
  37. #region *** Private fields ***
  38. private Uri? _uriGeneral;
  39. private Uri? _uriData;
  40. #endregion
  41. protected BaseCommand(Engine engine) : base(engine)
  42. {
  43. }
  44. protected override void OnInitialize()
  45. {
  46. base.OnInitialize();
  47. Configuration? conf;
  48. if (!FileSystem.File.Exists(Constants.CS_CONFIGURATION_FILE))
  49. {
  50. WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
  51. conf = new Configuration();
  52. }
  53. else
  54. {
  55. conf = JsonSerializer.Deserialize<Configuration>(
  56. FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
  57. if (conf == null)
  58. {
  59. conf = new Configuration();
  60. WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
  61. }
  62. }
  63. Configuration = conf;
  64. }
  65. protected virtual void OnDataReceived(MonData[] data)
  66. {
  67. }
  68. protected virtual void OnGeneralReceived(MonGeneral general)
  69. {
  70. }
  71. protected override void OnValidateArguments()
  72. {
  73. base.OnValidateArguments();
  74. IsDebug = Engine.Configuration.IsConsoleDebug;
  75. if (IsDebug)
  76. DebugConsoleWriter.IsWriteDebugEnabled = true;
  77. _uriGeneral = ContainsArgument(CS_ARG_NAME_URIGENERAL) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIGENERAL)) : Configuration.GeneralUri;
  78. _uriData = ContainsArgument(CS_ARG_NAME_URIDATA) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIDATA)) : Configuration.DataUri;
  79. }
  80. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  81. {
  82. FileSystem = new FileSystem();
  83. var args = base.OnSetupArguments().ToList();
  84. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  85. //args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  86. args.Add(new NamedArgument(CS_ARG_NAME_URIGENERAL, CS_ARG_DESC_URIGENERAL, CS_ARG_HINT_URIGENERAL, TypeValuesEnum.String,string.Empty, false));
  87. args.Add(new NamedArgument(CS_ARG_NAME_URIDATA, CS_ARG_DESC_URIDATA, CS_ARG_HINT_URIDATA, TypeValuesEnum.String,string.Empty, false));
  88. args.Add(new FlagArgument(CS_ARG_NAME_PURGE, CS_ARG_DESC_PURGE, CS_ARG_HINT_PURGE, false));
  89. return args;
  90. }
  91. public void Log(LogSeverityEnum type, string message, Exception e = null)
  92. {
  93. if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
  94. return;
  95. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  96. }
  97. private Uri? GetUri(string uriAsString)
  98. {
  99. if (string.IsNullOrEmpty(uriAsString))
  100. return null;
  101. if (!Uri.TryCreate(uriAsString, UriKind.Absolute, out var result))
  102. return null;
  103. return result;
  104. }
  105. protected override void BeginExecute()
  106. {
  107. base.BeginExecute();
  108. var rcvCfg = QMonReceiverConfiguration.CreateDefault();
  109. rcvCfg.SourceUriGeneral = _uriGeneral ?? Configuration.GeneralUri;
  110. rcvCfg.SourceUriData = _uriData ?? Configuration.DataUri;
  111. WriteInfo($"Starting listener for general ({rcvCfg.SourceUriGeneral}) and data ({rcvCfg.SourceUriData}) ...");
  112. Receiver = new QMonReceiver(this.FileSystem, rcvCfg,GetArgumentValueOrDefault<bool>(CS_ARG_NAME_PURGE), (IsDebug ? this.Engine.Configuration.DefaultLoggerFactory : null)!, OnGeneralReceived, OnDataReceived);
  113. }
  114. protected override Result EndExecute(Result incommingResult)
  115. {
  116. WriteInfo("Stopping listener...");
  117. Receiver?.Dispose();
  118. return base.EndExecute(incommingResult);
  119. }
  120. }
  121. }