Exemple #1
0
 /// <summary>
 /// Fires the adapter removed event.
 /// </summary>
 /// <param name="adapter">The adapter.</param>
 protected virtual void fire_AdapterRemoved(IBrailleIOAdapter adapter)
 {
     if (AdapterRemoved != null)
     {
         try
         {
             AdapterRemoved.Invoke(this, new IBrailleIOAdapterEventArgs(adapter));
         }
         catch (Exception ex) { System.Diagnostics.Debug.WriteLine("ERROR in AbstractBrailleIOAdapterManagerBase fire_AdapterRemoved:\r\n" + ex); }
     }
 }
        /// <summary>
        /// Removes an adapter from this switch.
        /// </summary>
        /// <param name="adapter">Adapter to be removed.</param>
        /// <param name="stopAdapter">Whether the adapter should be stopped after removal. Default value is <see langword="false"/>.</param>
        public void RemoveAdapter(IRemoteHubAdapter<byte[]> adapter, bool stopAdapter = false)
        {
            adapter.RemoteClientUpdated -= OnAdapterRemoteClientUpdated;
            adapter.RemoteClientRemoved -= OnAdapterRemoteClientRemoved;

            adapter.RemoveOnMessageReceivedCallback(OnPrivateMessageReceivedCallback);

            if (adapters.TryGetValue(adapter, out var idList))
            {
                if (adapters.TryRemove(adapter, out _))
                {
                    foreach (var remoteClientId in idList.Keys)
                    {
                        RemoveAdapterFromAdapterOfClients(remoteClientId, adapter);
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }

            if (stopAdapter)
            {
                adapter.Stop(); //don't need to remove clients due to stopping applied.
            }
            else
            {
                //remove clients from the adapter being removed.

                Guid[] allClientId;
                lock (adapterOfClients)
                {
                    allClientId = adapterOfClients.Where(i => i.Value != adapter).Select(i => i.Key).ToArray();
                }

                adapter.RemoveClient(allClientId);
            }

            adapter.ConnectionErrorOccurred -= OnAdapterConnectionErrorOccurred;
            adapter.AdapterStarted -= OnAdapterStarted;
            adapter.AdapterStopped -= OnAdapterStopped;

            AdapterRemoved?.Invoke(this, new AdapterEventArgs(adapter));
        }
Exemple #3
0
        /// <summary>Refreshes the device list and their state, and raises events.</summary>
        public static void Update()
        {
            configuration.Refresh();

            var adaptersToRefresh     = new List <DisplayDevice>(adaptersByDeviceName.Count);
            var removedAdapters       = new List <string>(adaptersByDeviceName.Keys);
            var newAdapters           = new List <DisplayAdapter>();
            var primaryAdapterChanged = false;

            DisplayAdapter adapter;
            var            displayDevices = DisplayAdapter.EnumDisplayDevices(null, false);
            var            aMax           = displayDevices.Count;

            for (var a = 0; a < aMax; ++a)
            {
                var displayDevice = displayDevices[a];

                if (adaptersByDeviceName.TryGetValue(displayDevice.DeviceName, out adapter))
                {
                    adaptersToRefresh.Add(displayDevice);
                    removedAdapters.Remove(adapter.DeviceName);
                }
                else
                {
                    adapter = new DisplayAdapter(displayDevice);
                    GetDisplayConfigInfo(adapter);
                    adaptersByDeviceName.Add(adapter.DeviceName, adapter);
                    newAdapters.Add(adapter);
                }

                if (adapter.State.HasFlag(DisplayAdapterStateIndicators.PrimaryDevice) && (primaryAdapterDeviceName != adapter.DeviceName))
                {
                    primaryAdapterChanged    = (primaryAdapterDeviceName != null);
                    primaryAdapterDeviceName = displayDevice.DeviceName;
                }
            }

            while (removedAdapters.Count > 0)
            {
                var s = removedAdapters[0];
                removedAdapters.RemoveAt(0);

                adapter = adaptersByDeviceName[s];
                adaptersByDeviceName.Remove(s);

                adapter.OnRemoved();
                AdapterRemoved?.Invoke(null, new DisplayDeviceManagerEventArgs(adapter));
            }

            while (newAdapters.Count > 0)
            {
                AdapterAdded?.Invoke(null, new DisplayDeviceManagerEventArgs(newAdapters[0]));
                newAdapters.RemoveAt(0);
            }

            while (adaptersToRefresh.Count > 0)
            {
                var device = adaptersToRefresh[0];
                adaptersToRefresh.RemoveAt(0);
                if (adaptersByDeviceName.TryGetValue(device.DeviceName, out adapter))
                {
                    adapter.Refresh(ref device);
                    GetDisplayConfigInfo(adapter);
                }
            }


            if (primaryAdapterChanged)
            {
                PrimaryAdapterChanged?.Invoke(null, EventArgs.Empty);
            }

            isInitialized = true;
        }