Subversion Repositories group.NITPanels

Rev

Rev 4 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;

namespace NITNavComm
{
    public partial class MainForm : Form
    {

        public NITPanels panels = new NITPanels();
        public FSXObject fsx = new FSXObject();

        public MainForm() {
            InitializeComponent();

            Devices_Rescan();
            txtStatus.Text = "Running";

        }


        private void SimConnect_Connect() {
            if (fsx.hSimConnect == null) {
                try {
                    fsx.hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
                    txtSimConnect.Text = "Available";

                    fsx.FsxInit();

                    // Make our devices aware of the fsx object
                    // Let the device to any Fsx initilization
                    foreach (NITDevice device in panels.devices) {
                        if (device.isOpen()) {
                            device.fsx = this.fsx;
                        }
                    }

                    this.SimConnect_InitDataRequest();
                } catch (COMException) {
                    Log("Could not connect to FSX");
                    txtSimConnect.Text = "Failed";
                }
            }
        }

        private void SimConnect_Disconnect() {
            this.InitFsxClosed();
        }

        protected override void DefWndProc(ref Message m) {
            if (m.Msg == 0x402) {
                if (fsx.hSimConnect != null) {
                    fsx.hSimConnect.ReceiveMessage();
                }
            } else {
                base.DefWndProc(ref m);
            }
        }

        private void SimConnect_InitDataRequest() {
            try {

                fsx.MapEvents();

                fsx.hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
                fsx.hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
                fsx.hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
                fsx.hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
                fsx.hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);

                // Request for aircrat data. This will also configure auto-updates for other required data.
                fsx.hSimConnect.RequestDataOnSimObjectType(FSXObject.DATA_REQUESTS.AIRCRAFT, FSXObject.DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);

            } catch (COMException e) {
                Log(e.Message);
            }
        }

        private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) {
            Log("Connected to FSX.");
            txtSimConnect.Text = "Connected";
        }

        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) {
            Log("FSX has exited.");
            this.SimConnect_Disconnect();
        }

        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
            Log("SimConnect Exception: " + (uint)(data.dwException));
        }

        // Automatic updates on various variables. Set through InitFsxReady
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
            foreach (NITDevice device in panels.devices) {
                if (device.isOpen()) {
                    device.FsxEvent(data);
                }
            } 
        }

        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
            switch (data.dwRequestID) {
                // Request basic aircraft data to get the object ID
                case ((uint)FSXObject.DATA_REQUESTS.AIRCRAFT): {
                        uint ObjectID = data.dwObjectID;
                        FSXObject.Aircraft_Data aircraftdata = (FSXObject.Aircraft_Data)data.dwData[0];

                        this.fsx.simdata.objectid = ObjectID;
                        this.fsx.simdata.aircraft = aircraftdata;

                        this.fsx.FsxReady();
                        foreach (NITDevice device in panels.devices)
                            if (device.Open())
                                device.FsxReady();

                        this.InitFsxReady();

                        break;
                    }
            }           
        }

        private void InitFsxReady() {

            Log("Aircraft: "
                + this.fsx.simdata.aircraft.atc_id + ", "
                + this.fsx.simdata.aircraft.atc_model + ", "
                + this.fsx.simdata.aircraft.atc_type + ", "
                + this.fsx.simdata.aircraft.title
            );

            inputTimer.Enabled = true;
        }

        private void InitFsxClosed() {
            if (fsx.hSimConnect != null) {
                fsx.hSimConnect.Dispose();
                txtSimConnect.Text = "Available";
            } else {
                txtSimConnect.Text = "Failed";
                fsx.hSimConnect = null;
            }
        }

        private void Devices_Rescan() {
            Log("Scanning for devices...");

            panels.UsbScan();

            this.deviceGrid.DataSource = panels.devices;
            this.txtDevices.Text = panels.devices.Count.ToString();

            if (panels.devices.Count > 0) {
                this.cmDevTest.Enabled = true;
                Log(panels.devices.Count.ToString() + " devices found during scan.");

            } else {
                Log("No devices found, check connections and rescan.");
            }
        }



        private void cmdRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmdDevRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmDevTest_Click(object sender, EventArgs e) {
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;

            if (!device.Open()) {
                Log("Could not open device " + device.type + "(" + device.serial + ").");
                return;
            }
            if (device.type == "NITNavComm") {
                NITCommNavForm form = new NITCommNavForm();
                form.setDevice((NITNavCommDevice)device);
                form.Show(this);
            } else if (device.type == "NITAudioSel") {
                NITAudioSelForm form = new NITAudioSelForm();
                form.setDevice((NITAudioSelDevice)device);
                form.Show(this);
            } else {
                Log("No device type for " + device.type + " (" + device.serial + ").");
            }
        }

        public void Log(string msg) {
            txtLog.AppendText(msg + "\r\n");
        }

        public void Log(byte[] buffer) {
            StringBuilder sb = new StringBuilder();
            foreach (byte data in buffer) {
                sb.Append(data.ToString("X") + " ");
            }
            Log(sb.ToString());
        }

        private void quitNITPanels() {        
            inputTimer.Enabled = false;

            foreach (NITDevice device in panels.devices) {
                if (device.isOpen())
                    device.Close();
            }        
             
            Application.Exit();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
            if (MainForm.QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
                this.quitNITPanels();
            }
        }

        
        public static bool QuestionBox(string caption, string message) {
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.Yes) {
                return true;
            }
            return false;
        }

        public static void NITMessageBox(string caption, string message) {
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            MessageBox.Show(message, caption, buttons);
        }

        private void txtLog_VisibleChanged(object sender, EventArgs e) {
            if (txtLog.Visible) {
                txtLog.SelectionStart = txtLog.TextLength;
                txtLog.ScrollToCaret();
            }
        }

        private void cmdSimConnect_Click(object sender, EventArgs e) {
            if (fsx.hSimConnect != null)
                this.SimConnect_Disconnect();

            this.SimConnect_Connect();
        }

        private void cmdRequest_Click(object sender, EventArgs e) {

        }

        private void inputTimer_Tick(object sender, EventArgs e) {
            // @TODO: Each device now has its own input timer, remove the below
            //foreach (NITDevice device in panels.devices) {
            //    if (device.isOpen())
            //        device.SimButtons();
            //}

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
            this.quitNITPanels();
        }

        private void debugTimer_Tick(object sender, EventArgs e) {
            
        }

    }
}