Пример #1
0
        /// <summary>
        ///     Executes the CreateAlarm action.
        /// </summary>
        /// <param name="startLocalTime">In value for the StartLocalTime action parameter.</param>
        /// <param name="duration">In value for the Duration action parameter.</param>
        /// <param name="recurrence">In value for the Recurrence action parameter.</param>
        /// <param name="enabled">In value for the Enabled action parameter.</param>
        /// <param name="roomUuid">In value for the RoomUUID action parameter.</param>
        /// <param name="programUri">In value for the ProgramURI action parameter.</param>
        /// <param name="programMetaData">In value for the ProgramMetaData action parameter.</param>
        /// <param name="playMode">In value for the PlayMode action parameter. Default value of NORMAL.</param>
        /// <param name="volume">In value for the Volume action parameter.</param>
        /// <param name="includeLinkedZones">In value for the IncludeLinkedZones action parameter.</param>
        /// <returns>Out value for the AssignedID action parameter.</returns>
        public async Task <ActionResult> CreateAlarm(String startLocalTime, String duration,
                                                     AargtypeRecurrenceEnum recurrence, Boolean enabled, String roomUuid, String programUri,
                                                     String programMetaData, AargtypeAlarmPlayModeEnum playMode, UInt16 volume, Boolean includeLinkedZones)
        {
            var loIn = new object[10];

            loIn[0] = startLocalTime;
            loIn[1] = duration;
            loIn[2] = AlarmClock.ToStringAargtypeRecurrence(recurrence);
            loIn[3] = enabled;
            loIn[4] = roomUuid;
            loIn[5] = programUri;
            loIn[6] = programMetaData;
            loIn[7] = AlarmClock.ToStringAargtypeAlarmPlayMode(playMode);
            loIn[8] = volume;
            loIn[9] = includeLinkedZones;
            var action = new SoapAction
            {
                ArgNames =
                    new[]
                {
                    "StartLocalTime", "Duration", "Recurrence", "Enabled", "RoomUUID", "ProgramURI", "ProgramMetaData",
                    "PlayMode", "Volume", "IncludeLinkedZones", "AssignedID"
                },
                Name = CsActionCreateAlarm,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
        public async Task <DeviceLink> GetDeviceLinkCode(string householdId)
        {
            var loIn = new object[1];

            loIn[0] = householdId;

            var action = new SoapAction
            {
                ArgNames           = new[] { "householdId" },
                Name               = "getDeviceLinkCode",
                ExpectedReplyCount = 3
            };
            string header = "<s:Header>" +
                            "<credentials xmlns=\"{0}\">" +
                            "<deviceId>{2}</deviceId>" +
                            "<deviceProvider>Sonos</deviceProvider>" +
                            "</credentials>" +
                            "</s:Header>";

            header = string.Format(header, ServiceType, "", _deviceId);

            SoapActionResult result = await InvokeActionAsync(action, loIn, header, false);

            var actionResult = new ActionResult(result.XElement);

            if (actionResult.Exception != null)
            {
                Debug.WriteLine("GetDeviceLinkCode Exception: " + actionResult.Exception.Message);
                return(null);
            }
            var deviceLink = new DeviceLink(actionResult.Result);

            return(deviceLink);
        }
Пример #3
0
        protected async Task <SoapActionResult> InvokeActionAsync(SoapAction action, object[] args, string header,
                                                                  bool addXmlNs, string accessToken)
        {
            SoapActionResult result;

            try
            {
                var          rootElementStart = "<" + action.Name + " xmlns=\"" + ServiceType + "\">";
                var          rootElementEnd   = "</" + action.Name + ">";
                const string format           = "<{0}>{1}</{0}>";

                if (addXmlNs)
                {
                    rootElementStart = "<u:" + action.Name + " xmlns:u=\"" + ServiceType + "\">";
                    rootElementEnd   = "</u:" + action.Name + ">";
                }

                var soap = new StringBuilder(rootElementStart);

                if (args != null)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        soap.AppendFormat(format, action.ArgNames[i], FormatArg(args[i]));
                    }
                }

                soap.AppendFormat(rootElementEnd);

                result = await SoapRequestAsync(soap.ToString(), action.Name, header).ConfigureAwait(false);

                // just return in case of error
                if (result.Error != null)
                {
                    return(new SoapActionResult(result.Error, result.XElement));
                }

                if (action.ExpectedReplyCount != 0)
                {
                    // Children of u:<VERB>Response are the result we want
                    var xn = XName.Get(action.Name + "Response", ServiceType);
                    var responseElement = result.XElement.Descendants(xn).FirstOrDefault();
                    result.XElement = responseElement;
                }
            }
            catch (Exception ex)
            {
                result = new SoapActionResult(new SoapActionError(ex, action.Name), new XElement("ERROR"));
            }

            return(result);
        }
        /// <summary>
        ///     Executes the GetCurrentConnectionIDs action.
        /// </summary>
        /// <returns>Out value for the ConnectionIDs action parameter.</returns>
        public async Task <ActionResult> GetCurrentConnectionIDs()
        {
            var loIn = new object[0];

            var action = new SoapAction
            {
                ArgNames           = new string[0],
                Name               = CsActionGetCurrentConnectionIDs,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #5
0
        /// <summary>
        ///     Executes the GetHouseholdTimeAtStamp action.
        /// </summary>
        /// <param name="timeStamp">In value for the TimeStamp action parameter.</param>
        /// <returns>Out value for the HouseholdUTCTime action parameter.</returns>
        public async Task <ActionResult> GetHouseholdTimeAtStamp(String timeStamp)
        {
            var loIn = new object[1];

            loIn[0] = timeStamp;
            var action = new SoapAction
            {
                ArgNames           = new[] { "TimeStamp" },
                Name               = CsActionGetHouseholdTimeAtStamp,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #6
0
        /// <summary>
        ///     Executes the GetHeadphoneConnected action.
        /// </summary>
        /// <param name="instanceId">In value for the InstanceID action parameter.</param>
        /// <returns>Out value for the CurrentHeadphoneConnected action parameter.</returns>
        public async Task <ActionResult> GetHeadphoneConnected(UInt32 instanceId)
        {
            var loIn = new object[1];

            loIn[0] = instanceId;
            var action = new SoapAction
            {
                ArgNames           = new[] { "InstanceID" },
                Name               = CsActionGetHeadphoneConnected,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #7
0
        /// <summary>
        ///     Executes the GetStringX action.
        /// </summary>
        /// <param name="variableName">In value for the VariableName action parameter.</param>
        /// <returns>Out value for the StringValue action parameter.</returns>
        public async Task <ActionResult> GetStringX(String variableName)
        {
            var loIn = new object[1];

            loIn[0] = variableName;
            var action = new SoapAction
            {
                ArgNames           = new[] { "VariableName" },
                Name               = CsActionGetStringX,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #8
0
        /// <summary>
        ///     Executes the AddMember action.
        /// </summary>
        /// <param name="memberId">In value for the MemberID action parameter.</param>
        public async Task <ActionResult> AddMember(String memberId)
        {
            var loIn = new object[1];

            loIn[0] = memberId;
            var action = new SoapAction
            {
                ArgNames           = new[] { "MemberID" },
                Name               = CsActionAddMember,
                ExpectedReplyCount = 3
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #9
0
        /// <summary>
        ///     Executes the GetTimeZoneRule action.
        /// </summary>
        /// <param name="index">In value for the Index action parameter.</param>
        /// <returns>Out value for the TimeZone action parameter.</returns>
        public async Task <ActionResult> GetTimeZoneRule(Int32 index)
        {
            var loIn = new object[1];

            loIn[0] = index;
            var action = new SoapAction
            {
                ArgNames           = new[] { "Index" },
                Name               = CsActionGetTimeZoneRule,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #10
0
        /// <summary>
        ///     Executes the GetEQ action.
        /// </summary>
        /// <param name="instanceId">In value for the InstanceID action parameter.</param>
        /// <param name="eQType">In value for the EQType action parameter.</param>
        /// <returns>Out value for the CurrentValue action parameter.</returns>
        public async Task <ActionResult> GetEq(UInt32 instanceId, String eQType)
        {
            var loIn = new object[2];

            loIn[0] = instanceId;
            loIn[1] = eQType;
            var action = new SoapAction
            {
                ArgNames           = new[] { "InstanceID", "EQType" },
                Name               = CsActionGetEq,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #11
0
        /// <summary>
        ///     Executes the GetWebCode action.
        /// </summary>
        /// <param name="accountType">In value for the AccountType action parameter.</param>
        /// <returns>Out value for the WebCode action parameter.</returns>
        public async Task <ActionResult> GetWebCode(UInt32 accountType)
        {
            var loIn = new object[1];

            loIn[0] = accountType;

            var action = new SoapAction
            {
                ArgNames           = new[] { "AccountType" },
                Name               = CsActionGetWebCode,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #12
0
        /// <summary>
        ///     Executes the GetLoudness action.
        /// </summary>
        /// <param name="instanceId">In value for the InstanceID action parameter.</param>
        /// <param name="channel">In value for the Channel action parameter.</param>
        /// <returns>Out value for the CurrentLoudness action parameter.</returns>
        public async Task <ActionResult> GetLoudness(UInt32 instanceId, AargtypeChannelEnum channel)
        {
            var loIn = new object[2];

            loIn[0] = instanceId;
            loIn[1] = RenderingControlExtensions.ToStringAargtypeChannel(channel);
            var action = new SoapAction
            {
                ArgNames           = new[] { "InstanceID", "Channel" },
                Name               = CsActionGetLoudness,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #13
0
        public async Task <DeviceAuthToken> GetDeviceAuthToken(string householdId, string linkCode)
        {
            var loIn = new object[2];

            loIn[0] = householdId;
            loIn[1] = linkCode;

            var action = new SoapAction
            {
                ArgNames           = new[] { "householdId", "linkCode" },
                Name               = "getDeviceAuthToken",
                ExpectedReplyCount = 2
            };
            string header = "<s:Header>" +
                            "<credentials xmlns=\"{0}\">" +
                            "<sessionId>{1}</sessionId>" +
                            "<deviceId>{2}</deviceId>" +
                            "<deviceProvider>Sonos</deviceProvider>" +
                            "</credentials>" +
                            "</s:Header>";

            header = string.Format(header, ServiceType, "", _deviceId);
            //var te = await GetMessageHeader();
            SoapActionResult result = await InvokeActionAsync(action, loIn, header, false);

            Exception ex = null;

            if (result.Error != null)
            {
                ex = result.Error.Exception;
            }

            var actionResult = new ActionResult(result.XElement, ex);

            if (actionResult.Exception != null)
            {
                return(null);
            }
            var deviceAuthToken = new DeviceAuthToken(actionResult.Result);

            _credentialsOk = null;
            _sonosMusicApi.SetDeviceAuthToken(MusicServiceContainer.Title, deviceAuthToken);
            return(deviceAuthToken);
        }
        /// <summary>
        ///     Executes the CheckForUpdate action.
        /// </summary>
        /// <param name="updateType">In value for the UpdateType action parameter.</param>
        /// <param name="cachedOnly">In value for the CachedOnly action parameter.</param>
        /// <param name="version">In value for the Version action parameter.</param>
        /// <returns>Out value for the UpdateItem action parameter.</returns>
        public async Task <ActionResult> CheckForUpdate(AargtypeUpdateTypeEnum updateType, Boolean cachedOnly,
                                                        String version)
        {
            var loIn = new object[3];

            loIn[0] = ZoneGroupTopologyExtensions.ToStringAargtypeUpdateType(updateType);
            loIn[1] = cachedOnly;
            loIn[2] = version;
            var action = new SoapAction
            {
                ArgNames           = new[] { "UpdateType", "CachedOnly", "Version" },
                Name               = CsActionCheckForUpdate,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #15
0
        /// <summary>
        ///     Executes the ProvisionCredentialedTrialAccountX action.
        /// </summary>
        /// <param name="accountType">In value for the AccountType action parameter.</param>
        /// <param name="accountId">In value for the AccountID action parameter.</param>
        /// <param name="accountPassword">In value for the AccountPassword action parameter.</param>
        /// <returns>Out value for the IsExpired action parameter.</returns>
        public async Task <ActionResult> ProvisionCredentialedTrialAccountX(UInt32 accountType, String accountId,
                                                                            String accountPassword)
        {
            var loIn = new object[3];

            loIn[0] = accountType;
            loIn[1] = accountId;
            loIn[2] = accountPassword;
            var action = new SoapAction
            {
                ArgNames           = new[] { "AccountType", "AccountID", "AccountPassword" },
                Name               = CsActionProvisionCredentialedTrialAccountX,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #16
0
        /// <summary>
        ///     Executes the SetRelativeVolume action.
        /// </summary>
        /// <param name="instanceId">In value for the InstanceID action parameter.</param>
        /// <param name="channel">In value for the Channel action parameter.</param>
        /// <param name="adjustment">In value for the Adjustment action parameter.</param>
        /// <returns>Out value for the NewVolume action parameter. With range of 0 to 100. Increment of 1.</returns>
        public async Task <ActionResult> SetRelativeVolume(UInt32 instanceId, AargtypeChannelEnum channel,
                                                           Int32 adjustment)
        {
            var loIn = new object[3];

            loIn[0] = instanceId;
            loIn[1] = RenderingControlExtensions.ToStringAargtypeChannel(channel);
            loIn[2] = adjustment;
            var action = new SoapAction
            {
                ArgNames           = new[] { "InstanceID", "Channel", "Adjustment" },
                Name               = CsActionSetRelativeVolume,
                ExpectedReplyCount = 1
            };

            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #17
0
        public async Task <ActionResult> GetMetadata(string id, int index, int count)
        {
            var loIn = new object[3];

            loIn[0] = id;
            loIn[1] = index;
            loIn[2] = count;

            var action = new SoapAction
            {
                ArgNames           = new[] { "id", "index", "count" },
                Name               = "getMetadata",
                ExpectedReplyCount = count
            };
            string header = await GetMessageHeader();

            SoapActionResult soapActionResult = await InvokeActionAsync(action, loIn, header, false);


            // TODO: check for execption
            return(new ActionResult(soapActionResult.XElement));
        }
Пример #18
0
        /// <summary>
        ///     Executes the RampToVolume action.
        /// </summary>
        /// <param name="instanceId">In value for the InstanceID action parameter.</param>
        /// <param name="channel">In value for the Channel action parameter.</param>
        /// <param name="rampType">In value for the RampType action parameter.</param>
        /// <param name="desiredVolume">In value for the DesiredVolume action parameter. With range of 0 to 100. Increment of 1.</param>
        /// <param name="resetVolumeAfter">In value for the ResetVolumeAfter action parameter.</param>
        /// <param name="programUri">In value for the ProgramURI action parameter.</param>
        /// <returns>Out value for the RampTime action parameter.</returns>
        public async Task <ActionResult> RampToVolume(UInt32 instanceId, AargtypeChannelEnum channel,
                                                      AargtypeRampTypeEnum rampType, UInt16 desiredVolume, Boolean resetVolumeAfter, String programUri)
        {
            var loIn = new object[6];

            loIn[0] = instanceId;
            loIn[1] = RenderingControlExtensions.ToStringAargtypeChannel(channel);
            loIn[2] = RenderingControlExtensions.ToStringAargtypeRampType(rampType);
            loIn[3] = desiredVolume;
            loIn[4] = resetVolumeAfter;
            loIn[5] = programUri;
            var action = new SoapAction
            {
                ArgNames =
                    new[] { "InstanceID", "Channel", "RampType", "DesiredVolume", "ResetVolumeAfter", "ProgramURI" },
                Name = CsActionRampToVolume,
                ExpectedReplyCount = 1
            };
            SoapActionResult result = await InvokeActionAsync(action, loIn);

            // TODO: check for execption
            return(new ActionResult(result.XElement));
        }
Пример #19
0
        private async Task <SoapActionResult> SoapRequestAsync(string soap, string action, string header)
        {
            //string req = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            //var req = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            var req = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                      " s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                      header +
                      "<s:Body>" +
                      soap +
                      "</s:Body>" +
                      "</s:Envelope>";
            var uri = new Uri(ControlUrl);

            var r = (HttpWebRequest)WebRequest.Create(uri);

#if false
            Debug.WriteLine(req);
#endif
            // We only ever need to do this over non-cellular networks
            r.SetNetworkPreference(NetworkSelectionCharacteristics.NonCellular);
            r.Method = "POST";
            byte[] b = Encoding.UTF8.GetBytes(req);
            r.Headers["SOAPACTION"] = "\"" + ServiceType + "#" + action + "\"";
            r.ContentType           = "text/xml; charset=\"utf-8\"";

            using (var stream = await r.GetRequestStreamAsync())
            {
                stream.Write(b, 0, b.Length);
            }

            WebResponse resp      = null;
            Exception   exception = null;
            try
            {
                resp = await r.GetResponseAsync();
            }
            catch (WebException ex)
            {
                exception = ConvertException(ex);
            }

            SoapActionError error = null;
            if (exception != null)
            {
                error = new SoapActionError(exception, req);
                Debug.WriteLine("error: " + error.Exception.Message);
            }

            var result = new SoapActionResult(error, null);
            if (result.Error == null && resp != null)
            {
                Stream stream = null;
                try
                {
                    stream = resp.GetResponseStream();
                    stream.Seek(0, SeekOrigin.Begin);
                    if (resp.Headers["Content-Encoding"] == "gzip")
                    {
                        stream = new GZipInputStream(stream);
                    }
                    result.XElement = XElement.Load(stream);
                }
                catch (Exception e)
                {
                    result.XElement = new XElement("ERROR");
                    if (result.Error != null)
                    {
                        result.Error.Exception = e;
                        result.Error.Request   = req;
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            else
            {
                result.XElement = new XElement("ERROR");
            }
            if (resp != null)
            {
                resp.Dispose();
            }

            return(result);
        }