using Community;
using MahApps.Metro.Controls;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using DAL;
using System.IO;
namespace ViewModel
{
///
/// 通讯服务器管理逻辑类
///
public class CommunicationServerVM : NotificationObject
{
///
/// 系统软件ID
///
int SoftID { get; set; }
///
/// 自动获取端口号
///
public DelegateCommand GetPortCommand { get; set; }
///
/// 添加
///
public DelegateCommand AddTCPIPCommand { get; set; }
///
/// 删除
///
public DelegateCommand DeleteTCPIPCommand { get; set; }
///
/// 保存TCPIP参数
///
public DelegateCommand SaveParCommand { get; set; }
///
/// 发送信息方法
///
public DelegateCommand SendInfoCommand { get; set; }
///
/// 连接监听套接字
///
public Socket SocketListen { get; set; }
private ShowInfo showmsg;
///
/// 监听连接状态
///
public ShowInfo Showmsg
{
get { return showmsg; }
set
{
showmsg = value;
this.RaisePropertyChanged("Showmsg");
}
}
#region 监听开关
private bool monitorsWitch;
///
/// 监听开关
///
public bool MonitorsWitch
{
get { return monitorsWitch; }
set
{
monitorsWitch = value;
if (value)
{
// StartListen();
StartListen();
}
else
{
EndListen();
}
this.RaisePropertyChanged("MonitorsWitch");
}
}
#endregion
#region TCPIP通讯连接参数
private TCPIPClass serverConn;
///
/// TCPIP通讯连接参数
///
public TCPIPClass ServerConn
{
get { return serverConn; }
set
{
serverConn = value;
this.RaisePropertyChanged("ServerConn");
}
}
#endregion
#region 已连接通讯对象集合
private ObservableCollection commList;
///
/// 已连接对象集合
///
public ObservableCollection CommList
{
get { return commList; }
set
{
commList = value;
//this.RaisePropertyChanged("ConnectionClassList");
}
}
#endregion
#region 当前选中的通讯对象
private CommunicationManagementClass selectComm;
///
/// 当前选中的通讯对象 Text="{Binding SelectComm.ActionName}" IsEditable="True"
///
public CommunicationManagementClass SelectComm
{
get { return selectComm; }
set
{
selectComm = value;
this.RaisePropertyChanged("SelectComm");
}
}
#endregion
///
/// 生产喷码信息
///
public DelegateCommand GeneratePenmaDateCommand { get; set; }
///
/// OCV仪器初始化归零
///
public DelegateCommand OCVInitializeZeroCommand { get; set; }
///
/// OCV仪器状态查询
///
public DelegateCommand OCVStatusQueryCommand { get; set; }
#region 构造函数
///
/// 构造函数
///
///
///
public CommunicationServerVM(string ip, int port)
{
//IP= GetIP(); //获取IP地址 因为无法获取指定网卡的IP 此处不用
ServerConn.IP = ip;
ServerConn.Port = port; //端口号随便来
Showmsg = new ShowInfo();
CommList = new ObservableCollection();
}
MainWindowDAL dal = new MainWindowDAL();
///
/// 程序主线程队列 用于异步属性界面
///
Dispatcher MyDispatcher { get; set; }
///
/// 构造函数重载
///
/// 系统软件ID
/// 窗体对象,用于动态添加项
public CommunicationServerVM(int softID, Dispatcher dp)
{
MyDispatcher = dp;
PLCConn = new CommunicationManagementClass();
FrontStation = new CommunicationManagementClass();
SoftID = softID;
ServerConn = dal.GetTCPIPServer(SoftID);//new TCPIPClass();
CommList = dal.GetCommClientList1(SoftID);
if (CommList.Count > 0)
{
CommList.Insert(0, new CommunicationManagementClass() { ActionName = " ", ID = -1 });
SelectComm = CommList[1];
}
Showmsg = new ShowInfo();
GetPortCommand = new DelegateCommand();
GetPortCommand.ExecuteAction = new Action