/// <summary>
        /// Method is utilized to register an external adapter
        /// </summary>
        /// <param name="adapterDescription"></param>
        /// <returns></returns>
        public virtual MBoolResponse RegisterAdapter(MAdapterDescription adapterDescription)
        {
            //Check if the adapter description is already available
            if (RuntimeData.AdapterInstances.ContainsKey(adapterDescription.ID))
            {
                Console.WriteLine($"Adapter: {adapterDescription.Name} already available -> nothing to do.");
                return(new MBoolResponse(true));
            }

            ////Add a new remote adapter
            RemoteAdapter remoteAdapter = new RemoteAdapter(adapterDescription);

            remoteAdapter.Start();

            //Add the newly generated adapter description
            if (!RuntimeData.AdapterInstances.TryAdd(adapterDescription.ID, remoteAdapter))
            {
                Console.WriteLine($"Cannot add adapter description {adapterDescription.Name}");
            }

            //Fire event
            this.OnAdapterRegistered?.Invoke(this, remoteAdapter);

            return(new MBoolResponse(true));
        }
Пример #2
0
        /// <summary>
        /// Method starts the process
        /// </summary>
        public void Start()
        {
            //Cyclicyll update the status
            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                while (!this.Aborted)
                {
                    System.Threading.Thread.Sleep(this.UpdateTime);

                    try
                    {
                        //Get the status
                        this.Status = this.GetStatus().GetReadableString();

                        this.Initialized = true;

                        this.InactiveTime = TimeSpan.Zero;
                        this.Active       = true;
                    }
                    catch (Exception)
                    {
                        this.InactiveTime += UpdateTime;
                        this.Active        = false;
                        this.Status        = "Inactive: " + this.InactiveTime.Duration().ToString();

                        if (this.InactiveTime > RuntimeData.InactiveRemoveTime)
                        {
                            RemoteAdapter removed = null;
                            RuntimeData.AdapterInstances.TryRemove(this.Description.ID, out removed);

                            //Fire event if it gets inactiv
                            this.OnInactive?.Invoke(this, this);

                            //UIData.SynchronizeAdapters();
                        }
                    }
                }
            });
        }
        /// <summary>
        /// Method is called if an adapter should be unregistered
        /// </summary>
        /// <param name="adapterDescription"></param>
        /// <returns></returns>
        public MBoolResponse UnregisterAdapter(MAdapterDescription adapterDescription)
        {
            if (!RuntimeData.AdapterInstances.ContainsKey(adapterDescription.ID))
            {
                //Nothing to do no adapter available
                return(new MBoolResponse(true));
            }


            if (RuntimeData.AdapterInstances.ContainsKey(adapterDescription.ID))
            {
                RemoteAdapter adapter = null;

                RuntimeData.AdapterInstances.TryGetValue(adapterDescription.ID, out adapter);

                //Dispose the adapter
                adapter.Dispose();

                //Remove the adapter
                RuntimeData.AdapterInstances.TryRemove(adapterDescription.ID, out adapter);

                //Fire event
                this.OnAdapterUnregistered?.Invoke(this, adapter);

                return(new MBoolResponse(true));
            }


            Console.WriteLine("No matching adapter to unregister found");
            return(new MBoolResponse(false)
            {
                LogData = new List <string>()
                {
                    "No matching adapter found"
                }
            });
        }