|
| 1 | +using System; |
| 2 | +using System.Management.Automation; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using PsTransmission.Core.Services.Transmission; |
| 5 | +using Transmission.Base; |
| 6 | +using Transmission.NetCore.Client.Models; |
| 7 | + |
| 8 | +namespace Transmission.Session |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// <para type="synopsis"> |
| 12 | + /// Set Transmission alt speed state. |
| 13 | + /// </para> |
| 14 | + /// <para type="description"> |
| 15 | + /// Set Transmission alt speed state. |
| 16 | + /// </para> |
| 17 | + /// <para type="description"> |
| 18 | + /// Calls the 'session-set' endpoint: https://github.com/transmission/transmission/blob/master/extras/rpc-spec.txt |
| 19 | + /// </para> |
| 20 | + /// <example> |
| 21 | + /// <para>Example 1: Enable the alt speed limit</para> |
| 22 | + /// <code>PS C:\> Set-TransmissionAltSpeedLimits -Enable</code> |
| 23 | + /// <remarks>Enable the alt speed limit.</remarks> |
| 24 | + /// </example> |
| 25 | + /// <example> |
| 26 | + /// <para>Example 2: Disable the alt speed limit</para> |
| 27 | + /// <code>PS C:\> Set-TransmissionAltSpeedLimits -Disable</code> |
| 28 | + /// <remarks>Disable the alt speed limit.</remarks> |
| 29 | + /// </example> |
| 30 | + /// <para type="link" uri="(https://github.com/trossr32/ps-transmission)">[Github]</para> |
| 31 | + /// <para type="link" uri="(https://github.com/transmission/transmission/blob/master/extras/rpc-spec.txt)">[Transmission RPC API]</para> |
| 32 | + /// </summary> |
| 33 | + [Cmdlet(VerbsCommon.Set, "TransmissionAltSpeedLimits", HelpUri = "https://github.com/trossr32/ps-transmission")] |
| 34 | + [OutputType(typeof(SessionInformation))] |
| 35 | + public class SetTransmissionAltSpeedLimitsCmdlet : BaseTransmissionCmdlet |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// <para type="description"> |
| 39 | + /// Enable alt speed limits. |
| 40 | + /// </para> |
| 41 | + /// </summary> |
| 42 | + [Parameter(Mandatory = false)] |
| 43 | + public SwitchParameter Enable { get; set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// <para type="description"> |
| 47 | + /// Disable alt speed limits. |
| 48 | + /// </para> |
| 49 | + /// </summary> |
| 50 | + [Parameter(Mandatory = false)] |
| 51 | + public SwitchParameter Disable { get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Implements the <see cref="BeginProcessing"/> method for <see cref="SetTransmissionAltSpeedLimitsCmdlet"/>. |
| 55 | + /// </summary> |
| 56 | + protected override void BeginProcessing() |
| 57 | + { |
| 58 | + base.BeginProcessing(); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Implements the <see cref="ProcessRecord"/> method for <see cref="SetTransmissionAltSpeedLimitsCmdlet"/>. |
| 63 | + /// </summary> |
| 64 | + protected override void ProcessRecord() |
| 65 | + { |
| 66 | + try |
| 67 | + { |
| 68 | + var sessionSvc = new SessionService(); |
| 69 | + |
| 70 | + if (!Enable.IsPresent && !Disable.IsPresent) |
| 71 | + throw new Exception("Either the Enable or Disable parameter must be supplied"); |
| 72 | + |
| 73 | + var request = new SessionSettings { |
| 74 | + AlternativeSpeedEnabled = Enable.IsPresent |
| 75 | + }; |
| 76 | + |
| 77 | + bool success = Task.Run(async () => await sessionSvc.Set(request)).Result; |
| 78 | + |
| 79 | + if (success) |
| 80 | + WriteObject($"Alt speed settings {(Enable.IsPresent ? "enabled" : "disabled")} succesfully"); |
| 81 | + } |
| 82 | + catch (Exception e) |
| 83 | + { |
| 84 | + ThrowTerminatingError(new ErrorRecord(new Exception(e.Message, e), null, ErrorCategory.OperationStopped, null)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Implements the <see cref="EndProcessing"/> method for <see cref="SetTransmissionAltSpeedLimitsCmdlet"/>. |
| 90 | + /// Retrieve all torrents |
| 91 | + /// </summary> |
| 92 | + protected override void EndProcessing() |
| 93 | + { |
| 94 | + |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments