浏览代码

数据库操作微改

yc_t 7 年之前
父节点
当前提交
54177f91c5
共有 2 个文件被更改,包括 45 次插入31 次删除
  1. 3 2
      parkMonitor/App.config
  2. 42 29
      parkMonitor/server/CoreThread/CoreThreadTest2.cs

+ 3 - 2
parkMonitor/App.config

@@ -18,8 +18,9 @@
     <!--数据库连接配置文件-->
     <add key="SqlConnectionLocation" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;pwd=yct;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />
     <!--<add key="SqlConnectionStr" value="Data Source=52.77.33.102;port=3306;uid=Ubuntu1;pooling=true;pwd=12345678;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />-->
-<add key="SqlConnectionStr" value="Data Source=59.175.148.85;port=3306;uid=root;pooling=true;pwd=x5;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" /> 
-<!--日志写入地址配置文件-->
+    <add key="SqlConnectionStr" value="Data Source=59.175.148.85;port=3306;uid=root;pooling=true;pwd=x5;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />
+    <add key="localGarageId" value="1"/>
+    <!--日志写入地址配置文件-->
     <add key="LogPath" value="E:\\c#workspace\\LogDemo\\LoggerTest" />
     <!--测量数据误差-->
     <add key="length" value="50" />

+ 42 - 29
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -27,14 +27,20 @@ namespace parkMonitor.server.CoreThread
         //
         DBLocationOperator locationOper;
         DBOperation oper;
+        int localGarageId = 0;
 
         string[] equipNames = new string[] { EquipmentName.PLC, EquipmentName.NumMachine, EquipmentName.Web, EquipmentName.Queue };
 
         public CoreThreadTest2()
         {
-            equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
-            locationOper = new DBLocationOperator();
-            oper = new DBOperation();
+            try
+            {
+                equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
+                localGarageId = Convert.ToInt32(ConfigurationManager.AppSettings["localGarageId"]);
+                locationOper = new DBLocationOperator();
+                oper = new DBOperation();
+            }
+            catch (Exception) { }
         }
 
         public void Start()
@@ -88,34 +94,42 @@ namespace parkMonitor.server.CoreThread
 
         public void BeginWorking()
         {
-            while (true)
+            Task.Factory.StartNew(() =>
             {
-                Func<AbstractMessage> func = new Func<AbstractMessage>(GetExecuteNumberPlate);
-
-                TaskFactory taskFactory = new TaskFactory();
-                List<Task> tasklist = new List<Task>();
-                IAsyncResult result = func.BeginInvoke(null, null);
-                Action act1 = new Action(() =>
+                while (true)
                 {
-                    GetTotalStatus();
-                });
-                tasklist.Add(taskFactory.StartNew(act1));
-                Task.WaitAll(tasklist.ToArray());
-                Command queueCmd = (Command)func.EndInvoke(result);
-                Task.Factory.StartNew(() =>
-                {
-                    SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
-                    AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
-                    abstractCmd.executeCmd(queueCmd);
-                });
-                //同步数据库
-                Task.Factory.StartNew(() =>
+                    Func<AbstractMessage> func = new Func<AbstractMessage>(GetExecuteNumberPlate);
+
+                    TaskFactory taskFactory = new TaskFactory();
+                    List<Task> tasklist = new List<Task>();
+                    IAsyncResult result = func.BeginInvoke(null, null);
+                    Action act1 = new Action(() =>
+                    {
+                        GetTotalStatus();
+                    });
+                    tasklist.Add(taskFactory.StartNew(act1));
+                    Task.WaitAll(tasklist.ToArray());
+                    Command queueCmd = (Command)func.EndInvoke(result);
+                    Task.Factory.StartNew(() =>
+                    {
+                        SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
+                        AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
+                        abstractCmd.executeCmd(queueCmd);
+                    });
+                    Thread.Sleep(100);
+                }
+            });
+
+            //同步数据库
+            Task.Factory.StartNew(() =>
+            {
+                while (true)
                 {
                     try
                     {
                         //更新车库表车位数
-                        int freeSpaceCount = locationOper.getGarageFreeSpace(queueCmd.garageID);
-                        oper.UpdateGarageFreeSpace(freeSpaceCount, queueCmd.garageID);
+                        int freeSpaceCount = locationOper.getGarageFreeSpace(localGarageId);
+                        oper.UpdateGarageFreeSpace(freeSpaceCount, localGarageId);
                         //更新车位状态                       
                         int[] spaceID = locationOper.GetAllParkingSpaceID();
                         if (spaceID != null)
@@ -130,10 +144,9 @@ namespace parkMonitor.server.CoreThread
                     {
                         Console.WriteLine("无同步数据");
                     }
-                    Thread.Sleep(1800000);
-                });
-                Thread.Sleep(100);
-            }
+                    Thread.Sleep(8000);
+                }
+            });
         }
 
         /// <summary>挂起</summary>