using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OpenTK.Graphics.ES11; using SunlightCentralizedControlManagement_SCCM_.Properties; using SunlightCentralizedControlManagement_SCCM_.UserClass; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Media; using System.Windows.Threading; using TouchSocket.Core; using TouchSocket.Sockets; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; using static System.Net.WebRequestMethods; namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { /// /// 变量传递至ui /// public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { if (propertyName != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } } public class MainWindowViewModel : ViewModelBase { //调用配置文件 private static UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "SCCM.ini"); public static string SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件 public static string SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2"); public static string SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3"); public static string SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4"); public static string SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5"); public static SqlConnection conn_SC =new SqlConnection();//数据库 public static async void SQL_LINK()//连接dbc数据库 { if (conn_SC.State == ConnectionState.Open) conn_SC.Close(); try { if (SQMOD == "Windows Authentication") //连接数据库测试 { conn_SC = new SqlConnection("server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI"); } else { conn_SC = new SqlConnection("server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASWORD); } await conn_SC.OpenAsync(); //连接数据库 //conn_SC.Close(); } catch (Exception) { } } public void CountDown() { DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick { Interval = TimeSpan.FromSeconds(1)//秒 }; timer1s.Tick += Tick_Event_1S; timer1s.Start(); DispatcherTimer timer5s = new DispatcherTimer//初始化循环,每1秒调用一次Tick { Interval = TimeSpan.FromSeconds(5)//秒 }; timer5s.Tick += Tick_Event_5S; timer5s.Start(); //设置定时器 DispatcherTimer disTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) //毫秒 }; disTimer.Tick += new EventHandler(DisTimer_500MS);//每一秒执行的方法 disTimer.Start();//计时开始 }//时间周期初始化 void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S { Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); for (int i = 0; i < Machinesdata_Count; i++) { if (Selet_Machines(Machines, "State", i).ToString() == "802") { MachiensTcpClient[i].SendAsync("SC830"+ Selet_Machines(Machines, "SYSKEY", i).ToString()); Updata_Machines(Machines, "State", i, "101"); // DataRow dt = machinesdata[i]; //Updata_Machines(Machines, Convert.ToInt16(dt["ID"]), "State", "800"); } } for (int i = 0; i < Machinesdata_Count; i++) { string mac_s = Selet_Machines(Machines, "State", i).ToString(); if ((mac_s == "101") || (mac_s == "201") || (mac_s == "202") || (mac_s == "309"))//获取信息 { MachiensTcpClient[i].SendAsync("SC830" + Selet_Machines(Machines, "SYSKEY", i).ToString()); } } } void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S { for (int i = 0; i < Machinesdata_Count; i++) { if (Selet_Machines(Machines, "State", i).ToString() == "801") { MachiensTcpClient[i].SendAsync("SC800:SCCM[" + MachiensTcpClient[i].IP + ";" + MachiensTcpClient[i].Port + "]"); } } } void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS { DATA_view(); } private SQLiteHelper SQLiteHelpers = null; //定义数据库 private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径 public static DataTable Machines = new DataTable(); //设备表缓存 public static View.info[] inf = new View.info[999]; //定义总览信息卡 public MainWindowViewModel() { SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存 SQLiteHelpers.Close(); CountDown(); TcpClientNEW(); SQL_LINK(); } public static TcpClient[] MachiensTcpClient = new TcpClient[999]; public int Machinesdata_Count; public void TcpClientNEW() { // bool Machine_ ; DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''", "id asc");//获取连接有效的组 Machinesdata_Count = machinesdata.Count(); for (int i = 0; i < Machinesdata_Count; i++) { MachiensTcpClient[i] = new TcpClient(); DataRow dt = machinesdata[i]; Updata_Machines(Machines, "State", Convert.ToInt16(dt["ID"]), "800"); _ = AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接 , Selet_Machines(Machines, "IP", Convert.ToInt16(dt["ID"])).ToString() , Selet_Machines(Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString()); } } public static string SYS_WorkNumder; //工单号 public static int SYS_AT1; //附缸1-3 public static int SYS_AT2; public static int SYS_AT3; public string sys_Time; //显示系统时间 public string Sys_Time //通知UI控件参数改变 { get { return sys_Time; } set { sys_Time = value; OnPropertyChanged("Sys_Time"); } } public static string MachineLOG; public string sys_machine; //显示log public string Sys_machine //通知UI控件参数改变 { get { return sys_machine; } set { sys_machine = value; OnPropertyChanged("Sys_machine"); } } public static object Selet_Machines(DataTable DB, string name, int key)//查询 { try { lock (DB) { DataRow drEmployee = DB.Select("ID='" + key + "'").First(); object index = drEmployee.Field(name); return index; } } catch (Exception) { // LogGing.LogGingDATA("SDTD:" + ex.ToString()); return "ERR"; } } public static void Updata_Machines(DataTable DB, string name, int key, string Value)//更新数据 { try { lock (DB) { DataRow drEmployee = DB.Select("ID='" + key + "'").First(); drEmployee.BeginEdit(); drEmployee[name] = Value; drEmployee.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); } } catch (Exception) { // LogGing.LogGingDATA("SDTD:" + ex.ToString()); } } public static DataTable dt_d = new DataTable("DIO"); public static DataTable dt_a = new DataTable("AIO"); public static DataTable dt_m = new DataTable("M"); public static DataTable dt_TM = new DataTable(); public static DataTable dt_TP = new DataTable(); private async void DATA_view()//IO显示 { await Task.Run(() => { try { TechnologicalMachine_View = ToObservableCollection(Machines); } catch (Exception) { } try { if (dt_TP.Rows.Count > 0) { TechnologicalProcess_View = ToObservableCollection(dt_TP); Sys_machine = MachineLOG; } } catch (Exception) { } }); } ObservableCollection sysData_A = new ObservableCollection(); ObservableCollection sysData_D = new ObservableCollection(); ObservableCollection sysData_M = new ObservableCollection(); ObservableCollection technologicalProcess_View = new ObservableCollection(); ObservableCollection technologicalMachine_View = new ObservableCollection(); public ObservableCollection SYSData_A { get { return sysData_A; } set { sysData_A = value; RaisePropertyChanged("SYSData_A"); } } public ObservableCollection SYSData_D { get { return sysData_D; } set { sysData_D = value; RaisePropertyChanged("SYSData_D"); } } public ObservableCollection SYSData_M { get { return sysData_M; } set { sysData_M = value; RaisePropertyChanged("SYSData_M"); } } public ObservableCollection TechnologicalProcess_View { get { return technologicalProcess_View; } set { technologicalProcess_View = value; RaisePropertyChanged("TechnologicalProcess_View"); } } public ObservableCollection TechnologicalMachine_View { get { return technologicalMachine_View; } set { technologicalMachine_View = value; RaisePropertyChanged("TechnologicalMachine_View"); } } public class DATA_A { public string IOName { get; set; } public int AIO { get; set; } public string ID { get; set; } public string PLC { get; set; } public string type { get; set; } } public class DATA_D { public string IOName { get; set; } public bool DIO { get; set; } public string ID { get; set; } public string PLC { get; set; } public string type { get; set; } } public class DATA_M { public string IOName { get; set; } public double Value { get; set; } public string ID { get; set; } public string PLC { get; set; } public string type { get; set; } } public class TechnologicalP { public string ProgramID { get; set; } public string Program { get; set; } public double Step { get; set; } public string StepID { get; set; } public string StepName { get; set; } public string ParameterName { get; set; } public string Parameter1 { get; set; } public string Parameter2 { get; set; } public string Parameter3 { get; set; } public string Parameter4 { get; set; } public string Parameter5 { get; set; } public string DYELOT { get; set; } } public class TechnologicalM { public string Name { get; set; } public string WorkOrder { get; set; } public string Dyelot { get; set; } public string Temperature { get; set; } public string WaterLevel { get; set; } public string Process { get; set; } public string Step { get; set; } public string Message { get; set; } } public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() { Type t = typeof(T); PropertyInfo[] propertys = t.GetProperties(); ObservableCollection lst = new ObservableCollection(); string typeName = string.Empty; foreach (DataRow dr in dt.Rows) { T entity = new T(); foreach (PropertyInfo pi in propertys) { typeName = pi.Name; if (dt.Columns.Contains(typeName)) { if (!pi.CanWrite) continue; object value = dr[typeName]; if (value == DBNull.Value) continue; if (pi.PropertyType == typeof(string)) { pi.SetValue(entity, value.ToString(), null); } else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)) { pi.SetValue(entity, int.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)) { pi.SetValue(entity, DateTime.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(float)) { pi.SetValue(entity, float.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(double)) { pi.SetValue(entity, double.Parse(value.ToString()), null); } else { pi.SetValue(entity, value, null); } } } lst.Add(entity); } return lst; } } }