コード例 #1
0
ファイル: Device.cs プロジェクト: Dan-deWaal/IoTBoxTiles
        public async void ServerRequest(Device dev)
        {
            _dev             = dev;
            _dev.client_name = "Requesting...";
            UpdateUI();
            //Send a request to the server for Device Info
            RequestInfo requestinfo = new RequestInfo();

            requestinfo.hostname = _serverComm.GetNETBIOSName();
            requestinfo.localIP  = _serverComm.GetLocalIPAddress();
            if (DevicesForm._client_id != null)
            {
                requestinfo.client_id = DevicesForm._client_id;
            }
            string request = JsonConvert.SerializeObject(requestinfo);

            Console.WriteLine("Request to server: {0}", request);
            StringBuilder deviceUri = new StringBuilder(_serverComm.Root);

            deviceUri.Append("/device/");
            deviceUri.Append(device_id);
            deviceUri.Append("/connect");
            var result = await _serverComm.PostAsync(deviceUri.ToString(),
                                                     new StringContent(request), contentType : "application/json");

            if (result.Item1 != ServerResponse.Connected)
            {
                // should be handled better
                MessageBox.Show("Server disconnected during intialisation.");
                _dev.client_name = null;
            }
            else
            {
                //successful request
                string jsonstr = await result.Item2.Content.ReadAsStringAsync();

                Console.WriteLine("Request Response: {0}", jsonstr);
                ConnectRequest connectrequest = JsonConvert.DeserializeObject <ConnectRequest>(jsonstr);
                DevicesForm._client_id = connectrequest.client_id;
                Registry.SetValue(DevicesForm._keyName, _serverComm.Email, DevicesForm._client_id);
                Console.WriteLine("Registry written: {0} : {1} : {2}", DevicesForm._keyName, _serverComm.Email, DevicesForm._client_id);

                _dev.client_name = "Polling...";
                UpdateUI();
                pollCounter = 0;
                _timer.Start(); //start the polling timer
            }
        }