예제 #1
0
        public static void Main(string[] arguments)
        {
            // Init Common logger -> this will enable TVPlugin to write in the Mediaportal.log file
            var loggerName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]);
            var dataPath   = Log.GetPathName();
            var loggerPath = Path.Combine(dataPath, "log");

#if DEBUG
            if (loggerName != null)
            {
                loggerName = loggerName.Replace(".vshost", "");
            }
#endif
            CommonLogger.Instance = new CommonLog4NetLogger(loggerName, dataPath, loggerPath);


            Thread.CurrentThread.Name = "SetupTv";

            Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
            if (p.Length > 1)
            {
                System.Environment.Exit(0);
            }

            string DeploySql = string.Empty;
            string DeployPwd = string.Empty;

            foreach (string param in arguments)
            {
                switch (param.ToLowerInvariant())
                {
                case "/delete-db":
                    startupMode = StartupMode.DbCleanup;
                    break;

                case "/configure-db":
                    startupMode = StartupMode.DbConfig;
                    break;

                case "/debugoptions":
                    debugOptions = true;
                    break;
                }

                if (param.StartsWith("--Deploy"))
                {
                    switch (param.Substring(0, 12))
                    {
                    case "--DeployMode":
                        Log.Debug("---- started in Deploy mode ----");
                        startupMode = StartupMode.DeployMode;
                        break;

                    case "--DeploySql:":
                        DeploySql = param.Split(':')[1].ToLower();
                        break;

                    case "--DeployPwd:":
                        DeployPwd = param.Split(':')[1];
                        break;
                    }
                }
            }

            Application.SetCompatibleTextRenderingDefault(false);

            // set working dir from application.exe
            string applicationPath = Application.ExecutablePath;
            applicationPath = System.IO.Path.GetFullPath(applicationPath);
            applicationPath = System.IO.Path.GetDirectoryName(applicationPath);
            System.IO.Directory.SetCurrentDirectory(applicationPath);

            FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Application.ExecutablePath);

            try
            {
                Log.Info("---- SetupTv v" + versionInfo.FileVersion + " is starting up on " + OSInfo.OSInfo.GetOSDisplayVersion());
            } catch (Exception)
            {
                Log.Info("---- SetupTv v" + versionInfo.FileVersion + " is starting up on Windows 10 Pro for Workstations (???)");
            }
            Log.Info(OSInfo.OSInfo.GetLastInstalledWindowsUpdateTimestampAsString());
            Log.Info("Windows Media Player: [{0}]", OSInfo.OSInfo.GetWMPVersion());

            //Check for unsupported operating systems
            OSPrerequisites.OSPrerequisites.OsCheck(true);

            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            appSettings.Set("GentleConfigFile", String.Format(@"{0}\gentle.config", PathManager.GetDataPath));

            Application.ThreadException += Application_ThreadException;

            //test connection with database
            Log.Info("---- check connection with database ----");
            SetupDatabaseForm dlg = new SetupDatabaseForm(startupMode);

            if (startupMode == StartupMode.DeployMode)
            {
                if (DeploySql == "dbalreadyinstalled")
                {
                    Log.Info("---- ask user for connection details ----");
                    if (dlg.ShowDialog() != DialogResult.OK || startupMode != StartupMode.DeployMode)
                    {
                        return; // close the application without restart here.
                    }
                    dlg.CheckServiceName();
                    if (startupMode == StartupMode.DeployMode)
                    {
                        dlg.SaveGentleConfig();
                    }
                }
                else if (String.IsNullOrEmpty(DeploySql) || String.IsNullOrEmpty(DeployPwd))
                {
                    dlg.LoadConnectionDetailsFromConfig(true);
                }
                else
                {
                    if (DeploySql == "mysql")
                    {
                        dlg.provider                 = SetupDatabaseForm.ProviderType.MySql;
                        dlg.rbMySQL.Checked          = true;
                        dlg.tbUserID.Text            = "root";
                        dlg.tbServerHostName.Text    = Dns.GetHostName();
                        dlg.tbServiceDependency.Text = @"MySQL5";
                    }
                    else
                    {
                        dlg.provider                 = SetupDatabaseForm.ProviderType.SqlServer;
                        dlg.rbSQLServer.Checked      = true;
                        dlg.tbUserID.Text            = "sa";
                        dlg.tbServerHostName.Text    = Dns.GetHostName() + @"\SQLEXPRESS";
                        dlg.tbServiceDependency.Text = @"SQLBrowser";
                    }
                    dlg.tbPassword.Text     = DeployPwd;
                    dlg.tbDatabaseName.Text = dlg.schemaNameDefault;
                    dlg.schemaName          = dlg.schemaNameDefault;
                }
            }

            if (dlg.tbServerHostName.Text.Trim().ToLower() == "localhost" | dlg.tbServerHostName.Text.Trim() == "127.0.0.1")
            {
                Log.Info("*****************************************************************");
                Log.Info("* WARNING, connection host ({0}) not officially supported *", dlg.tbServerHostName.Text);
                Log.Info("*****************************************************************");
            }

            if ((startupMode != StartupMode.Normal && startupMode != StartupMode.DeployMode) ||
                (!dlg.TestConnection(startupMode)))
            {
                Log.Info("---- ask user for connection details ----");
                if (dlg.ShowDialog() != DialogResult.OK || startupMode != StartupMode.DeployMode)
                {
                    return; // close the application without restart here.
                }
            }
            dlg.CheckServiceName();
            if (startupMode == StartupMode.DeployMode)
            {
                dlg.SaveGentleConfig();
            }

            Log.Info("---- check if database needs to be updated/created ----");
            int currentSchemaVersion = dlg.GetCurrentShemaVersion(startupMode);
            if (currentSchemaVersion <= 36) // drop pre-1.0 DBs and handle -1
            {
                // Allow users to cancel DB recreation to backup their old DB
                if (currentSchemaVersion > 0)
                {
                    if (
                        MessageBox.Show(
                            "Your existing database cannot be upgraded and will be replaced by an empty database. Continue now?",
                            "DB recreation needed", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                            MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                Log.Info("---- create database ----");
                if (!dlg.ExecuteSQLScript("create"))
                {
                    MessageBox.Show("Failed to create the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                Log.Info("- Database created.");
                currentSchemaVersion = dlg.GetCurrentShemaVersion(startupMode);
            }

            Log.Info("---- upgrade database schema ----");
            if (!dlg.UpgradeDBSchema(currentSchemaVersion))
            {
                MessageBox.Show("Failed to upgrade the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Log.Info("---- check if tvservice is running ----");
            if (!ServiceHelper.IsRunning)
            {
                Log.Info("---- tvservice is not running ----");
                if (startupMode != StartupMode.DeployMode)
                {
                    DialogResult result = MessageBox.Show("The Tv service is not running.\rStart it now?",
                                                          "Mediaportal TV service", MessageBoxButtons.YesNo);
                    if (result != DialogResult.Yes)
                    {
                        return;
                    }
                }
                Log.Info("---- start tvservice----");
                ServiceHelper.Start();
            }

            ServiceHelper.WaitInitialized();
            int cards = 0;
            try
            {
                cards = RemoteControl.Instance.Cards;
            }
            catch (Exception)
            {
                Log.Info("---- restart tvservice----");
                ServiceHelper.Restart();
                ServiceHelper.WaitInitialized();
                try
                {
                    RemoteControl.Clear();
                    RemoteControl.HostName = Dns.GetHostName();
                    cards = RemoteControl.Instance.Cards;
                }
                catch (Exception ex)
                {
                    Log.Info("---- Unable to restart tv service----");
                    Log.Write(ex);
                    MessageBox.Show("Failed to startup tvservice" + ex);
                    return;
                }
            }

            var layer = new TvBusinessLayer();
            layer.SetLogLevel();

            // Mantis #0001991: disable mpg recording  (part I: force TS recording format)
            IList <Card> TvCards = Card.ListAll();
            foreach (Card card in TvCards)
            {
                if (card.RecordingFormat != 0)
                {
                    card.RecordingFormat = 0;
                    Log.Info("Card {0} switched from .MPG to .TS format", card.Name);
                    card.Persist();
                }
            }

            // Mantis #0002138: impossible to configure TVGroups
            layer.CreateGroup(TvConstants.TvGroupNames.AllChannels);

            // Avoid the visual part of SetupTv if in DeployMode
            if (startupMode == StartupMode.DeployMode)
            {
                return;
            }

            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                Application.EnableVisualStyles();
                Application.DoEvents();

                new Startup().Start();
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
        }
예제 #2
0
    public static void Main(string[] arguments)
    {
      // Init Common logger -> this will enable TVPlugin to write in the Mediaportal.log file
      var loggerName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]);
      var dataPath = Log.GetPathName();
      var loggerPath = Path.Combine(dataPath, "log");
#if DEBUG
      if (loggerName != null) loggerName = loggerName.Replace(".vshost", "");
#endif
      CommonLogger.Instance = new CommonLog4NetLogger(loggerName, dataPath, loggerPath);
      
      
      Thread.CurrentThread.Name = "SetupTv";

      Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
      if (p.Length > 1)
      {
        System.Environment.Exit(0);
      }

      string DeploySql = string.Empty;
      string DeployPwd = string.Empty;

      foreach (string param in arguments)
      {
        switch (param.ToLowerInvariant())
        {
          case "/delete-db":
            startupMode = StartupMode.DbCleanup;
            break;

          case "/configure-db":
            startupMode = StartupMode.DbConfig;
            break;

          case "/debugoptions":
            debugOptions = true;
            break;
        }

        if (param.StartsWith("--Deploy"))
        {
          switch (param.Substring(0, 12))
          {
            case "--DeployMode":
              Log.Debug("---- started in Deploy mode ----");
              startupMode = StartupMode.DeployMode;
              break;

            case "--DeploySql:":
              DeploySql = param.Split(':')[1].ToLower();
              break;

            case "--DeployPwd:":
              DeployPwd = param.Split(':')[1];
              break;
          }
        }
      }

      Application.SetCompatibleTextRenderingDefault(false);

      // set working dir from application.exe
      string applicationPath = Application.ExecutablePath;
      applicationPath = System.IO.Path.GetFullPath(applicationPath);
      applicationPath = System.IO.Path.GetDirectoryName(applicationPath);
      System.IO.Directory.SetCurrentDirectory(applicationPath);

      FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Application.ExecutablePath);

      Log.Info("---- SetupTv v" + versionInfo.FileVersion + " is starting up on " + OSInfo.OSInfo.GetOSDisplayVersion());

      //Check for unsupported operating systems
      OSPrerequisites.OSPrerequisites.OsCheck(true);

      NameValueCollection appSettings = ConfigurationManager.AppSettings;
      appSettings.Set("GentleConfigFile", String.Format(@"{0}\gentle.config", PathManager.GetDataPath));

      Application.ThreadException += Application_ThreadException;

      //test connection with database
      Log.Info("---- check connection with database ----");
      SetupDatabaseForm dlg = new SetupDatabaseForm(startupMode);

      if (startupMode == StartupMode.DeployMode)
      {
        if (DeploySql == "dbalreadyinstalled")
        {
          Log.Info("---- ask user for connection details ----");
          if (dlg.ShowDialog() != DialogResult.OK || startupMode != StartupMode.DeployMode)
            return; // close the application without restart here.
          
          dlg.CheckServiceName();
          if (startupMode == StartupMode.DeployMode)
          {
            dlg.SaveGentleConfig();
          }
        }
        else if (String.IsNullOrEmpty(DeploySql) || String.IsNullOrEmpty(DeployPwd))
        {
          dlg.LoadConnectionDetailsFromConfig(true);
        }
        else
        {
          if (DeploySql == "mysql")
          {
            dlg.provider = SetupDatabaseForm.ProviderType.MySql;
            dlg.rbMySQL.Checked = true;
            dlg.tbUserID.Text = "root";
            dlg.tbServerHostName.Text = Dns.GetHostName();
            dlg.tbServiceDependency.Text = @"MySQL5";
          }
          else
          {
            dlg.provider = SetupDatabaseForm.ProviderType.SqlServer;
            dlg.rbSQLServer.Checked = true;
            dlg.tbUserID.Text = "sa";
            dlg.tbServerHostName.Text = Dns.GetHostName() + @"\SQLEXPRESS";
            dlg.tbServiceDependency.Text = @"SQLBrowser";
          }
          dlg.tbPassword.Text = DeployPwd;
          dlg.tbDatabaseName.Text = dlg.schemaNameDefault;
          dlg.schemaName = dlg.schemaNameDefault;
        }
      }

      if (dlg.tbServerHostName.Text.Trim().ToLower() == "localhost" | dlg.tbServerHostName.Text.Trim() == "127.0.0.1")
      {
        Log.Info("*****************************************************************");
        Log.Info("* WARNING, connection host ({0}) not officially supported *", dlg.tbServerHostName.Text);
        Log.Info("*****************************************************************"); 
      }

      if ((startupMode != StartupMode.Normal && startupMode != StartupMode.DeployMode) ||
          (!dlg.TestConnection(startupMode)))
      {
        Log.Info("---- ask user for connection details ----");
        if (dlg.ShowDialog() != DialogResult.OK || startupMode != StartupMode.DeployMode)
          return; // close the application without restart here.
      }
      dlg.CheckServiceName();
      if (startupMode == StartupMode.DeployMode)
      {
        dlg.SaveGentleConfig();
      }

      Log.Info("---- check if database needs to be updated/created ----");
      int currentSchemaVersion = dlg.GetCurrentShemaVersion(startupMode);
      if (currentSchemaVersion <= 36) // drop pre-1.0 DBs and handle -1
      {
        // Allow users to cancel DB recreation to backup their old DB
        if (currentSchemaVersion > 0)
          if (
            MessageBox.Show(
              "Your existing database cannot be upgraded and will be replaced by an empty database. Continue now?",
              "DB recreation needed", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
              MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
            return;

        Log.Info("---- create database ----");
        if (!dlg.ExecuteSQLScript("create"))
        {
          MessageBox.Show("Failed to create the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
        }
        Log.Info("- Database created.");
        currentSchemaVersion = dlg.GetCurrentShemaVersion(startupMode);
      }

      Log.Info("---- upgrade database schema ----");
      // Get MySQL server version
      string currentServerVersion = dlg.GetCurrentServerVersion(startupMode);
      if (!dlg.UpgradeDBSchema(currentSchemaVersion, currentServerVersion))
      {
        MessageBox.Show("Failed to upgrade the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      Log.Info("---- check if tvservice is running ----");
      if (!ServiceHelper.IsRunning)
      {
        Log.Info("---- tvservice is not running ----");
        if (startupMode != StartupMode.DeployMode)
        {
          DialogResult result = MessageBox.Show("The Tv service is not running.\rStart it now?",
                                                "Mediaportal TV service", MessageBoxButtons.YesNo);
          if (result != DialogResult.Yes) return;
        }
        Log.Info("---- start tvservice----");
        ServiceHelper.Start();
      }

      ServiceHelper.WaitInitialized();
      int cards = 0;
      try
      {
        cards = RemoteControl.Instance.Cards;
      }
      catch (Exception)
      {
        Log.Info("---- restart tvservice----");
        ServiceHelper.Restart();
        ServiceHelper.WaitInitialized();
        try
        {
          RemoteControl.Clear();
          RemoteControl.HostName = Dns.GetHostName();
          cards = RemoteControl.Instance.Cards;
        }
        catch (Exception ex)
        {
          Log.Info("---- Unable to restart tv service----");
          Log.Write(ex);
          MessageBox.Show("Failed to startup tvservice" + ex);
          return;
        }
      }

      var layer = new TvBusinessLayer();
      layer.SetLogLevel();

      // Mantis #0001991: disable mpg recording  (part I: force TS recording format)
      IList<Card> TvCards = Card.ListAll();
      foreach (Card card in TvCards)
      {
        if (card.RecordingFormat != 0)
        {
          card.RecordingFormat = 0;
          Log.Info("Card {0} switched from .MPG to .TS format", card.Name);
          card.Persist();
        }
      }

      // Mantis #0002138: impossible to configure TVGroups 
      layer.CreateGroup(TvConstants.TvGroupNames.AllChannels);

      // Avoid the visual part of SetupTv if in DeployMode
      if (startupMode == StartupMode.DeployMode)
      {
        return;
      }

      try
      {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        Application.EnableVisualStyles();
        Application.DoEvents();

        new Startup().Start();
      }
      catch (Exception ex)
      {
        Log.Write(ex);
      }
    }