using DataEntity.Device;
using DataEntity.Page;
using DataEntity.Share;
using DataRWDAL.Device;
using HandyControl.Data;
using HxEnum;
using HxSocketImplement.Sockets;
using HxSocketImplement.Sockets.HxService;
using System;
using System.Collections.Generic;
using System.Windows;
using XCommon;
using XCommon.Log;
using XCommon.Tip;
using XHandler.View.MethodProperty;
using XHandler.View.Page;
using XImagingXhandler.XDAL;
using static HxEnum.StateEnum;
using Button = System.Windows.Controls.Button;
using Pagination = DataEntity.Page.Pagination;
using UserControl = System.Windows.Controls.UserControl;
namespace XHandler.View.Device
{
///
/// 设备管理页面
///
public partial class DeveiceManagement : UserControl
{
#region 变量
///
/// 分页数据
///
private Pagination m_pagination = new Pagination();
private Pagination m_paginationDeviceMethod = new Pagination();
///
/// 左侧DataGrid选中的DeviceId
///
private string m_deviceconfigId = string.Empty;
#endregion
#region 构造函数
///
/// 构造函数
///
public DeveiceManagement()
{
InitializeComponent();
}
#endregion
#region 初始化
///
/// 初始化
///
///
///
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
try
{
// 设备
GetData();
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 获取设备信息
///
private void GetData()
{
DataBinding(m_pagination);
}
///
/// 获取设备方法信息
///
private void GetDataDeviceMethod()
{
DataBindingDeviceMethod(m_paginationDeviceMethod);
}
#endregion
#region 设备相关
///
/// 搜索
///
///
///
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
m_deviceconfigId = string.Empty;
dgDeviceMethodData.ItemsSource = null;
tBoxDeviceName.Text = string.Empty;
pagerToolsControl_paging.Current = PaginationDefaultParameter.Current;
}
///
/// 添加设备
///
///
///
private void btnAddDevice_Click(object sender, RoutedEventArgs e)
{
DeviceAdd deviceAdd = new DeviceAdd();
deviceAdd.Owner = (Window)Shared.Main;
deviceAdd.OperationType = OperationTypeEnum.OperationType_Enum.AddEnum.ToString();
deviceAdd.LoadListEveForm -= GetData;
deviceAdd.LoadListEveForm += GetData;
deviceAdd.ShowDialog();
}
///
/// 分页绑定
///
///
private void DataBinding(Pagination pagination)
{
var result = DeviceConfigDB.GetPageData(pagination, GetSearch(),Shared.SoftwareInformation.software_information_id);
if (result != null)
{
for (int i = 0; i < result.Item1.Count; i++)
{
result.Item1[i].SId = i + 1;
result.Item1[i].CommunicateTypeName =
EnumManagement.GetEnumDescription(EnumManagement.GetField(result.Item1[i].CommunicateType));
}
pagerToolsControl_paging.IsTrigger = false;
pagerToolsControl_paging.DataCount = result.Item2; // 当前查到数量
pagerToolsControl_paging.Current = pagination.Current;
pagerToolsControl_paging.IsTrigger = true;
dgDeviceData.ItemsSource = result.Item1;
}
}
///
/// 获取搜索参数
///
///
private DeviceConfigModel GetSearch()
{
DeviceConfigModel searchModel = new DeviceConfigModel();
searchModel.Name = searchDeviceName.Text;
return searchModel;
}
#region 操作
///
/// 修改
///
///
///
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
try
{
Button btn = (Button)sender;
if (btn != null)
{
string deviceId = (string)btn.Tag;
var result = DeviceConfigDB.GetInfodById(deviceId);
if (result != null)
{
DeviceAdd deviceAdd = new DeviceAdd();
deviceAdd.Owner = (Window)Shared.Main;
deviceAdd.OperationType = OperationTypeEnum.OperationType_Enum.EditEnum.ToString();
deviceAdd.OperationId = deviceId;
deviceAdd.LoadListEveForm -= GetData;
deviceAdd.LoadListEveForm += GetData;
deviceAdd.ShowDialog();
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 删除
///
///
///
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
try
{
Button btn = (Button)sender;
if (btn != null)
{
// 获取所有设备信息
string deviceId = (string)btn.Tag;
var deviceConfigList = DeviceConfigMethodDB.GetListByDeviceconfigId(deviceId);
if (deviceConfigList != null && deviceConfigList.Count > 0)
{
//new MessageDialog("此设备下有方法,不能删除").ShowDialog();
ShowTip.ShowNotice("此设备下有方法,不能删除", InfoType.Warning);
return;
}
var result = DeviceConfigDB.GetInfodById(deviceId);
if (result != null)
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要删除【{result.Name}】吗?");
plsToolTipWin.ShowDialog();
if (plsToolTipWin.DialogResult == true)
{
DeviceConfigDB.DelById(new DeviceConfigModel { Id = deviceId });
GetData();
//new MessageDialog("删除成功").ShowDialog();
ShowTip.ShowNotice("删除成功", InfoType.Success);
}
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 方法
///
///
///
private void btnDeviceMethod_Click(object sender, RoutedEventArgs e)
{
try
{
Button btn = (Button)sender;
if (btn != null)
{
// 获取所有设备信息
m_deviceconfigId = (string)btn.Tag;
DeviceConfigModel deviceConfig = DeviceConfigDB.GetInfodById(m_deviceconfigId);
tBoxDeviceName.Text = deviceConfig.Name;
// 设备方法
GetDataDeviceMethod();
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
#endregion
#region 分页事件
///
/// 当前页改变时发生
///
///
///
private void pagerToolsControl_paging_PageCntChanged(object sender, C_EventArgsClass e)
{
m_pagination.Current = e.CurrentPageNo;
DataBinding(m_pagination);
}
///
/// 每页行数改变时发生
///
///
///
private void pagerToolsControl_paging_OnePageRowCntChanged(object sender, C_EventArgsClass e)
{
m_pagination.Current = e.CurrentPageNo;
m_pagination.PageSize = e.PagerNum;
DataBinding(m_pagination);
}
#endregion
#endregion
#region 设备方法相关
///
/// 搜索设备方法名
///
///
///
private void btnSearchMethodName_Click(object sender, RoutedEventArgs e)
{
pagerToolsDeviceMethod.Current = PaginationDefaultParameter.Current;
}
///
/// 添加方法
///
///
///
private void btnAddDeviceMethod_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(m_deviceconfigId))
{
DeviceMehtodAdd deviceMehtodAdd = new DeviceMehtodAdd();
deviceMehtodAdd.Owner = (Window)Shared.Main;
deviceMehtodAdd.OperationType = OperationTypeEnum.OperationType_Enum.AddEnum.ToString();
deviceMehtodAdd.LoadListEveForm -= GetDataDeviceMethod;
deviceMehtodAdd.LoadListEveForm += GetDataDeviceMethod;
deviceMehtodAdd.DeviceconfigId = m_deviceconfigId;
deviceMehtodAdd.ShowDialog();
}
else
{
ShowTip.ShowNotice("请选择数据再添加方法", InfoType.Warning);
}
}
///
/// 分页绑定
///
///
private void DataBindingDeviceMethod(Pagination pagination)
{
var result = DeviceConfigMethodDB.GetPageData(pagination, GetSearchDeviceMethod());
if (result != null)
{
for (int i = 0; i < result.Item1.Count; i++)
{
result.Item1[i].SId = i + 1;
}
pagerToolsDeviceMethod.IsTrigger = false;
pagerToolsDeviceMethod.DataCount = result.Item2; // 当前查到数量
pagerToolsDeviceMethod.Current = pagination.Current;
pagerToolsDeviceMethod.IsTrigger = true;
dgDeviceMethodData.ItemsSource = result.Item1;
}
}
///
/// 获取设备方法搜索参数
///
///
private DeviceConfigMethodModel GetSearchDeviceMethod()
{
DeviceConfigMethodModel searchModel = new DeviceConfigMethodModel();
searchModel.DeviceconfigId = m_deviceconfigId;
searchModel.ParameterName = searchDeviceMethodName.Text;
return searchModel;
}
#region 操作
///
/// 修改方法信息
///
///
///
private void btnMethodEdit_Click(object sender, RoutedEventArgs e)
{
try
{
Button btn = (Button)sender;
if (btn != null)
{
string deviceMethodId = (string)btn.Tag;
var result = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
if (result != null)
{
DeviceMehtodAdd deviceMehtodAdd = new DeviceMehtodAdd();
deviceMehtodAdd.Owner = (Window)Shared.Main;
deviceMehtodAdd.OperationType = OperationTypeEnum.OperationType_Enum.EditEnum.ToString();
deviceMehtodAdd.LoadListEveForm -= GetDataDeviceMethod;
deviceMehtodAdd.LoadListEveForm += GetDataDeviceMethod;
deviceMehtodAdd.DeviceMethodId = deviceMethodId;
deviceMehtodAdd.DeviceconfigId = m_deviceconfigId;
deviceMehtodAdd.ShowDialog();
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 删除
///
///
///
private void btnMethodDelete_Click(object sender, RoutedEventArgs e)
{
try
{
Button btn = (Button)sender;
if (btn != null)
{
string deviceMethodId = (string)btn.Tag;
// 获取设备方法参数
var getInfoByDeviceconfigMethodList = DeviceConfigMethodParametersDB.GetInfoByDeviceconfigMethodId(deviceMethodId);
if (getInfoByDeviceconfigMethodList != null && getInfoByDeviceconfigMethodList.Count > 0)
{
ShowTip.ShowNotice("此方法下有参数,不能删除", InfoType.Warning);
return;
}
var result = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
if (result != null)
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要删除【{result.Name}】吗?");
plsToolTipWin.ShowDialog();
if (plsToolTipWin.DialogResult == true)
{
DeviceConfigMethodDB.DelById(new DeviceConfigMethodModel { Id = deviceMethodId });
GetDataDeviceMethod();
//new MessageDialog("删除成功").ShowDialog();
ShowTip.ShowNotice("删除成功", InfoType.Success);
}
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 调试
///
///
///
private void btnMethodDebug_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(m_deviceconfigId))
{
Button btn = (Button)sender;
string deviceMethodId = (string)btn.Tag;
var deviceMethod = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要调试【{deviceMethod.ParameterName + "->" + deviceMethod.Name}】吗?");
plsToolTipWin.ShowDialog();
if (plsToolTipWin.DialogResult == true)
{
ShowTip.ShowNotice("执行中,请等待...", InfoType.Info);
var getDeviceConfigModel = DeviceConfigDB.GetInfodById(m_deviceconfigId); //获取设备
if (getDeviceConfigModel != null)
{
// 获取发送参数
Dictionary dictionaryList = new Dictionary();
var getInfoByDeviceconfigMethodList = DeviceConfigMethodParametersDB.GetInfoByDeviceconfigMethodId(deviceMethodId, ParameterTypeEnum.Send);
foreach (var item in getInfoByDeviceconfigMethodList)
{
dictionaryList.Add(item.ParameterName, ComUtility.ToTypeConvert(item.Value, item.DataType.ToString()));
}
InstructCommonMethodSendModel sendModel = HxSocketParameterService.GetInstructCommonMethodSendModel(new InstructCommonMethodSendModel()
{
EquipmentType = getDeviceConfigModel.Type.Trim(),
Ip = getDeviceConfigModel.Ip.Trim(),
Port = Convert.ToInt32(getDeviceConfigModel.Port.Trim()),
EquipmentId = getDeviceConfigModel.EquipmentId.Trim(),
VirtualConnectionState = getDeviceConfigModel.VirtualConnectionState,
CommunicateType = getDeviceConfigModel.CommunicateType,
SendData = new HxSendBase { experiment_id = HxSockServiceExecute.InstrunctionId(), parameters = dictionaryList, method = deviceMethod.ParameterName },
});
var resultModel = ResultHandle.InstructCommonMethod(sendModel, false);
if (resultModel != null && resultModel.status == StateEnum_Equipment.Completed)
{
ShowTip.ShowNotice("方法:" + deviceMethod.ParameterName + "-》" + resultModel.data.json, InfoType.Success, 5);
}
else
{
ShowTip.ShowNotice("调试失败,方法:" + deviceMethod.ParameterName + "-》" + resultModel?.ErrorMsg?.Error_Text, InfoType.Warning);
}
}
else
{
ShowTip.ShowNotice("设备数据未找到", InfoType.Error);
}
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 参数
///
///
///
private void btnMethodParamter_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(m_deviceconfigId))
{
Button btn = (Button)sender;
string deviceMethodId = (string)btn.Tag;
// 根据设备Id,获取设备信息
DeviceConfigModel deviceConfig = DeviceConfigDB.GetInfodById(m_deviceconfigId);
if (deviceConfig.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket)) // Socket-TCP标准协议
{
DeviceMethodParameters frm = new DeviceMethodParameters();
frm.Owner = (Window)Shared.Main;
frm.DeviceconfigId = m_deviceconfigId;
frm.DeviceconfigMethodId = deviceMethodId;
frm.ShowDialog();
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
#endregion
#region 分页事件
///
/// 当前页改变时发生
///
///
///
private void pagerToolsDeviceMethod_PageCntChanged(object sender, C_EventArgsClass e)
{
m_paginationDeviceMethod.Current = e.CurrentPageNo;
DataBindingDeviceMethod(m_paginationDeviceMethod);
}
///
/// 每页行数改变时发生
///
///
///
private void pagerToolsDeviceMethod_OnePageRowCntChanged(object sender, C_EventArgsClass e)
{
m_paginationDeviceMethod.Current = e.CurrentPageNo;
m_paginationDeviceMethod.PageSize = e.PagerNum;
DataBindingDeviceMethod(m_paginationDeviceMethod);
}
#endregion
#endregion
}
}