jellyfin/Emby.Dlna/PlayTo/TransportCommands.cs

185 lines
6.7 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2016-10-29 22:22:20 +00:00
using System.Collections.Generic;
2020-08-07 17:26:28 +00:00
using System.Globalization;
2016-10-29 22:22:20 +00:00
using System.Linq;
using System.Xml.Linq;
2019-01-13 19:16:19 +00:00
using Emby.Dlna.Common;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.Ssdp;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.PlayTo
2016-10-29 22:22:20 +00:00
{
public class TransportCommands
{
2020-08-07 17:26:28 +00:00
private const string CommandBase = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<SOAP-ENV:Body>" + "<m:{0} xmlns:m=\"{1}\">" + "{2}" + "</m:{0}>" + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
public List<StateVariable> StateVariables { get; } = new List<StateVariable>();
2016-10-29 22:22:20 +00:00
public List<ServiceAction> ServiceActions { get; } = new List<ServiceAction>();
2016-10-29 22:22:20 +00:00
public static TransportCommands Create(XDocument document)
{
var command = new TransportCommands();
2020-08-20 19:04:57 +00:00
var actionList = document.Descendants(UPnpNamespaces.Svc + "actionList");
2016-10-29 22:22:20 +00:00
2020-08-20 19:04:57 +00:00
foreach (var container in actionList.Descendants(UPnpNamespaces.Svc + "action"))
2016-10-29 22:22:20 +00:00
{
command.ServiceActions.Add(ServiceActionFromXml(container));
}
2020-08-20 19:04:57 +00:00
var stateValues = document.Descendants(UPnpNamespaces.ServiceStateTable).FirstOrDefault();
2016-10-29 22:22:20 +00:00
if (stateValues != null)
{
2020-08-20 19:04:57 +00:00
foreach (var container in stateValues.Elements(UPnpNamespaces.Svc + "stateVariable"))
2016-10-29 22:22:20 +00:00
{
command.StateVariables.Add(FromXml(container));
}
}
return command;
}
private static ServiceAction ServiceActionFromXml(XElement container)
{
2020-08-20 19:04:57 +00:00
var serviceAction = new ServiceAction
{
Name = container.GetValue(UPnpNamespaces.Svc + "name"),
};
var argumentList = serviceAction.ArgumentList;
2016-10-29 22:22:20 +00:00
2020-08-20 19:04:57 +00:00
foreach (var arg in container.Descendants(UPnpNamespaces.Svc + "argument"))
2016-10-29 22:22:20 +00:00
{
argumentList.Add(ArgumentFromXml(arg));
}
2020-08-20 19:04:57 +00:00
return serviceAction;
2016-10-29 22:22:20 +00:00
}
private static Argument ArgumentFromXml(XElement container)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
2016-10-29 22:22:20 +00:00
}
return new Argument
{
2020-08-20 19:04:57 +00:00
Name = container.GetValue(UPnpNamespaces.Svc + "name"),
Direction = container.GetValue(UPnpNamespaces.Svc + "direction"),
RelatedStateVariable = container.GetValue(UPnpNamespaces.Svc + "relatedStateVariable")
2016-10-29 22:22:20 +00:00
};
}
2018-09-12 17:26:21 +00:00
private static StateVariable FromXml(XElement container)
2016-10-29 22:22:20 +00:00
{
2020-12-02 14:38:52 +00:00
var allowedValues = Array.Empty<string>();
2020-08-20 19:04:57 +00:00
var element = container.Descendants(UPnpNamespaces.Svc + "allowedValueList")
2016-10-29 22:22:20 +00:00
.FirstOrDefault();
if (element != null)
{
2020-08-20 19:04:57 +00:00
var values = element.Descendants(UPnpNamespaces.Svc + "allowedValue");
2016-10-29 22:22:20 +00:00
2020-12-02 14:38:52 +00:00
allowedValues = values.Select(child => child.Value).ToArray();
2016-10-29 22:22:20 +00:00
}
return new StateVariable
{
2020-08-20 19:04:57 +00:00
Name = container.GetValue(UPnpNamespaces.Svc + "name"),
DataType = container.GetValue(UPnpNamespaces.Svc + "dataType"),
2020-12-02 14:38:52 +00:00
AllowedValues = allowedValues
2016-10-29 22:22:20 +00:00
};
}
public string BuildPost(ServiceAction action, string xmlNamespace)
{
var stateString = string.Empty;
foreach (var arg in action.ArgumentList)
{
2020-12-02 14:38:52 +00:00
if (string.Equals(arg.Direction, "out", StringComparison.Ordinal))
{
2016-10-29 22:22:20 +00:00
continue;
}
2016-10-29 22:22:20 +00:00
2020-12-02 14:38:52 +00:00
if (string.Equals(arg.Name, "InstanceID", StringComparison.Ordinal))
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, "0");
}
2016-10-29 22:22:20 +00:00
else
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, null);
}
2016-10-29 22:22:20 +00:00
}
2020-08-07 17:26:28 +00:00
return string.Format(CultureInfo.InvariantCulture, CommandBase, action.Name, xmlNamespace, stateString);
2016-10-29 22:22:20 +00:00
}
public string BuildPost(ServiceAction action, string xmlNamesapce, object value, string commandParameter = "")
{
var stateString = string.Empty;
foreach (var arg in action.ArgumentList)
{
2020-12-02 14:38:52 +00:00
if (string.Equals(arg.Direction, "out", StringComparison.Ordinal))
{
2016-10-29 22:22:20 +00:00
continue;
}
2020-12-02 14:38:52 +00:00
if (string.Equals(arg.Name, "InstanceID", StringComparison.Ordinal))
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, "0");
}
2016-10-29 22:22:20 +00:00
else
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, value.ToString(), commandParameter);
}
2016-10-29 22:22:20 +00:00
}
2020-08-07 17:26:28 +00:00
return string.Format(CultureInfo.InvariantCulture, CommandBase, action.Name, xmlNamesapce, stateString);
2016-10-29 22:22:20 +00:00
}
public string BuildPost(ServiceAction action, string xmlNamesapce, object value, Dictionary<string, string> dictionary)
{
var stateString = string.Empty;
foreach (var arg in action.ArgumentList)
{
2020-12-02 14:38:52 +00:00
if (string.Equals(arg.Name, "InstanceID", StringComparison.Ordinal))
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, "0");
}
2016-10-29 22:22:20 +00:00
else if (dictionary.ContainsKey(arg.Name))
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, dictionary[arg.Name]);
}
2016-10-29 22:22:20 +00:00
else
{
2016-10-29 22:22:20 +00:00
stateString += BuildArgumentXml(arg, value.ToString());
}
2016-10-29 22:22:20 +00:00
}
2020-08-07 17:26:28 +00:00
return string.Format(CultureInfo.InvariantCulture, CommandBase, action.Name, xmlNamesapce, stateString);
2016-10-29 22:22:20 +00:00
}
private string BuildArgumentXml(Argument argument, string value, string commandParameter = "")
{
var state = StateVariables.FirstOrDefault(a => string.Equals(a.Name, argument.RelatedStateVariable, StringComparison.OrdinalIgnoreCase));
if (state != null)
{
var sendValue = state.AllowedValues.FirstOrDefault(a => string.Equals(a, commandParameter, StringComparison.OrdinalIgnoreCase)) ??
2020-08-20 19:04:57 +00:00
(state.AllowedValues.Count > 0 ? state.AllowedValues[0] : value);
2016-10-29 22:22:20 +00:00
2020-08-07 17:26:28 +00:00
return string.Format(CultureInfo.InvariantCulture, "<{0} xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"{1}\">{2}</{0}>", argument.Name, state.DataType ?? "string", sendValue);
2016-10-29 22:22:20 +00:00
}
2020-08-07 17:26:28 +00:00
return string.Format(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", argument.Name, value);
2016-10-29 22:22:20 +00:00
}
}
}