using SkiaSharp; using SunlightCentralizedControlManagement_SCCM_.UserClass; using SunlightCentralizedControlManagement_SCCM_.ViewModel; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; namespace SunlightCentralizedControlManagement_SCCM_.View { /// /// MachinesSet.xaml 的交互逻辑 /// public partial class MachinesSet : UserControl { public MachinesSet() { InitializeComponent(); // 获取所有可用串口端口,并添加到comboBoxCOM // string[] ports = System.IO.Ports.SerialPort.GetPortNames(); comboBoxCOM0.ItemsSource = ports; comboBoxCOM0.Text = Configini.IniReadvalue("SYS", "COM"); comboBoxMachine.ItemsSource = MainWindowViewModel.Machines.AsEnumerable().Select(rowdata => rowdata.Field("name")).ToList();//转换机台 comboBoxtype.ItemsSource = type_; } string[] ports = { "PORT1", "PORT2", "PORT3", "PORT4" }; string[] type_ = { "828", "838" }; string link_model = "838" ; private readonly UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "SCCM.ini"); //private string SYS_machines = null; int ID_; private void UserControl_Loaded(object sender, RoutedEventArgs e) { Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; } private void Griddata_MouseDoubleClick(object sender, MouseButtonEventArgs e) { int rownum = Griddata.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { ID_ = int.Parse((Griddata.Columns[7].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text); comboBoxMachine.Text = (Griddata.Columns[1].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第1列, TextMachineGroup.Text = (Griddata.Columns[2].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, IP.Text = (Griddata.Columns[3].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, PORT.Text = (Griddata.Columns[4].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, comboBoxCOM0.Text = (Griddata.Columns[5].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, Station.Text = (Griddata.Columns[6].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, comboBoxtype.Text = (Griddata.Columns[8].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text; } } private void Save_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(Station.Text)) Station.Text = "255"; if (!string.IsNullOrEmpty(comboBoxMachine.Text)) { Dictionary Machines_DAT = new Dictionary();//缓存函数 if (MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from Machines where name ='" + comboBoxMachine.Text + "'", null).Tables[0].Rows.Count == 0) { lock (MainWindowViewModel.Machines.Rows.SyncRoot) { DataRow dat_ = MainWindowViewModel.Machines.NewRow(); dat_.BeginEdit(); dat_["Name"] = comboBoxMachine.Text; dat_["Groups"] = TextMachineGroup.Text; dat_["IP"] = IP.Text; dat_["PORT"] = PORT.Text; dat_["Serial"] = comboBoxCOM0.Text; dat_["Model"] = comboBoxtype.Text; dat_["Station"] = Station.Text; dat_["ID"] = 999; dat_["ERR"] = false; dat_["AUTO"] = false; dat_["CALL"] = false; dat_["LOCK"] = false; dat_["State"] = 800; dat_["Temperature"] = 0; dat_["WaterLevel"] = 0; dat_["WORK_RUN"] = -1; dat_["UserInfoStart"] = 900; if (comboBoxtype.Text.Contains(link_model)) { dat_["Desktop"] = true; dat_["Type"] = true; } else { dat_["Desktop"] = false; dat_["Type"] = false; } dat_.EndEdit(); MainWindowViewModel.Machines.Rows.Add(dat_); DatSteps(MainWindowViewModel.Machines); MainWindowViewModel.SQLiteHelpers.Delete("Machines", null, null);// for (int x = 0; x < MainWindowViewModel.Machines.Rows.Count; x++) { Machines_DAT.Add("ID", MainWindowViewModel.Machines.Rows[x]["ID"]); Machines_DAT.Add("Name", MainWindowViewModel.Machines.Rows[x]["Name"]); Machines_DAT.Add("IP", MainWindowViewModel.Machines.Rows[x]["IP"]); Machines_DAT.Add("PORT", MainWindowViewModel.Machines.Rows[x]["PORT"]); Machines_DAT.Add("Serial", MainWindowViewModel.Machines.Rows[x]["Serial"]); Machines_DAT.Add("Station", MainWindowViewModel.Machines.Rows[x]["Station"]); Machines_DAT.Add("Groups", MainWindowViewModel.Machines.Rows[x]["Groups"]); Machines_DAT.Add("Desktop", MainWindowViewModel.Machines.Rows[x]["Desktop"]); Machines_DAT.Add("Dispense", MainWindowViewModel.Machines.Rows[x]["Dispense"]); Machines_DAT.Add("Type", MainWindowViewModel.Machines.Rows[x]["Type"]); Machines_DAT.Add("Model", MainWindowViewModel.Machines.Rows[x]["Model"]); MainWindowViewModel.SQLiteHelpers.InsertData("Machines", Machines_DAT);//行插入 Machines_DAT.Clear(); } } } else { lock (MainWindowViewModel.Machines.Rows.SyncRoot) { DataRow dr = MainWindowViewModel.Machines.Rows[ID_]; dr.BeginEdit(); dr["Name"] = comboBoxMachine.Text; dr["Groups"] = TextMachineGroup.Text; dr["IP"] = IP.Text; dr["PORT"] = PORT.Text; dr["Serial"] = comboBoxCOM0.Text; dr["Model"] = comboBoxtype.Text; dr["Station"] = Station.Text; if (comboBoxtype.Text.Contains(link_model)) { dr["Desktop"] = true; dr["Type"] = true; } else { dr["Desktop"] = false; dr["Type"] = false; } dr.EndEdit(); MainWindowViewModel.SQLiteHelpers.Delete("Machines", null, null);// for (int x = 0; x < MainWindowViewModel.Machines.Rows.Count; x++) { Machines_DAT.Add("ID", MainWindowViewModel.Machines.Rows[x]["ID"]); Machines_DAT.Add("Name", MainWindowViewModel.Machines.Rows[x]["Name"]); Machines_DAT.Add("IP", MainWindowViewModel.Machines.Rows[x]["IP"]); Machines_DAT.Add("PORT", MainWindowViewModel.Machines.Rows[x]["PORT"]); Machines_DAT.Add("Serial", MainWindowViewModel.Machines.Rows[x]["Serial"]); Machines_DAT.Add("Station", MainWindowViewModel.Machines.Rows[x]["Station"]); Machines_DAT.Add("Groups", MainWindowViewModel.Machines.Rows[x]["Groups"]); Machines_DAT.Add("Desktop", MainWindowViewModel.Machines.Rows[x]["Desktop"]); Machines_DAT.Add("Dispense", MainWindowViewModel.Machines.Rows[x]["Dispense"]); Machines_DAT.Add("Type", MainWindowViewModel.Machines.Rows[x]["Type"]); Machines_DAT.Add("Model", MainWindowViewModel.Machines.Rows[x]["Model"]); Machines_DAT.Add("LOCK", "FALSE"); MainWindowViewModel.SQLiteHelpers.InsertData("Machines", Machines_DAT);//行插入 Machines_DAT.Clear(); } } } Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; comboBoxMachine.Text = null; TextMachineGroup.Text = null; IP.Text = null; PORT.Text = "7789"; comboBoxCOM0.Text = null; Station.Text = "00"; } else { textlog.Text = "Invalid"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } private void Delete_Click(object sender, RoutedEventArgs e)//删除 { if (!string.IsNullOrEmpty(comboBoxMachine.Text)) { MessageBoxResult vr = System.Windows.MessageBox.Show(Properties.Resources.Delete + comboBoxMachine.Text, "SCCM", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (vr == MessageBoxResult.OK) // 如果是确定,就执行下面代码 { lock (MainWindowViewModel.Machines.Rows.SyncRoot) { MainWindowViewModel.Machines.Rows.RemoveAt(ID_ - 1); DatSteps(MainWindowViewModel.Machines); } Dictionary Machines_DAT = new Dictionary();//缓存函数 MainWindowViewModel.SQLiteHelpers.Delete("Machines", null, null);// for (int x = 0; x < MainWindowViewModel.Machines.Rows.Count; x++) { Machines_DAT.Add("ID", MainWindowViewModel.Machines.Rows[x]["ID"]); Machines_DAT.Add("Name", MainWindowViewModel.Machines.Rows[x]["Name"]); Machines_DAT.Add("IP", MainWindowViewModel.Machines.Rows[x]["IP"]); Machines_DAT.Add("PORT", MainWindowViewModel.Machines.Rows[x]["PORT"]); Machines_DAT.Add("Serial", MainWindowViewModel.Machines.Rows[x]["Serial"]); Machines_DAT.Add("Station", MainWindowViewModel.Machines.Rows[x]["Station"]); Machines_DAT.Add("Groups", MainWindowViewModel.Machines.Rows[x]["Groups"]); Machines_DAT.Add("Desktop", MainWindowViewModel.Machines.Rows[x]["Desktop"]); Machines_DAT.Add("Dispense", MainWindowViewModel.Machines.Rows[x]["Dispense"]); Machines_DAT.Add("LOCK", "FALSE"); MainWindowViewModel.SQLiteHelpers.InsertData("Machines", Machines_DAT);//行插入 Machines_DAT.Clear(); } Griddata.ItemsSource = MainWindowViewModel.Machines.DefaultView; comboBoxMachine.Text = null; } } else { textlog.Text = "Invalid"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } public void DatSteps(DataTable db)//行号刷新 { int a = db.Rows.Count; for (int i = 0; i < a; i++) { DataRow dr = db.Rows[i]; dr.BeginEdit(); dr["ID"] = i; dr.EndEdit(); } } private void Test_Click(object sender, RoutedEventArgs e)//测试按钮 { if (comboBoxtype.Text.Contains(link_model)) {//网络 if (!string.IsNullOrEmpty(IP.Text) && !string.IsNullOrEmpty(PORT.Text)) { textlog.Text = "TEST"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 255)); //------------使用ping类------ string host = IP.Text; try { Ping p1 = new Ping(); PingReply reply = p1.Send(host); //发送主机名或Ip地址 //StringBuilder sbuilder; if (reply.Status == IPStatus.Success) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //连接服务器,绑定IP 与 端口 IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(IP.Text), int.Parse(PORT.Text)); try { socket.Connect(iPEndPoint); socket.Close();//离线 textlog.Text = "Link succeed"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0)); } catch (Exception) { textlog.Text = "Link failed"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 0)); } } else if (reply.Status == IPStatus.TimedOut) { textlog.Text = "Link timeout"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } else { textlog.Text = "No links"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } catch (Exception) { textlog.Text = "No links"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } else if (!string.IsNullOrEmpty(comboBoxCOM0.Text)) { } else { textlog.Text = "Invalid"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } else {//串口 if (!string.IsNullOrEmpty(comboBoxCOM0.Text) && !string.IsNullOrEmpty(Station.Text)) { textlog.Text = "TEST"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 255)); //------------使用ping类------ string host = IP.Text; Ping p1 = new Ping(); PingReply reply = p1.Send(host); //发送主机名或Ip地址 //StringBuilder sbuilder; if (reply.Status == IPStatus.Success) { try { textlog.Text = "Link succeed"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0)); } catch (Exception) { textlog.Text = "Link failed"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 0)); } } else { textlog.Text = "No links"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } else { textlog.Text = "Invalid"; textlog.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } } } } }