internal VirtualChannelGroup(Soundstructure device, string name, List <ISoundstructureItem> fromChannels)
        {
            Device              = device;
            Device.ValueChange += Device_ValueChange;
            Name            = name;
            VirtualChannels = new SoundstructureItemCollection(fromChannels);

#if DEBUG
            CrestronConsole.PrintLine("Received group \x22{0}\x22 with {1} channels",
                                      Name, Count());
#endif
        }
Beispiel #2
0
        private void SocketOnReceivedData(string data)
        {
#if DEBUG
            CrestronConsole.PrintLine("Soundstructure Rx: {0}", data);
#endif
            if (!DeviceCommunicating)
            {
                DeviceCommunicating = true;
            }

            if (data.Contains(' '))
            {
                List <string> elements = SoundstructureSocket.ElementsFromString(data);

                switch (elements[0])
                {
                case "error":
                {
                    ErrorLog.Error("Soundtructure received Error: {0}", elements[1]);
                }
                break;

                case "ran":
                    if (PresetRan != null)
                    {
                        PresetRan(this, elements[1]);
                    }
                    break;

                case "vcitem":
                    // this should be a response from the vclist command which sends back all virtual channels defined
                    try
                    {
                        List <uint> values = new List <uint>();

                        for (int element = 4; element < elements.Count(); element++)
                        {
                            values.Add(Convert.ToUInt32(elements[element]));
                        }

                        SoundstructurePhysicalChannelType type = (SoundstructurePhysicalChannelType)Enum.Parse(typeof(SoundstructurePhysicalChannelType), elements[3], true);

                        if (type == SoundstructurePhysicalChannelType.VOIP_OUT)
                        {
                            _listedItems.Add(new VoipOutChannel(this, elements[1], values.ToArray()));
                        }
                        else if (type == SoundstructurePhysicalChannelType.VOIP_IN)
                        {
                            _listedItems.Add(new VoipInChannel(this, elements[1], values.ToArray()));
                        }
                        else if (type == SoundstructurePhysicalChannelType.PSTN_OUT)
                        {
                            _listedItems.Add(new AnalogPhoneOutChannel(this, elements[1], values.ToArray()));
                        }
                        else if (type == SoundstructurePhysicalChannelType.PSTN_IN)
                        {
                            _listedItems.Add(new AnalogPhoneInChannel(this, elements[1], values.ToArray()));
                        }
                        else
                        {
                            _listedItems.Add(new VirtualChannel(this, elements[1],
                                                                (SoundstructureVirtualChannelType)Enum.Parse(typeof(SoundstructureVirtualChannelType), elements[2], true),
                                                                type, values.ToArray()));
                        }
                    }
                    catch (Exception e)
                    {
                        ErrorLog.Error("Error parsing Soundstructure vcitem: {0}", e.Message);
                    }
                    break;

                case "vcgitem":
                {
                    List <ISoundstructureItem> channels = new List <ISoundstructureItem>();
                    if (elements.Count() > 2)
                    {
                        for (int e = 2; e < elements.Count(); e++)
                        {
                            if (VirtualChannels.Contains(elements[e]))
                            {
                                channels.Add(VirtualChannels[elements[e]]);
                            }
                        }
                        VirtualChannelGroup group = new VirtualChannelGroup(this, elements[1], channels);
                        _listedItems.Add(group);
                    }
                    else
                    {
                        ErrorLog.Warn("Ignoring Soundstructure group item {0} as it has no members", elements[1]);
                    }
                }
                break;

                case "vcrename":
                {
                    List <ISoundstructureItem> channels = new List <ISoundstructureItem>();
                    foreach (VirtualChannel channel in VirtualChannels)
                    {
                        if (channel.Name == elements[1])
                        {
                            VirtualChannel newChannel = new VirtualChannel(this, elements[2],
                                                                           channel.VirtualChannelType, channel.PhysicalChannelType, channel.PhysicalChannelIndex.ToArray());
                            channels.Add(newChannel);
                        }
                        else
                        {
                            channels.Add(channel);
                        }
                    }
                    VirtualChannels = new SoundstructureItemCollection(channels);
                }
                break;

                case "vcgrename":
                {
                    List <ISoundstructureItem> groups = new List <ISoundstructureItem>();
                    foreach (VirtualChannelGroup group in VirtualChannelGroups)
                    {
                        if (group.Name == elements[1])
                        {
                            List <ISoundstructureItem> channels = new List <ISoundstructureItem>();
                            foreach (VirtualChannel channel in group)
                            {
                                channels.Add(channel);
                            }
                            VirtualChannelGroup newGroup = new VirtualChannelGroup(this, elements[2], channels);
                            groups.Add(newGroup);
                        }
                        else
                        {
                            groups.Add(group);
                        }
                    }
                    VirtualChannelGroups = new SoundstructureItemCollection(groups);
                }
                break;

                case "val":
                    // this should be a value response from a set or get
                {
                    try
                    {
                        if (elements[1] == "eth_settings" && elements[2] == "1")
                        {
                            LanAdapter = new SoundstructureEthernetSettings(elements[3]);
                            break;
                        }

                        bool commandOK = false;
                        SoundstructureCommandType commandType = SoundstructureCommandType.FADER;

                        try
                        {
                            commandType = (SoundstructureCommandType)Enum.Parse(typeof(SoundstructureCommandType), elements[1], true);
                            commandOK   = true;
                        }
                        catch
                        {
                            if (elements[1].StartsWith("voip_") && VirtualChannels.Contains(elements[2]))
                            {
                                VirtualChannel channel = VirtualChannels[elements[2]] as VirtualChannel;
                                if (channel.IsVoip && VoipInfoReceived != null)
                                {
                                    string info = data.Substring(data.IndexOf(channel.Name) + channel.Name.Length + 2,
                                                                 data.Length - data.IndexOf(channel.Name) - channel.Name.Length - 2);
                                    VoipInfoReceived(channel, new SoundstructureVoipInfoReceivedEventArgs(elements[1], info));
                                }
                            }
                        }

                        if (commandOK)
                        {
                            switch (commandType)
                            {
                            case SoundstructureCommandType.MATRIX_MUTE:
#if DEBUG
                                CrestronConsole.PrintLine("Matrix Mute Input: \x22{0}\x22 Output: \x22{1}\x22 Value: {2}", elements[2], elements[3], elements[4]);
#endif
                                break;

                            case SoundstructureCommandType.FADER:
                                if (elements[2] == "min" || elements[2] == "max")
                                {
                                    OnValueChange(elements[3], commandType, elements[2], Convert.ToDouble(elements[4]));
                                }
                                else
                                {
                                    OnValueChange(elements[2], commandType, Convert.ToDouble(elements[3]));
                                }
                                break;

                            case SoundstructureCommandType.PHONE_DIAL:
                                // Cannot parse reply for string values and we don't currently need to track this.
                                break;

                            default:
                                if (elements.Count > 3)
                                {
                                    OnValueChange(elements[2], commandType, Convert.ToDouble(elements[3]));
                                }
                                break;
                            }

                            if (!Initialized && CheckAllItemsHaveInitialised())
                            {
                                Initialized = true;

                                ErrorLog.Notice("Soundstructure Initialised");
                                CrestronConsole.PrintLine("Soundstructure Initialised!");

                                if (HasInitialised != null)
                                {
                                    HasInitialised(this);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        ErrorLog.Error("Soundstructure Rx: {0}, Error: {1}", data, e.Message);
                    }
                }
                break;

                default:
                    break;
                }
            }
            else
            {
                if (data == "vclist")
                {
                    VirtualChannels = new SoundstructureItemCollection(_listedItems);
                    _listedItems.Clear();

                    _socket.Send("vcglist");
                }
                else if (data == "vcglist")
                {
                    VirtualChannelGroups = new SoundstructureItemCollection(_listedItems);
                    _listedItems.Clear();

                    foreach (ISoundstructureItem item in VirtualChannelGroups)
                    {
                        item.Init();
                    }

                    foreach (ISoundstructureItem item in VirtualChannels)
                    {
                        if (!ChannelIsGrouped(item))
                        {
                            item.Init();
                        }
                    }
                }
            }
        }