コード例 #1
0
        private void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args)
        {
            if (Debug.Level == 2)
            {
                Debug.Console(2, this, "RX: '{0}'",
                              ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text);
            }

            //Debug.Console(1, this, "RX: '{0}'", args.Text);

            try
            {
                DeviceRx = args.Text;

                CommandPassthruFeedback.FireUpdate();

                if (args.Text.IndexOf("Welcome to the Tesira Text Protocol Server...", StringComparison.Ordinal) > -1)
                {
                    // Indicates a new TTP session
                    // moved to CustomActivate() method
                    CommunicationMonitor.Start();
                    CrestronInvoke.BeginInvoke((o) => HandleAttributeSubscriptions());
                }
                else if (args.Text.IndexOf("! ", StringComparison.Ordinal) > -1)
                {
                    // response is from a subscribed attribute

                    //(if(args.Text

                    const string pattern = "! [\\\"](.*?[^\\\\])[\\\"] (.*)";

                    var match = Regex.Match(args.Text, pattern);

                    if (!match.Success)
                    {
                        return;
                    }

                    var customName = match.Groups[1].Value;
                    var value      = match.Groups[2].Value;

                    AdvanceQueue(args.Text);

                    foreach (var controlPoint in Faders.Where(controlPoint => customName == controlPoint.Value.LevelCustomName || customName == controlPoint.Value.MuteCustomName))
                    {
                        controlPoint.Value.ParseSubscriptionMessage(customName, value);
                        return;
                    }
                    foreach (var controlPoint in Dialers.Where(controlPoint => customName == controlPoint.Value.AutoAnswerCustomName || customName == controlPoint.Value.ControlStatusCustomName || customName == controlPoint.Value.DialerCustomName))
                    {
                        controlPoint.Value.ParseSubscriptionMessage(customName, value);
                        return;
                    }
                    foreach (var controlPoint in States.Where(controlPoint => customName == controlPoint.Value.StateCustomName))
                    {
                        controlPoint.Value.ParseSubscriptionMessage(customName, value);
                        return;
                    }

                    foreach (var controlPoint in Switchers.Where(controlPoint => customName == controlPoint.Value.SelectorCustomName))
                    {
                        controlPoint.Value.ParseSubscriptionMessage(customName, value);
                        return;
                    }

                    foreach (var controlPoint in Meters.Where(controlPoint => customName == controlPoint.Value.MeterCustomName))
                    {
                        controlPoint.Value.ParseSubscriptionMessage(customName, value);
                        return;
                    }

                    // same for dialers
                    // same for switchers
                }
                else if (args.Text.IndexOf("+OK", StringComparison.Ordinal) > -1)
                {
                    if (args.Text == "+OK")       // Check for a simple "+OK" only 'ack' repsonse or a list response and ignore
                    {
                        return;
                    }
                    // response is not from a subscribed attribute.  From a get/set/toggle/increment/decrement command
                    //string pattern = "(?<=\" )(.*?)(?=\\+)";
                    //string data = Regex.Replace(args.Text, pattern, "");

                    AdvanceQueue(args.Text);
                }
                else if (args.Text.IndexOf("-ERR", StringComparison.Ordinal) > -1)
                {
                    // Error response
                    Debug.Console(2, this, "Error From DSP: '{0}'", args.Text);
                    switch (args.Text)
                    {
                    case "-ERR ALREADY_SUBSCRIBED":
                    {
                        WatchDogSniffer = false;
                        AdvanceQueue(args.Text);
                        break;
                    }


                    default:
                    {
                        WatchDogSniffer = false;

                        AdvanceQueue(args.Text);
                        break;
                    }
                    }
                }
            }
            catch (Exception e)
            {
                if (Debug.Level == 2)
                {
                    Debug.Console(2, this, "Error parsing response: '{0}'\n{1}", args.Text, e);
                }
            }
        }