예제 #1
0
 /// <summary>
 /// Start monitoring plex
 /// </summary>
 internal void Start()
 {
     //Find the plex executable
     _executableFileName = GetPlexExecutable();
     if (string.IsNullOrEmpty(_executableFileName))
     {
         OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server does not appear to be installed!", EventLogEntryType.Error));
         OnPlexStop(this, new EventArgs());
         State = PlexState.Stopped;
     }
     else
     {
         OnPlexStatusChange(this, new StatusChangeEventArgs("Plex executable found at " + _executableFileName));
         StartPlex();
         //load the settings and start a thread that will attempt to bring up all the auxiliary processes
         Settings settings = SettingsHandler.Load();
         //stop any running aux apps
         _auxAppMonitors.ForEach(a => a.Stop());
         _auxAppMonitors.Clear();
         settings.AuxiliaryApplications.ForEach(x => _auxAppMonitors.Add(new AuxiliaryApplicationMonitor(x)));
         //hook up the state change event for all the applications
         _auxAppMonitors.ForEach(x => x.StatusChange += new AuxiliaryApplicationMonitor.StatusChangeHandler(OnPlexStatusChange));
         _auxAppMonitors.AsParallel().ForAll(x => x.Start());
     }
 }
예제 #2
0
        public void OnPlexStateChange(PlexState state)
        {
            switch (state)
            {
            case PlexState.Running:
                OnStateChange($"Plex {state.ToString()}");
                break;

            case PlexState.Stopped:
                OnStateChange($"Plex {state.ToString()}");
                break;

            case PlexState.Updating:
                OnStateChange($"Plex is {state.ToString()}");
                break;

            case PlexState.Pending:
                OnStateChange($"Plex Start {state.ToString()}");
                break;

            case PlexState.Stopping:
                OnStateChange($"Plex {state.ToString()}");
                break;
            }
        }
예제 #3
0
        /// <summary>
        /// This event fires when the plex process we have a reference to exits
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Plex_Exited(object sender, EventArgs e)
        {
            OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server has stopped!"));
            //unsubscribe
            _plex.Exited -= Plex_Exited;

            //kill the supporting processes.
            KillSupportingProcesses();

            if (_plex != null)
            {
                _plex.Dispose();
                _plex = null;
            }

            //restart as required
            Settings settings = SettingsHandler.Load();

            if (State != PlexState.Stopping && settings.AutoRestart)
            {
                OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Waiting {0} seconds before re-starting the Plex process.", settings.RestartDelay)));
                State = PlexState.Pending;
                System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
                System.Threading.Timer          t         = new System.Threading.Timer((x) => { Start(); autoEvent.Set(); }, null, settings.RestartDelay * 1000, System.Threading.Timeout.Infinite);
                autoEvent.WaitOne();
                t.Dispose();
            }
            else
            {
                //set the status
                State = PlexState.Stopped;
            }
        }
예제 #4
0
 /// <summary>
 /// Restart plex, wait for the specified delay between stop and start
 /// </summary>
 /// <param name="msDelay">The amount of time in ms to wait before starting after stop</param>
 internal void Restart(int delay)
 {
     Stop();
     State = PlexState.Pending;
     System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
     System.Threading.Timer t = new System.Threading.Timer((x) => { Start(); autoEvent.Set(); }, null, delay, System.Threading.Timeout.Infinite);
     autoEvent.WaitOne();
     t.Dispose();
 }
예제 #5
0
 /// <summary>
 /// Restart plex, wait for the specified delay between stop and start
 /// </summary>
 /// <param name="msDelay">The amount of time in ms to wait before starting after stop</param>
 internal void Restart(int delay)
 {
     Stop();
     State = PlexState.Pending;
     System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
     System.Threading.Timer          t         = new System.Threading.Timer((x) => { Start(); autoEvent.Set(); }, null, delay, System.Threading.Timeout.Infinite);
     autoEvent.WaitOne();
     t.Dispose();
 }
예제 #6
0
        internal PmsMonitor()
        {
            State           = PlexState.Stopped;
            _auxAppMonitors = new List <AuxiliaryApplicationMonitor>();
            Settings settings = SettingsHandler.Load();

            settings.AuxiliaryApplications.ForEach(x => _auxAppMonitors.Add(new AuxiliaryApplicationMonitor(x)));
            //hook up the state change event for all the applications
            _auxAppMonitors.ForEach(x => x.StatusChange += new AuxiliaryApplicationMonitor.StatusChangeHandler(OnPlexStatusChange));
        }
예제 #7
0
        /// <summary>
        /// Start monitoring plex
        /// </summary>
        internal void Start()
        {
            //Find the plex executable
            _executableFileName = GetPlexExecutable();
            if (string.IsNullOrEmpty(_executableFileName))
            {
                OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server does not appear to be installed!", EventLogEntryType.Error));
                OnPlexStop(this, new EventArgs());
                State = PlexState.Stopped;
            }
            else
            {
                //load the settings
                Settings settings = SettingsHandler.Load();

                OnPlexStatusChange(this, new StatusChangeEventArgs("Plex executable found at " + _executableFileName));

                //map network drives
                if (settings.DriveMaps.Count > 0)
                {
                    OnPlexStatusChange(this, new StatusChangeEventArgs("Mapping Network Drives"));
                    foreach (DriveMap map in settings.DriveMaps)
                    {
                        try
                        {
                            map.MapDrive(true);
                            OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Map share {0} to letter '{1}' successful", map.ShareName, map.DriveLetter)));
                        }
                        catch (Exception ex)
                        {
                            OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Unable to map share {0} to letter '{1}': {2}", map.ShareName, map.DriveLetter, ex.Message), EventLogEntryType.Error));
                        }
                    }
                }


                StartPlex();

                //stop any running aux apps
                _auxAppMonitors.ForEach(a => a.Stop());
                _auxAppMonitors.Clear();
                settings.AuxiliaryApplications.ForEach(x => _auxAppMonitors.Add(new AuxiliaryApplicationMonitor(x)));
                //hook up the state change event for all the applications
                _auxAppMonitors.ForEach(x => x.StatusChange += new AuxiliaryApplicationMonitor.StatusChangeHandler(OnPlexStatusChange));
                _auxAppMonitors.AsParallel().ForAll(x => x.Start());
            }
        }
예제 #8
0
        /// <summary>
        /// Show the settings dialogue
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsCommand(object sender, EventArgs e)
        {
            if (_plexService != null)
            {
                Settings settings = null;
                try
                {
                    settings = Settings.Deserialize(_plexService.GetSettings());
                }
                catch
                {
                    disconnect();
                }

                if (settings != null)
                {
                    //Save the current server port setting for reference
                    int oldPort = settings.ServerPort;
                    SettingsWindowViewModel settingsViewModel = new SettingsWindowViewModel(settings);
                    SettingsWindow          settingsWindow    = new SettingsWindow(settingsViewModel);
                    if (settingsWindow.ShowDialog() == true)
                    {
                        PlexState status = PlexState.Pending;
                        try
                        {
                            _plexService.SetSettings(settingsViewModel.WorkingSettings.Serialize());
                            status = _plexService.GetStatus();
                        }
                        catch (Exception ex)
                        {
                            disconnect();
                            MessageBox.Show("Unable to save settings" + Environment.NewLine + ex.Message);
                        }
                        //The only setting that would require a restart of the service is the listening port.
                        //If that gets changed notify the user to restart the service from the service snap in
                        if (settingsViewModel.WorkingSettings.ServerPort != oldPort)
                        {
                            MessageBox.Show("Server port changed! You will need to restart the service from the services snap in for the change to be applied", "Settings changed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }
예제 #9
0
        public void OnPlexStateChange(PlexState state)
        {
            switch (state)
            {
            case PlexState.Running:
                OnStateChange(string.Format("Plex {0}", state.ToString()));
                break;

            case PlexState.Stopped:
                OnStateChange(string.Format("Plex {0}", state.ToString()));
                break;

            case PlexState.Pending:
                OnStateChange(string.Format("Plex Start {0}", state.ToString()));
                break;

            case PlexState.Stopping:
                OnStateChange(string.Format("Plex {0}", state.ToString()));
                break;
            }
        }
예제 #10
0
        /// <summary>
        /// Show the settings dialogue
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SettingsCommand(object sender, EventArgs e)
        {
            if (_plexService != null)
            {
                Settings settings = null;
                try
                {
                    settings = Settings.Deserialize(_plexService.GetSettings());
                }
                catch
                {
                    Disconnect();
                }

                if (settings != null)
                {
                    //Save the current server port setting for reference
                    int oldPort = settings.ServerPort;
                    SettingsWindowViewModel settingsViewModel = new SettingsWindowViewModel(settings);
                    settingsViewModel.AuxAppStartRequest += (s, args) =>
                    {
                        var requester = s as AuxiliaryApplicationViewModel;
                        if (requester != null)
                        {
                            _plexService.StartAuxApp(requester.Name);
                            requester.Running = _plexService.IsAuxAppRunning(requester.Name);
                        }
                    };
                    settingsViewModel.AuxAppStopRequest += (s, args) =>
                    {
                        var requester = s as AuxiliaryApplicationViewModel;
                        if (requester != null)
                        {
                            _plexService.StopAuxApp(requester.Name);
                            requester.Running = _plexService.IsAuxAppRunning(requester.Name);
                        }
                    };
                    settingsViewModel.AuxAppCheckRunRequest += (s, args) =>
                    {
                        var requester = s as AuxiliaryApplicationViewModel;
                        if (requester != null)
                        {
                            requester.Running = _plexService.IsAuxAppRunning(requester.Name);
                        }
                    };
                    _settingsWindow = new SettingsWindow(settingsViewModel);
                    if (_settingsWindow.ShowDialog() == true)
                    {
                        PlexState status = PlexState.Pending;
                        try
                        {
                            _plexService.SetSettings(settingsViewModel.WorkingSettings.Serialize());
                            status = _plexService.GetStatus();
                        }
                        catch (Exception ex)
                        {
                            Disconnect();
                            System.Windows.MessageBox.Show("Unable to save settings" + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        }
                        //The only setting that would require a restart of the service is the listening port.
                        //If that gets changed notify the user to restart the service from the service snap in
                        if (settingsViewModel.WorkingSettings.ServerPort != oldPort)
                        {
                            System.Windows.MessageBox.Show("Server port changed! You will need to restart the service from the services snap in for the change to be applied", "Settings changed!", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    _settingsWindow = null;
                }
            }
        }
예제 #11
0
 /// <summary>
 /// Start a new/get a handle on existing Plex process
 /// </summary>
 private void startPlex()
 {
     State = PlexState.Pending;
     //always try to get rid of the plex auto start registry entry
     purgeAutoStartRegistryEntry();
     // make sure we don't spawn a browser
     disableFirstRun();
     if (_plex == null)
     {
         //see if its running already
         _plex = Process.GetProcessesByName(PmsMonitor._plexName).FirstOrDefault();
         if (_plex == null)
         {
             OnPlexStatusChange(this, new StatusChangeEventArgs("Attempting to start Plex"));
             //plex process
             _plex = new Process();
             ProcessStartInfo plexStartInfo = new ProcessStartInfo(_executableFileName);
             plexStartInfo.WorkingDirectory = Path.GetDirectoryName(_executableFileName);
             plexStartInfo.UseShellExecute = false;
             //check version to see if we can use the startup argument
             string plexVersion = FileVersionInfo.GetVersionInfo(_executableFileName).FileVersion;
             Version v = new Version(plexVersion);
             Version minimumVersion = new Version("0.9.8.12");
             if (v.CompareTo(minimumVersion) == -1)
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server version is {0}. Cannot use startup argument.", plexVersion)));
             }
             else
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server version is {0}. Can use startup argument.", plexVersion)));
                 plexStartInfo.Arguments = "-noninteractive";
             }
             _plex.StartInfo = plexStartInfo;
             _plex.EnableRaisingEvents = true;
             _plex.Exited += new EventHandler(plex_Exited);
             try
             {
                 _plex.Start();
                 State = PlexState.Running;
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server Started."));
             }
             catch(Exception ex)
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server failed to start. " + ex.Message));
             }
         }
         else
         {
             //its running, most likely in the wrong session. monitor this instance and if it ends, start a new one
             //register to the exited event so we know when to start a new one
             OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server already running in session {0}.", _plex.SessionId)));
             try
             {
                 _plex.EnableRaisingEvents = true;
                 _plex.Exited += new EventHandler(plex_Exited);
                 State = PlexState.Running;
             }
             catch
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Unable to attach to already running Plex Media Server instance. The existing instance will continue unmanaged. Please close all instances of Plex Media Server on this computer prior to starting the service"));
                 OnPlexStop(this, new EventArgs());
             }
         }
     }
     //set the state back to stopped if we didn't achieve a running state
     if (State != PlexState.Running)
         State = PlexState.Stopped;
 }
예제 #12
0
        /// <summary>
        /// This event fires when the plex process we have a reference to exits
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void plex_Exited(object sender, EventArgs e)
        {
            OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server has stopped!"));
            //unsubscribe
            _plex.Exited -= plex_Exited;

            //kill the supporting processes.
            killSupportingProcesses();

            if (_plex != null)
            {
                _plex.Dispose();
                _plex = null;
            }

            //restart as required
            Settings settings = SettingsHandler.Load();
            if (State != PlexState.Stopping && settings.AutoRestart)
            {
                OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Waiting {0} seconds before re-starting the Plex process.", settings.RestartDelay)));
                State = PlexState.Pending;
                System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
                System.Threading.Timer t = new System.Threading.Timer((x) => { Start(); autoEvent.Set(); }, null, settings.RestartDelay * 1000, System.Threading.Timeout.Infinite);
                autoEvent.WaitOne();
                t.Dispose();
            }
            else
            {
                //set the status
                State = PlexState.Stopped;
            }
        }
예제 #13
0
 /// <summary>
 /// Stop the monitor and kill the processes
 /// </summary>
 internal void Stop()
 {
     State = PlexState.Stopping;
     endPlex();
 }
예제 #14
0
 /// <summary>
 /// Start monitoring plex
 /// </summary>
 internal void Start()
 {
     //Find the plex executable
     _executableFileName = getPlexExecutable();
     if (string.IsNullOrEmpty(_executableFileName))
     {
         OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server does not appear to be installed!", EventLogEntryType.Error));
         OnPlexStop(this, new EventArgs());
         State = PlexState.Stopped;
     }
     else
     {
         OnPlexStatusChange(this, new StatusChangeEventArgs("Plex executable found at " + _executableFileName));
         startPlex();
         //load the settings and start a thread that will attempt to bring up all the auxiliary processes
         Settings settings = SettingsHandler.Load();
         //stop any running aux apps
         _auxAppMonitors.ForEach(a => a.Stop());
         _auxAppMonitors.Clear();
         settings.AuxiliaryApplications.ForEach(x => _auxAppMonitors.Add(new AuxiliaryApplicationMonitor(x)));
         //hook up the state change event for all the applications
         _auxAppMonitors.ForEach(x => x.StatusChange += new AuxiliaryApplicationMonitor.StatusChangeHandler(OnPlexStatusChange));
         _auxAppMonitors.AsParallel().ForAll(x => x.Start());
     }
 }
예제 #15
0
 /// <summary>
 /// Start a new/get a handle on existing Plex process
 /// </summary>
 private void StartPlex()
 {
     State = PlexState.Pending;
     //always try to get rid of the plex auto start registry entry
     PurgeAutoStartRegistryEntry();
     // make sure we don't spawn a browser
     DisableFirstRun();
     if (_plex == null)
     {
         //see if its running already
         _plex = Process.GetProcessesByName(PmsMonitor._plexName).FirstOrDefault();
         if (_plex == null)
         {
             OnPlexStatusChange(this, new StatusChangeEventArgs("Attempting to start Plex"));
             //plex process
             _plex = new Process();
             ProcessStartInfo plexStartInfo = new ProcessStartInfo(_executableFileName);
             plexStartInfo.WorkingDirectory = Path.GetDirectoryName(_executableFileName);
             plexStartInfo.UseShellExecute  = false;
             //check version to see if we can use the startup argument
             string  plexVersion    = FileVersionInfo.GetVersionInfo(_executableFileName).FileVersion;
             Version v              = new Version(plexVersion);
             Version minimumVersion = new Version("0.9.8.12");
             if (v.CompareTo(minimumVersion) == -1)
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server version is {0}. Cannot use startup argument.", plexVersion)));
             }
             else
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server version is {0}. Can use startup argument.", plexVersion)));
                 plexStartInfo.Arguments = "-noninteractive";
             }
             _plex.StartInfo           = plexStartInfo;
             _plex.EnableRaisingEvents = true;
             _plex.Exited += new EventHandler(Plex_Exited);
             try
             {
                 _plex.Start();
                 State = PlexState.Running;
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server Started."));
             }
             catch (Exception ex)
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Plex Media Server failed to start. " + ex.Message));
             }
         }
         else
         {
             //its running, most likely in the wrong session. monitor this instance and if it ends, start a new one
             //register to the exited event so we know when to start a new one
             OnPlexStatusChange(this, new StatusChangeEventArgs(string.Format("Plex Media Server already running in session {0}.", _plex.SessionId)));
             try
             {
                 _plex.EnableRaisingEvents = true;
                 _plex.Exited += new EventHandler(Plex_Exited);
                 State         = PlexState.Running;
             }
             catch
             {
                 OnPlexStatusChange(this, new StatusChangeEventArgs("Unable to attach to already running Plex Media Server instance. The existing instance will continue unmanaged. Please close all instances of Plex Media Server on this computer prior to starting the service"));
                 OnPlexStop(this, new EventArgs());
             }
         }
     }
     //set the state back to stopped if we didn't achieve a running state
     if (State != PlexState.Running)
     {
         State = PlexState.Stopped;
     }
 }
예제 #16
0
 /// <summary>
 /// Stop the monitor and kill the processes
 /// </summary>
 internal void Stop()
 {
     State = PlexState.Stopping;
     EndPlex();
 }
예제 #17
0
 internal PmsMonitor()
 {
     State           = PlexState.Stopped;
     _auxAppMonitors = new List <AuxiliaryApplicationMonitor>();
 }