Esempio n. 1
0
        public async void ServerPolling(object sender, EventArgs e)
        {
            pollCounter++;
            Console.WriteLine("PollCounter: {0}", pollCounter);
            StringBuilder deviceUri = new StringBuilder(_serverComm.Root);

            deviceUri.Append("/device/");
            deviceUri.Append(device_id);
            deviceUri.Append("/connect/status");
            var pollresult = await _serverComm.GetAsync(deviceUri.ToString(), true);

            if (pollresult.Item1 == ServerResponse.Connected)
            {
                string jsonstr = await pollresult.Item2.Content.ReadAsStringAsync();

                ConnectionDetail connectiondetails = JsonConvert.DeserializeObject <ConnectionDetail>(jsonstr);
                Console.WriteLine("Poll Request status: {0}", connectiondetails.status);
                if (connectiondetails.status.Contains("success"))
                {
                    _timer.Stop();
                    ConnectDevice(connectiondetails);
                }
                if (connectiondetails.status.Contains("failure"))
                {
                    _timer.Stop();
                    _dev.client_name = null;
                    UpdateUI();
                    Console.WriteLine("Server Error: {0}", connectiondetails.details.error_message);
                }
                if (connectiondetails.status.Contains("connecting"))
                {
                    Console.WriteLine("Connecting...");
                }
            }
            if (pollCounter >= 10)
            {
                Console.WriteLine("Polling timed out");
                _dev.client_name = null;
                UpdateUI();
                _timer.Stop();
            }
        }