using DyeingComputer.View; using DyeingComputer.ViewModel; using System; using System.Collections.Generic; using System.Data; 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; 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 class QueueString { 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(); 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 static void LOGlog(string WorkOrder, string TYPE, string Command, string MESSAGE) { 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(); } public static void ERRlog(string WorkOrder, string ERRID, string MESSAGE, string Time) { Dictionary API_LOG = new Dictionary();//缓存函数 API_LOG.Add("NAME", WorkOrder); API_LOG.Add("ERRID", ERRID); API_LOG.Add("MESSAGE", MESSAGE); API_LOG.Add("TIME", Time); LOGDB("ERRLog", API_LOG);//行插入 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; } } }