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