Skip to content

Commit

Permalink
[CastIt.Cli] Updated play command. Added a new command
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Nov 24, 2021
1 parent dd3b067 commit bbeb6f9
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 41 deletions.
34 changes: 12 additions & 22 deletions CastIt.Cli/Commands/Files/PlayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,32 @@ public PlayCommand(IConsole appConsole, ICastItApiService castItApi)
{
}

[Argument(0, Description = "The play list id", ShowInHelpText = true)]
[Option(CommandOptionType.SingleValue, Description = "The play list id", LongName = "playlist-id", ShortName = "playlist-id")]
public long PlayListId { get; set; }

[Argument(1, Description = "The file id", ShowInHelpText = true)]
[Option(CommandOptionType.SingleValue, Description = "The file id", LongName = "file-id", ShortName = "file-id")]
public long FileId { get; set; }

//[Argument(0, Description = "The file's full path or url", ShowInHelpText = true)]
//public string Mrl { get; set; }
[Option(CommandOptionType.SingleValue, Description = "The filename", LongName = "filename", ShortName = "filename")]
public string Filename { get; set; }

//[Option(CommandOptionType.SingleOrNoValue, Description = "The audio stream index, defaults to 0", LongName = "audio_index", ShortName = "ai")]
//public int AudioStreamIndex { get; set; }

//[Option(CommandOptionType.SingleOrNoValue, Description = "The video stream index, defaults to 0", LongName = "video_index", ShortName = "vi")]
//public int VideoStreamIndex { get; set; }

//[Option(CommandOptionType.SingleOrNoValue, Description = "The subtitles stream index, defaults to -1", LongName = "subs_index", ShortName = "si")]
//public int SubsStreamIndex { get; set; } = -1;

//[Option(CommandOptionType.SingleOrNoValue, Description = "The video quality, only required for youtube videos", LongName = "quality")]
//public int Quality { get; set; } = 360;

//[Option(CommandOptionType.SingleOrNoValue, Description = "The seconds where the file will be started", LongName = "seconds", ShortName = "sec")]
//public int Seconds { get; set; }
[Option(CommandOptionType.SingleOrNoValue, Description = "Passing this param will force the playback of the file even if its already being played", LongName = "force", ShortName = "force")]
public bool Force { get; set; }

protected override async Task<int> Execute(CommandLineApplication app)
{
CheckIfWebServerIsRunning();
if (PlayListId <= 0 || FileId <= 0)
if (string.IsNullOrWhiteSpace(Filename) && (PlayListId <= 0 || FileId <= 0))
{
AppConsole.WriteLine($"PlaylistId = {PlayListId} or FileId = {FileId} are not valid");
AppConsole.WriteLine($"PlaylistId = {PlayListId}, FileId = {FileId} or Filename = {Filename} are not valid");
return ErrorCode;
}

var response = await CastItApi.Play(PlayListId, FileId);
var response = !string.IsNullOrWhiteSpace(Filename)
? await CastItApi.Play(Filename, Force)
: await CastItApi.Play(PlayListId, FileId);
CheckServerResponse(response);
AppConsole.WriteLine($"FileId = {FileId} was successfully loaded");
AppConsole.WriteLine("File was successfully loaded");
return SuccessCode;
}
}
Expand Down
2 changes: 1 addition & 1 deletion CastIt.Cli/Commands/MainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace CastIt.Cli.Commands
{
[Command(Name = "castit", OptionsComparison = StringComparison.InvariantCultureIgnoreCase)]
[Command(Name = "castit", Description = "Remote control for the server app.", OptionsComparison = StringComparison.InvariantCultureIgnoreCase)]
[VersionOptionFromMember("--version", MemberName = nameof(GetVersion))]
[Subcommand(typeof(PlayerCommands), typeof(PlayListCommands), typeof(ServerCommands), typeof(FilesCommand))]
public class MainCommand : BaseCommand
Expand Down
11 changes: 7 additions & 4 deletions CastIt.Cli/Commands/PlayLists/GetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ namespace CastIt.Cli.Commands.PlayLists
[Command(Name = "get", Description = "Retrieves the playlist + it's files", OptionsComparison = StringComparison.InvariantCultureIgnoreCase)]
public class GetCommand : BaseCommand
{
[Argument(0, Description = "The playlist id", ShowInHelpText = true)]
[Option(CommandOptionType.SingleValue, Description = "The play list id", LongName = "playlist-id", ShortName = "playlist-id")]
public long PlayListId { get; set; }

[Option(CommandOptionType.SingleValue, Description = "The playlist name", LongName = "name", ShortName = "name")]
public string Name { get; set; }

public GetCommand(IConsole appConsole, ICastItApiService castItApi)
: base(appConsole, castItApi)
{
Expand All @@ -21,13 +24,13 @@ protected override async Task<int> Execute(CommandLineApplication app)
{
CheckIfWebServerIsRunning();

if (PlayListId <= 0)
if (PlayListId <= 0 && string.IsNullOrWhiteSpace(Name))
{
AppConsole.WriteLine($"PlayListId = {PlayListId} is not valid");
AppConsole.WriteLine($"Either PlayListId = {PlayListId} or the Name = {Name} is not valid");
return ErrorCode;
}

var response = await CastItApi.GetPlayList(PlayListId);
var response = await CastItApi.GetPlayList(PlayListId, Name);
CheckServerResponse(response);

var table = new ConsoleTable("Id", "Name", "Information", "Position", "Playing", "Played Time");
Expand Down
2 changes: 1 addition & 1 deletion CastIt.Cli/Commands/PlayLists/SetOptionsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SetOptionsCommand(IConsole appConsole, ICastItApiService castItApi)
protected override async Task<int> Execute(CommandLineApplication app)
{
//CheckIfWebServerIsRunning();
var response = await CastItApi.SetOptions(PlayListId, Loop, Shuffle);
var response = await CastItApi.SetPlayListOptions(PlayListId, Loop, Shuffle);
CheckServerResponse(response);

AppConsole.WriteLine("Playlist was successfully updated");
Expand Down
32 changes: 32 additions & 0 deletions CastIt.Cli/Commands/Player/SetFileOptionsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using CastIt.Cli.Interfaces.Api;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.Threading.Tasks;

namespace CastIt.Cli.Commands.Player
{
[Command(Name = "set-file-options", Description = "Sets the file options of the current played file", OptionsComparison = StringComparison.InvariantCultureIgnoreCase)]
public class SetCurrentPlayedFileOptionsCommand : BaseCommand
{
[Option(CommandOptionType.SingleOrNoValue, Description = "The audio stream index, defaults to 0", LongName = "audio-index", ShortName = "audio-index")]
public int AudioStreamIndex { get; set; }

[Option(CommandOptionType.SingleOrNoValue, Description = "The subtitles stream index, defaults to -1", LongName = "subs-index", ShortName = "subs-index")]
public int SubsStreamIndex { get; set; } = -1;

[Option(CommandOptionType.SingleOrNoValue, Description = "The video quality, only required for youtube videos", LongName = "quality", ShortName = "quality")]
public int Quality { get; set; }

public SetCurrentPlayedFileOptionsCommand(IConsole appConsole, ICastItApiService castItApi)
: base(appConsole, castItApi)
{
}

protected override async Task<int> Execute(CommandLineApplication app)
{
var response = await CastItApi.SetCurrentPlayedFileOptions(AudioStreamIndex, SubsStreamIndex, Quality);
CheckServerResponse(response);
return SuccessCode;
}
}
}
2 changes: 2 additions & 0 deletions CastIt.Cli/Commands/Player/StatusCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected override async Task<int> Execute(CommandLineApplication app)

var response = await CastItApi.GetStatus();
CheckServerResponse(response);
//just to avoid printing a lot of stuff..
response.Result.ThumbnailRanges.Clear();
PrettyPrintAsJson(response.Result);

return SuccessCode;
Expand Down
3 changes: 2 additions & 1 deletion CastIt.Cli/Commands/PlayerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace CastIt.Cli.Commands
typeof(VolumeCommand),
typeof(GoToCommand),
typeof(SettingsCommand),
typeof(StatusCommand)
typeof(StatusCommand),
typeof(SetCurrentPlayedFileOptionsCommand)
)]
public class PlayerCommands : BaseCommand
{
Expand Down
11 changes: 10 additions & 1 deletion CastIt.Cli/Interfaces/Api/ICastItApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public interface ICastItApi

[Get("/Player/Status")]
Task<AppResponseDto<ServerPlayerStatusResponseDto>> GetStatus();

[Post("/Player/Play")]
Task<EmptyResponseDto> Play(PlayFileFromNameRequestDto dto);

[Post("/Player/SetCurrentPlayedFileOptions")]
Task<EmptyResponseDto> SetCurrentPlayedFileOptions([Body] SetMultiFileOptionsRequestDto dto);
#endregion

#region PlayLists
Expand All @@ -68,6 +74,9 @@ public interface ICastItApi
[Get("/PlayLists/{id}")]
Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(long id);

[Get("/PlayLists/ByName/{name}")]
Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(string name);

[Post("/PlayLists")]
Task<AppResponseDto<PlayListItemResponseDto>> AddNewPlayList();

Expand All @@ -78,7 +87,7 @@ public interface ICastItApi
Task<EmptyResponseDto> UpdatePlayListPosition(long id, int newIndex);

[Put("/PlayLists/{id}/SetOptions")]
Task<EmptyResponseDto> SetOptions(long id, [Body] SetPlayListOptionsRequestDto dto);
Task<EmptyResponseDto> SetPlayListOptions(long id, [Body] SetPlayListOptionsRequestDto dto);

[Delete("/PlayLists/{id}")]
Task<EmptyResponseDto> DeletePlayList(long id);
Expand Down
10 changes: 6 additions & 4 deletions CastIt.Cli/Interfaces/Api/ICastItApiService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using CastIt.Domain.Dtos;
using CastIt.Domain.Dtos.Responses;
using CastIt.Domain.Enums;
using CastIt.Domain.Models.Device;
using CastIt.Infrastructure.Models;
using Microsoft.AspNetCore.JsonPatch;
using Refit;
using System.Collections.Generic;
using System.Threading.Tasks;
using CastIt.Domain.Enums;
using Refit;

namespace CastIt.Cli.Interfaces.Api
{
Expand All @@ -25,11 +25,11 @@ public interface ICastItApiService
Task<EmptyResponseDto> GoToPosition(double position);
Task<EmptyResponseDto> Seek(double seconds);
Task<AppListResponseDto<GetAllPlayListResponseDto>> GetAllPlayLists();
Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(long id);
Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(long id, string name);
Task<AppResponseDto<PlayListItemResponseDto>> AddNewPlayList();
Task<EmptyResponseDto> UpdatePlayList(long id, string name);
Task<EmptyResponseDto> UpdatePlayListPosition(long id, int newIndex);
Task<EmptyResponseDto> SetOptions(long id, bool loop, bool shuffle);
Task<EmptyResponseDto> SetPlayListOptions(long id, bool loop, bool shuffle);
Task<EmptyResponseDto> RemoveFilesThatStartsWith(long id, string path);
Task<EmptyResponseDto> RemoveAllMissingFiles(long id);
Task<EmptyResponseDto> RemoveFiles(long id, List<long> fileIds);
Expand All @@ -42,6 +42,8 @@ public interface ICastItApiService
Task<AppResponseDto<ServerAppSettings>> GetCurrentSettings();
Task<EmptyResponseDto> UpdateSettings(JsonPatchDocument<ServerAppSettings> patch);
Task<AppResponseDto<ServerPlayerStatusResponseDto>> GetStatus();
Task<EmptyResponseDto> Play(string filename, bool force);
Task<EmptyResponseDto> SetCurrentPlayedFileOptions(int audioStreamIndex, int subsStreamIndex, int quality);
Task<EmptyResponseDto> LoopFile(long playListId, long fileId, bool loop);
Task<EmptyResponseDto> UpdateFilePosition(long playListId, long fileId, int newIndex);
Task<EmptyResponseDto> SortFiles(long id, [Query] SortModeType sortMode);
Expand Down
1 change: 1 addition & 0 deletions CastIt.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class Program
{
private static async Task<int> Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
//This is required, so calls to Directory.GetCurrentDirectory return the right path and calls like 'castit --start-server' works properly
var basePath = AppDomain.CurrentDomain.BaseDirectory;
Directory.SetCurrentDirectory(basePath);
Expand Down
65 changes: 58 additions & 7 deletions CastIt.Cli/Services/CastItApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ public async Task<AppResponseDto<ServerPlayerStatusResponseDto>> GetStatus()
}
return response;
}

#endregion

#region PlayLists
Expand All @@ -363,12 +362,14 @@ public async Task<AppListResponseDto<GetAllPlayListResponseDto>> GetAllPlayLists
return response;
}

public async Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(long id)
public async Task<AppResponseDto<PlayListItemResponseDto>> GetPlayList(long id, string name)
{
var response = new AppResponseDto<PlayListItemResponseDto>();
try
{
response = await Api.GetPlayList(id);
response = !string.IsNullOrWhiteSpace(name)
? await Api.GetPlayList(name)
: await Api.GetPlayList(id);
}
catch (ApiException apiEx)
{
Expand Down Expand Up @@ -446,25 +447,25 @@ public async Task<EmptyResponseDto> UpdatePlayListPosition(long id, int newIndex
return response;
}

public async Task<EmptyResponseDto> SetOptions(long id, bool loop, bool shuffle)
public async Task<EmptyResponseDto> SetPlayListOptions(long id, bool loop, bool shuffle)
{
var response = new EmptyResponseDto();
try
{
response = await Api.SetOptions(id, new SetPlayListOptionsRequestDto
response = await Api.SetPlayListOptions(id, new SetPlayListOptionsRequestDto
{
Loop = loop,
Shuffle = shuffle
});
}
catch (ApiException apiEx)
{
Logger.LogError(apiEx, $"{nameof(SetOptions)}: Api exception occurred");
Logger.LogError(apiEx, $"{nameof(SetPlayListOptions)}: Api exception occurred");
await HandleApiException(apiEx, response);
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(SetOptions)}: Unknown error occurred");
Logger.LogError(ex, $"{nameof(SetPlayListOptions)}: Unknown error occurred");
HandleUnknownException(response);
}
return response;
Expand Down Expand Up @@ -720,6 +721,56 @@ public async Task<EmptyResponseDto> LoopFile(long playListId, long fileId, bool
}
return response;
}

public async Task<EmptyResponseDto> Play(string filename, bool force)
{
var response = new EmptyResponseDto();
try
{
response = await Api.Play(new PlayFileFromNameRequestDto
{
Filename = filename,
Force = force
});
}
catch (ApiException apiEx)
{
Logger.LogError(apiEx, $"{nameof(Play)}: Api exception occurred");
await HandleApiException(apiEx, response);
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(Play)}: Unknown error occurred");
HandleUnknownException(response);
}
return response;
}

public async Task<EmptyResponseDto> SetCurrentPlayedFileOptions(int audioStreamIndex, int subsStreamIndex, int quality)
{
var request = new SetMultiFileOptionsRequestDto
{
AudioStreamIndex = audioStreamIndex,
SubtitleStreamIndex = subsStreamIndex,
Quality = quality
};
var response = new EmptyResponseDto();
try
{
response = await Api.SetCurrentPlayedFileOptions(request);
}
catch (ApiException apiEx)
{
Logger.LogError(apiEx, $"{nameof(SetCurrentPlayedFileOptions)}: Api exception occurred");
await HandleApiException(apiEx, response);
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(SetCurrentPlayedFileOptions)}: Unknown error occurred");
HandleUnknownException(response);
}
return response;
}
#endregion
}
}

0 comments on commit bbeb6f9

Please sign in to comment.