using Community;
using ControlzEx.Theming;
using DAL;
using HalconDotNet;
using LiveCharts;
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 System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using ViewModel.MotionControl;
using ViewModel.Robots;
using ViewModel.Vision;
using Views;
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 ERP交互管理类
private ERPManageVM eRPVM;
///
/// ERP交互管理类
///
public ERPManageVM ERPVM
{
get { return eRPVM; }
set
{
eRPVM = value;
RaisePropertyChanged("ERPVM");
}
}
#endregion
#region 视觉对象A
private HalconVisual visualA;
///
/// 视觉对象A
///
public HalconVisual VisualA
{
get { return visualA; }
set
{
visualA = value;
RaisePropertyChanged("VisualA");
}
}
#endregion
#region 视觉对象B
private HalconVisual visualB;
///
/// 视觉对象B
///
public HalconVisual VisualB
{
get { return visualB; }
set
{
visualB = value;
RaisePropertyChanged("VisualB");
}
}
#endregion
#region 视觉对象C
private HalconVisual visualC;
///
/// 视觉对象C
///
public HalconVisual VisualC
{
get { return visualC; }
set
{
visualC = value;
RaisePropertyChanged("VisualC");
}
}
#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 软件开启时间
///
/// 软件开启时间
///
DateTime OpenTime { get; set; }
///
/// 生产运行开始时间
///
DateTime RunStartTime { get; set; }
private string openTimeStr;
///
/// 软件开启时间
///
public string OpenTimeStr
{
get { return openTimeStr; }
set
{
openTimeStr = value;
this.RaisePropertyChanged("OpenTimeStr");
}
}
#region CT时间
private TimeSpan cTTime;
///
/// CT时间
///
public TimeSpan CTTime
{
get { return cTTime; }
set
{
cTTime = value;
ShowCtTime = value.TotalSeconds.ToString();
this.RaisePropertyChanged("CTTime");
}
}
private string showCtTime;
///
/// 显示CT时间
///
public string ShowCtTime
{
get { return showCtTime; }
set
{
showCtTime = value;
this.RaisePropertyChanged("ShowCtTime");
}
}
#endregion
#endregion
#region 图表相关
private ChartVM cVM;
///
/// 图表对象
///
public ChartVM CVM
{
get { return cVM; }
set
{
cVM = value;
this.RaisePropertyChanged("CVM");
}
}
#endregion
#region Ttay料盘
private TrayModel tray1;
///
/// 料盘1
///
public TrayModel Tray1
{
get { return tray1; }
set
{
tray1 = value;
this.RaisePropertyChanged("Tray1");
}
}
private TrayModel tray2;
///
/// 料盘2
///
public TrayModel Tray2
{
get { return tray2; }
set
{
tray2 = value;
this.RaisePropertyChanged("Tray2");
}
}
private TrayModel tray3;
///
/// 料盘3
///
public TrayModel Tray3
{
get { return tray3; }
set
{
tray3 = value;
this.RaisePropertyChanged("Tray3");
}
}
private TrayModel tray4;
///
/// 料盘4
///
public TrayModel Tray4
{
get { return tray4; }
set
{
tray4 = value;
this.RaisePropertyChanged("Tray4");
}
}
private TrayModel tray5;
///
/// 料盘5
///
public TrayModel Tray5
{
get { return tray5; }
set
{
tray5 = value;
this.RaisePropertyChanged("Tray5");
}
}
private TrayModel tray6;
///
/// 料盘6
///
public TrayModel Tray6
{
get { return tray6; }
set
{
tray6 = value;
this.RaisePropertyChanged("Tray6");
}
}
private TrayModel tray7;
///
/// 料盘7
///
public TrayModel Tray7
{
get { return tray7; }
set
{
tray7 = value;
this.RaisePropertyChanged("Tray7");
}
}
private TrayModel tray8;
///
/// 料盘8
///
public TrayModel Tray8
{
get { return tray8; }
set
{
tray8 = value;
this.RaisePropertyChanged("Tray8");
}
}
#endregion
#region 空跑模式
private bool runningMode;
///
/// 空跑模式
///
public bool RunningMode
{
get { return runningMode; }
set
{
runningMode = value;
if (!value)
{
RunningModePlas = false;
}
this.RaisePropertyChanged("RunningMode");
}
}
private bool runningModePlas;
///
/// 联机空跑模式
///
public bool RunningModePlas
{
get { return runningModePlas; }
set
{
runningModePlas = value;
if (value)
{
RunningMode = true;
}
this.RaisePropertyChanged("RunningModePlas");
}
}
#endregion
#region 清理机台模式
private bool cleanMachine;
///
/// 清理机台模式
///
public bool CleanMachine
{
get { return cleanMachine; }
set
{
cleanMachine = value;
this.RaisePropertyChanged("CleanMachine");
}
}
#endregion
#region 不进行MES验证
private bool mesCheck;
///
/// 不进行MES验证
///
public bool MesCheck
{
get { return mesCheck; }
set
{
mesCheck = value;
this.RaisePropertyChanged("MesCheck");
}
}
#endregion
#region 不进行数据上传
private bool noUpData;
///
/// 不进行数据上传
///
public bool NoUpData
{
get { return noUpData; }
set
{
noUpData = value;
this.RaisePropertyChanged("NoUpData");
}
}
#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;
}
}
if (StandVMList != null)
{
foreach (var item in StandVMList)
{
item.IsEnable = value == Visibility.Visible ? true : false;
}
}
//PermissionButtonName = Application.Current.Resources[value == Visibility.Collapsed ? "编辑模式" : "普通模式"].ToString();
PermissionButtonName = value == Visibility.Collapsed ? "编辑模式" : "普通模式";
if (value == Visibility.Collapsed)
{
SuperPrivilege = value;
}
else
{
if (GridWidth == "30*")
{
GridWidth = "0";
}
}
}
}
#endregion
#region 超级权限
private Visibility superPrivilege;
///
/// 超级权限
///
public Visibility SuperPrivilege
{
get { return superPrivilege; }
set
{
superPrivilege = value;
this.RaisePropertyChanged("SuperPrivilege");
if (value == Visibility.Visible)
{
AllVis = value;
}
if (HVlist != null)
{
foreach (HalconVisual hv in HVlist)
{
hv.SuperPrivilege = value;
}
}
if (MCVM != null)
{
MCVM.DisplayLevel = value;
}
}
}
#endregion
#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; }
#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 = "准备中";
if (OutputList != null)
{
OutputList[0].Switchs = false; //红灯
OutputList[1].Switchs = true; //黄灯
OutputList[2].Switchs = false; //绿灯
OutputList[3].Switchs = false; //蜂鸣器
}
break;
case RunStatuEnum.Prepareations:
ShowStatu.StatusBackground = ColorStr.Green;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "准备就绪";
OutputList[0].Switchs = false; //红灯
OutputList[1].Switchs = true; //黄灯
OutputList[2].Switchs = false; //绿灯
OutputList[3].Switchs = false; //蜂鸣器
break;
case RunStatuEnum.Normal:
ShowStatu.StatusBackground = ColorStr.Green;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "运行中";
OutputList[0].Switchs = false; //红灯
OutputList[1].Switchs = false; //黄灯
OutputList[2].Switchs = true; //绿灯
OutputList[3].Switchs = false; //蜂鸣器
break;
case RunStatuEnum.Stop:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "停止中";
OutputList[0].Switchs = false; //红灯
OutputList[1].Switchs = true; //黄灯
OutputList[2].Switchs = false; //绿灯
OutputList[3].Switchs = false; //蜂鸣器
break;
case RunStatuEnum.EmgStop:
ShowStatu.StatusBackground = ColorStr.Yellow;
ShowStatu.StatusForeground = ColorStr.Black;
ShowStatu.StatuInfo = "紧急停止中";
OutputList[0].Switchs = true; //红灯
OutputList[1].Switchs = true; //黄灯
OutputList[2].Switchs = false; //绿灯
OutputList[3].Switchs = false; //蜂鸣器
break;
case RunStatuEnum.Exceptional:
ShowStatu.StatusBackground = ColorStr.Red;
ShowStatu.StatusForeground = ColorStr.White;
OutputList[0].Switchs = true; //红灯
OutputList[1].Switchs = false; //黄灯
OutputList[2].Switchs = false; //绿灯
OutputList[3].Switchs = true; //蜂鸣器
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 SwitchLanguageCommand { get; set; }
///
/// 保存站别参数
///
public DelegateCommand SaveStandCommand { 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 当前生产的机种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(ShuaxingContent), info);
SystemPar.DefaultMachineID = value;
dal.UpdateSystemPar(SystemPar); //更新默认机种
}
}
private void ShuaxingContent(object obj)
{
Tray1 = new TrayModel(CurrentMachine, "料盘", 0);
//Tray2 = new TrayModel(CurrentMachine, "料盘", 2);
}
#endregion
#region 当前生产机种对象
private MachineModel currentMachine;
///
/// 当前生产机种对象
///
public MachineModel CurrentMachine
{
get { return currentMachine; }
set
{
currentMachine = value;
this.RaisePropertyChanged("CurrentMachine");
}
}
#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 PLC连接管理
private PLCVM plcConVM;
///
/// PLC连接管理
///
public PLCVM PlcConVM
{
get { return plcConVM; }
set
{
plcConVM = value;
this.RaisePropertyChanged("PlcConVM");
}
}
///
/// 当前PLC连接对象
///
FeiYuHsl CurrHsl { get; set; }
#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 StandModel selectStand;
///
/// 选中的站别
///
public StandModel SelectStand
{
get { return selectStand; }
set
{
selectStand = value;
this.RaisePropertyChanged("SelectStand");
}
}
#endregion
#region 站别逻辑控制集合
private List standVMList;
///
/// 站别逻辑控制集合
///
public List StandVMList
{
get { return standVMList; }
set { standVMList = value; RaisePropertyChanged("StandVMList"); }
}
#endregion
#region 构造函数
///
/// 构造函数
///
public MainWindowVM(Window mywd)
{
//adtIO3224.adtIO3224_initial();//初始化IO卡
Mywindow = mywd;
MyDispatcher = mywd.Dispatcher;
GridWidth = "30*";
Initialize(); //初始化
LoadingFunctionInterface();//加载功能界面
#region 实例化各运行方法
this.StartCommand = new DelegateCommand();
this.StartCommand.ExecuteAction = new Action