Radarr/src/NzbDrone.Api/Commands/CommandModule.cs

77 lines
2.6 KiB
C#
Raw Normal View History

2018-11-23 07:03:32 +00:00
using System;
2013-09-11 06:33:47 +00:00
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Messaging.Commands;
2013-09-14 06:42:09 +00:00
using NzbDrone.Core.Messaging.Events;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.ProgressMessaging;
using NzbDrone.SignalR;
2018-11-23 07:03:32 +00:00
using Radarr.Http;
2019-12-22 22:08:53 +00:00
using Radarr.Http.Extensions;
using Radarr.Http.Validation;
namespace NzbDrone.Api.Commands
{
2018-11-23 07:03:32 +00:00
public class CommandModule : RadarrRestModuleWithSignalR<CommandResource, CommandModel>, IHandle<CommandUpdatedEvent>
{
private readonly IManageCommandQueue _commandQueueManager;
private readonly IServiceFactory _serviceFactory;
private readonly Logger _logger;
public CommandModule(IManageCommandQueue commandQueueManager,
IBroadcastSignalRMessage signalRBroadcaster,
IServiceFactory serviceFactory,
Logger logger)
: base(signalRBroadcaster)
{
_commandQueueManager = commandQueueManager;
_serviceFactory = serviceFactory;
_logger = logger;
2013-09-11 06:33:47 +00:00
GetResourceById = GetCommand;
CreateResource = StartCommand;
GetResourceAll = GetStartedCommands;
2013-09-11 06:33:47 +00:00
PostValidator.RuleFor(c => c.Name).NotBlank();
}
2013-09-11 06:33:47 +00:00
private CommandResource GetCommand(int id)
{
return _commandQueueManager.Get(id).ToResource();
2013-09-11 06:33:47 +00:00
}
private int StartCommand(CommandResource commandResource)
{
var commandType = _serviceFactory.GetImplementations(typeof(Command))
.SingleOrDefault(c => c.Name.Replace("Command", "").Equals(commandResource.Name, StringComparison.InvariantCultureIgnoreCase));
if (commandType == null)
{
_logger.Error("Found no matching command for {0}", commandResource.Name);
return 0;
}
2013-05-21 03:20:29 +00:00
dynamic command = Request.Body.FromJson(commandType);
command.Trigger = CommandTrigger.Manual;
var trackedCommand = _commandQueueManager.Push(command, CommandPriority.Normal, CommandTrigger.Manual);
2013-09-11 06:33:47 +00:00
return trackedCommand.Id;
}
private List<CommandResource> GetStartedCommands()
2013-09-11 06:33:47 +00:00
{
return _commandQueueManager.GetStarted().ToResource();
}
2013-09-11 06:33:47 +00:00
public void Handle(CommandUpdatedEvent message)
{
if (message.Command.Body.SendUpdatesToClient)
2013-09-11 06:33:47 +00:00
{
BroadcastResourceChange(ModelAction.Updated, message.Command.ToResource());
2013-09-11 06:33:47 +00:00
}
}
}
2018-11-23 07:03:32 +00:00
}