Ejemplo n.º 1
0
        public async Task Rescan()
        {
            try
            {
                Log.Debug("[Rescan] Start !");

                List <DeviceModel> remainDMs  = new List <DeviceModel>(DeviceModelCollection);
                List <DeviceModel> newDMs     = new List <DeviceModel>();
                string             deviceList = await GetPluggedDevicesFromService();

                string[] deviceArray = deviceList.Split(':');

                foreach (var deviceString in deviceArray)
                {
                    if (deviceString == "")
                    {
                        continue;
                    }

                    string[] deviceData = deviceString.Split(',');

                    if (deviceData.Length != 6)
                    {
                        Log.Debug("[Rescan] Invalid device info : " + deviceArray);
                        continue;
                    }

                    var get = DeviceModelCollection.Find(find => find.ModelName == deviceData[0]);
                    if (get == null)
                    {
                        DeviceModel dm = await DeviceModel.ToDeviceModelAsync(
                            deviceData[0], deviceData[1], deviceData[2],
                            deviceData[3], deviceData[4], deviceData[5]);

                        if (dm != null)
                        {
                            newDMs.Add(dm);
                        }
                    }
                    else
                    {
                        if (deviceData[5] == "true")
                        {
                            get.Plugged = true;
                            get.Sync    = true;
                        }
                        else if (deviceData[5] == "false")
                        {
                            get.Plugged = true;
                            get.Sync    = false;
                        }

                        remainDMs.Remove(get);
                    }
                }

                foreach (var dm in newDMs)
                {
                    Rect  r = new Rect(0, 0, dm.PixelWidth, dm.PixelHeight);
                    Point p = GetFreeRoomPositionForRect(r);
                    dm.PixelLeft = p.X;
                    dm.PixelTop  = p.Y;
                    dm.Plugged   = true;

                    // Delete temp data as same type as new, because new device is plugging
                    LayerPage.Self.ClearDeviceData(dm.Type);

                    if (dm.Type == (int)DeviceType.Notebook || dm.Type == (int)DeviceType.Desktop)
                    {
                        DeviceModelCollection.Insert(0, dm);
                    }
                    else
                    {
                        DeviceModelCollection.Add(dm);
                    }
                }

                // The rest mean device was unplugged
                foreach (var dm in remainDMs)
                {
                    dm.Plugged = false;
                }

                RefreshStage();
                NotifyConnectedDialog();
            }
            catch
            {
                MaskManager.GetInstance().ShowMask(MaskType.NoSupportDevice);
                Log.Debug("[Rescan] Rescan pluggeddevices failed !");
            }
        }