Exemplo n.º 1
0
        private void onRaumFeldEventPropertySetReceived(RaumFeldEventPropertySet args)
        {
            NetWorkSubscriberPayload payload = SubscriberDictionary.Select(d => d).Where(d => d.Value == args.EventSID).FirstOrDefault().Key;

            if (payload == null)
            {
                return;
            }

            foreach (var property in args.Properties)
            {
                if (property.SystemUpdateID != null)
                {
                    RaumFeldEvent raumFeldEvent = new RaumFeldEvent()
                    {
                        MediaDevice = payload.MediaDevice,
                        EventSID    = args.EventSID,
                    };
                    raumFeldEvent.ChangedValues.Add("SystemUpdateID", property.SystemUpdateID);
                    eventAggregator.GetEvent <SystemUpdateIDChangedEvent>().Publish(raumFeldEvent);
                }
                if (property.LastChange != null)
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(property.LastChange);
                    XmlNodeList xmlNodes = xmlDocument.SelectNodes("//*");

                    foreach (var item in xmlNodes)
                    {
                        RaumFeldEvent raumFeldEvent = new RaumFeldEvent()
                        {
                            MediaDevice = payload.MediaDevice,
                            EventSID    = args.EventSID,
                        };

                        switch ((string)item.NamespaceUri)
                        {
                        case "urn:schemas-upnp-org:metadata-1-0/RCS/":
                            raumFeldEvent.SericeType = ServiceTypes.RENDERINGCONTROL;
                            break;

                        case "urn:schemas-upnp-org:metadata-1-0/AVT/":
                            raumFeldEvent.SericeType = ServiceTypes.AVTRANSPORT;
                            break;

                        default:
                            break;
                        }

                        var values = new Dictionary <string, string>();
                        foreach (var a in item.Attributes)
                        {
                            raumFeldEvent.ChangedValues.Add(a.NodeName, (string)a.NodeValue);
                        }

                        switch (item.NodeName)
                        {
                        // RenderingControlEvents
                        case "Mute":
                            eventAggregator.GetEvent <MuteChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "Volume":
                            eventAggregator.GetEvent <VolumeChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "RoomMutes":
                            eventAggregator.GetEvent <RoomMutesChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "RoomVolumes":
                            eventAggregator.GetEvent <RoomVolumesChangedEvent>().Publish(raumFeldEvent);
                            break;

                        // AVTransportEvents
                        case "AVTransportURI":
                            eventAggregator.GetEvent <AVTransportURIChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "AVTransportURIMetaData":
                            eventAggregator.GetEvent <AVTransportURIMetaDataChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "CurrentTrack":
                            eventAggregator.GetEvent <CurrentTrackChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "CurrentTrackURI":
                            eventAggregator.GetEvent <CurrentTrackURIChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "CurrentPlayMode":
                            eventAggregator.GetEvent <CurrentPlayModeChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "CurrentTrackMetaData":
                            eventAggregator.GetEvent <CurrentTrackMetaDataChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "CurrentTransportActions":
                            eventAggregator.GetEvent <CurrentTransportActionsChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "NumberOfTracks":
                            eventAggregator.GetEvent <NumberOfTracksChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "PowerState":
                            eventAggregator.GetEvent <PowerStateChangedEvent>().Publish(raumFeldEvent);
                            break;

                        case "RoomStates":
                            eventAggregator.GetEvent <RoomStatesChangedEvent>().Publish(raumFeldEvent);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private async void onConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            //await DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
            //{
            try
            {
                Dictionary <string, string> resultHeaders = new Dictionary <string, string>();

                // Add "\r\n" to remove EndOfLine
                string[] stringSeparators = new string[] { ": ", "\r\n" };
                string[] linesplit;

                using (DataReader dataReader = new DataReader(args.Socket.InputStream))
                {
                    string line     = string.Empty;
                    byte[] readByte = new byte[1];

                    // Read the HTTP-Headers
                    do
                    {
                        uint l = await dataReader.LoadAsync(1);

                        if (l == 0)
                        {
                            break;
                        }
                        dataReader.ReadBytes(readByte);
                        line += Encoding.ASCII.GetString(readByte);

                        if (line.Length > 2 && line.Substring(line.Length - 2) == "\r\n")
                        {
                            linesplit = line.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
                            line      = string.Empty;
                            if ((linesplit?.Count() ?? 0) == 2)
                            {
                                resultHeaders[linesplit[0]] = linesplit[1];
                            }
                        }
                        // Blankline == Headers finished; Content starts
                        else if (line.Length == 2 && line == "\r\n")
                        {
                            break;
                        }
                    } while (true);

                    if (resultHeaders.TryGetValue("Content-Length", out string value))
                    {
                        if (UInt32.TryParse(value, out uint length))
                        {
                            uint   resultLength = 0;
                            string data         = string.Empty;

                            resultLength = await dataReader.LoadAsync(length);

                            if (resultLength != 0)
                            {
                                byte[] byteBuffer = new byte[resultLength];
                                dataReader.ReadBytes(byteBuffer);
                                data += Encoding.UTF8.GetString(byteBuffer);

                                RaumFeldEventPropertySet propset = data.ToString().Deserialize <RaumFeldEventPropertySet>();

                                if (propset != null)
                                {
                                    propset.EventSID = resultHeaders["SID"];
                                    eventAggregator.GetEvent <RaumFeldEventPropertySetReceivedEvent>().Publish(propset);
                                }
                            }
                        }
                    }

                    dataReader.DetachStream();
                }

                using (DataWriter dataWriter = new DataWriter(args.Socket.OutputStream))
                {
                    dataWriter.WriteBytes(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n\r\n"));
                    await dataWriter.FlushAsync();

                    dataWriter.DetachStream();
                }

                args.Socket.InputStream.Dispose();
                args.Socket.OutputStream.Dispose();
                args.Socket.Dispose();
            }
            catch (Exception exception)
            {
                var errorStatus = SocketError.GetStatus(exception.HResult);

                throw;
            }
            //});
        }