Skip to content

Commit

Permalink
Added PlayerController updates
Browse files Browse the repository at this point in the history
   - Added PlayerController intents and types
   - Added System.ExceptionRequest intent
   - Updated to support strong Error types
   - Added StopDirective (for audio)
   - Added ClearQueue and Stop response helpers to ResponseBuilder
  • Loading branch information
matthiasxc committed Mar 14, 2017
1 parent 927ab06 commit 1b16904
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Alexa.NET/Request/Type/Error.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,7 +10,8 @@ namespace Alexa.NET.Request.Type
public class Error
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public ErrorType Type { get; set; }

[JsonProperty("message")]
public string Message { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions Alexa.NET/Request/Type/ErrorCause.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public class ErrorCause
{
[JsonProperty("requestId")]
public string requestId { get; set; }
}
}
28 changes: 28 additions & 0 deletions Alexa.NET/Request/Type/ErrorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public enum ErrorType
{
[EnumMember(Value = "INVALID_RESPONSE")]
InvalidResponse,
[EnumMember(Value = "DEVICE_COMMUNICATION_ERROR")]
DeviceCommunicationError,
[EnumMember(Value = "INTERNAL_ERROR")]
InternalError,
[EnumMember(Value = "MEDIA_ERROR_UNKNOWN")]
MediaErrorUnknown,
[EnumMember(Value = "MEDIA_ERROR_INVALID_REQUEST")]
InvalidMediaRequest,
[EnumMember(Value = "MEDIA_ERROR_SERVICE_UNAVAILABLE")]
MediaServiceUnavailable,
[EnumMember(Value = "MEDIA_ERROR_INTERNAL_SERVER_ERROR")]
InternalServerError,
[EnumMember(Value = "MEDIA_ERROR_INTERNAL_DEVICE_ERROR")]
InternalDeviceError
}
}
30 changes: 30 additions & 0 deletions Alexa.NET/Request/Type/PlaybackControllerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public class PlaybackControllerRequest : Request
{
public PlaybackControllerRequestType PlaybackRequestType
{
get
{
switch (this.Type.Split('.')[1])
{
case "NextCommandIssued":
return PlaybackControllerRequestType.Next;
case "PauseCommandIssued":
return PlaybackControllerRequestType.Pause;
case "PlayCommandIssued":
return PlaybackControllerRequestType.Play;
case "PreviousCommandIssued":
return PlaybackControllerRequestType.Previous;
default:
return PlaybackControllerRequestType.Unknown;
}
}
}
}
}
22 changes: 22 additions & 0 deletions Alexa.NET/Request/Type/PlaybackControllerRequestType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public enum PlaybackControllerRequestType
{
[EnumMember(Value = "NextCommandIssued")]
Next,
[EnumMember(Value = "PauseCommandIssued")]
Pause,
[EnumMember(Value = "PlayCommandIssued")]
Play,
[EnumMember(Value = "PreviousCommandIssued")]
Previous,
[EnumMember(Value = "Unknown")]
Unknown
}
}
18 changes: 18 additions & 0 deletions Alexa.NET/Request/Type/Reason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public enum Reason
{
[EnumMember(Value = "USER_INITATED")]
UserInitiated,
[EnumMember(Value = "ERROR")]
Error,
[EnumMember(Value = "EXCEEDED_MAX_REPROMPTS")]
ExceededMaxReprompts
}
}
9 changes: 8 additions & 1 deletion Alexa.NET/Request/Type/RequestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public override object ReadJson(JsonReader reader, System.Type objectType, objec

public Request Create(string requestType)
{
//AudioPlayer requests are very similar, map to single type
//AudioPlayer and PlaybackController requests are very similar,
// map them to respective types
if (requestType.StartsWith("AudioPlayer"))
requestType = "AudioPlayer";
else if (requestType.StartsWith("PlaybackController"))
requestType = "PlaybackController";

switch (requestType)
{
Expand All @@ -48,6 +51,10 @@ public Request Create(string requestType)
return new SessionEndedRequest();
case "AudioPlayer":
return new AudioPlayerRequest();
case "PlaybackController":
return new PlaybackControllerRequest();
case "System.ExceptionEncountered":
return new SystemExceptionRequest();
default:
throw new ArgumentOutOfRangeException(nameof(Type), $"Unknown request type: {requestType}.");
}
Expand Down
4 changes: 3 additions & 1 deletion Alexa.NET/Request/Type/SessionEndedRequest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Alexa.NET.Request.Type
{
public class SessionEndedRequest : Request
{
[JsonProperty("reason")]
public string Reason { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Reason Reason { get; set; }
}
}
16 changes: 16 additions & 0 deletions Alexa.NET/Request/Type/SystemExceptionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Alexa.NET.Request.Type
{
public class SystemExceptionRequest : Request
{
[JsonProperty("error")]
public Error Error { get; set; }
[JsonProperty("cause")]
public ErrorCause ErrorCause { get; set; }
}
}
2 changes: 1 addition & 1 deletion Alexa.NET/Response/Directive/ClearQueueDirective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class ClearQueueDirective : IDirective
[JsonProperty("clearBehavior")]
[JsonRequired]
[JsonConverter(typeof(StringEnumConverter))]
public PlayBehavior ClearBehavior { get; set; }
public ClearBehavior ClearBehavior { get; set; }
}
}
14 changes: 14 additions & 0 deletions Alexa.NET/Response/Directive/StopDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Alexa.NET.Response.Directive
{
public class StopDirective : IDirective
{
[JsonProperty("type")]
public string Type => "AudioPlayer.Stop";
}
}
19 changes: 18 additions & 1 deletion Alexa.NET/ResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,25 @@ public static SkillResponse AudioPlayerPlay(PlayBehavior playBehavior, string ur

return response;
}

public static SkillResponse AudioPlayerStop()
{
var response = BuildResponse(null, true, null, null, null);
response.Response.Directives.Add(new StopDirective());
return response;
}

public static SkillResponse AudioPlayerClearQueue(ClearBehavior clearBehavior)
{
var response = BuildResponse(null, true, null, null, null);
response.Response.Directives.Add(new ClearQueueDirective()
{
ClearBehavior = clearBehavior
});
return response;
}
#endregion

#region Main Response Builder
private static SkillResponse BuildResponse(IOutputSpeech outputSpeech, bool shouldEndSession, Session sessionAttributes, Reprompt reprompt, ICard card)
{
Expand Down
4 changes: 2 additions & 2 deletions Alexa.NET/project.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Alexa.NET",
"title": "Alexa.NET",
"version": "1.0.0-beta-2",
"version": "1.0.0-beta-4",
"authors": [ "Tim Heuer" ],
"description": "A simple .NET Core library for handling Alexa Skill request/responses.",

Expand All @@ -27,6 +27,6 @@
"repository": { "url": "https://github.com/timheuer/alexa-skills-dotnet" },
"requireLicenseAcceptance": false,
"tags": [ "amazon", "alexa", "echo", "dot", "echo dot", "skills" ],
"releaseNotes": "Updated to include Locale"
"releaseNotes": "Updated to include Locale and Audio/PlayerController request support."
}
}

0 comments on commit 1b16904

Please sign in to comment.