コード例 #1
0
 public IAsyncOperation<InternetRadioAddPresetResult> AddPresetAsync(AllJoynMessageInfo info, string interfaceMemberPresetName, string interfaceMemberPresetAddress)
 {
     return Task.Run(() =>
     {
         StartupTask.WriteTelemetryEvent("Action_AllJoyn");
         var newPreset = new Track();
         newPreset.Name = interfaceMemberPresetName;
         newPreset.Address = interfaceMemberPresetAddress;
         this.playlistManager.CurrentPlaylist.Tracks.Add(newPreset);
         return InternetRadioAddPresetResult.CreateSuccessResult();
     }).AsAsyncOperation();
 }
コード例 #2
0
ファイル: Track.cs プロジェクト: codekaizen/internetradio
        internal static Track Deserialize(XElement xml)
        {
            Track track = null;

            var nameElement = xml.Descendants("Name").FirstOrDefault();
            var addressElement = xml.Descendants("Address").FirstOrDefault();

            if (null != nameElement &&
                null != addressElement)
            {
                track = new Track()
                {
                    Name = nameElement.Value,
                    Address = addressElement.Value
                };
            }
            else
            {
                Debug.WriteLine("Track: Invalid track XML");
            }

            return track;
        }
コード例 #3
0
        private Track deserialzePreset(string serializedPreset)
        {
            var preset = new Track();
            var presetData = serializedPreset.Split(';');

            preset.Name = presetData.ElementAtOrDefault(0);

            Uri channelUri;
            if (Uri.TryCreate(presetData.ElementAtOrDefault(1), UriKind.Absolute, out channelUri))
            {
                preset.Address = channelUri.ToString();
            }

            return preset;
        }
コード例 #4
0
ファイル: RadioManager.cs プロジェクト: farukc/internetradio
 private void playChannel(Track track)
 {
     Debug.WriteLine("RadioManager: Play Track - " + track.Name);
     this.radioPlaybackManager.Play(new Uri(track.Address));
 }
コード例 #5
0
 private void playChannel(Track track)
 {
     if (null == track)
     {
         Debug.WriteLine("RadioManager: Play Track failed due to null track");
         return;
     }
     Debug.WriteLine("RadioManager: Play Track - " + track.Name);
     this.radioPlaybackManager.Play(new Uri(track.Address));
 }
コード例 #6
0
ファイル: WebServer.cs プロジェクト: codekaizen/internetradio
        private async Task writeResponseAsync(string request, IOutputStream os, StreamSocketInformation socketInfo)
        {
            try
            {
                request = request.TrimEnd('\0'); //remove possible null from POST request

                string[] requestParts = request.Split('/');

                // Request for the root page, so redirect to home page
                if (request.Equals("/"))
                {
                    await redirectToPage(NavConstants.HOME_PAGE, os);
                }
                // Request for the home page
                else if (request.Contains(NavConstants.HOME_PAGE))
                {
                    // Generate the default config page
                    string html = await GeneratePageHtml(NavConstants.HOME_PAGE);
                    string onState = (this.playbackManager.PlaybackState == PlaybackState.Playing) ? "On" : "Off";

                    html = html.Replace("#onState#", onState);
                    html = html.Replace("#radioVolume#", (this.playbackManager.Volume * 100).ToString());
                    html = html.Replace("#currentTrack#", this.playlistManager.CurrentTrack.Name);

                    await WebHelper.WriteToStream(html, os);

                }
                // Request for the settings page
                else if (request.Contains(NavConstants.SETTINGS_PAGE))
                {
                    if (!string.IsNullOrEmpty(request))
                    {
                        string settingParam = "";
                        IDictionary<string, string> parameters = WebHelper.ParseGetParametersFromUrl(new Uri(string.Format("http://0.0.0.0/{0}", request)));
                        bool waitForPlaying = (this.playbackManager.PlaybackState == PlaybackState.Playing);
                        bool waitForTrackChange = false;
                        string trackName = this.playlistManager.CurrentTrack.Name;

                        settingParam = "onStateVal";
                        if (parameters.ContainsKey(settingParam) && !string.IsNullOrWhiteSpace(parameters[settingParam]))
                        {
                            switch (parameters[settingParam])
                            {
                                case "On":
                                    if (!waitForPlaying)
                                        this.playbackManager.Play(new Uri(this.playlistManager.CurrentTrack.Address));
                                    waitForPlaying = true;
                                    break;
                                case "Off":
                                    this.playbackManager.Pause();
                                    waitForPlaying = false;
                                    break;
                            }
                        }
                        settingParam = "volumeSlide";
                        if (parameters.ContainsKey(settingParam) && !string.IsNullOrWhiteSpace(parameters[settingParam]))
                        {
                            double newVolume = this.playbackManager.Volume;
                            if (double.TryParse(parameters[settingParam], out newVolume))
                            {
                                newVolume = Math.Round(newVolume / 100, 2);
                                if (newVolume >= 0 && newVolume <= 1 && newVolume != this.playbackManager.Volume)
                                {
                                    this.playbackManager.Volume = newVolume;
                                }
                            }
                        }
                        settingParam = "trackAction";
                        if (parameters.ContainsKey(settingParam) && !string.IsNullOrWhiteSpace(parameters[settingParam]))
                        {
                            waitForTrackChange = true;
                            switch (parameters[settingParam])
                            {
                                case "prev":
                                    this.playbackManager.Pause();
                                    this.playlistManager.PreviousTrack();
                                    break;
                                case "next":
                                    this.playbackManager.Pause();
                                    this.playlistManager.NextTrack();
                                    break;
                                case "track":
                                    if (parameters.ContainsKey("trackName") && !string.IsNullOrWhiteSpace(parameters["trackName"]))
                                    {
                                        if (trackName != parameters["trackName"])
                                        {
                                            this.playbackManager.Pause();
                                            this.playlistManager.PlayTrack(parameters["trackName"]);
                                        }
                                        else
                                            waitForTrackChange = false;
                                    }
                                    break;
                            }
                            if (waitForTrackChange) { waitForPlaying = true; }
                        }

                        DateTime timeOut = DateTime.Now.AddSeconds(30);
                        Debug.WriteLine("Waiting on State: playback={0}; trackname={1}", (waitForPlaying)?"Playing":"NotPlaying", trackName);
                        while (DateTime.Now.CompareTo(timeOut) < 0 && (
                            (this.playbackManager.PlaybackState == PlaybackState.Playing) != waitForPlaying
                            || (this.playlistManager.CurrentTrack.Name != trackName) != waitForTrackChange
                            ));

                        if (DateTime.Now.CompareTo(timeOut) >= 0)
                        {
                            Debug.WriteLine("track did not start playing in time limit");
                        }
                    }
                    //handle UI interaction
                    await redirectToPage(NavConstants.HOME_PAGE, os);
                }
                else if (request.Contains(NavConstants.ADDSTATION_PAGE))
                {
                    string html = await GeneratePageHtml(NavConstants.ADDSTATION_PAGE);
                    string trackName = "";
                    string trackUrl = "";
                    if (!string.IsNullOrEmpty(request))
                    {
                        string settingParam = "trackName";
                        IDictionary<string, string> parameters = WebHelper.ParseGetParametersFromUrl(new Uri(string.Format("http://0.0.0.0/{0}", request)));
                        if (parameters.ContainsKey(settingParam) && !string.IsNullOrWhiteSpace(parameters[settingParam]))
                        {
                            Track trackToUpdate = playlistManager.CurrentPlaylist.Tracks.First(t => t.Name == parameters[settingParam]);
                            if (null != trackToUpdate)
                            {
                                trackName = trackToUpdate.Name;
                                trackUrl = trackToUpdate.Address;
                            }
                        }
                    }
                    html = html.Replace("var stationName = '';", string.Format("var stationName = '{0}';", trackName));
                    html = html.Replace("var stationUrl = '';", string.Format("var stationUrl = '{0}';", trackUrl));
                    await WebHelper.WriteToStream(html, os);
                }
                else if (request.Contains(NavConstants.ADDSTATIONSET_PAGE))
                {
                    if (!string.IsNullOrEmpty(request))
                    {
                        Track origTrack = null;
                        Track newTrack = null;
                        string trackAction = "";
                        IDictionary<string, string> parameters = WebHelper.ParseGetParametersFromUrl(new Uri(string.Format("http://0.0.0.0/{0}", request)));
                        if (parameters.ContainsKey("name") && !string.IsNullOrWhiteSpace(parameters["name"]))
                        {
                            if (parameters.ContainsKey("url") && !string.IsNullOrWhiteSpace(parameters["url"]))
                            {
                                newTrack = new Track() { Name = parameters["name"], Address = parameters["url"] };
                            }
                        }
                        if (parameters.ContainsKey("nameOrig") && !string.IsNullOrWhiteSpace(parameters["nameOrig"]))
                        {
                            origTrack = this.playlistManager.CurrentPlaylist.Tracks.First(t => t.Name == parameters["nameOrig"]);
                        }
                        if (parameters.ContainsKey("trackAction") && !string.IsNullOrWhiteSpace(parameters["trackAction"]))
                        {
                            trackAction = parameters["trackAction"];
                        }

                        if (null != newTrack)
                        {
                            switch (trackAction)
                            {
                                case "Update":
                                    if (null != origTrack)
                                    {
                                        this.playlistManager.CurrentPlaylist.Tracks[this.playlistManager.CurrentPlaylist.Tracks.IndexOf(origTrack)] = newTrack;
                                    }
                                    else
                                    { 
                                        this.playlistManager.CurrentPlaylist.Tracks.Add(newTrack);
                                    }
                                    break;
                                case "Remove":
                                    if (null != origTrack)
                                        this.playlistManager.CurrentPlaylist.Tracks.Remove(origTrack);
                                    break;
                                case "":
                                case "Add":
                                    this.playlistManager.CurrentPlaylist.Tracks.Add(newTrack);
                                    break;
                            }
                        }
                    }
                    await redirectToPage(NavConstants.HOME_PAGE, os);
                }
                // Request for a file that is in the Assets\Web folder (e.g. logo, css file)
                else
                {
                    using (Stream resp = os.AsStreamForWrite())
                    {
                        bool exists = true;
                        try
                        {
                            var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

                            // Map the requested path to Assets\Web folder
                            string filePath = NavConstants.ASSETSWEB + request.Replace('/', '\\');

                            // Open the file and write it to the stream
                            using (Stream fs = await folder.OpenStreamForReadAsync(filePath))
                            {
                                string contentType = "";
                                if (request.Contains("css"))
                                {
                                    contentType = "Content-Type: text/css\r\n";
                                }
                                if (request.Contains("htm"))
                                {
                                    contentType = "Content-Type: text/html\r\n";
                                }
                                string header = String.Format("HTTP/1.1 200 OK\r\n" +
                                                "Content-Length: {0}\r\n{1}" +
                                                "Connection: close\r\n\r\n",
                                                fs.Length,
                                                contentType);
                                byte[] headerArray = Encoding.UTF8.GetBytes(header);
                                await resp.WriteAsync(headerArray, 0, headerArray.Length);
                                await fs.CopyToAsync(resp);
                            }
                        }
                        catch (FileNotFoundException ex)
                        {
                            exists = false;

                            // Log telemetry event about this exception
                            var events = new Dictionary<string, string> { { "WebServer", ex.Message } };
                            TelemetryManager.WriteTelemetryEvent("FailedToOpenStream", events);
                        }

                        // Send 404 not found if can't find file
                        if (!exists)
                        {
                            byte[] headerArray = Encoding.UTF8.GetBytes(
                                                  "HTTP/1.1 404 Not Found\r\n" +
                                                  "Content-Length:0\r\n" +
                                                  "Connection: close\r\n\r\n");
                            await resp.WriteAsync(headerArray, 0, headerArray.Length);
                        }

                        await resp.FlushAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception in writeResponseAsync(): " + ex.Message);
                Debug.WriteLine(ex.StackTrace);

                // Log telemetry event about this exception
                var events = new Dictionary<string, string> { { "WebServer", ex.Message } };
                TelemetryManager.WriteTelemetryEvent("FailedToWriteResponse", events);

                try
                {
                    // Try to send an error page back if there was a problem servicing the request
                    string html = helper.GenerateErrorPage("There's been an error: " + ex.Message + "<br><br>" + ex.StackTrace);
                    await WebHelper.WriteToStream(html, os);
                }
                catch (Exception e)
                {
                    TelemetryManager.WriteTelemetryException(e);
                }
            }
        }