Esempio n. 1
0
        public void StartThread()
        {
            Byte[]        bytes, sendBytes;
            NetworkStream stream = mcduClient.GetStream();

            var  ledsState      = new Dictionary <int, bool>();
            bool firstIteration = false;

            while (true)
            {
                try
                {
                    if (mcduClient.ReceiveBufferSize > 0)
                    {
                        bytes = new byte[mcduClient.ReceiveBufferSize];
                        stream.Read(bytes, 0, mcduClient.ReceiveBufferSize);
                        string msg = Encoding.ASCII.GetString(bytes);

                        foreach (
                            string op
                            in msg.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                            )
                        {
                            if (op.Contains("SW:") && op.Contains(":ON"))
                            {
                                int code = int.Parse(op.Split(':')[1]);
                                xPlaneConnector.SendCommand(
                                    dataRefManager.getXPlaneCommands(mcduNumber)[code]
                                    );
                            }
                        }
                    }

                    // Done on purpose to sync LEDs at startup
                    // TODO Clean this code
                    if (!firstIteration)
                    {
                        foreach (KeyValuePair <int, DataRefElement> dataRef in dataRefManager.getLedXPlaneDataRefs(mcduNumber))
                        {
                            ledsState.Add(dataRef.Key, false);

                            xPlaneConnector.Subscribe(dataRef.Value, 5, (dr, value) =>
                            {
                                var state = value != 0;

                                if (ledsState[dataRef.Key] != state)
                                {
                                    sendBytes = Encoding.ASCII.GetBytes("B1:LED:" + dataRef.Key + ":" + (state ? "ON" : "OFF") + Environment.NewLine);
                                    stream.Write(sendBytes, 0, sendBytes.Length);
                                }


                                ledsState[dataRef.Key] = state;
                            });
                        }

                        firstIteration = true;
                    }
                }
                catch { }
            }
        }
Esempio n. 2
0
 private void btBatteryOn_Click(object sender, EventArgs e)
 {
     connector.SendCommand(XPlaneConnector.Commands.ElectricalBattery1On);
 }
 public void OnXplaneCommand(object sender, ClientEventArgs <XPlaneCommand> e)
 {
     mXplaneConnector.SendCommand(e.Value);
 }