2013-04-27 05:03:34 +03:00
|
|
|
using System;
|
2013-09-11 09:33:47 +03:00
|
|
|
using System.Collections.Generic;
|
2013-04-27 05:03:34 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-08-31 06:08:19 +03:00
|
|
|
using NzbDrone.Api.Mapping;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Api.Validation;
|
2013-05-13 03:36:23 +03:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-09-14 09:42:09 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands.Tracking;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.ProgressMessaging;
|
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Commands
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
public class CommandModule : NzbDroneRestModuleWithSignalR<CommandResource, Command>, IHandle<CommandUpdatedEvent>
|
2013-04-27 05:03:34 +03:00
|
|
|
{
|
2013-09-14 09:36:07 +03:00
|
|
|
private readonly ICommandExecutor _commandExecutor;
|
2013-05-13 03:36:23 +03:00
|
|
|
private readonly IContainer _container;
|
2013-08-29 07:43:26 +03:00
|
|
|
private readonly ITrackCommands _trackCommands;
|
2013-04-27 05:03:34 +03:00
|
|
|
|
2013-09-14 09:36:07 +03:00
|
|
|
public CommandModule(ICommandExecutor commandExecutor, IContainer container, ITrackCommands trackCommands)
|
|
|
|
: base(commandExecutor)
|
2013-04-27 05:03:34 +03:00
|
|
|
{
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor = commandExecutor;
|
2013-05-13 03:36:23 +03:00
|
|
|
_container = container;
|
2013-08-29 07:43:26 +03:00
|
|
|
_trackCommands = trackCommands;
|
2013-04-27 05:03:34 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
GetResourceById = GetCommand;
|
|
|
|
CreateResource = StartCommand;
|
|
|
|
GetResourceAll = GetAllCommands;
|
|
|
|
|
|
|
|
PostValidator.RuleFor(c => c.Name).NotBlank();
|
2013-04-27 05:03:34 +03:00
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
private CommandResource GetCommand(int id)
|
|
|
|
{
|
|
|
|
return _trackCommands.GetById(id).InjectTo<CommandResource>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private int StartCommand(CommandResource commandResource)
|
2013-04-27 05:03:34 +03:00
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
var commandType =
|
2013-09-11 09:33:47 +03:00
|
|
|
_container.GetImplementations(typeof(Command))
|
|
|
|
.Single(c => c.Name.Replace("Command", "")
|
|
|
|
.Equals(commandResource.Name, StringComparison.InvariantCultureIgnoreCase));
|
2013-04-27 05:03:34 +03:00
|
|
|
|
2013-05-21 06:20:29 +03:00
|
|
|
dynamic command = Request.Body.FromJson(commandType);
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-09-14 09:36:07 +03:00
|
|
|
var trackedCommand = (Command)_commandExecutor.PublishCommandAsync(command);
|
2013-09-11 09:33:47 +03:00
|
|
|
return trackedCommand.Id;
|
|
|
|
}
|
2013-08-31 06:08:19 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
private List<CommandResource> GetAllCommands()
|
|
|
|
{
|
|
|
|
return ToListResource(_trackCommands.RunningCommands);
|
2013-04-27 05:03:34 +03:00
|
|
|
}
|
2013-08-28 09:51:42 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
public void Handle(CommandUpdatedEvent message)
|
2013-08-28 09:51:42 +03:00
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
if (message.Command.SendUpdatesToClient)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.Command.Id);
|
|
|
|
}
|
2013-08-28 09:51:42 +03:00
|
|
|
}
|
2013-04-27 05:03:34 +03:00
|
|
|
}
|
|
|
|
}
|