Bladeren bron

数据采集

oyq28 8 maanden geleden
bovenliggende
commit
83f71f2626
2 gewijzigde bestanden met toevoegingen van 39 en 18 verwijderingen
  1. 4 0
      opcua/Device/body/RequestBody.cs
  2. 35 18
      opcua/Form_Main.cs

+ 4 - 0
opcua/Device/body/RequestBody.cs

@@ -16,6 +16,9 @@ namespace IMCS.Device
         public string Type
         { get; set; }
 
+        public string Ip
+        { get; set; }
+
         public string Path
         { get; set; }
 
@@ -33,6 +36,7 @@ namespace IMCS.Device
 
         public string prgName
         { get; set; }
+
     }
 
     /// <summary>

+ 35 - 18
opcua/Form_Main.cs

@@ -27,6 +27,8 @@ using System.Collections;
 using System.Runtime.Remoting.Messaging;
 using System.Reflection.Emit;
 using System.Runtime.Remoting.Channels;
+using System.Net.NetworkInformation;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
 
 namespace IMCS
 {
@@ -96,6 +98,7 @@ namespace IMCS
                     if (!request.Url.ToString().Contains("favicon"))
                     {
                         RequestBody opcBody = null;
+                        ResponseBody responseBody = new ResponseBody();
                         try
                         {
                             Stream stream = context.Request.InputStream;
@@ -103,7 +106,7 @@ namespace IMCS
                             string body = reader.ReadToEnd();
 
                             YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
-                            ResponseBody responseBody = new ResponseBody();
+                            
 
                             opcBody = JsonConvert.DeserializeObject<RequestBody>(body);
 
@@ -125,14 +128,19 @@ namespace IMCS
                             {
                                 opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
                             }*/
-
-                            // 从map中取出opcServer 判断是否存在,以及是否可用
-                            opcServerMap.TryGetValue(opcBody.ServerUrl, out opcServer);
+                            
+                                // 从map中取出opcServer 判断是否存在,以及是否可用
+                                opcServerMap.TryGetValue(opcBody.ServerUrl, out opcServer);
 
                             // 如果key不存在,或者存在单连接不上,则需要重新创建连接
                             if (!opcServerMap.ContainsKey(opcBody.ServerUrl) || !opcServer.Session.Connected)
                             {
-                                opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
+                                Ping pingSender = new Ping();
+                                PingReply reply = pingSender.Send(opcBody.Ip);
+                                if (reply.Status == IPStatus.Success)
+                                {
+                                    opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
+                                }
 
                                 if (opcServer != null && opcServer.Session != null && opcServer.Session.Connected) { 
                                     // 存储Server bean
@@ -147,7 +155,7 @@ namespace IMCS
                             //    opcServer.Session.Reconnect();
                             //}
 
-                            if (opcServer.Session!=null && opcServer.Session.Connected)
+                            if (opcServer!=null && opcServer.Session!=null && opcServer.Session.Connected)
                             {
                                 if (opcBody.Type == ActionTypeEnum.Connect.ToString()) //连接状态
                                 {
@@ -161,6 +169,7 @@ namespace IMCS
 
                                     // 封装需要采集字段的数据
                                     this.addAddress(opcBody);
+                                    
                                     List<string> values = opcServer.ReadValues(opcBody.Addresses);
                                     responseBody.values = values;
                                     /*RunDatasInfo runDatasInfo = new RunDatasInfo();
@@ -563,15 +572,8 @@ namespace IMCS
                             response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
                             string retJsonData = JsonConvert.SerializeObject(responseBody);
 
+                            YG.Log.Instance.WriteLogAdd($"WMS--->>{responseBody.Json_SerializeObject()}--->>{body}\r\n");
 
-                            using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
-                            {
-                                YG.Log.Instance.WriteLogAdd($"WMS--->>{responseBody.Json_SerializeObject()}--->>{body}\r\n");
-
-                                writer.Write(responseBody.Json_SerializeObject());
-                                writer.Close();
-                                response.Close();
-                            }
 
                         }
                         catch (Exception opcex)
@@ -579,10 +581,25 @@ namespace IMCS
                             // 删除map中对应的bean
                             opcServerMap.Remove(opcBody.ServerUrl);
                             // 断开连接
-                            opcServer.Disconnect();
+                            if (opcServer.Session != null)
+                            {
+                                opcServer.Disconnect();
+                            }
+                            responseBody.msg = opcex.Message;
+                            responseBody.result = false;
 
                             AddList(DateTime.Now.ToString(), "POST", request.Url.ToString(), opcex.Message);
                         }
+                        finally
+                        {
+                            using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
+                            {
+                                
+                                writer.Write(responseBody.Json_SerializeObject());
+                                writer.Close();
+                                //response.Close();
+                            }
+                        }
                     }
                 }
                 else if (request.HttpMethod == "GET")
@@ -708,10 +725,10 @@ namespace IMCS
             opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.2");
             opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.3");
             opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.4");
+            opcBody.Addresses.Add("ns=2;s=/Plc/DB10.DBX106.1");
 
-
-            opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/timeBCD");
-            opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/textIndex");
+            /*opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/timeBCD");
+            opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/textIndex");*/
             opcBody.Addresses.Add("ns=2;s=/Channel/GeometricAxis/feedRateOvr");
             opcBody.Addresses.Add("ns=2;s=/Channel/Spindle/speedOvr");
             opcBody.Addresses.Add("ns=2;s=/Channel/Spindle/actSpeed");