using DataEntity.Device;
|
using DataEntity.Rack;
|
using DataEntity.Share;
|
using DataRWDAL.Device;
|
using DataRWDAL.Rack;
|
using HandyControl.Data;
|
using HxEnum;
|
using Microsoft.Win32;
|
using NPOI.HSSF.Record.Chart;
|
using System;
|
using System.Collections.Generic;
|
using System.Windows;
|
using System.Windows.Input;
|
using XCommon;
|
using XCommon.Check.Data;
|
using XCommon.file;
|
using XCommon.Log;
|
using XCommon.Tip;
|
using XHandler.View.Dialog;
|
|
namespace XHandler.View.Device
|
{
|
/// <summary>
|
/// 设备编辑 页面
|
/// </summary>
|
public partial class DeviceAdd : System.Windows.Window
|
{
|
#region 参数
|
/// <summary>
|
/// 操作类型(添加、修改)
|
/// </summary>
|
public string OperationType = "";
|
|
/// <summary>
|
/// 主键ID
|
/// </summary>
|
public string OperationId = "";
|
|
/// <summary>
|
/// 委托列表数据
|
/// </summary>
|
public delegate void DelegateForm();
|
|
/// <summary>
|
/// 委托方法名称
|
/// </summary>
|
public event DelegateForm LoadListEveForm;
|
|
/// <summary>
|
/// 加载控件老数据
|
/// </summary>
|
Dictionary<string, object> oldDic = new Dictionary<string, object>();
|
|
/// <summary>
|
/// 图片路径
|
/// </summary>
|
private string ImgSrc = string.Empty;
|
#endregion
|
|
#region 构造函数
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public DeviceAdd()
|
{
|
InitializeComponent();
|
}
|
#endregion
|
|
#region 初始化
|
/// <summary>
|
/// 初始化
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
{
|
try
|
{
|
#region 设备类型
|
// 获取设备类型列表
|
List<DeviceConfigTypeModel> deviceconfigTypeList = DeviceConfigTypeDB.GetAlllList(Shared.SoftwareInformation.software_information_id);
|
cBoxDeviceType.ItemsSource = deviceconfigTypeList;
|
cBoxDeviceType.SelectedIndex = 0;
|
#endregion
|
|
#region 通讯方式
|
// 根据通讯方式枚举值,获取下拉值
|
cBoxCommunicateType.ItemsSource = ComUtility.GetDropDownList<CommunicateTypeEnum>();
|
cBoxCommunicateType.SelectedIndex = 0;
|
#endregion
|
|
if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
|
{
|
var model = DeviceConfigDB.GetInfodById(OperationId);
|
if (model != null)
|
{
|
tBoxName.Text = model.Name;
|
cBoxDeviceType.SelectedValue = model.Type;
|
tBoxEquipmentId.Text = model.EquipmentId;
|
tBoxEquipmentModel.Text = model.EquipmentModel;
|
tBoxManufactor.Text = model.Manufactor;
|
// 禁用设备
|
cBoxDisableState.IsChecked = model.DisableState == EnumManagement.GetEnumValue(DisableStateEnum.Disable) ? true : false;
|
// 虚拟设备
|
cBoxVirtualConnectionState.IsChecked = model.VirtualConnectionState == EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Virtually) ? true : false;
|
cBoxCommunicateType.SelectedValue = model.CommunicateType;
|
tBoxRemark.Text = model.Remark;
|
|
if (model.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))
|
{
|
tBoxIp.Text = model.Ip;
|
tBoxPort.Text = model.Port;
|
}
|
|
// imgBase64String显示图片
|
if (!string.IsNullOrWhiteSpace(model.EquipmentImg))
|
{
|
UpLogFileViewTxt(model.EquipmentImg);
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
LoggerHelper.ErrorLog("ERROR:", ex);
|
}
|
}
|
#endregion
|
|
#region 选择图片
|
/// <summary>
|
/// 选择图片
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnAddDevice_Click(object sender, RoutedEventArgs e)
|
{
|
OpenFileDialog fileDialog = new OpenFileDialog();
|
fileDialog.Multiselect = false;
|
fileDialog.Title = "请选择文件";
|
fileDialog.Filter = "JPG类型图片(*jpg*)|*.jpg|PNG类型图片(*png*)|*.png*"; //设置要选择的文件的类型
|
if (fileDialog.ShowDialog() == true)
|
{
|
var tempFilePath = fileDialog.FileName;
|
|
// 获取图片Base64字符串
|
string pic = ImportFile.GetImageByString(tempFilePath);
|
UpLogFileViewTxt(pic);
|
}
|
}
|
|
/// <summary>
|
/// imgBase64String显示图片
|
/// </summary>
|
/// <param name="imgBase64String"></param>
|
private void UpLogFileViewTxt(string imgBase64String)
|
{
|
srcImage.Source = ImportFile.GetImage(imgBase64String);
|
ImgSrc = imgBase64String;
|
}
|
#endregion
|
|
#region 确定
|
/// <summary>
|
/// 确定
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnOk_Click(object sender, RoutedEventArgs e)
|
{
|
if (!CheckData())
|
{
|
return;
|
}
|
|
DeviceConfigModel deviceConfigModel = new DeviceConfigModel();
|
if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
|
{
|
deviceConfigModel = DeviceConfigDB.GetInfodById(OperationId);
|
}
|
deviceConfigModel.Name = tBoxName.Text;
|
deviceConfigModel.Type = cBoxDeviceType.SelectedValue.ToString();
|
deviceConfigModel.EquipmentId = tBoxEquipmentId.Text;
|
deviceConfigModel.EquipmentImg = ImgSrc;
|
// 禁用设备(1禁用,0不禁用)
|
deviceConfigModel.DisableState = cBoxDisableState.IsChecked == true ?
|
EnumManagement.GetEnumValue(DisableStateEnum.Disable) : EnumManagement.GetEnumValue(DisableStateEnum.Enable);
|
// 虚拟链接状态(1:虚拟,0:真实)
|
deviceConfigModel.VirtualConnectionState = cBoxVirtualConnectionState.IsChecked == true ?
|
EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Virtually) : EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Real);
|
|
deviceConfigModel.CommunicateType = Convert.ToInt32(cBoxCommunicateType.SelectedValue);
|
deviceConfigModel.Remark = tBoxRemark.Text;
|
|
deviceConfigModel.EquipmentModel = string.Empty;
|
if (!string.IsNullOrWhiteSpace(tBoxEquipmentModel.Text))
|
{
|
deviceConfigModel.EquipmentModel = tBoxEquipmentModel.Text;
|
}
|
|
deviceConfigModel.Manufactor = string.Empty;
|
if (!string.IsNullOrWhiteSpace(tBoxManufactor.Text))
|
{
|
deviceConfigModel.Manufactor = tBoxManufactor.Text;
|
}
|
|
deviceConfigModel.Ip = string.Empty;
|
deviceConfigModel.PlcHeartbeatAddress = null;
|
deviceConfigModel.PlcErrorAddress = null;
|
deviceConfigModel.Port = string.Empty;
|
deviceConfigModel.PortName = string.Empty;
|
deviceConfigModel.StopBits = null;
|
deviceConfigModel.BaudRate = null;
|
deviceConfigModel.DataBits = null;
|
deviceConfigModel.Parity = null;
|
if (deviceConfigModel.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))
|
{
|
deviceConfigModel.Ip = tBoxIp.Text;
|
deviceConfigModel.Port = tBoxPort.Text;
|
}
|
|
int updateCnt = 0;
|
if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
|
{
|
deviceConfigModel.Id = OperationId;
|
deviceConfigModel.ModifyTime = DateTime.Now;
|
deviceConfigModel.ModifyName = Shared.User.username;
|
updateCnt = DeviceConfigDB.Update(deviceConfigModel);
|
}
|
else
|
{
|
deviceConfigModel.ProjectId = Shared.SoftwareInformation.software_information_id;
|
deviceConfigModel.CreateTime = DateTime.Now;
|
deviceConfigModel.CreatName = Shared.User.username;
|
deviceConfigModel.Id = Guid.NewGuid().ToString();
|
updateCnt = DeviceConfigDB.Add(deviceConfigModel);
|
}
|
|
#region 点位
|
PositionModel positionModel = PositionDB.GetInfoByRelationId(deviceConfigModel.Id);
|
if (positionModel == null)
|
{
|
positionModel = new PositionModel();
|
positionModel.id = Guid.NewGuid().ToString();
|
positionModel.type = EnumManagement.GetEnumValue(PositonTypeEnum.Device);
|
positionModel.name = deviceConfigModel.Name;
|
positionModel.relation_Id = deviceConfigModel.Id;
|
positionModel.software_information_id = Shared.SoftwareInformation.software_information_id;
|
positionModel.creat_name = Shared.User.username;
|
positionModel.create_time = DateTime.Now;
|
PositionDB.Add(positionModel);
|
}
|
else
|
{
|
positionModel.name = deviceConfigModel.Name;
|
}
|
#endregion
|
|
if (updateCnt > 0)
|
{
|
LoadListEveForm();
|
//new MessageDialog("保存成功").ShowDialog();
|
ShowTip.ShowNotice("保存成功", InfoType.Success);
|
this.Close();
|
}
|
}
|
|
/// <summary>
|
/// 检查输入数据
|
/// </summary>
|
/// <returns></returns>
|
private bool CheckData()
|
{
|
try
|
{
|
if (string.IsNullOrWhiteSpace(cBoxDeviceType.SelectedValue.ToString()))
|
{
|
//new MessageDialog("请选择设备类型").ShowDialog();
|
ShowTip.ShowNotice("请选择设备类型", InfoType.Warning);
|
return false;
|
}
|
if (string.IsNullOrWhiteSpace(tBoxEquipmentId.Text))
|
{
|
//new MessageDialog("请填写设备ID").ShowDialog();
|
ShowTip.ShowNotice("请填写设备ID", InfoType.Warning);
|
tBoxEquipmentId.Focus();
|
return false;
|
}
|
if (string.IsNullOrWhiteSpace(tBoxName.Text))
|
{
|
//new MessageDialog("请填写名称").ShowDialog();
|
ShowTip.ShowNotice("请填写名称", InfoType.Warning);
|
tBoxName.Focus();
|
return false;
|
}
|
if (cBoxCommunicateType.SelectedValue == null)
|
{
|
//new MessageDialog("请选通讯方式").ShowDialog();
|
ShowTip.ShowNotice("请选通讯方式", InfoType.Warning);
|
tBoxName.Focus();
|
cBoxCommunicateType.Focus();
|
return false;
|
}
|
|
if (string.IsNullOrWhiteSpace(tBoxIp.Text))
|
{
|
//new MessageDialog("请填写IP").ShowDialog();
|
ShowTip.ShowNotice("请填写IP", InfoType.Warning);
|
tBoxIp.Focus();
|
return false;
|
}
|
|
if (!ValidateData.IsIp(tBoxIp.Text))
|
{
|
//new MessageDialog("请填写正确的IP").ShowDialog();
|
ShowTip.ShowNotice("请填写正确的IP", InfoType.Warning);
|
tBoxIp.Focus();
|
return false;
|
}
|
|
if (string.IsNullOrWhiteSpace(tBoxPort.Text))
|
{
|
//new MessageDialog("请填写端口").ShowDialog();
|
ShowTip.ShowNotice("请填写端口", InfoType.Warning);
|
tBoxPort.Focus();
|
return false;
|
}
|
|
if (!ValidateData.IsNumber(tBoxPort.Text))
|
{
|
//new MessageDialog("请填写正确的端口").ShowDialog();
|
ShowTip.ShowNotice("请填写正确的端口", InfoType.Warning);
|
tBoxPort.Focus();
|
return false;
|
}
|
|
if (string.IsNullOrWhiteSpace(ImgSrc))
|
{
|
//new MessageDialog("请选择图片").ShowDialog();
|
ShowTip.ShowNotice("请选择图片", InfoType.Warning);
|
return false;
|
}
|
|
|
//if (groupBox_serialPort.Visible)
|
//{
|
// if (string.IsNullOrWhiteSpace(uiTextBox_PortName.Text))
|
// {
|
// ShowWarningTip("请填写端口名称");
|
// uiTextBox_PortName.Focus();
|
// return false;
|
// }
|
|
// if (string.IsNullOrWhiteSpace(uiTextBox_BaudRate.Text))
|
// {
|
// ShowWarningTip("请填写波特率");
|
// uiTextBox_BaudRate.Focus();
|
// return false;
|
// }
|
|
// if (string.IsNullOrWhiteSpace(uiTextBox_DataBits.Text))
|
// {
|
// ShowWarningTip("请填写数据位长度");
|
// uiTextBox_DataBits.Focus();
|
// return false;
|
// }
|
//}
|
}
|
catch(Exception ex)
|
{
|
LoggerHelper.ErrorLog("CheckData error", ex);
|
}
|
|
return true;
|
}
|
#endregion
|
|
#region 拖动窗体
|
/// <summary>
|
/// 拖动窗体
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
|
{
|
if (e.ChangedButton == MouseButton.Left)
|
{
|
this.DragMove();
|
}
|
}
|
#endregion
|
|
#region 关闭页面
|
/// <summary>
|
/// 关闭页面
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnClose_Click(object sender, RoutedEventArgs e)
|
{
|
this.Close();
|
}
|
|
/// <summary>
|
/// 取消
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnCancel_Click(object sender, RoutedEventArgs e)
|
{
|
this.Close();
|
}
|
#endregion
|
|
#region ESC关闭画面
|
/// <summary>
|
/// ESC关闭画面
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
|
{
|
if (e.Key == Key.Escape)
|
{
|
this.Close();
|
}
|
}
|
#endregion
|
}
|
}
|