public async void SetDevice(DeviceItem item) { remoteFiles.Clear(); device = item; var root = await SendCommand("root"); if (root == null) { return; } NavigateRemote(root); }
public MainViewModel() { items = new ObservableCollection <DeviceItem>(); items.Add(new DeviceItem() { Name = "Connect via IP address", Color = "#e4715f" }); if (IsInDesignMode) { return; } udpClient = new UdpClient(8888); timer1 = new DispatcherTimer(); timer1.Interval = TimeSpan.FromSeconds(1); timer1.Tick += timer1_Tick; timer1.Start(); Task.Run(() => { var endpoint = new IPEndPoint(IPAddress.Any, 8888); while (true) { var bytes = udpClient.Receive(ref endpoint); var data = Encoding.UTF8.GetString(bytes); var ip = endpoint.Address.ToString(); var array = (JArray)JsonConvert.DeserializeObject(data); var id = array[0].Value <string>(); Application.Current.Dispatcher.BeginInvoke(new Action(() => { var item = items.SingleOrDefault((i) => { if (i.ID == null) { return(false); } return(i.ID.Equals(id)); }); if (item == null) { var newItem = new DeviceItem() { ID = id, Name = array[1].Value <string>(), Device = array[2].Value <string>(), Battery = array[3].Value <string>(), Storage = array[4].Value <string>(), Address = ip }; newItem.Color = Colors[Getss(newItem.ID)]; items.Insert(0, newItem); } else { item.Name = array[1].Value <string>(); item.Battery = array[3].Value <string>(); item.Storage = array[4].Value <string>(); item.LastUpdated = 0; } }), DispatcherPriority.Background); Task.Delay(100); } }); }