Przeglądaj źródła

界面修改,线程计数调整,数据库回滚调整

yc_t 7 lat temu
rodzic
commit
84be1c4977

+ 3 - 0
parkMonitor/DataBase/DBConnection.cs

@@ -16,12 +16,15 @@ namespace parkMonitor.DataBase
         public static string localStr = "SqlConnectionLocation";
         public static string remoteStr = "SqlConnectionStr";
         public static string localConf, remoteConf;
+        public static string localIP, remoteIP;
         public static void Init()
         {
             try
             {
                 localConf = ConfigurationManager.AppSettings[localStr];
                 remoteConf = ConfigurationManager.AppSettings[remoteStr];
+                localIP = ConfigurationManager.AppSettings["localDBIP"];
+                remoteIP = ConfigurationManager.AppSettings["remoteDBIP"];
             }
             catch
             {

+ 19 - 17
parkMonitor/DataBase/IDBOperation.cs

@@ -105,27 +105,25 @@ namespace parkMonitor.DataBase
             int count = 0;
             if (conn == null)
             {
-                if (conn.ConnectionString == DBConnection.remoteConf)
-                {
-                    UILogServer.ins.error("传入非法数据库连接对象,请检查后手动重置数据库状态");
-                    Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "传入非法数据库连接对象");
-                    Operation.malfunctionRemote = true;
-                    Operation.malfunctionLocal = true;
-                }
+                UILogServer.ins.error("传入非法数据库连接对象,请检查后手动重置数据库状态");
+                Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "传入非法数据库连接对象");
+                Operation.malfunctionRemote = true;
+                Operation.malfunctionLocal = true;
             }
             while (conn != null && conn.State != ConnectionState.Open)
             {
                 mt.EndTiming();
-                if (mt.IsLonger(30, 1, false, out count) && count >= 5)
+                if (mt.IsLonger(30, 1, false, out count) && count >= 1)
                 {
-                    if (conn.ConnectionString == DBConnection.remoteConf)
+                    if (conn.DataSource == DBConnection.remoteIP)
                     {
-                        UILogServer.ins.error("暂时无法连接远端数据库, 请检查网络连接后点击“启动远端DB”恢复。");
+                        UILogServer.ins.error("暂时无法连接远端数据库, 请检查网络连接后点击“控制面板-启动远端DB”恢复。");
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "与远端DB失去连接");
                         Operation.malfunctionRemote = true;
-                    }else if (conn.ConnectionString == DBConnection.localConf)
+                    }
+                    else if (conn.DataSource == DBConnection.localIP)
                     {
-                        UILogServer.ins.error("暂时无法连接本地数据库,请检查网络连接后点击“启动本地DB”恢复。");
+                        UILogServer.ins.error("暂时无法连接本地数据库,请检查网络连接后点击“控制面板-启动本地DB”恢复。");
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "与本地DB失去连接");
                         Operation.malfunctionLocal = true;
                     }
@@ -153,7 +151,7 @@ namespace parkMonitor.DataBase
             returnedValue = 0;
             try
             {
-                for(int i = 0; i < sqls.Count; i++)
+                for (int i = 0; i < sqls.Count; i++)
                 {
                     cmd.CommandText = sqls[i];
                     cmd.ExecuteNonQuery();
@@ -167,7 +165,11 @@ namespace parkMonitor.DataBase
             }
             catch
             {
-                transaction.Rollback();
+                try
+                {
+                    transaction.Rollback();
+                }
+                catch { return false; }
                 UILogServer.ins.error("数据库操作失败,事务回滚");
             }
             return false;
@@ -186,15 +188,15 @@ namespace parkMonitor.DataBase
             while (true)
             {
                 mt.EndTiming();
-                if (mt.IsLonger(30, 1, false, out counter) && counter >= 5)
+                if (mt.IsLonger(30, 1, false, out counter) && counter >= 1)
                 {
-                    if (con.ConnectionString == DBConnection.remoteStr)
+                    if (con.DataSource == DBConnection.remoteIP)
                     {
                         UILogServer.ins.error("数据库操作异常,暂停处理自动命令。回滚后点击“启动远端DB”恢复");
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "操作远端DB异常");
                         Operation.malfunctionRemote = true;
                     }
-                    else
+                    else if (con.DataSource == DBConnection.localIP)
                     {
                         UILogServer.ins.error("数据库操作异常,暂停处理自动命令。回滚后点击“启动本地DB”恢复");
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "操作远端DB异常");

+ 27 - 1
parkMonitor/controlPanel/ControlPanel.Designer.cs

@@ -32,6 +32,8 @@
             this.btn_refresh = new System.Windows.Forms.Button();
             this.lb_confirmed = new System.Windows.Forms.Label();
             this.flp_buffer = new System.Windows.Forms.FlowLayoutPanel();
+            this.btn_remoteDB = new System.Windows.Forms.Button();
+            this.btn_localDB = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // btn_buffer_confirm
@@ -72,11 +74,33 @@
             this.flp_buffer.Size = new System.Drawing.Size(114, 132);
             this.flp_buffer.TabIndex = 5;
             // 
+            // btn_remoteDB
+            // 
+            this.btn_remoteDB.Location = new System.Drawing.Point(151, 34);
+            this.btn_remoteDB.Name = "btn_remoteDB";
+            this.btn_remoteDB.Size = new System.Drawing.Size(98, 23);
+            this.btn_remoteDB.TabIndex = 6;
+            this.btn_remoteDB.Text = "复位远端数据库";
+            this.btn_remoteDB.UseVisualStyleBackColor = true;
+            this.btn_remoteDB.Click += new System.EventHandler(this.btn_remoteDB_Click);
+            // 
+            // btn_localDB
+            // 
+            this.btn_localDB.Location = new System.Drawing.Point(151, 74);
+            this.btn_localDB.Name = "btn_localDB";
+            this.btn_localDB.Size = new System.Drawing.Size(98, 23);
+            this.btn_localDB.TabIndex = 7;
+            this.btn_localDB.Text = "复位本地数据库";
+            this.btn_localDB.UseVisualStyleBackColor = true;
+            this.btn_localDB.Click += new System.EventHandler(this.btn_localDB_Click);
+            // 
             // ControlPanel
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(182, 230);
+            this.ClientSize = new System.Drawing.Size(356, 230);
+            this.Controls.Add(this.btn_localDB);
+            this.Controls.Add(this.btn_remoteDB);
             this.Controls.Add(this.flp_buffer);
             this.Controls.Add(this.lb_confirmed);
             this.Controls.Add(this.btn_refresh);
@@ -93,5 +117,7 @@
         private System.Windows.Forms.Button btn_refresh;
         private System.Windows.Forms.Label lb_confirmed;
         private System.Windows.Forms.FlowLayoutPanel flp_buffer;
+        private System.Windows.Forms.Button btn_remoteDB;
+        private System.Windows.Forms.Button btn_localDB;
     }
 }

+ 38 - 1
parkMonitor/controlPanel/ControlPanel.cs

@@ -1,4 +1,6 @@
-using parkMonitor.server.CoreThread;
+using MySql.Data.MySqlClient;
+using parkMonitor.DataBase;
+using parkMonitor.server.CoreThread;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -97,5 +99,40 @@ namespace parkMonitor.controlPanel
             ParkingBufferManager.ins.checkedManually = true;
         }
 
+        private void btn_remoteDB_Click(object sender, EventArgs e)
+        {
+            bool initState = false;
+            if (Operation.malfunctionRemote)
+            {
+                using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                {
+                    Operation.TryOpen(conn);
+                    initState = conn.Ping();
+                }
+                if (initState)
+                {
+                    Operation.malfunctionRemote = false;
+                    MessageBox.Show("远端数据库已成功复位");
+                }
+            }
+        }
+
+        private void btn_localDB_Click(object sender, EventArgs e)
+        {
+            bool initState = false;
+            if (Operation.malfunctionLocal)
+            {
+                using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                {
+                    Operation.TryOpen(conn);
+                    initState = conn.Ping();
+                }
+                if (initState)
+                {
+                    Operation.malfunctionLocal = false;
+                    MessageBox.Show("本地数据库已成功复位");
+                }
+            }
+        }
     }
 }

+ 11 - 5
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -763,12 +763,18 @@ namespace parkMonitor.server.CoreThread
                             List<string> strs = new List<string>();
                             strs.Add(updateParkingSpaceSql);
                             strs.Add(updateFreeSpaceSql);
-                            if (!Operation.MyTransaction(conn, strs, out temp))
+                            while (!Operation.MyTransaction(conn, strs, out temp))
                             {
-                                //数据库操作失败写日志
-                                Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
-                                //Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
+                                //此处为关键步骤,不记入日志
+                                Thread.Sleep(5000);
+                                if (MyTimer.restart)
+                                {
+                                    Rollback(queueCmd, ppp.parkingSpaceID,false,true, lmToBeReleased);
+                                }
+                                ////数据库操作失败写日志
+                                //Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
+                                //Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
+                                ////Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
                             }
                         }
                     }

+ 26 - 19
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -105,6 +105,24 @@ namespace parkMonitor.server.CoreThread
             Command queueCmdRecord = null;
             while (!isClosing)
             {
+                lock (lockObj)
+                {
+                    //判断当前计数是否允许线程创建
+                    if (queueCmdRecord != null)
+                    {
+                        if (queueCmdRecord.id / 6 == Robot.robot1.id && Robot.robot1.waitCount >= 2)
+                        {
+                            Thread.Sleep(10000);
+                            continue;
+                        }
+                        if (queueCmdRecord.id / 6 == Robot.robot2.id && Robot.robot2.waitCount >= 2)
+                        {
+                            Thread.Sleep(10000);
+                            continue;
+                        }
+                    }
+                }
+
                 if (initialized && EntityForCore.ins.globalStatus)
                 {
                     Command queueCmd = null;
@@ -127,41 +145,30 @@ namespace parkMonitor.server.CoreThread
                     });
                     Task.WaitAll(getSysStatus, getCmd);
 
+                    
                     lock (lockObj)
                     {
-                        queueCmdRecord = (Command)queueCmd.Clone();
-                        
-                        //判断当前计数是否允许线程创建
                         if (queueCmdRecord != null)
                         {
-                            if (queueCmdRecord.id / 6 == Robot.robot1.id && Robot.robot1.waitCount >= 2)
+                            //对依赖同一机械手的线程计数
+                            if (queueCmdRecord.id == Robot.robot1.id)
                             {
-                                Thread.Sleep(10000);
-                                continue;
+                                Robot.robot1.waitCount += 1;
                             }
-                            if (queueCmdRecord.id / 6 == Robot.robot2.id && Robot.robot2.waitCount >= 2)
+                            else if (queueCmdRecord.id == Robot.robot2.id)
                             {
-                                Thread.Sleep(10000);
-                                continue;
+                                Robot.robot2.waitCount += 1;
                             }
                         }
-                        //对依赖同一机械手的线程计数
-                        if (queueCmdRecord.id == Robot.robot1.id)
-                        {
-                            Robot.robot1.waitCount += 1;
-                        }
-                        else if (queueCmdRecord.id == Robot.robot2.id)
-                        {
-                            Robot.robot2.waitCount += 1;
-                        }
+                        queueCmdRecord = (Command)queueCmd.Clone();
                     }
-
                     Task.Factory.StartNew(() =>
                     {
                         SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
                         AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
                         abstractCmd.executeCmd(queueCmd);
                     });
+                    
                 }
                 Thread.Sleep(100);
             }