2
0
Эх сурвалжийг харах

added new remote control commands

Luke Pulverenti 12 жил өмнө
parent
commit
dfab2be6f5

+ 144 - 0
MediaBrowser.Api/SessionsService.cs

@@ -130,6 +130,46 @@ namespace MediaBrowser.Api
         public PlaystateCommand Command { get; set; }
         public PlaystateCommand Command { get; set; }
     }
     }
 
 
+    [Route("/Sessions/{Id}/System/{Command}", "POST")]
+    [Api(("Issues a system command to a client"))]
+    public class SendSystemCommand : IReturnVoid
+    {
+        /// <summary>
+        /// Gets or sets the id.
+        /// </summary>
+        /// <value>The id.</value>
+        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
+        public Guid Id { get; set; }
+
+        /// <summary>
+        /// Gets or sets the command.
+        /// </summary>
+        /// <value>The play command.</value>
+        [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
+        public SystemCommand Command { get; set; }
+    }
+
+    [Route("/Sessions/{Id}/Message", "POST")]
+    [Api(("Issues a command to a client to display a message to the user"))]
+    public class SendMessageCommand : IReturnVoid
+    {
+        /// <summary>
+        /// Gets or sets the id.
+        /// </summary>
+        /// <value>The id.</value>
+        [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
+        public Guid Id { get; set; }
+
+        [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public string Text { get; set; }
+
+        [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public string Header { get; set; }
+
+        [ApiMember(Name = "TimeoutMs", Description = "The message timeout. If omitted the user will have to confirm viewing the message.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public long? TimeoutMs { get; set; }
+    }
+
     /// <summary>
     /// <summary>
     /// Class SessionsService
     /// Class SessionsService
     /// </summary>
     /// </summary>
@@ -273,6 +313,110 @@ namespace MediaBrowser.Api
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Posts the specified request.
+        /// </summary>
+        /// <param name="request">The request.</param>
+        public void Post(SendSystemCommand request)
+        {
+            var task = SendSystemCommand(request);
+
+            Task.WaitAll(task);
+        }
+
+        private async Task SendSystemCommand(SendSystemCommand request)
+        {
+            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
+
+            if (session == null)
+            {
+                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
+            }
+
+            if (!session.SupportsRemoteControl)
+            {
+                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
+            }
+
+            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
+
+            if (socket != null)
+            {
+                try
+                {
+                    await socket.SendAsync(new WebSocketMessage<string>
+                    {
+                        MessageType = "SystemCommand",
+                        Data = request.Command.ToString()
+
+                    }, CancellationToken.None).ConfigureAwait(false);
+                }
+                catch (Exception ex)
+                {
+                    Logger.ErrorException("Error sending web socket message", ex);
+                }
+            }
+            else
+            {
+                throw new InvalidOperationException("The requested session does not have an open web socket.");
+            }
+        }
+
+        /// <summary>
+        /// Posts the specified request.
+        /// </summary>
+        /// <param name="request">The request.</param>
+        public void Post(SendMessageCommand request)
+        {
+            var task = SendMessageCommand(request);
+
+            Task.WaitAll(task);
+        }
+
+        private async Task SendMessageCommand(SendMessageCommand request)
+        {
+            var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
+
+            if (session == null)
+            {
+                throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
+            }
+
+            if (!session.SupportsRemoteControl)
+            {
+                throw new ArgumentException(string.Format("Session {0} does not support remote control.", session.Id));
+            }
+
+            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
+
+            if (socket != null)
+            {
+                try
+                {
+                    await socket.SendAsync(new WebSocketMessage<MessageCommand>
+                    {
+                        MessageType = "MessageCommand",
+
+                        Data = new MessageCommand
+                        {
+                            Header = request.Header,
+                            TimeoutMs = request.TimeoutMs,
+                            Text = request.Text
+                        }
+
+                    }, CancellationToken.None).ConfigureAwait(false);
+                }
+                catch (Exception ex)
+                {
+                    Logger.ErrorException("Error sending web socket message", ex);
+                }
+            }
+            else
+            {
+                throw new InvalidOperationException("The requested session does not have an open web socket.");
+            }
+        }
+
         /// <summary>
         /// <summary>
         /// Posts the specified request.
         /// Posts the specified request.
         /// </summary>
         /// </summary>

+ 6 - 0
MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj

@@ -314,6 +314,9 @@
     <Compile Include="..\MediaBrowser.Model\Session\BrowseRequest.cs">
     <Compile Include="..\MediaBrowser.Model\Session\BrowseRequest.cs">
       <Link>Session\BrowseRequest.cs</Link>
       <Link>Session\BrowseRequest.cs</Link>
     </Compile>
     </Compile>
+    <Compile Include="..\MediaBrowser.Model\Session\MessageCommand.cs">
+      <Link>Session\MessageCommand.cs</Link>
+    </Compile>
     <Compile Include="..\MediaBrowser.Model\Session\PlayRequest.cs">
     <Compile Include="..\MediaBrowser.Model\Session\PlayRequest.cs">
       <Link>Session\PlayRequest.cs</Link>
       <Link>Session\PlayRequest.cs</Link>
     </Compile>
     </Compile>
@@ -323,6 +326,9 @@
     <Compile Include="..\MediaBrowser.Model\Session\SessionInfoDto.cs">
     <Compile Include="..\MediaBrowser.Model\Session\SessionInfoDto.cs">
       <Link>Session\SessionInfoDto.cs</Link>
       <Link>Session\SessionInfoDto.cs</Link>
     </Compile>
     </Compile>
+    <Compile Include="..\MediaBrowser.Model\Session\SystemCommand.cs">
+      <Link>Session\SystemCommand.cs</Link>
+    </Compile>
     <Compile Include="..\MediaBrowser.Model\System\SystemInfo.cs">
     <Compile Include="..\MediaBrowser.Model\System\SystemInfo.cs">
       <Link>System\SystemInfo.cs</Link>
       <Link>System\SystemInfo.cs</Link>
     </Compile>
     </Compile>

+ 6 - 0
MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj

@@ -295,6 +295,9 @@
     <Compile Include="..\MediaBrowser.Model\Session\BrowseRequest.cs">
     <Compile Include="..\MediaBrowser.Model\Session\BrowseRequest.cs">
       <Link>Session\BrowseRequest.cs</Link>
       <Link>Session\BrowseRequest.cs</Link>
     </Compile>
     </Compile>
+    <Compile Include="..\MediaBrowser.Model\Session\MessageCommand.cs">
+      <Link>Session\MessageCommand.cs</Link>
+    </Compile>
     <Compile Include="..\MediaBrowser.Model\Session\PlayRequest.cs">
     <Compile Include="..\MediaBrowser.Model\Session\PlayRequest.cs">
       <Link>Session\PlayRequest.cs</Link>
       <Link>Session\PlayRequest.cs</Link>
     </Compile>
     </Compile>
@@ -304,6 +307,9 @@
     <Compile Include="..\MediaBrowser.Model\Session\SessionInfoDto.cs">
     <Compile Include="..\MediaBrowser.Model\Session\SessionInfoDto.cs">
       <Link>Session\SessionInfoDto.cs</Link>
       <Link>Session\SessionInfoDto.cs</Link>
     </Compile>
     </Compile>
+    <Compile Include="..\MediaBrowser.Model\Session\SystemCommand.cs">
+      <Link>Session\SystemCommand.cs</Link>
+    </Compile>
     <Compile Include="..\MediaBrowser.Model\System\SystemInfo.cs">
     <Compile Include="..\MediaBrowser.Model\System\SystemInfo.cs">
       <Link>System\SystemInfo.cs</Link>
       <Link>System\SystemInfo.cs</Link>
     </Compile>
     </Compile>

+ 2 - 0
MediaBrowser.Model/MediaBrowser.Model.csproj

@@ -76,6 +76,7 @@
     <Compile Include="Querying\SimilarItemsQuery.cs" />
     <Compile Include="Querying\SimilarItemsQuery.cs" />
     <Compile Include="Querying\UserQuery.cs" />
     <Compile Include="Querying\UserQuery.cs" />
     <Compile Include="Session\BrowseRequest.cs" />
     <Compile Include="Session\BrowseRequest.cs" />
+    <Compile Include="Session\MessageCommand.cs" />
     <Compile Include="Session\PlayRequest.cs" />
     <Compile Include="Session\PlayRequest.cs" />
     <Compile Include="Session\PlaystateCommand.cs" />
     <Compile Include="Session\PlaystateCommand.cs" />
     <Compile Include="Entities\ImageDownloadOptions.cs" />
     <Compile Include="Entities\ImageDownloadOptions.cs" />
@@ -113,6 +114,7 @@
     <Compile Include="Serialization\IJsonSerializer.cs" />
     <Compile Include="Serialization\IJsonSerializer.cs" />
     <Compile Include="Serialization\IXmlSerializer.cs" />
     <Compile Include="Serialization\IXmlSerializer.cs" />
     <Compile Include="Session\SessionInfoDto.cs" />
     <Compile Include="Session\SessionInfoDto.cs" />
+    <Compile Include="Session\SystemCommand.cs" />
     <Compile Include="Updates\CheckForUpdateResult.cs" />
     <Compile Include="Updates\CheckForUpdateResult.cs" />
     <Compile Include="Updates\PackageTargetSystem.cs" />
     <Compile Include="Updates\PackageTargetSystem.cs" />
     <Compile Include="Updates\InstallationInfo.cs" />
     <Compile Include="Updates\InstallationInfo.cs" />

+ 12 - 0
MediaBrowser.Model/Session/MessageCommand.cs

@@ -0,0 +1,12 @@
+
+namespace MediaBrowser.Model.Session
+{
+    public class MessageCommand
+    {
+        public string Header { get; set; }
+        
+        public string Text { get; set; }
+
+        public long? TimeoutMs { get; set; }
+    }
+}

+ 14 - 0
MediaBrowser.Model/Session/SystemCommand.cs

@@ -0,0 +1,14 @@
+
+namespace MediaBrowser.Model.Session
+{
+    public enum SystemCommand
+    {
+        GoHome,
+        GoToSettings,
+        VolumeUp,
+        VolumeDown,
+        Mute,
+        Unmute,
+        ToggleMute
+    }
+}

+ 36 - 0
MediaBrowser.WebDashboard/ApiClient.js

@@ -3465,6 +3465,42 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
             });
             });
         };
         };
 
 
+        self.sendSystemCommand = function (sessionId, command) {
+
+            if (!sessionId) {
+                throw new Error("null sessionId");
+            }
+
+            if (!command) {
+                throw new Error("null command");
+            }
+
+            var url = self.getUrl("Sessions/" + sessionId + "/System/" + command);
+
+            return self.ajax({
+                type: "POST",
+                url: url
+            });
+        };
+
+        self.sendMessageCommand = function (sessionId, options) {
+
+            if (!sessionId) {
+                throw new Error("null sessionId");
+            }
+
+            if (!options) {
+                throw new Error("null options");
+            }
+
+            var url = self.getUrl("Sessions/" + sessionId + "/Message", options);
+
+            return self.ajax({
+                type: "POST",
+                url: url
+            });
+        };
+
         self.sendPlayStateCommand = function (sessionId, command, options) {
         self.sendPlayStateCommand = function (sessionId, command, options) {
 
 
             if (!sessionId) {
             if (!sessionId) {

+ 1 - 1
MediaBrowser.WebDashboard/packages.config

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
 <packages>
-  <package id="MediaBrowser.ApiClient.Javascript" version="3.0.164" targetFramework="net45" />
+  <package id="MediaBrowser.ApiClient.Javascript" version="3.0.165" targetFramework="net45" />
   <package id="ServiceStack.Common" version="3.9.58" targetFramework="net45" />
   <package id="ServiceStack.Common" version="3.9.58" targetFramework="net45" />
   <package id="ServiceStack.Text" version="3.9.58" targetFramework="net45" />
   <package id="ServiceStack.Text" version="3.9.58" targetFramework="net45" />
 </packages>
 </packages>