/// <summary>
        /// Main Background worker thread entry
        /// </summary>
        /// <param name="bgwContext">The command and control struct</param>
        public void DoWork(object sender, BgwSwitchPanelContext bgwContext)
        {
            m_worker = sender as BackgroundWorker;

            m_worker.ReportProgress(0, "SwitchPanelSupport - About to start"); // first message..

            m_bgwContext  = bgwContext;
            m_pfspManager = new PFSwPanelManager( );
            bool abort        = false;
            bool eventsHooked = false;

            // Try to connect to the HID driver
            if (m_pfspManager.OpenDevice( ))
            {
                // attach to the events from the HID device
                m_pfspManager.DeviceAttached += PfspManager_DeviceAttached;
                m_pfspManager.DeviceRemoved  += PfspManager_DeviceRemoved;
                m_pfspManager.ButtonUp       += PfspManager_ButtonUp;
                m_pfspManager.ButtonDown     += PfspManager_ButtonDown;
                m_pfspManager.RotaryChanged  += PfspManager_RotaryChanged;
                eventsHooked = true;

                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Off);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Off);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Off);

                // Try to load the config file
                m_panelConfig = SwitchPanelConfig.FromJson(m_bgwContext.ConfigFile);
                if (m_panelConfig.Valid)
                {
                    m_worker.ReportProgress(0, $" Config: {m_panelConfig.ConfigName}"); // first message..
                }
                else
                {
                    m_worker.ReportProgress(0, $" Invalid Config File - aborting"); // first message..
                    m_worker.ReportProgress(0, $"   {SwitchPanelConfig.ErrorMsg}"); // first message..
                    abort = true;
                }
            }
            else
            {
                m_worker.ReportProgress(0, "SwitchPanelSupport - Device open failed"); // first message..
                abort = true;
            }
            // Try to connect the JoystickServer
            if (int.TryParse(m_bgwContext.ServerPort, out int portNo))
            {
                if (UdpMessenger.CheckIP(m_bgwContext.ServerIP))
                {
                    m_udp = new UdpMessenger(m_bgwContext.ServerIP, portNo);
                }
                else
                {
                    m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid IP # {m_bgwContext.ServerIP}");
                }
            }
            else
            {
                m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid Port number # {m_bgwContext.ServerPort}");
            }

            // Report about incidents..

            // Task loop - wait until killed - handling happens in the events
            while (!abort)
            {
                if (m_bgwContext.LedChanged)
                {
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, m_bgwContext.L_Led);
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, m_bgwContext.N_Led);
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, m_bgwContext.R_Led);
                    m_bgwContext.LedChanged = false; // commit
                }

                Thread.Sleep(200);                    // check every 200 ms for abort or LED changes
                abort = m_worker.CancellationPending; // check once in a while
            }

            // clean UDP
            m_udp = null;

            // clean HID up
            // if the device is still attached
            if (m_devAttached)
            {
                // final message...
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Red);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Green);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Amber);

                m_pfspManager.CloseDevice( );
            }
            // events
            if (eventsHooked)
            {
                m_pfspManager.DeviceAttached -= PfspManager_DeviceAttached;
                m_pfspManager.DeviceRemoved  -= PfspManager_DeviceRemoved;
                m_pfspManager.ButtonUp       -= PfspManager_ButtonUp;
                m_pfspManager.ButtonDown     -= PfspManager_ButtonDown;
                m_pfspManager.RotaryChanged  -= PfspManager_RotaryChanged;
            }
            // the HID object
            m_pfspManager.Dispose( );
            m_pfspManager = null;

            m_worker.ReportProgress(100, "SwitchPanelSupport - Ending now"); // final message..
        }
Exemplo n.º 2
0
        /// <summary>
        /// Main Background worker thread entry
        /// </summary>
        /// <param name="bgwContext">The command and control struct</param>
        public void DoWork(object sender, BgwSwitchPanelContext bgwContext)
        {
            m_worker = sender as BackgroundWorker;

            m_worker.ReportProgress(0, "SwitchPanelSupport - About to start"); // first message..

            m_bgwContext  = bgwContext;
            m_pfspManager = new PFSwPanelManager( );
            bool abort        = false;
            bool eventsHooked = false;

            // Try to connect to the HID driver
            if (m_pfspManager.OpenDevice( ))
            {
                // attach to the events from the HID device
                m_pfspManager.DeviceAttached += PfspManager_DeviceAttached;
                m_pfspManager.DeviceRemoved  += PfspManager_DeviceRemoved;
                m_pfspManager.ButtonUp       += PfspManager_ButtonUp;
                m_pfspManager.ButtonDown     += PfspManager_ButtonDown;
                m_pfspManager.RotaryChanged  += PfspManager_RotaryChanged;
                eventsHooked = true;

                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Off);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Off);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Off);

                // Try to load the config file
                m_panelConfig = SwitchPanelConfig.FromJson(m_bgwContext.ConfigFile);
                if (m_panelConfig.Valid)
                {
                    m_worker.ReportProgress(0, $" Config: {m_panelConfig.ConfigName}"); // first message..
                }
                else
                {
                    m_worker.ReportProgress(0, $" Invalid Config File - aborting"); // first message..
                    m_worker.ReportProgress(0, $"   {SwitchPanelConfig.ErrorMsg}"); // first message..
                    abort = true;
                }
            }
            else
            {
                m_worker.ReportProgress(0, "SwitchPanelSupport - Device open failed"); // first message..
                abort = true;
            }
            if (!VJoyHandler.Instance.AreLibrariesLoaded( ))
            {
                m_worker.ReportProgress(0, $"SwitchPanelSupport - Libraries are not loaded"); // first message..
                abort = true;
            }
            else
            {
                // First try to connect the Joystick interface
                if ((m_bgwContext.JoystickNo > 0) && (m_bgwContext.JoystickNo <= 16))
                {
                    if (!VJoyHandler.Instance.Connect(m_bgwContext.JoystickNo))
                    {
                        m_worker.ReportProgress(0, $"SwitchPanelSupport - Cannot connect joystick # {m_bgwContext.JoystickNo}"); // first message..
                        // we continue with keyboard support only
                    }
                }
                else
                {
                    m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid Joystick # {m_bgwContext.JoystickNo}"); // first message..
                    // we continue with keyboard support only
                }
            }


            // Task loop - wait until killed - handling happens in the events
            while (!abort)
            {
                if (m_bgwContext.LedChanged)
                {
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, m_bgwContext.L_Led);
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, m_bgwContext.N_Led);
                    m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, m_bgwContext.R_Led);
                    m_bgwContext.LedChanged = false; // commit
                }

                Thread.Sleep(200);                    // check every 200 ms for abort or LED changes
                abort = m_worker.CancellationPending; // check once in a while
            }

            // clean vJoy up
            VJoyHandler.Instance.Disconnect( ); // shut the Joystick handler

            // clean HID up
            // if the device is still attached
            if (m_devAttached)
            {
                // final message...
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Red);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Green);
                m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Amber);

                m_pfspManager.CloseDevice( );
            }
            // events
            if (eventsHooked)
            {
                m_pfspManager.DeviceAttached -= PfspManager_DeviceAttached;
                m_pfspManager.DeviceRemoved  -= PfspManager_DeviceRemoved;
                m_pfspManager.ButtonUp       -= PfspManager_ButtonUp;
                m_pfspManager.ButtonDown     -= PfspManager_ButtonDown;
                m_pfspManager.RotaryChanged  -= PfspManager_RotaryChanged;
            }
            // the HID object
            m_pfspManager.Dispose( );
            m_pfspManager = null;

            m_worker.ReportProgress(100, "SwitchPanelSupport - Ending now"); // final message..
        }