using Community;
using ControlzEx.Theming;
using DAL;
using HalconDotNet;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using Model;
using Model.HSL;
using Model.MotionControl;
using Model.Vision;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using ViewModel.MotionControl;
using ViewModel.Robots;
using ViewModel.Vision;
using Views;
using Views.Vision;
namespace ViewModel
{
public partial class MainWindowVM : NotificationObject
{
private string gridWidth;
///
/// Grid的宽
///
public string GridWidth
{
get { return gridWidth; }
set
{
gridWidth = value;
this.RaisePropertyChanged("GridWidth");
}
}
#region 通讯服务器对象
private CommunicationServerVM serverComm;
///
/// 通讯服务器对象
///
public CommunicationServerVM ServerComm
{
get { return serverComm; }
set
{
serverComm = value;
RaisePropertyChanged("ServerComm");
}
}
#endregion
#region PLC连接管理
private PLCVM plcConVM;
///
/// PLC连接管理
///
public PLCVM PlcConVM
{
get { return plcConVM; }
set
{
plcConVM = value;
this.RaisePropertyChanged("PlcConVM");
}
}
#endregion
#region ERP交互管理类
private ERPManageVM eRPVM;
///
/// ERP交互管理类
///
public ERPManageVM ERPVM
{
get { return eRPVM; }
set
{
eRPVM = value;
RaisePropertyChanged("ERPVM");
}
}
#endregion
#region 串口相关
private SerialComVM sCVM;
///
/// 串口服务对象
///
public SerialComVM SCVM
{
get { return sCVM; }
set
{
sCVM = value;
this.RaisePropertyChanged("SCVM");
}
}
#endregion
#region 机器人对象
private FeiYuRobotVM robot;
///
/// 机器人对象
///
public FeiYuRobotVM Robot
{
get { return robot; }
set
{
robot = value;
RaisePropertyChanged("Robot");
}
}
#endregion
#region 运动控制对象VM
private MotionControlVM mCVM;
///
/// 运动控制对象VM
///
public MotionControlVM MCVM
{
get { return mCVM; }
set
{
mCVM = value;
RaisePropertyChanged("MCVM");
}
}
#endregion
#region 软件开启时间
///
/// 软件开启时间
///
TimeSpan OpenTime { get; set; }
///
/// 生产运行开始时间
///
DateTime RunStartTime { get; set; }
#region 已运行时间显示
private ShowInfo runTimeShow;
///
/// 已运行时间显示
///
public ShowInfo RunTimeShow
{
get { return runTimeShow; }
set
{
runTimeShow = value;
this.RaisePropertyChanged("RunTimeShow");
}
}
#endregion
///
/// 提示报警时间
///
DateTime TiShiBaojingTime { get; set; }
#endregion
#region 图表相关
private ChartModel pie;
///
/// 图表对象
///
public ChartModel Pie
{
get { return pie; }
set
{
pie = value;
this.RaisePropertyChanged("ChartModel");
}
}
#region 图表相关2
private ChartVM cVM1;
///
/// 图表对象
///
public ChartVM CVM1
{
get { return cVM1; }
set
{
cVM1 = value;
this.RaisePropertyChanged("CVM1");
}
}
#endregion
#region 图表2 饼图
private ChartVM cVM2;
///
/// 图表2 饼图
///
public ChartVM CVM2
{
get { return cVM2; }
set
{
cVM2 = value;
this.RaisePropertyChanged("CVM2");
}
}
#endregion
private ChartModel curve1;
///
/// 电流噪音曲线图1
///
public ChartModel Curve1
{
get { return curve1; }
set
{
curve1 = value;
this.RaisePropertyChanged("Curve1");
}
}
private ChartModel curve2;
///
/// 电流噪音曲线图2
///
public ChartModel Curve2
{
get { return curve2; }
set
{
curve2 = value;
this.RaisePropertyChanged("Curve2");
}
}
#endregion
#region 空跑模式
private bool runningMode;
///
/// 空跑模式
///
public bool RunningMode
{
get { return runningMode; }
set
{
runningMode = value;
this.RaisePropertyChanged("RunningMode");
}
}
///
/// GRR模式
///
public bool GrrModel { get; set; }
#endregion
#region 系统设置权限控制
private string permissionButtonName;
///
/// 权限按钮名称
///
public string PermissionButtonName
{
get { return permissionButtonName; }
set
{
permissionButtonName = value;
this.RaisePropertyChanged("PermissionButtonName");
}
}
#region 管理员权限
private Visibility allVis;
///
/// 管理员权限
///
public Visibility AllVis
{
get { return allVis; }
set
{
allVis = value;
this.RaisePropertyChanged("AllVis");
if (HVlist != null)
{
foreach (HalconVisual hv in HVlist)
{
hv.DisplayLevel = value;
}
}
// PermissionButtonName = Application.Current.Resources[value == Visibility.Collapsed ? "编辑模式" : "普通模式"].ToString();
PermissionButtonName = value == Visibility.Collapsed ? "编辑模式" : "普通模式";
if (value == Visibility.Collapsed)
{
SuperPrivilege = value;
ContorEnable = false;
}
else
{
if (GridWidth == "0")
{
GridWidth = "30*";
}
ContorEnable = true;
}
if (aoi2 != null)
{
if (value == Visibility.Visible)
{
((TextBox)aoi2.FindName("bads")).IsEnabled = true;
((TextBox)aoi2.FindName("quantitys")).IsEnabled = true;
((TextBox)aoi2.FindName("produce")).IsEnabled = true;
}
else
{
((TextBox)aoi2.FindName("bads")).IsEnabled = false;
((TextBox)aoi2.FindName("quantitys")).IsEnabled = false;
((TextBox)aoi2.FindName("produce")).IsEnabled = false;
}
}
}
}
#endregion
#region 超级权限
private Visibility superPrivilege;
///
/// 超级权限
///
public Visibility SuperPrivilege
{
get { return superPrivilege; }
set
{
superPrivilege = value;
this.RaisePropertyChanged("SuperPrivilege");
if (value == Visibility.Visible)
{
AllVis = value;
}
//try
//{
// TabItem p = null;
// if (TabItemList != null)
// {
// p = TabItemList.Where(t => t.Header.ToString() == "参数设置").FirstOrDefault();
// }
// if (value == Visibility.Visible)
// {
// AllVis = value;
// if (p != null)
// {
// ParameterSetting ps = p.Content as ParameterSetting;
// (ps.FindName("holeBCX") as DataGridTextColumn).Visibility = Visibility.Visible;
// (ps.FindName("holeBCY") as DataGridTextColumn).Visibility = Visibility.Visible;
// (ps.FindName("nutBCX") as DataGridTextColumn).Visibility = Visibility.Visible;
// (ps.FindName("nutBCY") as DataGridTextColumn).Visibility = Visibility.Visible;
// }
// }
// else
// {
// if (p != null)
// {
// ParameterSetting ps = p.Content as ParameterSetting;
// (ps.FindName("holeBCX") as DataGridTextColumn).Visibility = Visibility.Collapsed;
// (ps.FindName("holeBCY") as DataGridTextColumn).Visibility = Visibility.Collapsed;
// (ps.FindName("nutBCX") as DataGridTextColumn).Visibility = Visibility.Collapsed;
// (ps.FindName("nutBCY") as DataGridTextColumn).Visibility = Visibility.Collapsed;
// }
// }
//}
//catch
//{
//}
if (HVlist != null)
{
foreach (HalconVisual hv in HVlist)
{
hv.SuperPrivilege = value;
}
}
if (MCVM != null)
{
MCVM.DisplayLevel = value;
}
}
}
#endregion
private bool contorEnable;
///
/// 控件启用
///
public bool ContorEnable
{
get { return contorEnable; }
set
{
contorEnable = value;
this.RaisePropertyChanged("ContorEnable");
}
}
#endregion
#region 系统参数对象
private SystemParameter systemPar;
///
/// 系统参数对象
///
public SystemParameter SystemPar
{
get { return systemPar; }
set
{
systemPar = value;
this.RaisePropertyChanged("SystemPar");
}
}
#endregion
#region 数据对象
///
/// 数据对象
///
public MainWindowDAL dal = new MainWindowDAL();
///
/// 视觉数据对象
///
public VisualDAL Vdal = new VisualDAL();
#endregion
#region 控制卡
///
/// Io控制卡ID
///
public ushort IoCardID { get; set; }
#endregion
#region 输入信号集合
private List inputList;
///
/// 输入信号集合
///
public List InputList
{
get { return inputList; }
set
{
inputList = value;
if (value != null && value.Count > 0)
{
InputGrid = GetInputGrid();
}
}
}
#endregion
#region 输出信号集合
private List outputList;
///
/// 输出信号集合
///
public List OutputList
{
get { return outputList; }
set
{
outputList = value;
if (value != null && value.Count > 0)
{
OutputGrid = GetOutputGrid();
}
}
}
#endregion
#region 各线程
///
/// 状态监控线程
///
Thread ThStateMonitoring { get; set; }
///
/// 运行线程
///
Thread ThRun { get; set; }
///
/// 网络连接控制线程
///
Thread ThConnect { get; set; }
///
/// IE4.0数据上传线程
///
Thread ThDataUpload { get; set; }
#endregion
#region 机器运行状态
private RunStatuEnum runStatu;
///
/// 机器运行状态枚举
///
public RunStatuEnum RunStatu
{
get { return runStatu; }
set
{
runStatu = value;
switch (value)
{
case RunStatuEnum.Prepare:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "准备中";
break;
case RunStatuEnum.Prepareations:
ShowStatu.StatusBackground = ColorStr.Green;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "准备就绪";
break;
case RunStatuEnum.Normal:
ShowStatu.StatusBackground = ColorStr.Green;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "运行中";
break;
case RunStatuEnum.Stop:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "停止中";
break;
case RunStatuEnum.EmgStop:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "紧急停止中";
break;
case RunStatuEnum.Exceptional:
ShowStatu.StatusBackground = ColorStr.Red;
ShowStatu.StatusForeground = ColorStr.White;
break;
case RunStatuEnum.Unknown:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "状态未知";
break;
}
}
}
private ShowInfo showStatu;
///
/// 机器状态 显示
///
public ShowInfo ShowStatu
{
get { return showStatu; }
set
{
showStatu = value;
this.RaisePropertyChanged("ShowStatu");
}
}
#endregion
#region 异常信息集合
private ObservableCollection excepList;
///
/// 异常信息集合
///
public ObservableCollection ExcepList
{
get { return excepList; }
set
{
excepList = value;
}
}
#endregion
#region 各种运行方法
///
/// 开始运行
///
public DelegateCommand StartCommand { get; set; }
///
/// 停止运行
///
public DelegateCommand StopCommand { get; set; }
///
/// 重启软件
///
public DelegateCommand ResetSoftCommand { get; set; }
///
/// 退出程序
///
public DelegateCommand CloseCommand { get; set; }
///
/// 保存站别信息
///
public DelegateCommand SaveStandCommand { get; set; }
///
/// 保存系统信息
///
public DelegateCommand SaveSystemPerCommand { get; set; }
///
/// 读取产能数据
///
public DelegateCommand ReadDateCommand { get; set; }
///
/// 导出产能数据
///
public DelegateCommand ExportDateCommand { get; set; }
///
/// 读取产能上一页
///
public DelegateCommand ReadPageUpCommand { get; set; }
///
/// 读取产能下一页
///
public DelegateCommand ReadPageDownCommand { get; set; }
#endregion
#region 选中的TabItem
private TabItem selectTabItem;
///
/// 选中的TabItem
///
public TabItem SelectTabItem
{
get { return selectTabItem; }
set
{
selectTabItem = value;
if (AllVis == Visibility.Visible)
{
if (value.Tag != null && value.Tag.ToString() == "v")
{
GridWidth = "0";
}
else
{
GridWidth = "30*";
}
}
else
{
GridWidth = "30*";
}
this.RaisePropertyChanged("SelectTabItem");
}
}
#endregion
#region 选中的TabItem索引
private int selectedIndex;
///
/// 选中的TabItem索引
///
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndex = value;
this.RaisePropertyChanged("SelectedIndex");
}
}
private int selectedIndex1;
///
/// 选中的TabItem索引1
///
public int SelectedIndex1
{
get { return selectedIndex1; }
set
{
selectedIndex1 = value;
this.RaisePropertyChanged("SelectedIndex1");
}
}
#endregion
#region 当前生产的机种ID
private int currentMachineID;
///
/// 当前生产的机种
///
public int CurrentMachineID
{
get { return currentMachineID; }
set
{
currentMachineID = value;
this.RaisePropertyChanged("CurrentMachineID");
if (value <= 0)
{
return;
}
CurrentMachine = AllMachineList.Where(w => w.ID == value).FirstOrDefault(); //dal.ByIDGeMachine(value); //获取当前生产机种对象
if (Robot != null)
{
Robot.MachineObj = CurrentMachine;
}
if (MCVM != null)
{
MCVM.MachineObj = CurrentMachine;
}
if (HVlist != null)
{
foreach (HalconVisual hv in HVlist)
{
hv.MachineObj = CurrentMachine;
hv.QRModelInitialize = false;
hv.CodeModelInitialize = false;
}
}
//异步新增项 这是微软的BUG 只能这么麻烦了
// MyDispatcher.BeginInvoke(DispatcherPriority.Background, new ParameterizedThreadStart(shuxingContent), info);
SystemPar.DefaultMachineID = value;
dal.UpdateSystemPar(SystemPar); //更新默认机种
}
}
#endregion
#region 当前生产机种对象
private MachineModel currentMachine;
///
/// 当前生产机种对象
///
public MachineModel CurrentMachine
{
get { return currentMachine; }
set
{
currentMachine = value;
this.RaisePropertyChanged("CurrentMachine");
}
}
#endregion
#region 站别集合
private ObservableCollection standList;
///
/// 站别集合
///
public ObservableCollection StandList
{
get { return standList; }
set
{
standList = value;
this.RaisePropertyChanged("StandList");
}
}
#endregion
#region 选中的站别
private StandModel selectStand;
///
/// 选中的站别
///
public StandModel SelectStand
{
get { return selectStand; }
set
{
selectStand = value;
this.RaisePropertyChanged("SelectStand");
}
}
#endregion
#region 各步骤
#region 回原点步骤枚举
///
/// 回原点步骤枚举
///
GengralProcessEnum GoHomeSteps { get; set; }
#endregion
#endregion
#region 输入输出Grid控件
private Grid inputGrid;
///
/// 输入信号Grid
///
public Grid InputGrid
{
get { return inputGrid; }
set
{
inputGrid = value;
this.RaisePropertyChanged("InputGrid");
}
}
private Grid outputGrid;
///
/// 输出信号Grid
///
public Grid OutputGrid
{
get { return outputGrid; }
set
{
outputGrid = value;
this.RaisePropertyChanged("OutputGrid");
}
}
#endregion
#region 光源串口号
private string serialProt;
///
/// 光源串口号
///
public string SerialProt
{
get { return serialProt; }
set
{
if (!string.IsNullOrEmpty(value) && LightCOM != null)
{
if (!LightCOM.OpenSerialCom(value))
{
// MessageBox.Show(LightCOM.ErrorMag);
}
else
{
serialProt = value;
if (value != SystemPar.SerialProt)
{
SystemPar.SerialProt = value;
try
{
dal.UpdateSystemPar(SystemPar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
this.RaisePropertyChanged("SerialProt");
}
}
}
#endregion
#region 光源串口对象
private SerialCom lightCOM;
///
/// 光源串口对象
///
public SerialCom LightCOM
{
get { return lightCOM; }
set
{
lightCOM = value;
this.RaisePropertyChanged("LightCOM");
}
}
#endregion
#region 系统功能控件集合
private ObservableCollection tabItemList;
///
/// 系统功能控件集合
///
public ObservableCollection TabItemList
{
get { return tabItemList; }
set
{
tabItemList = value;
this.RaisePropertyChanged("TabItemList");
}
}
#endregion
#region 视觉对象VM集合
///
/// 视觉对象VM集合
///
List HVlist { get; set; }
#endregion
#region 生产数据总数结合
private List dataList;
///
/// 测试数据集合
///
public List DataList
{
get { return dataList; }
set
{
dataList = value;
this.RaisePropertyChanged("DataList");
}
}
#endregion
#region 计划生产数量
private DataCollectionModel planData;
///
/// 计划生产数量
///
public DataCollectionModel PlanData
{
get { return planData; }
set
{
planData = value;
this.RaisePropertyChanged("PlanData");
}
}
#endregion
#region 实际生产数量
private DataCollectionModel actualData;
///
/// 实际生产数量
///
public DataCollectionModel ActualData
{
get { return actualData; }
set
{
actualData = value;
this.RaisePropertyChanged("ActualData");
}
}
#endregion
#region 不启用MES
private bool mesCheck;
///
/// 不启用MES
///
public bool MesCheck
{
get { return mesCheck; }
set
{
mesCheck = value;
this.RaisePropertyChanged("MesCheck");
}
}
#endregion
#region PLC连接对象
private FeiYuHsl myPLC;
///
/// PLC连接对象
///
public FeiYuHsl MyPLC
{
get { return myPLC; }
set
{
myPLC = value;
RaisePropertyChanged("MyPLC");
}
}
#endregion
///
/// 万用表
///
CommunicationManagementClass WangYongbiao { get; set; }
///
/// 老化柜连接
///
CommunicationManagementClass Laohuagui { get; set; }
private List showDataList;
///
/// 显示数据
///
public List ShowDataList
{
get { return showDataList; }
set
{
showDataList = value;
RaisePropertyChanged("ShowDataList");
}
}
#region 构造函数
///
/// 构造函数
///
public MainWindowVM(Window mywd)
{
Mywindow = mywd;
MyDispatcher = mywd.Dispatcher;
GridWidth = "30*";
Initialize(); //初始化
LightCOM = new SerialCom();
SerialProt = SystemPar.SerialProt;
LoadingFunctionInterface();//加载功能界面
AllVis = Visibility.Collapsed;
#region 实例化各运行方法
this.StartCommand = new DelegateCommand();
this.StartCommand.ExecuteAction = new Action