using DeviceCenter.model;
using DeviceCenter.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace DeviceCenter
{
///
/// AddDevice.xaml 的交互逻辑
///
public partial class AddDevice : Window
{
private Config config = Config.getInstance();
private ObservableCollection areas = new ObservableCollection();
private ObservableCollection buildings = new ObservableCollection();
private ObservableCollection floors = new ObservableCollection();
public Device device { get; set; }
public AddDevice()
{
InitializeComponent();
cb_area.ItemsSource = areas;
cb_building.ItemsSource = buildings;
cb_floor.ItemsSource = floors;
http.get("/area/all", new Dictionary { ["size"] = "1000" }).Then(res =>
{
Console.WriteLine(res);
res.content.ToList().ForEach(areas.Add);
});
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(tb_name.Text))
{
MessageBox.Show("请输入名称");
return;
}
if (string.IsNullOrWhiteSpace(tb_ip.Text))
{
MessageBox.Show("请输入ip");
return;
}
if (cb_type.SelectedValue == null)
{
MessageBox.Show("请选择设备类型");
return;
}
if ((DeviceType)cb_type.SelectedValue == DeviceType.ACS && string.IsNullOrWhiteSpace(tb_port.Text))
{
MessageBox.Show("请输入端口");
return;
}
Device device;
if ((DeviceType)cb_type.SelectedValue == DeviceType.ACS)
{
device = new AcsDevice();
}
else
{
device = new CarCamDevice();
}
device.ip = tb_ip.Text;
if (!string.IsNullOrEmpty(tb_port.Text))
{
device.port = int.Parse(tb_port.Text);
}
device.name = tb_name.Text;
device.username = tb_username.Text;
device.password = tb_password.Text;
device.status = DeviceStatus.IDLE;
device.areaId = (long?)cb_area.SelectedValue;
device.buildingId = (long?)cb_building.SelectedValue;
device.floorId = (long?)cb_floor.SelectedValue;
if (cb_dir.SelectedValue != null)
{
device.direction = (Direction)cb_dir.SelectedValue;
}
this.DialogResult = true;
this.device = device;
Close();
}
private void cb_area_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
http.get("/building/all", new Dictionary
{
["size"] = "1000",
["query"] = "%7B%22areaId%22:" + cb_area.SelectedValue + "%7d"
}).Then(res =>
{
buildings.Clear();
res.content.ToList().ForEach(buildings.Add);
}).Catch(err =>
{
Console.WriteLine(err);
});
}
private void cb_building_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
http.get("/floor/all", new Dictionary
{
["size"] = "1000",
["query"] = "%7B%22buildingId%22:" + cb_building.SelectedValue + "%7d"
}).Then(res =>
{
floors.Clear();
res.content.ToList().ForEach(floors.Add);
}).Catch(err =>
{
Console.WriteLine(err);
});
}
}
}