Jelajahi Sumber

Add qmonlib.console scan command (not working yet)

Dalibor Votruba 2 tahun lalu
induk
melakukan
94a5fe112d

+ 145 - 110
Workbench/QMonitor/qmonlib.console/Commands/BaseCommand.cs

@@ -1,114 +1,149 @@
-using System.IO.Abstractions;
-using System.Text.Json;
-using Quadarax.Foundation.Core.Console;
-using Quadarax.Foundation.Core.Logging;
-using Quadarax.Foundation.Core.QConsole;
-using Quadarax.Foundation.Core.QConsole.Argument;
-using Quadarax.Foundation.Core.QConsole.Command.Base;
+using System.IO.Abstractions;
+using System.Text.Json;
+using Quadarax.Foundation.Core.Logging;
+using Quadarax.Foundation.Core.QConsole;
+using Quadarax.Foundation.Core.QConsole.Argument;
+using Quadarax.Foundation.Core.QConsole.Command.Base;
+using Quadarax.Foundation.Core.QConsole.Value;
 using Quadarax.Foundation.Core.QMonitor.Console.Options;
+using Quadarax.Foundation.Core.Value;
 
-namespace Quadarax.Foundation.Core.QMonitor.Console.Commands
-{
-    internal abstract class BaseCommand : AbstractCommand, ILogHandler
-    {
-        #region *** Constants ***
-
-        protected const string CS_ARG_NAME_DUMP = "dump";
-        private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
-        private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";
-
-        protected const string CS_ARG_NAME_DEBUG = "dbg";
-        private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
-        private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
-
-        protected const string CS_ARG_NAME_FORCE = "force";
-        private const string CS_ARG_HINT_FORCE = "<is_force>";
-        private const string CS_ARG_DESC_FORCE = "Flag if set, then overrite existing entity, otherwise cancel operation.";
-
-
-        #endregion
-
-        protected Configuration Configuration { get; private set; }
-
-        protected bool? IsForce { get; private set; }
-
-        protected bool IsDebug { get; private set; }
-
-        protected IFileSystem FileSystem { get; private set; }
-
-        protected BaseCommand(Engine engine) : base(engine)
-        {
-
-        }
-
-        protected override void OnInitialize()
-        {
-            base.OnInitialize();
-            var conf =JsonSerializer.Deserialize<Configuration>(FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
-            if (conf == null)
-            {
-                conf = new Configuration();
-                WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
-            }
-                
-            Configuration = conf;
-        }
-
-        protected override void OnValidateArguments()
-        {
+namespace Quadarax.Foundation.Core.QMonitor.Console.Commands
+{
+    internal abstract class BaseCommand : AbstractCommand, ILogHandler
+    {
+        #region *** Constants ***
+
+        protected const string CS_ARG_NAME_DUMP = "dump";
+        private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
+        private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";
+
+        protected const string CS_ARG_NAME_DEBUG = "dbg";
+        private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
+        private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
+
+        protected const string CS_ARG_NAME_URIGENERAL = "uri-gen";
+        private const string CS_ARG_HINT_URIGENERAL = "<general_uri>";
+        private const string CS_ARG_DESC_URIGENERAL = "Uri listener specification for receiving general metadata. If not set use default.";
+
+        protected const string CS_ARG_NAME_URIDATA = "uri-dat";
+        private const string CS_ARG_HINT_URIDATA = "";
+        private const string CS_ARG_DESC_URIDATA = "Uri listener specification for receiving data. If not set use default.";
+
+        #endregion
+
+        #region *** Properties ***
+        protected Configuration Configuration { get; private set; }
+
+        protected bool IsDebug { get; private set; }
+        protected IFileSystem FileSystem { get; private set; }
+
+        protected QMonReceiver Receiver { get; private set; }
+
+        #endregion
+
+        #region *** Private fields ***
+        private Uri? _uriGeneral;
+        private Uri? _uriData;
+        #endregion
+
+        protected BaseCommand(Engine engine) : base(engine)
+        {
+
+        }
+
+        protected override void OnInitialize()
+        {
+            base.OnInitialize();
+            Configuration? conf;
+            if (!FileSystem.File.Exists(Constants.CS_CONFIGURATION_FILE))
+            {
+                WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
+                conf = new Configuration();
+            }
+            else
+            {
+                conf = JsonSerializer.Deserialize<Configuration>(
+                    FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
+                if (conf == null)
+                {
+                    conf = new Configuration();
+                    WriteWarning($"Configuration file '{Constants.CS_CONFIGURATION_FILE}' not found. Use default.");
+                }
+            }
+
+            Configuration = conf;
+            
+        }
+
+        protected virtual void OnDataReceived(MonData[] data)
+        {
+        }
+
+        protected virtual void OnGeneralReceived(MonGeneral general)
+        {
+        }
+
+        protected override void OnValidateArguments()
+        {
             base.OnValidateArguments();
 
-            if (ContainsArgument(CS_ARG_NAME_FORCE))
-            {
-                IsForce = WasArgumentSpecified(CS_ARG_NAME_FORCE);
-                if (IsForce.GetValueOrDefault(false))
-                    WriteWarning("Force flag is set!");
-            }
-
-            IsDebug = WasArgumentSpecified(CS_ARG_NAME_DEBUG);
-
-            if (IsDebug)
-                DebugConsoleWriter.IsWriteDebugEnabled = true;
-        }
-
-        protected override IEnumerable<AbstractArgument> OnSetupArguments()
-        {
-            FileSystem = new FileSystem();
-            var args = base.OnSetupArguments().ToList();
-            args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
-            args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
-            return args;
-        }
-
-
-        protected void AppendForceArgument(IList<AbstractArgument> args, string alternativeUserDescription = null)
-        {
-            args.Add(new FlagArgument(CS_ARG_NAME_FORCE, string.IsNullOrEmpty(alternativeUserDescription) ? CS_ARG_DESC_FORCE : alternativeUserDescription, CS_ARG_HINT_FORCE, false));
-        }
-
-        public void Log(LogSeverityEnum type, string message, Exception e = null)
-        {
-            if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
-                return;
-            WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
-        }
-
-        protected void WriteDtoToConsole(IDto dto)
-        {
-            var props = dto.GetAllPropertyValues();
-            using (var wName = new ConsoleWriter(ConsoleColor.White))
-            {
-                using (var wValue = new ConsoleWriter(ConsoleColor.Yellow))
-                {
-                    var maxKeyLength = props.Keys.Max(x => x.Length);
-                    foreach (var prop in props)
-                    {
-                        wName.Write(prop.Key);
-                        wName.Write(new string(' ', maxKeyLength - prop.Key.Length) + " :\t");
-                        wValue.WriteLine(prop.Value?.ToString());
-                    }
-                }
-            }
-        }
-    }
-}
+            IsDebug = WasArgumentSpecified(CS_ARG_NAME_DEBUG);
+
+            if (IsDebug)
+                DebugConsoleWriter.IsWriteDebugEnabled = true;
+                
+            _uriGeneral = ContainsArgument(CS_ARG_NAME_URIGENERAL) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIGENERAL)) : Configuration.GeneralUri;
+            _uriData = ContainsArgument(CS_ARG_NAME_URIDATA) ? GetUri(GetArgumentValueOrDefault<string>(CS_ARG_NAME_URIDATA)) : Configuration.DataUri;
+            
+        }
+
+        protected override IEnumerable<AbstractArgument> OnSetupArguments()
+        {
+            FileSystem = new FileSystem();
+            var args = base.OnSetupArguments().ToList();
+            args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
+            args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
+            args.Add(new NamedArgument(CS_ARG_NAME_URIGENERAL, CS_ARG_DESC_URIGENERAL, CS_ARG_HINT_URIGENERAL, TypeValuesEnum.String,string.Empty, false));
+            args.Add(new NamedArgument(CS_ARG_NAME_URIDATA, CS_ARG_DESC_URIDATA, CS_ARG_HINT_URIDATA, TypeValuesEnum.String,string.Empty, false));
+            return args;
+        }
+
+
+        public void Log(LogSeverityEnum type, string message, Exception e = null)
+        {
+            if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
+                return;
+            WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
+        }
+
+
+
+        private Uri? GetUri(string uriAsString)
+        {
+            if (string.IsNullOrEmpty(uriAsString))
+                return null;
+            if (!Uri.TryCreate(uriAsString, UriKind.Absolute, out var result))
+                return null;
+            return result;
+        }
+
+        protected override void BeginExecute()
+        {
+            base.BeginExecute();
+            var rcvCfg = QMonReceiverConfiguration.CreateDefault();
+            rcvCfg.SourceUriGeneral = _uriGeneral ?? Configuration.GeneralUri;
+            rcvCfg.SourceUriData = _uriData ?? Configuration.DataUri;
+            WriteInfo($"Starting listener for general ({rcvCfg.SourceUriGeneral}) and data ({rcvCfg.SourceUriData}) ...");
+            Receiver = new QMonReceiver(this.FileSystem, rcvCfg,new ConsoleLogger(), OnGeneralReceived, OnDataReceived);
+        }
+
+        protected override Result EndExecute(Result incommingResult)
+        {
+            WriteInfo("Stopping listener...");
+            Receiver?.Dispose();
+            return base.EndExecute(incommingResult);
+        }
+
+    }
+}

+ 38 - 0
Workbench/QMonitor/qmonlib.console/Commands/MonitorCommand.cs

@@ -0,0 +1,38 @@
+using Quadarax.Foundation.Core.QConsole;
+using Quadarax.Foundation.Core.QConsole.Attributes;
+using Quadarax.Foundation.Core.Value;
+
+namespace Quadarax.Foundation.Core.QMonitor.Console.Commands
+{
+    [CommandDefinition]
+    internal class MonitorCommand : BaseCommand
+    {
+        #region *** Constants ***
+
+        private const string CS_CMD_MONITOR_NAME = "monitor";
+        private const string CS_CMD_MONITOR_DESC = "Monitor specific single channel";
+
+        protected const string CS_ARG_NAME_CHANNEL = "channel";
+        private const string CS_ARG_HINT_CHANNEL = "<chennel>";
+        private const string CS_ARG_DESC_CHANNEL = "Specify full channel name to monitor. Channel is complex key instanceIdentifier:channelName";
+
+        protected const string CS_ARG_NAME_DEBUG = "dbg";
+        private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
+        private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
+
+        #endregion
+
+
+        public MonitorCommand(Engine engine) : base(engine)
+        {
+        }
+
+        protected override Result OnExecute()
+        {
+            throw new NotImplementedException();
+        }
+
+        public override string Name => CS_CMD_MONITOR_NAME;
+        public override string Description => CS_CMD_MONITOR_DESC;
+    }
+}

+ 61 - 0
Workbench/QMonitor/qmonlib.console/Commands/ScanCommand.cs

@@ -0,0 +1,61 @@
+using Quadarax.Foundation.Core.QConsole;
+using Quadarax.Foundation.Core.QConsole.Attributes;
+using Quadarax.Foundation.Core.Value;
+
+namespace Quadarax.Foundation.Core.QMonitor.Console.Commands
+{
+    [CommandDefinition]
+    internal class ScanCommand : BaseCommand
+    {
+
+        #region *** Constants ***
+
+        private const string CS_CMD_SCAN_NAME = "scan";
+        private const string CS_CMD_SCAN_DESC = "Scans for general metadata informations.";
+
+        #endregion
+
+        
+        #region *** Properties ***
+        public override string Name => CS_CMD_SCAN_NAME;
+        public override string Description => CS_CMD_SCAN_DESC;
+        #endregion
+
+        #region *** Private fields ***
+        private IDictionary<string, MonItemGeneral> _cachedGenerals = new Dictionary<string, MonItemGeneral>();
+        #endregion
+
+        #region *** Constructor ***
+        public ScanCommand(Engine engine) : base(engine)
+        {
+        }
+        #endregion  
+
+        #region *** Execute ***
+        protected override Result OnExecute()
+        {
+            var waitToData = TimeSpan.FromSeconds(10);
+            WriteInfo($"Waiting for collecting general metadata {waitToData.TotalSeconds} seconds...");
+            Task.Delay(waitToData).Wait();
+            WriteInfo($"Collected {_cachedGenerals.Count} items above.");
+            return new Result();
+        }
+        #endregion
+
+        #region *** Private Operations ***
+        protected override void OnGeneralReceived(MonGeneral general)
+        {
+            foreach (var item in general.Items)
+            {
+                if(!_cachedGenerals.ContainsKey($"{general.InstanceIdentifier}_{item.Name}"))
+                {
+                    _cachedGenerals.Add($"{general.InstanceIdentifier}_{item.Name}", item);
+                    WriteInfo("[" + general.InstanceIdentifier + "] " + item.Name);
+                }
+            }
+        }
+
+        #endregion
+
+    }
+}

+ 1 - 1
Workbench/QMonitor/qmonlib.console/Program.cs

@@ -6,7 +6,7 @@ using Quadarax.Foundation.Core.QConsole.Configuration;
 using Quadarax.Foundation.Core.QConsole.Context;
 using Quadarax.Foundation.Core.Reflection.Extensions;
 
-var config = new StartupConfiguration("BO Console", Assembly.GetExecutingAssembly().GetName().Version);
+var config = new StartupConfiguration("QMonitor Console", Assembly.GetExecutingAssembly().GetName().Version);
 config.ConsoleCopyright = Assembly.GetExecutingAssembly().GetCopyright();
 config.AllowInteractive = false;
 config.WaitOnKeyInNonInteractiveMode = false;

+ 11 - 0
Workbench/QMonitor/qmonlib.console/Properties/launchSettings.json

@@ -0,0 +1,11 @@
+{
+  "profiles": {
+    "qmonlib.console": {
+      "commandName": "Project"
+    },
+    "Scan": {
+      "commandName": "Project",
+      "commandLineArgs": "scan -dbg"
+    }
+  }
+}

+ 29 - 0
Workbench/QMonitor/qmonlib/Interfaces/IReceiverHandler.cs

@@ -0,0 +1,29 @@
+namespace Quadarax.Foundation.Core.QMonitor.Interfaces
+{
+    public delegate void ReceiverHandlerDataChanged(IReceiverHandler sender, ReceiverHandlerStateEnum state);
+    public interface IReceiverHandler : IDisposable
+    {
+        string InstanceIdentifier { get;  }
+        MonItemGeneral Descriptor { get; }
+        ReceiverHandlerStateEnum State { get; }
+        bool IsLiveBroadcast { get; }
+        DateTime Timestamp { get; }
+
+        event ReceiverHandlerDataChanged? DataChanged;
+
+        void Play(bool isLive);
+        void Stop();
+        void Rewind(TimeSpan? duration = null, bool isBackward = false);
+        void Record();
+
+        DateTime? GetFirstAvailableDataTimestamp();
+    }
+
+    public enum ReceiverHandlerStateEnum
+    {
+        Play,
+        Stop,
+        Rewind,
+        Record
+    }
+}

+ 7 - 0
Workbench/QMonitor/qmonlib/QMonReceiver.cs

@@ -6,6 +6,7 @@ using System.Text;
 using System.Text.Json;
 using System.Text.Json.Serialization;
 using Quadarax.Foundation.Core.Logging;
+using Quadarax.Foundation.Core.QMonitor.Interfaces;
 
 namespace Quadarax.Foundation.Core.QMonitor
 {
@@ -58,6 +59,12 @@ namespace Quadarax.Foundation.Core.QMonitor
         }
         #endregion
 
+        #region *** Public Operations ***
+        public IReceiverHandler CreateHandler(string instanceIdentifier, string name)
+        {
+            return null;
+        }
+        #endregion
 
         #region *** Private overrides ***
 

+ 63 - 0
Workbench/QMonitor/qmonlib/QMonReceiverHandler.cs

@@ -0,0 +1,63 @@
+using Quadarax.Foundation.Core.QMonitor.Interfaces;
+using Quadarax.Foundation.Core.Object;
+
+namespace Quadarax.Foundation.Core.QMonitor
+{
+    public class QMonReceiverHandler : DisposableObject, IReceiverHandler
+    {
+        #region *** PRoperties ***
+
+        public string InstanceIdentifier { get; }
+        public MonItemGeneral Descriptor { get; }
+        public ReceiverHandlerStateEnum State => throw new NotImplementedException();
+        public bool IsLiveBroadcast => throw new NotImplementedException();
+        public DateTime Timestamp => throw new NotImplementedException();
+
+        public event ReceiverHandlerDataChanged? DataChanged;
+        #endregion
+
+        #region *** Constructor ***
+        internal QMonReceiverHandler(string instanceIdentifier, MonItemGeneral descriptor)
+        {
+            if(string.IsNullOrEmpty(instanceIdentifier)) throw new ArgumentNullException(nameof(instanceIdentifier));
+
+            InstanceIdentifier = instanceIdentifier;
+            Descriptor = descriptor ?? throw new ArgumentNullException(nameof(descriptor));
+        }
+        #endregion
+
+        #region *** Public Operations ***
+        public DateTime? GetFirstAvailableDataTimestamp()
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Play(bool isLive)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Record()
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Rewind(TimeSpan? duration = null, bool isBackward = false)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Stop()
+        {
+            throw new NotImplementedException();
+        }
+        #endregion
+
+        #region *** Private Operations ***
+        protected override void OnDisposing()
+        {
+            DataChanged = null;
+        }
+        #endregion
+    }
+}

+ 4 - 0
Workbench/QMonitor/qmonlib/qmonlib.csproj

@@ -9,6 +9,10 @@
     <AssemblyName>qdr.fnd.core.qmonlib</AssemblyName>
   </PropertyGroup>
 
+  <ItemGroup>
+    <None Remove="j4uvxxpn.zse~" />
+  </ItemGroup>
+
   <ItemGroup>
     <PackageReference Include="qdr.fnd.core" Version="0.0.1-alpha" />
     <PackageReference Include="System.IO.Abstractions" Version="19.2.69" />