Преглед изворни кода

使用redis进行单例锁

林豪 左 пре 3 година
родитељ
комит
645b6f5817

+ 32 - 25
Projects/永冠OPP/WCS.Service/Extensions/DeviceExtension.cs

@@ -36,10 +36,11 @@ namespace WCS.Service.Extensions
         /// <param name="act"></param>
         public void EX(Action<StationDeviceGroup> act)
         {
+            var key = $"WCS:Lock:{Entity.CODE}";
             try
             {
-                if (ExDevice.Any(v => v == Entity.CODE)) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
-                OperateExDevice(Entity.CODE);
+                if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
+                ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 act(this);
             }
             catch (DoException ex)
@@ -56,7 +57,7 @@ namespace WCS.Service.Extensions
             }
             finally
             {
-                OperateExDevice(Entity.CODE);
+                ProtocolProxy.YG150Redis.Del(key);
             }
         }
 
@@ -266,10 +267,12 @@ namespace WCS.Service.Extensions
         /// <param name="act"></param>
         public void EX(Action<StationDevice> act)
         {
+            var key = $"WCS:Lock:{Entity.CODE}";
+
             try
             {
-                if (ExDevice.Any(v => v == Entity.CODE)) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
-                OperateExDevice(Entity.CODE);
+                if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
+                ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 act(this);
             }
             catch (DoException ex)
@@ -286,7 +289,7 @@ namespace WCS.Service.Extensions
             }
             finally
             {
-                OperateExDevice(Entity.CODE);
+                ProtocolProxy.YG150Redis.Del(key);
             }
         }
     }
@@ -362,10 +365,12 @@ namespace WCS.Service.Extensions
         /// <param name="act"></param>
         public void EX(Action<RGVDevice> act)
         {
+            var key = $"WCS:Lock:{Entity.CODE}";
+
             try
             {
-                if (ExDevice.Any(v => v == Entity.CODE)) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
-                OperateExDevice(Entity.CODE);
+                if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
+                ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 act(this);
             }
             catch (DoException ex)
@@ -382,7 +387,7 @@ namespace WCS.Service.Extensions
             }
             finally
             {
-                OperateExDevice(Entity.CODE);
+                ProtocolProxy.YG150Redis.Del(key);
             }
         }
 
@@ -668,10 +673,12 @@ namespace WCS.Service.Extensions
         /// <param name="act"></param>
         public void EX(Action<SRMDevice> act)
         {
+            var key = $"WCS:Lock:{Entity.CODE}";
+
             try
             {
-                if (ExDevice.Any(v => v == Entity.CODE)) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
-                OperateExDevice(Entity.CODE);
+                if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"[{Entity.CODE}]--触发并发管控");
+                ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 act(this);
             }
             catch (DoException ex)
@@ -688,7 +695,7 @@ namespace WCS.Service.Extensions
             }
             finally
             {
-                OperateExDevice(Entity.CODE);
+                ProtocolProxy.YG150Redis.Del(key);
             }
         }
 
@@ -698,36 +705,36 @@ namespace WCS.Service.Extensions
         /// <param name="act"></param>
         public void EXOutStock(Action<SRMDevice> act)
         {
+            var key = "WCS:Lock:";
             try
             {
-                var code = "";
                 if (Entity.CODE == "SRM3" || Entity.CODE == "SRM4")
                 {
-                    code = "SRM3-SRM4-Out";
-                    if (ExDevice.Any(v => v == code)) throw new WarnException($"触发出库并发管控--[{code}]");
-                    OperateExDevice(code);
+                    key += "SRM3-SRM4-Out";
+                    if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"触发出库并发管控--[{Entity.CODE}]");
+                    ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 }
                 if (Entity.CODE == "SRM5" || Entity.CODE == "SRM6")
                 {
-                    code = "SRM5-SRM6-Out";
-                    if (ExDevice.Any(v => v == code)) throw new WarnException($"触发出库并发管控--[{code}]");
-                    OperateExDevice(code);
+                    key += "SRM5-SRM6-Out";
+                    if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"触发出库并发管控--[{Entity.CODE}]");
+                    ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 }
 
                 if (Entity.CODE == "SRM7" || Entity.CODE == "SRM8")
                 {
-                    code = "SRM7-SRM8-Out";
-                    if (ExDevice.Any(v => v == code)) throw new WarnException($"触发出库并发管控--[{code}]");
-                    OperateExDevice(code);
+                    key += "SRM7-SRM8-Out";
+                    if (ProtocolProxy.YG150Redis.Get(key) != null) throw new WarnException($"触发出库并发管控--[{Entity.CODE}]");
+                    ProtocolProxy.YG150Redis.Set(key, Entity.CODE);
                 }
 
                 act(this);
             }
             finally
             {
-                if (Entity.CODE == "SRM3" || Entity.CODE == "SRM4") OperateExDevice("SRM3-SRM4-Out");
-                if (Entity.CODE == "SRM5" || Entity.CODE == "SRM6") OperateExDevice("SRM5-SRM6-Out");
-                if (Entity.CODE == "SRM7" || Entity.CODE == "SRM8") OperateExDevice("SRM7-SRM8-Out");
+                if (Entity.CODE == "SRM3" || Entity.CODE == "SRM4") ProtocolProxy.YG150Redis.Del($"{key}SRM3-SRM4-Out");
+                if (Entity.CODE == "SRM5" || Entity.CODE == "SRM6") ProtocolProxy.YG150Redis.Del($"{key}SRM5-SRM6-Out");
+                if (Entity.CODE == "SRM7" || Entity.CODE == "SRM8") ProtocolProxy.YG150Redis.Del($"{key}SRM7-SRM8-Out");
             }
         }
 

+ 3 - 0
Projects/永冠OPP/WCS.Service/ProtocolProxy.cs

@@ -95,6 +95,7 @@ namespace WCS.Service
         private static Dictionary<string, Playerback> Clients = new Dictionary<string, Playerback>();
 
         private static RedisClient Redis;
+        public static RedisClient YG150Redis;
 
         static ProtocolProxy()
         {
@@ -111,6 +112,8 @@ namespace WCS.Service
                 var obj = MessagePackSerializer.Deserialize(type, bytes);
                 return obj;
             };
+
+            YG150Redis = new RedisClient("192.168.249.150,password=123456,database=1");
         }
 
         //static ConcurrentDictionary<string, DeviceData> Datas = new ConcurrentDictionary<string, DeviceData>();