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

66 lines
2.3 KiB
C#
Raw Normal View History

using System;
2013-09-11 06:33:47 +00:00
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Api.Extensions;
2013-09-11 06:33:47 +00:00
using NzbDrone.Api.Validation;
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;
2013-09-11 06:33:47 +00:00
namespace NzbDrone.Api.Commands
{
public class CommandModule : NzbDroneRestModuleWithSignalR<CommandResource, CommandModel>, IHandle<CommandUpdatedEvent>
{
private readonly IManageCommandQueue _commandQueueManager;
private readonly IServiceFactory _serviceFactory;
public CommandModule(IManageCommandQueue commandQueueManager,
IBroadcastSignalRMessage signalRBroadcaster,
IServiceFactory serviceFactory)
: base(signalRBroadcaster)
{
_commandQueueManager = commandQueueManager;
_serviceFactory = serviceFactory;
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))
.Single(c => c.Name.Replace("Command", "").Equals(commandResource.Name, StringComparison.InvariantCultureIgnoreCase));
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
}
}
}
}