Exemplo n.º 1
0
        public void ChangeVolume(SonosDevice device, double volume)
        {
            var parameters = new Dictionary <string, string>
            {
                { "Volume", volume.ToString("F2") }
            };

            StartActionInNewTask(device, new Action("Change Volume"), parameters);
        }
Exemplo n.º 2
0
        public void PlayRadio(SonosDevice device, string stream, string title)
        {
            var parameters = new Dictionary <string, string>
            {
                { "Stream", stream },
                { "Title", title }
            };

            StartActionInNewTask(device, new Action("Play Radio"), parameters);
        }
Exemplo n.º 3
0
        public void PlayFile(SonosDevice device, string file, string title, string album)
        {
            var parameters = new Dictionary <string, string>
            {
                { "File", file },
                { "Title", title },
                { "Album", album }
            };

            StartActionInNewTask(device, new Action("Play File"), parameters);
        }
Exemplo n.º 4
0
        private async Task SendAvTransportControl(SonosDevice device, string command)
        {
            var service = device.Services.Single(s => s.Id.Contains("AVTransport"));
            var action  = service.Actions.Single(s => s.Name.Equals(command));
            var values  = new Dictionary <string, string>
            {
                { "InstanceID", "0" },
                { "Speed", "1" }
            };

            await _soapClient.ExecuteAsync(device, service, action, values);
        }
Exemplo n.º 5
0
        private SonosDevice GetMaster(SonosDevice device)
        {
            if (device.IsMaster)
            {
                return(device);
            }

            var masterId = device.CurrentUri?.Replace("x-rincon:", string.Empty) ?? string.Empty;
            var master   = (SonosDevice)Devices.SingleOrDefault(d => d.Id.Equals(masterId, StringComparison.OrdinalIgnoreCase));

            return(master ?? device);
        }
Exemplo n.º 6
0
        private async Task SendUrl(SonosDevice device, string url, string metadata)
        {
            metadata = HttpUtility.HtmlEncode(metadata);
            url      = HttpUtility.HtmlEncode(url);

            var service = device.Services.Single(s => s.Id.Contains("AVTransport"));
            var action  = service.Actions.Single(s => s.Name.Equals("SetAVTransportURI"));
            var values  = new Dictionary <string, string>
            {
                { "InstanceID", "0" },
                { "CurrentURI", url },
                { "CurrentURIMetaData", metadata }
            };

            await _soapClient.ExecuteAsync(device, service, action, values);
        }
        private void CreateDevice(string deviceDescriptionXmlPath)
        {
            var document = new XmlDocument();

            document.Load(deviceDescriptionXmlPath);

            var url              = new Uri(deviceDescriptionXmlPath, UriKind.Absolute);
            var ip               = url.Host;
            var port             = url.Port;
            var namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("upnp", "urn:schemas-upnp-org:device-1-0");
            var id    = document.SelectSingleNode("//upnp:UDN", namespaceManager)?.InnerText;
            var name  = document.SelectSingleNode("//upnp:modelName", namespaceManager)?.InnerText;
            var model = document.SelectSingleNode("//upnp:modelNumber", namespaceManager)?.InnerText;

            lock (_lock)
            {
                if (string.IsNullOrEmpty(id) || _detectedSonosIds.Contains(id))
                {
                    return;
                }

                _detectedSonosIds.Add(id);
            }

            var upnpServices = GetServices(document, namespaceManager).ToList();

            foreach (var service in upnpServices)
            {
                var serviceUrl = $"http://{ip}:{port}{service.DescriptionUrl}";
                var actions    = GetActions(serviceUrl);
                service.Actions.AddRange(actions);
            }

            var device = new SonosDevice(id, ip, name);

            device.Services.AddRange(upnpServices);
            device.Type = model;

            OnDeviceFound(device);
        }
        public async Task <Dictionary <string, string> > ExecuteAsync(SonosDevice device, UpnpService service, UpnpAction action, Dictionary <string, string> values)
        {
            var uri        = new Uri($"http://{device.IpAddress}:1400{service.ControlUrl}");
            var soapAction = $"{service.Id}#{action.Name}";

            var body = new StringBuilder();

            body.Append($"<u:{action.Name} xmlns:u=\"{service.Type}\">");

            foreach (var argument in action.InputArguments)
            {
                string value;
                if (values.TryGetValue(argument, out value))
                {
                    body.Append($"<{argument}>{value}</{argument}>");
                }
            }
            body.Append($"</u:{action.Name}>");

            var result = new Dictionary <string, string>();

            try
            {
                var document = await PostRequestInternalAsync(uri, soapAction, body.ToString());

                foreach (var argument in action.OutputArguments)
                {
                    var value = document.SelectSingleNode("//" + argument)?.InnerText;
                    result.Add(argument, value);
                }
            }
            catch (WebException e)
            {
                _log.Error($"Unable to execute action {action.Name} for device {device.Name}: {e.Message}");
            }
            catch (Exception e)
            {
                _log.Error($"Unable to execute action {action.Name} for device {device.Name}: {e.Message}", e);
            }

            return(result);
        }
        public async Task<Dictionary<string, string>> ExecuteAsync(SonosDevice device, UpnpService service, UpnpAction action, Dictionary<string, string> values)
        {
            var uri = new Uri($"http://{device.IpAddress}:1400{service.ControlUrl}");
            var soapAction = $"{service.Id}#{action.Name}";

            var body = new StringBuilder();
            body.Append($"<u:{action.Name} xmlns:u=\"{service.Type}\">");

            foreach (var argument in action.InputArguments)
            {
                string value;
                if (values.TryGetValue(argument, out value))
                {
                    body.Append($"<{argument}>{value}</{argument}>");
                }
            }
            body.Append($"</u:{action.Name}>");

            var result = new Dictionary<string, string>();

            try
            {
                var document = await PostRequestInternalAsync(uri, soapAction, body.ToString());

                foreach (var argument in action.OutputArguments)
                {
                    var value = document.SelectSingleNode("//" + argument)?.InnerText;
                    result.Add(argument, value);
                }
            }
            catch (WebException e)
            {
                _log.Error($"Unable to execute action {action.Name} for device {device.Name}: {e.Message}");
            }
            catch (Exception e)
            {
                _log.Error($"Unable to execute action {action.Name} for device {device.Name}: {e.Message}", e);
            }

            return result;
        }
        private void CreateDevice(string deviceDescriptionXmlPath)
        {
            var document = new XmlDocument();
            document.Load(deviceDescriptionXmlPath);

            var url = new Uri(deviceDescriptionXmlPath, UriKind.Absolute);
            var ip = url.Host;
            var port = url.Port;
            var namespaceManager = new XmlNamespaceManager(document.NameTable);
            namespaceManager.AddNamespace("upnp", "urn:schemas-upnp-org:device-1-0");
            var id = document.SelectSingleNode("//upnp:UDN", namespaceManager)?.InnerText;
            var name = document.SelectSingleNode("//upnp:modelName", namespaceManager)?.InnerText;
            var model = document.SelectSingleNode("//upnp:modelNumber", namespaceManager)?.InnerText;

            lock (_lock)
            {
                if (string.IsNullOrEmpty(id) || _detectedSonosIds.Contains(id))
                {
                    return;
                }

                _detectedSonosIds.Add(id);
            }

            var upnpServices = GetServices(document, namespaceManager).ToList();

            foreach (var service in upnpServices)
            {
                var serviceUrl = $"http://{ip}:{port}{service.DescriptionUrl}";
                var actions = GetActions(serviceUrl);
                service.Actions.AddRange(actions);
            }

            var device = new SonosDevice(id, ip, name);
            device.Services.AddRange(upnpServices);
            device.Type = model;

            OnDeviceFound(device);
        }
 public SonosScriptObject(ISonosGateway gateway, SonosDevice device)
 {
     _gateway = gateway;
     _device = device;
 }
Exemplo n.º 12
0
 public void Stop(SonosDevice device)
 {
     StartActionInNewTask(device, new Action("Stop"), new Dictionary <string, string>(0));
 }
Exemplo n.º 13
0
 public void Pause(SonosDevice device)
 {
     StartActionInNewTask(device, new Action("Pause"), new Dictionary <string, string>(0));
 }
Exemplo n.º 14
0
 private void UpdateVariable(SonosDevice device, string variable, object value)
 {
     _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, variable, value));
 }
Exemplo n.º 15
0
        private async Task UpdateDeviceVariablesAsync(SonosDevice device)
        {
            var avTransport       = device.Services.Single(s => s.Id.Contains("AVTransport"));
            var renderingControl  = device.Services.Single(s => s.Id.Contains(":RenderingControl"));
            var deviceProperties  = device.Services.Single(s => s.Id.Contains("DeviceProperties"));
            var getMediaInfo      = avTransport.Actions.Single(s => s.Name.Equals("GetMediaInfo"));
            var getTransportInfo  = avTransport.Actions.Single(s => s.Name.Equals("GetTransportInfo"));
            var getPositionInfo   = avTransport.Actions.Single(s => s.Name.Equals("GetPositionInfo"));
            var getVolume         = renderingControl.Actions.Single(s => s.Name.Equals("GetVolume"));
            var getZoneAttributes = deviceProperties.Actions.Single(s => s.Name.Equals("GetZoneAttributes"));

            var values = new Dictionary <string, string>
            {
                { "InstanceID", "0" }
            };

            var mediaInfo = await _soapClient.ExecuteAsync(device, avTransport, getMediaInfo, values);

            var transportInfo = await _soapClient.ExecuteAsync(device, avTransport, getTransportInfo, values);

            var positionInfo = await _soapClient.ExecuteAsync(device, avTransport, getPositionInfo, values);

            values.Add("Channel", "Master");
            var volume = await _soapClient.ExecuteAsync(device, renderingControl, getVolume, values);

            values.Clear();
            var zoneAttributes = await _soapClient.ExecuteAsync(device, deviceProperties, getZoneAttributes, values);

            string currentUri;
            string metadata;
            string transportState;
            string currentVolume;

            if (!mediaInfo.TryGetValue("CurrentURI", out currentUri) ||
                !mediaInfo.TryGetValue("CurrentURIMetaData", out metadata) ||
                !transportInfo.TryGetValue("CurrentTransportState", out transportState) ||
                !volume.TryGetValue("CurrentVolume", out currentVolume))
            {
                return;
            }

            string trackUri;

            positionInfo.TryGetValue("TrackURI", out trackUri);

            transportState = transportState[0] + transportState.Substring(1).ToLowerInvariant();

            device.CurrentUri     = currentUri;
            device.TransportState = transportState;
            device.Volume         = double.Parse(currentVolume);
            device.IsMaster       = string.IsNullOrEmpty(trackUri) || !trackUri.StartsWith("x-rincon:RINCON");
            device.Zone           = zoneAttributes["CurrentZoneName"] ?? string.Empty;

            var master = GetMaster(device);

            if (!ReferenceEquals(master, device))
            {
                device.TransportState = master.TransportState;
            }

            UpdateVariable(device, "TransportState", device.TransportState);
            UpdateVariable(device, "CurrentUri", device.CurrentUri);
            UpdateVariable(device, "Volume", device.Volume);
            UpdateVariable(device, "IsMaster", device.IsMaster);
            UpdateVariable(device, "Zone", device.Zone);
        }
 public SonosScriptObject(ISonosGateway gateway, SonosDevice device)
 {
     _gateway = gateway;
     _device  = device;
 }
 private void OnDeviceFound(SonosDevice e)
 {
     DeviceFound?.Invoke(this, e);
 }
 private void OnDeviceFound(SonosDevice e)
 {
     DeviceFound?.Invoke(this, e);
 }