You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Radarr/NzbDrone/Router.cs

121 lines
4.4 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.SysTray;
namespace NzbDrone
{
public class Router
{
12 years ago
private readonly INzbDroneServiceFactory _nzbDroneServiceFactory;
private readonly ServiceProvider _serviceProvider;
12 years ago
private readonly IConsoleService _consoleService;
private readonly EnvironmentProvider _environmentProvider;
private readonly SysTrayProvider _sysTrayProvider;
12 years ago
private readonly Logger _logger;
12 years ago
public Router(INzbDroneServiceFactory nzbDroneServiceFactory, ServiceProvider serviceProvider,
IConsoleService consoleService, EnvironmentProvider environmentProvider, SysTrayProvider sysTrayProvider, Logger logger)
{
12 years ago
_nzbDroneServiceFactory = nzbDroneServiceFactory;
_serviceProvider = serviceProvider;
12 years ago
_consoleService = consoleService;
_environmentProvider = environmentProvider;
_sysTrayProvider = sysTrayProvider;
12 years ago
_logger = logger;
}
public void Route(IEnumerable<string> args)
{
Route(GetApplicationMode(args));
}
12 years ago
public void Route(ApplicationModes applicationModes)
{
if (!_environmentProvider.IsUserInteractive)
{
12 years ago
applicationModes = ApplicationModes.Service;
}
12 years ago
_logger.Info("Application mode: {0}", applicationModes);
12 years ago
switch (applicationModes)
{
12 years ago
case ApplicationModes.Service:
{
12 years ago
_logger.Trace("Service selected");
12 years ago
_serviceProvider.Run(_nzbDroneServiceFactory.Build());
break;
}
12 years ago
case ApplicationModes.Console:
{
12 years ago
_logger.Trace("Console selected");
12 years ago
_nzbDroneServiceFactory.Start();
if (_consoleService.IsConsoleApplication)
{
_consoleService.WaitForClose();
}
else
{
_sysTrayProvider.Start();
}
break;
}
12 years ago
case ApplicationModes.InstallService:
{
12 years ago
_logger.Trace("Install Service selected");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
12 years ago
_consoleService.PrintServiceAlreadyExist();
}
else
{
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
12 years ago
case ApplicationModes.UninstallService:
{
12 years ago
_logger.Trace("Uninstall Service selected");
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
12 years ago
_consoleService.PrintServiceDoestExist();
}
else
{
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
default:
{
12 years ago
_consoleService.PrintHelp();
break;
}
}
}
12 years ago
public static ApplicationModes GetApplicationMode(IEnumerable<string> args)
{
12 years ago
if (args == null) return ApplicationModes.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
12 years ago
if (cleanArgs.Count == 0) return ApplicationModes.Console;
if (cleanArgs.Count != 1) return ApplicationModes.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
12 years ago
if (arg == "i") return ApplicationModes.InstallService;
if (arg == "u") return ApplicationModes.UninstallService;
12 years ago
return ApplicationModes.Help;
}
}
}