2020-01-22 13:00:07 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2016-10-29 15:22:20 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Xml.Linq;
|
2019-01-13 12:16:19 -07:00
|
|
|
using Emby.Dlna.Common;
|
2016-10-29 15:34:54 -07:00
|
|
|
using Emby.Dlna.Ssdp;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
2016-10-29 15:34:54 -07:00
|
|
|
namespace Emby.Dlna.PlayTo
|
2016-10-29 15:22:20 -07:00
|
|
|
{
|
|
|
|
public class TransportCommands
|
|
|
|
{
|
|
|
|
private List<StateVariable> _stateVariables = new List<StateVariable>();
|
|
|
|
public List<StateVariable> StateVariables
|
|
|
|
{
|
2019-01-13 13:31:14 -07:00
|
|
|
get => _stateVariables;
|
|
|
|
set => _stateVariables = value;
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<ServiceAction> _serviceActions = new List<ServiceAction>();
|
|
|
|
public List<ServiceAction> ServiceActions
|
|
|
|
{
|
2019-01-13 13:31:14 -07:00
|
|
|
get => _serviceActions;
|
|
|
|
set => _serviceActions = value;
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static TransportCommands Create(XDocument document)
|
|
|
|
{
|
|
|
|
var command = new TransportCommands();
|
|
|
|
|
|
|
|
var actionList = document.Descendants(uPnpNamespaces.svc + "actionList");
|
|
|
|
|
|
|
|
foreach (var container in actionList.Descendants(uPnpNamespaces.svc + "action"))
|
|
|
|
{
|
|
|
|
command.ServiceActions.Add(ServiceActionFromXml(container));
|
|
|
|
}
|
|
|
|
|
|
|
|
var stateValues = document.Descendants(uPnpNamespaces.ServiceStateTable).FirstOrDefault();
|
|
|
|
|
|
|
|
if (stateValues != null)
|
|
|
|
{
|
|
|
|
foreach (var container in stateValues.Elements(uPnpNamespaces.svc + "stateVariable"))
|
|
|
|
{
|
|
|
|
command.StateVariables.Add(FromXml(container));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ServiceAction ServiceActionFromXml(XElement container)
|
|
|
|
{
|
|
|
|
var argumentList = new List<Argument>();
|
|
|
|
|
|
|
|
foreach (var arg in container.Descendants(uPnpNamespaces.svc + "argument"))
|
|
|
|
{
|
|
|
|
argumentList.Add(ArgumentFromXml(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ServiceAction
|
|
|
|
{
|
|
|
|
Name = container.GetValue(uPnpNamespaces.svc + "name"),
|
|
|
|
|
|
|
|
ArgumentList = argumentList
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Argument ArgumentFromXml(XElement container)
|
|
|
|
{
|
|
|
|
if (container == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(container));
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Argument
|
|
|
|
{
|
|
|
|
Name = container.GetValue(uPnpNamespaces.svc + "name"),
|
|
|
|
Direction = container.GetValue(uPnpNamespaces.svc + "direction"),
|
|
|
|
RelatedStateVariable = container.GetValue(uPnpNamespaces.svc + "relatedStateVariable")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private static StateVariable FromXml(XElement container)
|
2016-10-29 15:22:20 -07:00
|
|
|
{
|
|
|
|
var allowedValues = new List<string>();
|
|
|
|
var element = container.Descendants(uPnpNamespaces.svc + "allowedValueList")
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
{
|
|
|
|
var values = element.Descendants(uPnpNamespaces.svc + "allowedValue");
|
|
|
|
|
|
|
|
allowedValues.AddRange(values.Select(child => child.Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new StateVariable
|
|
|
|
{
|
|
|
|
Name = container.GetValue(uPnpNamespaces.svc + "name"),
|
|
|
|
DataType = container.GetValue(uPnpNamespaces.svc + "dataType"),
|
2018-09-12 10:26:21 -07:00
|
|
|
AllowedValues = allowedValues.ToArray()
|
2016-10-29 15:22:20 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public string BuildPost(ServiceAction action, string xmlNamespace)
|
|
|
|
{
|
|
|
|
var stateString = string.Empty;
|
|
|
|
|
|
|
|
foreach (var arg in action.ArgumentList)
|
|
|
|
{
|
|
|
|
if (arg.Direction == "out")
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
continue;
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
if (arg.Name == "InstanceID")
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, "0");
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
else
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, null);
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format(CommandBase, action.Name, xmlNamespace, stateString);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string BuildPost(ServiceAction action, string xmlNamesapce, object value, string commandParameter = "")
|
|
|
|
{
|
|
|
|
var stateString = string.Empty;
|
|
|
|
|
|
|
|
foreach (var arg in action.ArgumentList)
|
|
|
|
{
|
|
|
|
if (arg.Direction == "out")
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
continue;
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
if (arg.Name == "InstanceID")
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, "0");
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
else
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, value.ToString(), commandParameter);
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format(CommandBase, action.Name, xmlNamesapce, stateString);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string BuildPost(ServiceAction action, string xmlNamesapce, object value, Dictionary<string, string> dictionary)
|
|
|
|
{
|
|
|
|
var stateString = string.Empty;
|
|
|
|
|
|
|
|
foreach (var arg in action.ArgumentList)
|
|
|
|
{
|
|
|
|
if (arg.Name == "InstanceID")
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, "0");
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
else if (dictionary.ContainsKey(arg.Name))
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, dictionary[arg.Name]);
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
else
|
2019-03-01 09:12:22 -07:00
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
stateString += BuildArgumentXml(arg, value.ToString());
|
2019-03-01 09:12:22 -07:00
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format(CommandBase, action.Name, xmlNamesapce, stateString);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) ??
|
2019-01-07 16:27:46 -07:00
|
|
|
state.AllowedValues.FirstOrDefault() ??
|
2016-10-29 15:22:20 -07:00
|
|
|
value;
|
|
|
|
|
|
|
|
return string.Format("<{0} xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"{1}\">{2}</{0}>", argument.Name, state.DataType ?? "string", sendValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Format("<{0}>{1}</{0}>", argument.Name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
}
|