|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Data;
|
|
|
using System.Windows.Documents;
|
|
|
using System.Windows.Input;
|
|
|
using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using System.Windows.Navigation;
|
|
|
using System.Windows.Shapes;
|
|
|
using System.Xml.Linq;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Sln.Wcs.Configs;
|
|
|
using Sln.Wcs.Model.Domain;
|
|
|
using SqlSugar;
|
|
|
|
|
|
namespace Sln_Wpf.Page
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// DeviceInfo.xaml 的交互逻辑
|
|
|
/// </summary>
|
|
|
public partial class DeviceInfo : UserControl
|
|
|
{
|
|
|
private List<BaseEquipInfo> _equips;
|
|
|
private List<BaseEquipParameter> _parameters;
|
|
|
private BaseEquipInfo _selectedDevice;
|
|
|
private ISqlSugarClient? sqlSugarClient;
|
|
|
public DeviceInfo()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
|
|
|
sqlSugarClient.AsTenant().ChangeDatabase("wcs");
|
|
|
_equips = sqlSugarClient.Queryable<BaseEquipInfo>().ToList();
|
|
|
_parameters = sqlSugarClient.Queryable<BaseEquipParameter>().ToList();
|
|
|
BindDeviceData();
|
|
|
BindParameterData();
|
|
|
}
|
|
|
|
|
|
private void BindDeviceData()
|
|
|
{
|
|
|
// 为状态添加颜色
|
|
|
var displayData = _equips.Select(d => new
|
|
|
{
|
|
|
EquipId = d.EquipNo,
|
|
|
EquipName = d.EquipName,
|
|
|
EquipType = d.EquipType,
|
|
|
IpAddress = d.ServerIp,
|
|
|
PortAddress = d.ServerPort,
|
|
|
CurrentFloor = d.WorkshopLevel,
|
|
|
RunStatus = d.EquipStatus,
|
|
|
OnlineStatus = d.UseFlag,
|
|
|
LastCommTime = d.UpdateTime,
|
|
|
StatusColor = d.EquipStatus == 0 ? Brushes.Red : (d.EquipStatus == 1 ? Brushes.Green : Brushes.Orange)
|
|
|
}).ToList();
|
|
|
|
|
|
DeviceDataGrid.ItemsSource = displayData;
|
|
|
}
|
|
|
|
|
|
private void BindParameterData()
|
|
|
{
|
|
|
ParameterDataGrid.ItemsSource = _parameters;
|
|
|
}
|
|
|
|
|
|
private void LoadDeviceFilter()
|
|
|
{
|
|
|
var devices = _equips.Select(d => d.EquipName).Distinct().ToList();
|
|
|
ParamDeviceFilter.Items.Clear();
|
|
|
ParamDeviceFilter.Items.Add("全部设备");
|
|
|
foreach (var device in devices)
|
|
|
{
|
|
|
ParamDeviceFilter.Items.Add(device);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void BtnAdd_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
AddEquipWindow addEquipWindow = new AddEquipWindow();
|
|
|
bool? result = addEquipWindow.ShowDialog();
|
|
|
if (result == true)
|
|
|
{
|
|
|
_equips = sqlSugarClient.Queryable<BaseEquipInfo>().ToList();
|
|
|
BindDeviceData();
|
|
|
}
|
|
|
// 实际项目中:打开新增设备对话框,输入设备信息后保存
|
|
|
}
|
|
|
|
|
|
// 设备信息 - 编辑
|
|
|
private void BtnEdit_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (_selectedDevice == null)
|
|
|
{
|
|
|
MessageBox.Show("请先选择要编辑的设备", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
return;
|
|
|
}
|
|
|
AddEquipWindow addEquipWindow = new AddEquipWindow(_selectedDevice);
|
|
|
bool? result = addEquipWindow.ShowDialog();
|
|
|
if (result == true)
|
|
|
{
|
|
|
_equips = sqlSugarClient.Queryable<BaseEquipInfo>().ToList();
|
|
|
BindDeviceData();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设备信息 - 删除
|
|
|
private void BtnDelete_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (_selectedDevice == null)
|
|
|
{
|
|
|
MessageBox.Show("请先选择要删除的设备", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var result = MessageBox.Show($"确定要删除设备 {_selectedDevice.EquipName} 吗?\n删除后相关参数也将被清除。", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
{
|
|
|
_equips.Remove(_selectedDevice);
|
|
|
_parameters.RemoveAll(p => p.EquipName == _selectedDevice.EquipName);
|
|
|
BindDeviceData();
|
|
|
var flag = sqlSugarClient.Deleteable<BaseEquipInfo>().Where(d => d.EquipNo == _selectedDevice.EquipNo).ExecuteCommand();
|
|
|
//BindParameterData();
|
|
|
//LoadDeviceFilter();
|
|
|
_selectedDevice = null;
|
|
|
if (flag > 0)
|
|
|
MessageBox.Show("删除成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设备信息 - 查询
|
|
|
private void BtnQuery_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
_equips = sqlSugarClient.Queryable<BaseEquipInfo>().ToList();
|
|
|
string keyword = SearchKeyword.Text;
|
|
|
int filterType = 0;
|
|
|
switch ((FilterType.SelectedItem as ComboBoxItem).Content)
|
|
|
{
|
|
|
case "全部设备":
|
|
|
filterType = 0;
|
|
|
break;
|
|
|
case "AGV":
|
|
|
filterType = 1;
|
|
|
break;
|
|
|
case "提升机":
|
|
|
filterType = 2;
|
|
|
break;
|
|
|
default:
|
|
|
filterType = 0;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
var queryData = _equips.AsEnumerable();
|
|
|
|
|
|
// 按设备类型筛选
|
|
|
if (filterType != 0)
|
|
|
{
|
|
|
queryData = queryData.Where(d => d.EquipType == filterType);
|
|
|
}
|
|
|
|
|
|
// 按关键字搜索
|
|
|
if (!string.IsNullOrEmpty(keyword) && keyword != "请输入设备名称或ID")
|
|
|
{
|
|
|
queryData = queryData.Where(d => d.EquipName.Contains(keyword) || d.EquipNo.Contains(keyword));
|
|
|
}
|
|
|
|
|
|
var displayData = queryData.Select(d => new
|
|
|
{
|
|
|
EquipId = d.EquipNo,
|
|
|
EquipName = d.EquipName,
|
|
|
EquipType = d.EquipType,
|
|
|
IpAddress = d.ServerIp,
|
|
|
PortAddress = d.ServerPort,
|
|
|
CurrentFloor = d.WorkshopLevel,
|
|
|
RunStatus = d.EquipStatus,
|
|
|
OnlineStatus = d.UseFlag,
|
|
|
LastCommTime = d.UpdateTime,
|
|
|
StatusColor = d.EquipStatus == 0 ? Brushes.Red : (d.EquipStatus == 1 ? Brushes.Green : Brushes.Orange)
|
|
|
}).ToList();
|
|
|
|
|
|
DeviceDataGrid.ItemsSource = displayData;
|
|
|
MessageBox.Show($"查询完成,共找到 {displayData.Count} 条记录", "查询结果", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
|
|
|
// 设备表格选择变化
|
|
|
private void DeviceDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
{
|
|
|
var selected = DeviceDataGrid.SelectedItem;
|
|
|
if (selected != null)
|
|
|
{
|
|
|
// 通过反射获取DeviceId
|
|
|
var deviceId = selected.GetType().GetProperty("EquipId")?.GetValue(selected)?.ToString();
|
|
|
_selectedDevice = _equips.FirstOrDefault(d => d.EquipNo == deviceId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设备参数 - 新增
|
|
|
private void BtnParamAdd_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
AddParameterWindow addWindow = new AddParameterWindow();
|
|
|
addWindow.ShowDialog();
|
|
|
}
|
|
|
|
|
|
// 设备参数 - 编辑
|
|
|
private void BtnParamEdit_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
var selectedParam = ParameterDataGrid.SelectedItem as BaseEquipParameter;
|
|
|
if (selectedParam == null)
|
|
|
{
|
|
|
MessageBox.Show("请先选择要编辑的参数", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
return;
|
|
|
}
|
|
|
MessageBox.Show($"编辑参数:{selectedParam.ParamName}\n当前值:{selectedParam.ParamValue}", "编辑参数", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
|
|
|
// 设备参数 - 删除
|
|
|
private void BtnParamDelete_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
var selectedParam = ParameterDataGrid.SelectedItem as BaseEquipParameter;
|
|
|
if (selectedParam == null)
|
|
|
{
|
|
|
MessageBox.Show("请先选择要删除的参数", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var result = MessageBox.Show($"确定要删除参数 {selectedParam.ParamName} 吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
{
|
|
|
_parameters.Remove(selectedParam);
|
|
|
BindParameterData();
|
|
|
var flag = sqlSugarClient.Deleteable<BaseEquipParameter>().Where(d => d.EquipCode == selectedParam.EquipCode).ExecuteCommand();
|
|
|
//BindParameterData();
|
|
|
//LoadDeviceFilter();
|
|
|
selectedParam = null;
|
|
|
if (flag > 0)
|
|
|
MessageBox.Show("删除成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设备参数 - 查询
|
|
|
private void BtnParamQuery_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
_parameters = sqlSugarClient.Queryable<BaseEquipParameter>().ToList();
|
|
|
string keyword = ParamDeviceFilter.SelectedItem?.ToString();
|
|
|
var querydata = _parameters.Where(d => d.EquipType == keyword);
|
|
|
}
|
|
|
|
|
|
// 参数设备筛选
|
|
|
private void ParamDeviceFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
{
|
|
|
string selectedDevice = (ParamDeviceFilter.SelectedItem as ComboBoxItem)?.Content.ToString();
|
|
|
|
|
|
if (string.IsNullOrEmpty(selectedDevice) || selectedDevice == "全部设备")
|
|
|
{
|
|
|
ParameterDataGrid.ItemsSource = _parameters;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var filtered = _parameters.Where(p => p.EquipType == selectedDevice).ToList();
|
|
|
ParameterDataGrid.ItemsSource = filtered;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |