diff --git a/UserClass/APILog.cs b/UserClass/APILog.cs index 58f788c..7ec122a 100644 --- a/UserClass/APILog.cs +++ b/UserClass/APILog.cs @@ -1,6 +1,8 @@ using DyeingComputer.View; using DyeingComputer.ViewModel; +using OpenTK.Audio.OpenAL; using ScottPlot.Colormaps; +using ScottPlot.TickGenerators.TimeUnits; using System; using System.Collections.Generic; using System.Data; @@ -8,6 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; +using System.Windows.Forms; using System.Xml.Linq; using static DyeingComputer.UserClass.SqliteHelper; @@ -15,46 +18,87 @@ namespace DyeingComputer.UserClass { public class APILog { + static bool QueueT=false; private static SQLiteHelper SQLiteHelpers = null; //定义数据库 private readonly static string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 private readonly static string LOGAddress = Environment.CurrentDirectory + "\\DataBase\\LOG.db"; //数据库路径 - - public async static void DBlog(string Name ,string ID ,string Api,string MESSAGE) + public class QueueString { - await Task.Run(() => - { - Dictionary API_LOG = new Dictionary();//缓存函数 - API_LOG.Add("NAME",Name); - API_LOG.Add("ID", ID); - API_LOG.Add("API", Api); - API_LOG.Add("MESSAGE", MESSAGE); - API_LOG.Add("TIME", MainWindowViewModel.SYSTime); + public string ID { get; set; } + public Dictionary DAT { get; set; } + } + public static class Queue + { + public static string ID { get; set; } + public static Dictionary DAT { get; set; } + } + // 创建一个字符串类型的队列 + public static Queue DBQueue = new Queue(); - SQLiteHelpers = new SQLiteHelper(LOGAddress); //数据库连接路径 - SQLiteHelpers.Open(); //打开数据库 - SQLiteHelpers.InsertData("SERVER", API_LOG);//行插入 - SQLiteHelpers.Close(); //关闭连接 - API_LOG.Clear(); - }); + public static void DBlog(string Name, string ID, string Api, string MESSAGE) + { + Dictionary API_LOG = new Dictionary();//缓存函数 + API_LOG.Add("NAME", Name); + API_LOG.Add("ID", ID); + API_LOG.Add("API", Api); + API_LOG.Add("MESSAGE", MESSAGE); + API_LOG.Add("TIME", MainWindowViewModel.SYSTime); + LOGDB("SERVER", API_LOG);//行插入 + API_LOG.Clear(); } - public async static void LOGlog(string WorkOrder, string TYPE, string Command, string MESSAGE) + public static void LOGlog(string WorkOrder, string TYPE, string Command, string MESSAGE) { - await Task.Run(() => - { - Dictionary API_LOG = new Dictionary();//缓存函数 - API_LOG.Add("WorkOrder", WorkOrder); - API_LOG.Add("TYPE", TYPE); - API_LOG.Add("Command", Command); - API_LOG.Add("MESSAGE", MESSAGE); - API_LOG.Add("TIME", MainWindowViewModel.SYSTime); + Dictionary API_LOG = new Dictionary();//缓存函数 + API_LOG.Add("WorkOrder", WorkOrder); + API_LOG.Add("TYPE", TYPE); + API_LOG.Add("Command", Command); + API_LOG.Add("MESSAGE", MESSAGE); + API_LOG.Add("TIME", MainWindowViewModel.SYSTime); + LOGDB("OperationLog", API_LOG);//行插入 + API_LOG.Clear(); + } + public static void IOlog(string IOName, string TYPE_ , object dat, string ID="0000", string PLC = "0000") + { + Dictionary API_LOG = new Dictionary();//缓存函数 + API_LOG.Add("IOName", IOName); + API_LOG.Add("type", TYPE_); + if(TYPE_ == "M") API_LOG.Add("Value", dat); + if ((TYPE_ == "AI") ||(TYPE_ == "AO")) API_LOG.Add("AIO", dat); + if ((TYPE_ == "DQ") || (TYPE_ == "DO")) API_LOG.Add("DIO", dat); + API_LOG.Add("ID", ID); + API_LOG.Add("PLC", PLC); + API_LOG.Add("TIME", MainWindowViewModel.SYSTime); + LOGDB("IOLog", API_LOG);//行插入 + API_LOG.Clear(); + } - SQLiteHelpers = new SQLiteHelper(LOGAddress); //数据库连接路径 - SQLiteHelpers.Open(); //打开数据库 - SQLiteHelpers.InsertData("OperationLog", API_LOG);//行插入 - SQLiteHelpers.Close(); //关闭连接 - API_LOG.Clear(); + public static void LOGDB(string NAME, Dictionary dat) + { + DBQueue.Enqueue(new QueueString + { + ID = NAME, + DAT = dat }); + + if (!QueueT) + { + QueueT = true; + LOGDB(); + } + if(DBQueue.Count==0) QueueT=false; } + public static void LOGDB() + { + SQLiteHelpers = new SQLiteHelper(LOGAddress); //数据库连接路径 + SQLiteHelpers.Open(); //打开数据库 + while (DBQueue.Count > 0)//信息发送队列 + { + QueueString t = DBQueue.Dequeue(); + SQLiteHelpers.InsertData(t.ID, t.DAT);//行插入 + } + SQLiteHelpers.Close(); //关闭连接 + QueueT = false; + } } } diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index b551ee7..3f6e9ef 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -122,6 +122,8 @@ namespace DyeingComputer.ViewModel execute:ProgramgroupView_run_ );//开始/暂停事件 ProgramgroupView_stop = new RelayCommand( execute: ProgramgroupView_stop_);//结束事件 + + } DataTable dt_con = new DataTable(); @@ -240,6 +242,7 @@ namespace DyeingComputer.ViewModel SK5 = Selet_sys("SK5");// } + private void ProgramgroupView_run_()////运行/暂停 { if (WORK_RUN == 2) @@ -1856,8 +1859,11 @@ namespace DyeingComputer.ViewModel dt_ParameterSet = SQLiteHelpers.ExecuteDataSet("select * from Parameters order by ParameterID asc", null).Tables[0]; dt_SysSet = SQLiteHelpers.ExecuteDataSet("select * from System order by ParameterID asc", null).Tables[0]; dt_TP = SQLiteHelpers.ExecuteDataSet("select * from RUN", null).Tables[0]; - SQLiteHelpers.Close(); + + dt_d.RowChanged += new DataRowChangeEventHandler(ROW_changed_D); + dt_a.RowChanged += new DataRowChangeEventHandler(ROW_changed_A); + //创建dt_d主建 DataColumn[] dt_dcols =new DataColumn[] { dt_d.Columns["ID"] }; dt_d.PrimaryKey = dt_dcols; @@ -1935,6 +1941,35 @@ namespace DyeingComputer.ViewModel }); } } + private static void ROW_changed_D(object sender, DataRowChangeEventArgs e) + { + try + { + int MD = int.Parse(e.Row.Field("ID")); + if (MD < 3001 || MD > 3004) + { + APILog.IOlog(e.Row.Field("IOName") + , e.Row.Field("type") + , e.Row.Field("DIO") + , e.Row.Field("ID") + , e.Row.Field("PLC")); + } + } + catch (Exception) { } + } + private static void ROW_changed_A(object sender, DataRowChangeEventArgs e) + { + try + { + APILog.IOlog(e.Row.Field("IOName") + , e.Row.Field("type") + , e.Row.Field("AIO") + , e.Row.Field("ID") + , e.Row.Field("PLC")); + } + catch (Exception) { } + } + public static double Selet_dtm(string key)//M寄存器 { try