Ejemplo n.º 1
0
        /// <summary>
        /// Pairing device with user credentials (_credentials)
        /// </summary>
        private async Task CreatePairing()
        {
            _log.Debug($"Pairing device with user credentials");

            try
            {
                var ps = new Dictionary <string, string>()
                {
                    { "username", _credentials.Username },
                    { "password", _credentials.Password },
                    { "type", "samsungtv" }
                };

                var deviceConnectionString = await SendRequest("create-pairing", ps);

                var devConnJson = JObject.Parse(deviceConnectionString);

                if (
                    ((devConnJson.HasValue("status") && (devConnJson.GetStringValue("status") == "0"))) ||
                    ((devConnJson.HasValue("error")) && (devConnJson.GetStringValue("error") == "bad login")) ||
                    (!devConnJson.HasValue("deviceId"))
                    )
                {
                    if ((devConnJson.HasValue("error")) && (devConnJson.GetStringValue("error") == "bad login"))
                    {
                        _status = StatusEnum.PairingFailed;
                    }
                    else
                    {
                        _status = StatusEnum.LoginFailed;
                    }
                }
                else
                {
                    _status = StatusEnum.Paired;

                    _deviceConnection = new DeviceConnection()
                    {
                        deviceId = devConnJson.GetStringValue("deviceId").ToString(),
                        password = devConnJson.GetStringValue("password").ToString()
                    };

                    _log.Debug("Received User Connection:");
                    _log.Debug(_deviceConnection.ToString());
                }
            }
            catch (WebException wex)
            {
                _log.Error(wex, "Error while pairing device");
                _status = StatusEnum.ConnectionNotAvailable;
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Error while pairing device");
                _status = StatusEnum.GeneralError;
            }
        }