Exemple #1
0
      private void _listDevicesFill() {
         var opco = (cmbOpco.SelectedItem != null) ? ((OpcoComboBoxItem)cmbOpco.SelectedItem).Opco : null;

         this.Freeze();

         var comparer = listBoxes.ListViewItemSorter;
         listBoxes.ListViewItemSorter = null;
         listBoxes.Items.Clear();

         if (_devices != null && _devices.Count != 0) {
            foreach (var dev in _devices) {
               if (opco == null || dev.Operator == opco) {
                  if (!_isExcluded(dev.DeviceId)) {
                     var li = new DeviceListItem(dev);
                     listBoxes.Items.Add(li);
                  }
               }
            }

            _onSizeChanged(null, null);
         }

         listBoxes.ListViewItemSorter = comparer;
         _updateSelected();

         this.Unfreeze();
      }
      protected override void OnRequestComplete(object response) {
         if (response == null) {
            MessageBox.Show("Communication or internal error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }

         var respType = response.GetType();
         if (respType == typeof(BplCollection<DiagnosticsTaskInfo>)) {
            var tasks = (BplCollection<DiagnosticsTaskInfo>)response;

            listTasks.Items.Clear();
            _selectedTask = null;

            _selectedDevice = null;
            listDevices.Items.Clear();

            if (tasks.Count > 0) {
               foreach (var task in tasks) {
                  var li = new TaskListItem(task);
                  listTasks.Items.Add(li);
               }
               _onSizeChanged(null, null);
            }
         } else if (respType == typeof(BplCollection<DiagnosticsTaskDevice>)) {
            var devices = (BplCollection<DiagnosticsTaskDevice>)response;

            this.Freeze();
            var comparer = listDevices.ListViewItemSorter;
            listDevices.ListViewItemSorter = null;
            listDevices.Items.Clear();
            _selectedDevice = null;

            if (devices.Count > 0) {
               foreach (var dev in devices) {
                  var li = new DeviceListItem(dev);
                  listDevices.Items.Add(li);
               }
               _onSizeChanged(null, null);
            }
            listDevices.ListViewItemSorter = comparer;
            this.Unfreeze();
         } else if (respType == typeof(DeleteDiagnosticsTaskResponse)) {
            var swlr = ((SwlResponse)response).swlr;
            if (swlr != SwlResult.OK) {
               var taskItem = (TaskListItem)listTasks.SelectedItems[0];
               MessageBox.Show("Failed to delete task.", "Delete task \x22" + taskItem.Task.TaskName + "\x22", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            _listTasksRefresh();
         } else if (respType == typeof(AddDevicesToDiagnosticsTaskResponse)) {
            var resp = (AddDevicesToDiagnosticsTaskResponse)response;
            if (resp.swlr != SwlResult.OK) {
               var taskItem = (TaskListItem)listTasks.SelectedItems[0];

               if (resp.DeviceIds.Count == 0) {
                  MessageBox.Show("Failed to add device(s) to task.", "Add device(s) to task \x22" + taskItem.Task.TaskName + "\x22",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
               } else {
                  var caption = "Failed to add device(s) to task\x22" + taskItem.Task.TaskName + "\x22";
                  var msg = "Following device(s) already have this task type:\r\n";

                  foreach (var deviceId in resp.DeviceIds) {
                     msg += (deviceId.LocalId.ToString() + "\r\n");
                  }

                  MessageBox.Show(msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
            }

            _listDevicesRefresh();
         } else if (respType == typeof(RemoveDeviceFromDiagnosticsTaskResponse)) {
            var swlr = ((SwlResponse)response).swlr;
            if (swlr != SwlResult.OK) {
               var taskItem = (TaskListItem)listTasks.SelectedItems[0];
               MessageBox.Show("Failed to remove device from task.", "Remove device from task \x22" + taskItem.Task.TaskName + "\x22", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            _listDevicesRefresh();
         }
      }
        public void RegisterDevice(Device device, DeviceListItem dli)
        {
            try {
                var kvp = new List<KeyValuePair<String, String>>();
                var useragent = String.Format("{0}", this.GetType().Assembly.GetName().Version.ToString());

                // if the Id is an IP, we need to find the real SerialNumber.
                var realSerialNo = device.SerialNumber;
                var kvpid = new KeyValuePair<string, string>("Id", realSerialNo);
                if(realSerialNo.Contains(":")) {
                    // it is probably an IP:PORT combo. Lets try to get the real serial number, or what we have if we can't find it.
                    var tid = device.Properties.FirstOrValue(x => x.Key == "ro.serialno" || x.Key == "ro.boot.serialno", new KeyValuePair<string, string>("default.serialno", realSerialNo)).Value;
                    if(!string.IsNullOrWhiteSpace(tid) && tid != realSerialNo) {
                        kvpid = new KeyValuePair<string, string>("Id", tid);
                        realSerialNo = tid;
                    }
                }

                // should we record
                if(!ShouldRecordStatistics(realSerialNo)) {
                    this.LogDebug("Skipping registration of device because delay not reached.");
                    return;
                }

                kvp.Add(kvpid);
                kvp.Add(new KeyValuePair<string, string>("ProductName", dli.ProductName));
                kvp.Add(new KeyValuePair<string, string>("ModelName", dli.ModelName));
                kvp.Add(new KeyValuePair<string, string>("DeviceName", dli.DeviceName));

                device.Properties.Add("ro.droidexplorer.version", useragent);
                device.Properties.Add("ro.droidexplorer.root", true.ToString());
                device.Properties.Add("ro.droidexplorer.busybox", true.ToString());
                device.Properties.Add("ro.droidexplorer.architecture", Architecture.IsRunningX64 ? "x64" : "x86");
                device.Properties.Add("ro.droidexplorer.platform", Environment.OSVersion.Platform.ToString());
                device.Properties.Add("ro.droidexplorer.platformversion", Environment.OSVersion.VersionString);

                var propCount = 0;
                foreach(var item in device.Properties.Where(item => !FilteredProperties.Contains(item.Key))) {
                    kvp.Add(new KeyValuePair<String, String>(String.Format("Properties[{0}].Name", propCount), item.Key));
                    kvp.Add(new KeyValuePair<String, String>(String.Format("Properties[{0}].Value", propCount), item.Value));
                    ++propCount;
                }

                var req = HttpWebRequest.Create(CreateUrl("device/add")) as HttpWebRequest;
                req.UserAgent = useragent;
                AddAuthenticationHeaders(req);

                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";

                var data = CreateFormattedPostRequest(kvp);

                var bytes = data.GetBytes();
                req.ContentLength = bytes.Length;
                req.Timeout = 60 * 1000;
                using(var rs = req.GetRequestStream()) {
                    rs.Write(bytes, 0, bytes.Length);
                }
                using(var resp = req.GetResponse() as HttpWebResponse) {
                    if(resp.StatusCode != HttpStatusCode.OK) {
                        this.LogError(String.Format("POST Statistics Failed (Error Code: {1}): {0}", resp.StatusDescription, resp.StatusCode));
                    }
                }

                // track when we recorded this
                Settings.Instance.SystemSettings.SetLastRecordCloud(realSerialNo);
            } catch(WebException ex) {
                this.LogError(ex.Message, ex);
            }
        }