AddDevice.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using DeviceCenter.model;
  2. using DeviceCenter.utils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. namespace DeviceCenter
  10. {
  11. /// <summary>
  12. /// AddDevice.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class AddDevice : Window
  15. {
  16. private Config config = Config.getInstance();
  17. private ObservableCollection<Area> areas = new ObservableCollection<Area>();
  18. private ObservableCollection<Building> buildings = new ObservableCollection<Building>();
  19. private ObservableCollection<Floor> floors = new ObservableCollection<Floor>();
  20. public Device device { get; set; }
  21. public AddDevice()
  22. {
  23. InitializeComponent();
  24. cb_area.ItemsSource = areas;
  25. cb_building.ItemsSource = buildings;
  26. cb_floor.ItemsSource = floors;
  27. http.get<GetAreasResponse>("/area/all", new Dictionary<string, string> { ["size"] = "1000" }).Then(res =>
  28. {
  29. Console.WriteLine(res);
  30. res.content.ToList().ForEach(areas.Add);
  31. });
  32. }
  33. private void Button_Click(object sender, RoutedEventArgs e)
  34. {
  35. if (string.IsNullOrWhiteSpace(tb_name.Text))
  36. {
  37. MessageBox.Show("请输入名称");
  38. return;
  39. }
  40. if (string.IsNullOrWhiteSpace(tb_ip.Text))
  41. {
  42. MessageBox.Show("请输入ip");
  43. return;
  44. }
  45. if (cb_type.SelectedValue == null)
  46. {
  47. MessageBox.Show("请选择设备类型");
  48. return;
  49. }
  50. if ((DeviceType)cb_type.SelectedValue == DeviceType.ACS && string.IsNullOrWhiteSpace(tb_port.Text))
  51. {
  52. MessageBox.Show("请输入端口");
  53. return;
  54. }
  55. Device device;
  56. if ((DeviceType)cb_type.SelectedValue == DeviceType.ACS)
  57. {
  58. device = new AcsDevice();
  59. }
  60. else
  61. {
  62. device = new CarCamDevice();
  63. }
  64. device.ip = tb_ip.Text;
  65. if (!string.IsNullOrEmpty(tb_port.Text))
  66. {
  67. device.port = int.Parse(tb_port.Text);
  68. }
  69. device.name = tb_name.Text;
  70. device.username = tb_username.Text;
  71. device.password = tb_password.Text;
  72. device.status = DeviceStatus.IDLE;
  73. device.areaId = (long?)cb_area.SelectedValue;
  74. device.buildingId = (long?)cb_building.SelectedValue;
  75. device.floorId = (long?)cb_floor.SelectedValue;
  76. if (cb_dir.SelectedValue != null)
  77. {
  78. device.direction = (Direction)cb_dir.SelectedValue;
  79. }
  80. this.DialogResult = true;
  81. this.device = device;
  82. Close();
  83. }
  84. private void cb_area_SelectionChanged(object sender, SelectionChangedEventArgs e)
  85. {
  86. http.get<GetBuildingResponse>("/building/all", new Dictionary<string, string>
  87. {
  88. ["size"] = "1000",
  89. ["query"] = "%7B%22areaId%22:" + cb_area.SelectedValue + "%7d"
  90. }).Then(res =>
  91. {
  92. buildings.Clear();
  93. res.content.ToList().ForEach(buildings.Add);
  94. }).Catch(err =>
  95. {
  96. Console.WriteLine(err);
  97. });
  98. }
  99. private void cb_building_SelectionChanged(object sender, SelectionChangedEventArgs e)
  100. {
  101. http.get<GetFloorResponse>("/floor/all", new Dictionary<string, string>
  102. {
  103. ["size"] = "1000",
  104. ["query"] = "%7B%22buildingId%22:" + cb_building.SelectedValue + "%7d"
  105. }).Then(res =>
  106. {
  107. floors.Clear();
  108. res.content.ToList().ForEach(floors.Add);
  109. }).Catch(err =>
  110. {
  111. Console.WriteLine(err);
  112. });
  113. }
  114. }
  115. }