diff --git a/View/InOutView.xaml b/View/InOutView.xaml index bfe41e5..b4cae51 100644 --- a/View/InOutView.xaml +++ b/View/InOutView.xaml @@ -19,7 +19,8 @@ - - + - - + + + + + + + + + + - - + + - - - + + + diff --git a/View/InOutView.xaml.cs b/View/InOutView.xaml.cs index 6602378..1da20fe 100644 --- a/View/InOutView.xaml.cs +++ b/View/InOutView.xaml.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -14,6 +15,7 @@ using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using static DyeingComputer.UserClass.SqliteHelper; namespace DyeingComputer.View { @@ -26,6 +28,8 @@ namespace DyeingComputer.View { InitializeComponent(); } + private SQLiteHelper SQLiteHelpers = null; //定义数据库 + private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 private void Grid_D_SelectionChanged(object sender, SelectionChangedEventArgs e) { @@ -41,5 +45,58 @@ namespace DyeingComputer.View { } + + + private void Grid_D_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) + { + string ID; + string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息 + + int rownum = Grid_D.SelectedIndex;//获取鼠标选中行并定义变量 + if (rownum != -1)//判断鼠标定位是否有效 + { + ID = (Grid_D.Columns[0].GetCellContent(Grid_D.Items[rownum]) as TextBlock).Text;//定位第0列, + + Dictionary datagrid_v = new Dictionary();//缓存函数 + datagrid_v.Add("PLC", newValue); + SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + SQLiteHelpers.Open(); //打开数据库 + SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 + SQLiteHelpers.Close();//关闭数据库 + } + MainWindowViewModel.SQL_data();//重新获得io表 + MainWindowViewModel.D_view = true; + } + + private void Grid_D_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) + { + MainWindowViewModel.D_view = false; + } + + private void Grid_A_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) + { + string ID; + string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息 + + int rownum = Grid_A.SelectedIndex;//获取鼠标选中行并定义变量 + if (rownum != -1)//判断鼠标定位是否有效 + { + ID = (Grid_A.Columns[0].GetCellContent(Grid_A.Items[rownum]) as TextBlock).Text;//定位第0列, + + Dictionary datagrid_v = new Dictionary();//缓存函数 + datagrid_v.Add("PLC", newValue); + SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + SQLiteHelpers.Open(); //打开数据库 + SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 + SQLiteHelpers.Close();//关闭数据库 + } + MainWindowViewModel.SQL_data();//重新获得io表 + MainWindowViewModel.A_view = true; + } + + private void Grid_A_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) + { + MainWindowViewModel.A_view = false; + } } } diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index 3080577..e257eab 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -54,8 +54,8 @@ namespace DyeingComputer.ViewModel public class MainWindowViewModel : ViewModelBase { - private SQLiteHelper SQLiteHelpers = null; //定义数据库 - private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 + private static SQLiteHelper SQLiteHelpers = null; //定义数据库 + private readonly static string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 private readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db"; //数据库路径 private IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "DyeingComputer.ini"); private PID pid = new PID(); @@ -1164,7 +1164,7 @@ namespace DyeingComputer.ViewModel public static DataTable dt_d = new DataTable("DIO"); public static DataTable dt_a = new DataTable("AIO"); public static DataTable dt_m = new DataTable("M"); - void SQL_data()//获得io表 + public static void SQL_data()//获得io表 { SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 @@ -1196,12 +1196,15 @@ namespace DyeingComputer.ViewModel await Task.Run(() => { - for (ushort i = 0; i < DO_L; i++) { Updata_dtd((i + 2001).ToString(), DO[i]); } - for (ushort i = 0; i < DQ_L; i++) { DQ[i] = Selet_dtd((i + 3001).ToString()); } - for (ushort i = 0; i < RW_L; i++) { Updata_dta((i + 4001).ToString(), RW[i]); } - for (ushort i = 0; i < DW_L; i++) { DW[i] = Convert.ToUInt16(Selet_dta((i + 5001).ToString())); } + for (ushort i = 0; i < DO_L; i++) { Upplc_dtd((i + 2001).ToString(), DO[i]); } + for (ushort i = 0; i < DQ_L; i++) { DQ[i] = Seplc_dtd((i + 3001).ToString()); } + for (ushort i = 0; i < RW_L; i++) { Upplc_dta((i + 4001).ToString(), RW[i]); } + for (ushort i = 0; i < DW_L; i++) { DW[i] = Convert.ToUInt16(Seplc_dta((i + 5001).ToString())); } }); } + + public static object D_view=true; + public static object A_view=true; private async void IO_view()//IO显示 { await Task.Run(() => @@ -1214,13 +1217,13 @@ namespace DyeingComputer.ViewModel try { - SYSData_A = ToObservableCollection(dt_a); + if ((bool)A_view) SYSData_A = ToObservableCollection(dt_a); } catch (Exception ex) { Name_err = "IO_SYSData_A" + "(" + ex.Message + ")"; } try { - SYSData_D = ToObservableCollection(dt_d); + if ((bool)D_view) SYSData_D = ToObservableCollection(dt_d); } catch (Exception ex) { Name_err = "IO_SYSData_D" + "(" + ex.Message + ")"; } }); @@ -1341,6 +1344,83 @@ namespace DyeingComputer.ViewModel } } + public static void Upplc_dtd(string key, bool Value)//更新PLCD + { + try + { + lock (dt_d)//锁 + { + DataRow drEmploye = dt_d.Select("PLC=" + key).First(); + drEmploye.BeginEdit(); + drEmploye["DIO"] = Value; + drEmploye.EndEdit(); + drEmploye.AcceptChanges(); + drEmploye.ClearErrors(); + } + } + catch (Exception ex) + { + LogGing.LogGingDATA("PLCDTD:" + ex.ToString()); + } + } + public static void Upplc_dta(string key, int Value)//更新PLCA寄存器 + { + try + { + lock (dt_a) + { + DataRow drEmploye = dt_a.Select("PLC=" + key).First(); + drEmploye.BeginEdit(); + drEmploye["AIO"] = Value; + drEmploye.EndEdit(); + drEmploye.AcceptChanges(); + drEmploye.ClearErrors(); + } + } + catch (Exception ex) + { + LogGing.LogGingDATA("PLCDTA:" + ex.ToString()); + } + } + public static bool Seplc_dtd(string key)//PLCD寄存器 + { + try + { + lock (dt_d) + { + // DataRow[] arrRows = dt_d.Select("ID='" + key + "'"); + // bool index = Convert.ToBoolean(arrRows[0]["DIO"].ToString()); + DataRow drEmployee = dt_d.Select("PLC=" + key).First(); + bool index = drEmployee.Field("DIO"); + return index; + } + } + catch (Exception ex) + { + LogGing.LogGingDATA("PLCDTD:" + ex.ToString()); + return false; + } + } + public static int Seplc_dta(string key)//PLCA寄存器 + { + try + { + lock (dt_a) + { + // DataRow[] arrRows = dt_a.Select("ID='" + key + "'"); + // int index = Convert.ToInt16(arrRows[0]["AIO"].ToString()); + DataRow drEmployee = dt_a.Select("PLC=" + key).First(); + int index = drEmployee.Field("AIO"); + return index; + } + } + catch (Exception ex) + { + LogGing.LogGingDATA("PLCDTA:" + ex.ToString()); + return -1; + } + } + ObservableCollection sysData_A = new ObservableCollection(); ObservableCollection sysData_D = new ObservableCollection(); ObservableCollection sysData_M = new ObservableCollection(); @@ -1379,6 +1459,7 @@ namespace DyeingComputer.ViewModel 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 @@ -1386,6 +1467,7 @@ namespace DyeingComputer.ViewModel 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 @@ -1393,6 +1475,7 @@ namespace DyeingComputer.ViewModel 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 ObservableCollection ToObservableCollection(DataTable dt) where T : class, new()