Exemple #1
0
        public void AsyncCommand(string cmd, string parameter)
        {
            var commandInfo = new CommandInfo {
                Command = cmd, Parameter = parameter
            };

            _parent.Log("JRiver: MPC REMOTE ********************");
            var bw = new BackgroundWorker();

            bw.DoWork += AsyncCommandWorker;
            bw.RunWorkerAsync(commandInfo);
        }
Exemple #2
0
        public void RefreshNowPlaying()
        {
            lock (Locker)
            {
                if (_parent.IsConnected())
                {
                    try
                    {
                        _parent.Log("JRiver: Commencing NowPlaying Check for NowPlaying Screen Update");
                        string NPurl   = "http://" + _parent.IP + ":" + _parent.Port + "/MCWS/v1/Playback/Info?Zone=-1" + "&Token=" + _parent.JRiverAuthToken;
                        var    request = WebRequest.Create(NPurl);
                        //var newPlayback = false;

                        request.Headers.Add("Token", _parent.JRiverAuthToken);
                        var response = request.GetResponse();

                        if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
                        {
                            //  Use MPC Remote
                            _parent.MpcLoaded = true;
                            // Get the stream containing content returned by the server.
                            System.IO.Stream dataStream = response.GetResponseStream();
                            // Open the stream using a StreamReader.
                            System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                            XmlSerializer          serializer = new XmlSerializer(typeof(Jriver.Api.InfoZone.Response));
                            var deserialized = (Jriver.Api.InfoZone.Response)serializer.Deserialize(reader);
                            //_parent.Log("InfoZone: " + reader.ReadToEnd().ToString());
                            var playbackState = getItemName(deserialized, "State");

                            _parent.Log("Jriver: Player State:" + playbackState);
                            _parent.Log("Jriver: isNewMedia:" + _nowPlaying.IsNewMedia);

                            if (playbackState == "1")
                            {
                                _nowPlaying.IsPaused  = true;
                                _nowPlaying.IsPlaying = false;
                            }
                            if (playbackState == "2")
                            {
                                _nowPlaying.IsPaused  = false;
                                _nowPlaying.IsPlaying = true;
                            }
                            if (playbackState == "0")
                            {
                                _nowPlaying.FileName  = "";
                                _nowPlaying.Title     = "";
                                _nowPlaying.IsPaused  = false;
                                _nowPlaying.IsPlaying = false;
                                // _nowPlaying.FanartURL = "";
                                // _nowPlaying.ThumbURL = "";
                                // _nowPlaying.IsNewMedia = false;
                                _parent.Log("JRiver Log: Nothing is Playing");
                                return;
                            }

                            _nowPlaying.Title = String.IsNullOrEmpty(getItemName(deserialized, "Name")) ? "Blank" : getItemName(deserialized, "Name");
                            _parent.Log("JRiver:NP NowPlaying.title:" + _nowPlaying.Title);
                            var fileFields = getFileInfo(getItemName(deserialized, "FileKey"));
                            _nowPlaying.FileName = String.IsNullOrEmpty(getFieldValue(fileFields, "Filename")) ? "NotGiven" : getFieldValue(fileFields, "Filename");

                            if (_nowPlaying.IsNewMedia)
                            {
                                _parent.Log("JRiver:  newPlayback or NewMedia is True:");
                                //        //    Console.WriteLine("" + server.art);
                                //        //    Console.WriteLine("" + server.chapterSource);
                                _nowPlaying.Director = String.IsNullOrEmpty(getFieldValue(fileFields, "Director")) ? "Unknown" : getFieldValue(fileFields, "Director");;
                                _parent.Log("JRiver:NP NowPlaying.Director:" + _nowPlaying.Director);
                                //        //     Console.WriteLine("" + server.duration);
                                //        //    Console.WriteLine("" + server.grandparentArt);
                                _nowPlaying.Plot = String.IsNullOrEmpty(getFieldValue(fileFields, "Tag Line")) ? "" : getFieldValue(fileFields, "Tag Line");
                                _parent.Log("JRiver:NP NowPlaying.Plot:" + _nowPlaying.Plot);
                                _parent.Log("JRiver:NP NowPlaying.FileName:" + _nowPlaying.FileName);

                                //        _nowPlaying.Title = String.IsNullOrEmpty(server.title) ? "" : server.title;
                                //        _parent.Log("Plex:NP NowPlaying.Title:" + _nowPlaying.Title);
                                //        // Make changes here to recognise ts recordings as TV and use regex to populate season/episode dat

                                var mediaSubType = getFieldValue(fileFields, "Media Sub Type");
                                var mediaType    = getFieldValue(fileFields, "Media Type");

                                _parent.Log("JRiver: mediaSubType:" + mediaSubType);
                                _parent.Log("JRiver: mediaType:" + mediaType);

                                if (mediaType == "Video")
                                {
                                    if (mediaSubType == "TV Show")
                                    {
                                        _nowPlaying.MediaType = "TvShow";
                                    }
                                    else
                                    {
                                        _nowPlaying.MediaType = "Movie";
                                    }
                                }
                                else if (mediaType == "Audio")
                                {
                                    _nowPlaying.MediaType = "Audio";
                                }
                                else if (mediaType == "TV")
                                {
                                    _nowPlaying.MediaType = "Movie";
                                    _nowPlaying.FileName  = ReplaceInvalidChars(_nowPlaying.FileName);
                                }
                                else
                                {
                                    _nowPlaying.MediaType = "Movie";
                                }

                                _parent.Log("JRiver:NP NowPlaying.MediaType:" + _nowPlaying.MediaType);

                                if (_nowPlaying.MediaType == "TvShow")
                                {
                                    int SeasonNumber  = 0;
                                    int EpisodeNumber = 0;
                                    int.TryParse(getFieldValue(fileFields, "Season"), out SeasonNumber);
                                    int.TryParse(getFieldValue(fileFields, "Episode"), out EpisodeNumber);
                                    _nowPlaying.SeasonNumber  = SeasonNumber;
                                    _nowPlaying.EpisodeNumber = EpisodeNumber;
                                    _nowPlaying.Plot          = getFieldValue(fileFields, "Description");
                                    _nowPlaying.ShowTitle     = String.IsNullOrEmpty(getFieldValue(fileFields, "Series")) ? "Blank" : getFieldValue(fileFields, "Series");
                                    _parent.Log("JRiver:NP NowPlaying.Showtitle:" + _nowPlaying.ShowTitle);
                                }

                                // _nowPlaying.FirstAired = String.IsNullOrEmpty(getFieldValue(fileFields, "Date")) ? "NotGiven" : getFieldValue(fileFields, "Filename"); ;
                                var serverArt = getItemName(deserialized, "ImageURL");
                                _parent.Log("JRiver: server.Art EQUALS ===========" + serverArt);

                                // var sw = Stopwatch.StartNew();

                                var extrafanart = false;
                                var filePath    = "BAD";
                                try
                                {
                                    filePath    = Path.GetDirectoryName(_nowPlaying.FileName);
                                    extrafanart = true;  //Path Seems to be okay
                                    _parent.Log("JRIVER:   nowPlaying filePath appears intact ");
                                }
                                catch
                                {
                                    _parent.Log("JRIVER:   nowPlaying Bad filePath: Caught: ");
                                    extrafanart = false;
                                    filePath    = "BAD";
                                }

                                if (String.IsNullOrEmpty(filePath))
                                {
                                    filePath = "BAD";
                                }

                                string[] files = new string[] { };

                                try
                                {
                                    if (extrafanart)
                                    {
                                        files = System.IO.Directory.GetFiles(filePath);
                                    }
                                    else
                                    {
                                        files = new string[] { "Empty" };
                                    }
                                }
                                catch
                                {
                                    _parent.Log("JRIVER:  Caught exception with GetFiles/FilePath.  ");
                                    extrafanart = false;
                                    filePath    = "BAD";
                                    files       = new string[] { "Empty" };
                                }

                                string extrafanartbackdropfile = "";

                                if (extrafanart)
                                {
                                    var extrafanartpath = Path.Combine(filePath, "extrafanart");
                                    _parent.Log("JRiver:  nowPlaying extrafanartPath to check equals:" + extrafanartpath);
                                    if (Directory.Exists(extrafanartpath))
                                    {
                                        var extrafanartFiles = System.IO.Directory.GetFiles(extrafanartpath, "*.jpg");
                                        _parent.Log("JRiver:  nowPlaying extrafanartPath Exists:");
                                        _parent.Log("JRiver:  extrafanart Files: " + string.Join(",  ", extrafanartFiles));
                                        // check files
                                        if (extrafanartFiles != null && extrafanartFiles.Length > 0)
                                        {
                                            extrafanart = true;
                                            // get random item
                                            var idx = new Random().Next(extrafanartFiles.Length);
                                            _parent.Log("JRiver:  ExtrafanartFiles Index ==============" + idx);
                                            extrafanartbackdropfile = extrafanartFiles[idx];
                                            _parent.Log("JRiver:  extrabackdropfile  ==============" + extrafanartbackdropfile);
                                        }
                                    }
                                }

                                if (filePath != "BAD")
                                {
                                    var fanartPath = Path.Combine(filePath, "fanart.jpg");
                                    var LogoPath   = Path.Combine(filePath, "logo.png");
                                    var ThumbPath  = Path.Combine(filePath, "poster.jpg");
                                    var ThumbPath2 = Path.Combine(filePath, "folder.jpg");

                                    string CustomArt = "Default.";
                                    if (!String.IsNullOrEmpty(_nowPlaying.FileName))
                                    {
                                        CustomArt = _nowPlaying.FileName.Remove(_nowPlaying.FileName.LastIndexOf('.'));   // remove characters up to . deals with .ts and .mp4
                                    }

                                    CustomArt = CustomArt + ".jpg";
                                    _parent.Log("Jriver: CustomArt: Check: " + CustomArt);

                                    _parent.Log("JRiver: ** filePath ** :" + filePath);
                                    //_parent.Log("JRiver: Files in path:" + string.Join(",  ", files) );
                                    _parent.Log("JRiver: Checking.... ** fanArt.Jpg ** ** Logo.Png ** ** poster.Jpg **  ** folder.jpg ** :");
                                    int pos = Array.IndexOf(files, fanartPath);

                                    if (extrafanart == true && extrafanartbackdropfile != "")
                                    {
                                        _nowPlaying.FanartURL = extrafanartbackdropfile;
                                    }
                                    else if (pos > -1)
                                    {
                                        _nowPlaying.FanartURL = files[pos];  //if fanart.jpg exisits in directory with movie use this otherwise default to JRiver Thumb
                                    }
                                    else
                                    {
                                        _nowPlaying.FanartURL = @"http://" + _parent.IP + ":" + _parent.Port + @"/" + serverArt + "&Type=Full&Token=" + _parent.JRiverAuthToken;
                                    }


                                    pos = Array.IndexOf(files, LogoPath);
                                    if (pos > -1)
                                    {
                                        _nowPlaying.LogoURL = files[pos];
                                    }
                                    else
                                    {
                                        _nowPlaying.LogoURL = "";
                                    }
                                    pos = Array.IndexOf(files, ThumbPath);
                                    int pos2 = Array.IndexOf(files, ThumbPath2);
                                    int pos3 = Array.IndexOf(files, CustomArt);

                                    if (pos > -1)
                                    {
                                        _nowPlaying.ThumbURL = files[pos];
                                    }
                                    else if (pos2 > -1)
                                    {
                                        _nowPlaying.ThumbURL = files[pos2];
                                    }
                                    else if (pos3 > -1)
                                    {
                                        _nowPlaying.ThumbURL = files[pos3];
                                    }
                                    else
                                    {
                                        // _nowPlaying.FanartURL = @"http://" + _parent.IP + ":" + _parent.Port + @"/"+ serverArt + "&Type=Full&Token="+_parent.JRiverAuthToken;
                                        _nowPlaying.ThumbURL = @"http://" + _parent.IP + ":" + _parent.Port + @"/" + serverArt + "&Type=Full&Token=" + _parent.JRiverAuthToken;
                                    }

                                    //sw.Stop();
                                }
                                else   // No filePath information.  Nothing to get except http file from Jrvier

                                {
                                    _nowPlaying.FanartURL = @"http://" + _parent.IP + ":" + _parent.Port + @"/" + serverArt + "&Type=Full&Token=" + _parent.JRiverAuthToken;
                                    _nowPlaying.ThumbURL  = @"http://" + _parent.IP + ":" + _parent.Port + @"/" + serverArt + "&Type=Full&Token=" + _parent.JRiverAuthToken;
                                    _nowPlaying.LogoURL   = "";
                                }
                                // _parent.Log("JRIVER: STOPWATCH: Elapsed MS " + sw.ElapsedMilliseconds);


                                _parent.Log("JRiver:  nowPlaying Fanart equals:" + _nowPlaying.FanartURL);
                                _parent.Log("JRiver:  nowPlaying Logo equals:" + _nowPlaying.LogoURL);
                                _parent.Log("JRiver:  nowPlaying Thumb equals:" + _nowPlaying.ThumbURL);

                                _nowPlaying.Studio = String.IsNullOrEmpty(getFieldValue(fileFields, "Studios")) ? "" : getFieldValue(fileFields, "Studios");
                                _parent.Log("JRiver:NP NowPlaying.Studio:" + _nowPlaying.Studio);

                                _nowPlaying.Genre = String.IsNullOrEmpty(getFieldValue(fileFields, "Genre")) ? "" : getFieldValue(fileFields, "Genre");
                                _parent.Log("JRiver:NP NowPlaying.Genre:" + _nowPlaying.Genre);

                                if (_nowPlaying.MediaType == "TvShow" || _nowPlaying.MediaType == "Movie")
                                {
                                    List <string> MovieIcons = new List <string>();
                                    MovieIcons = GetMovieIcons(fileFields);

                                    _nowPlaying.MovieIcons = String.Join(",", MovieIcons);
                                    _parent.Log("JRiver:NP NowPlaying.MovieIcons:" + _nowPlaying.MovieIcons);
                                }
                            }

                            var Volume = getItemName(deserialized, "VolumeDisplay");  //Volume

                            if (Volume == "Muted")
                            {
                                _nowPlaying.IsMuted = true;
                            }
                            else
                            {
                                _nowPlaying.IsMuted = false;
                                int VolumeInt = 0;
                                int.TryParse(Volume.Remove(Volume.Length - 1), out VolumeInt);

                                try
                                {
                                    _parent.Log("JRiver:Volume: VolumeDisplay Equals:" + Volume);
                                    _nowPlaying.Volume = VolumeInt;
                                }
                                catch (Exception ex)
                                {
                                    _parent.Log("Exception in Volume Conversion:" + ex);
                                }
                            }
                            _parent.Log("JRiver:NP NowPlaying.title:" + _nowPlaying.Title);

                            if (Convert.ToUInt64(getItemName(deserialized, "DurationMS")) > 0)
                            {
                                // duration for Plex given in millseconds - convert to seconds and round
                                // Convert Duration to Timespan with seconds only
                                var RoundSeconds = Math.Round(Convert.ToInt64(getItemName(deserialized, "DurationMS")) / 1000.00, 1);
                                _nowPlaying.Duration = new TimeSpan(0, Convert.ToInt32("0"), Convert.ToInt32("0"), Convert.ToInt32(RoundSeconds));
                            }

                            _parent.Log("JRiver:NP server.Media.Duration:" + getItemName(deserialized, "DurationMS") + ":  _nowPlaying.Duration (calculated) :" + _nowPlaying.Duration);

                            var RoundOffset = Math.Round(Convert.ToInt64(getItemName(deserialized, "PositionMS")) / 1000.00, 1);
                            _nowPlaying.Time = new TimeSpan(0, 0, 0, Convert.ToInt32(RoundOffset));

                            _parent.Log("JRiver:NP NowPlaying.Time:" + _nowPlaying.Time + "Calcuated on server.viewOffset:" + getItemName(deserialized, "PositionMS"));

                            var percent = Math.Floor(100.0 * Convert.ToInt32("0" + getItemName(deserialized, "PositionMS"), CultureInfo.InvariantCulture) / Convert.ToInt32("0" + getItemName(deserialized, "DurationMS"), CultureInfo.InvariantCulture));
                            if (Double.IsNaN(percent))
                            {
                                percent = 0;
                            }
                            _nowPlaying.Progress = (int)percent;

                            _parent.Log("JRiver:NP NowPlaying.Progress:" + _nowPlaying.Progress);



                            //        if (server.type == "episode")
                            //        {
                            //            _nowPlaying.EpisodeNumber = Convert.ToInt32(server.index);
                            //            _nowPlaying.SeasonNumber = Convert.ToInt32(server.parentIndex);


                            //        }



                            if (_nowPlaying.MediaType == "Audio")
                            {
                                _nowPlaying.Artist = String.IsNullOrEmpty(getItemName(deserialized, "Artist")) ? "Unknown" : getItemName(deserialized, "Artist");
                                _nowPlaying.Album  = String.IsNullOrEmpty(getItemName(deserialized, "Album")) ? "Unknown" : getItemName(deserialized, "Album");
                                _nowPlaying.Track  = String.IsNullOrEmpty(getItemName(deserialized, "PlayingNowTracks")) ? 1 : Convert.ToInt32(getItemName(deserialized, "PlayingNowTracks"));
                            }

                            // _nowPlaying.LogoURL = "";

                            //  _nowPlaying.MovieIcons = "";

                            _parent.Log("JRiver Remote:  Filename" + _nowPlaying.FileName + " IsPlaying :" + _nowPlaying.IsPlaying + " IsPaused :" + _nowPlaying.IsPaused + " MediaType :" + _nowPlaying.MediaType);

                            //        // check for endwith ts filename
                            //        // actually check for mpegts
                            //        // may be recorded movies in mpegts format as well.....
                            //        // check for regex s00e00 contents - and when both present swap

                            //        // Change of plans - run for all files regardless of type and regardless of mediacontainer


                            //        if (1==1)     //(server.Media.container == "mpegts" && server.type == "movie")
                            //        {
                            //            _parent.Log("Plex Remote:  New changing recorded TV to TV Shows and extracting season/episode data");

                            //            Regex regex = new Regex(@"[Ss](?<season>\d{1,2})[Ee](?<episode>\d{1,2})");

                            //            _parent.Log("Plex Remote:  Server Title Equals:" + server.title);

                            //            if (server.title != null)
                            //            {
                            //                Match match = regex.Match(server.title);
                            //                if (match.Success)
                            //                {
                            //                    _nowPlaying.MediaType = "TvShow";
                            //                    _nowPlaying.SeasonNumber = Convert.ToInt32(match.Groups["season"].Value);
                            //                    _nowPlaying.EpisodeNumber = Convert.ToInt32(match.Groups["episode"].Value);
                            //                    _parent.Log("Plex Remote: Regex TV Conversion: From :" + server.title);


                            //                    //string replacement = "`$``";
                            //                    //string newtitle = replacement;
                            //                    // string newtitle = regex.Replace(server.title, replacement);

                            //                    string[] lines = Regex.Split(server.title, @"[Ss](?<season>\d{1,2})[Ee](?<episode>\d{1,2})");
                            //                    string title1 = lines[0];
                            //                    string newtitle = title1.Replace(".", " ");
                            //                    _parent.Log("Plex Remote:  New title equals :" + newtitle);
                            //                    //_nowPlaying.ShowTitle = newtitle;
                            //                    _nowPlaying.Title = newtitle;
                            //                }

                            //            }
                            //        }



                            //        return;
                            //    }

                            //}
                            //    // if no local Client Playback then nothing Playing
                            //    _nowPlaying.FileName = "";
                            //    _nowPlaying.Title = "";
                            //    _nowPlaying.IsPlaying = false;
                            //    _nowPlaying.IsPaused = false;
                            //    _nowPlaying.IsPlaying = false;
                            //    _nowPlaying.IsNewMedia = false;
                            //    _parent.Log("Plex Log 2nd: Nothing is Playing");
                            //    return;

                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        _parent.Log("Exception in NowPlaying Jriver System" + ex);
                    }
                }
            }
        }
Exemple #3
0
 public void StopAsync()
 {
     _parent.Log("Cancelling downloads");
     _workerDownloads.CancelAsync();
 }