Ver código fonte

fix:优化采集方法

wang.sq@aliyun.com 5 meses atrás
pai
commit
1c78c19287
1 arquivos alterados com 43 adições e 35 exclusões
  1. 43 35
      HEIDENHAIN/Form1.cs

+ 43 - 35
HEIDENHAIN/Form1.cs

@@ -125,18 +125,21 @@ namespace HEIDENHAIN
             {
                 if (!request.Url.ToString().Contains("favicon"))
                 {
+                    ResponseBody responseBody = new ResponseBody();
+                    string body = null;
+                    RequestBody hdhBody =null;
                     try
                     {
                         Stream stream = context.Request.InputStream;
                         StreamReader reader = new StreamReader(stream, Encoding.UTF8);
-                        string body = reader.ReadToEnd();
+                        body = reader.ReadToEnd();
 
                         //YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
-                        ResponseBody responseBody = new ResponseBody();
 
-                        RequestBody hdhBody = JsonConvert.DeserializeObject<RequestBody>(body);
+                        hdhBody = JsonConvert.DeserializeObject<RequestBody>(body);
+
+                        AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":设备:" + hdhBody.MachineName, "OK:数据接受");
 
-                        AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":设备:" + hdhBody.MachineName, "OK");
                         if (hdhBody.Type == ActionTypeEnum.Connect.ToString())
                         {
                             Ping pingSender = new Ping();
@@ -147,12 +150,12 @@ namespace HEIDENHAIN
                             }
                         }
                         else
-                        {   
+                        {
                             //第一次连接加入数组,以支持多台设备
                             if (deviceList == null || (deviceList.Where(m => m.Key == hdhBody.MachineName).Count() == 0))
                             {
                                 m_ControlState = connect(hdhBody.MachineName);
-                                 
+
                                 //DNC连接正常,加入数组
                                 if (m_ControlState.ToString() == "DNC_STATE_MACHINE_IS_AVAILABLE")
                                 {
@@ -165,11 +168,11 @@ namespace HEIDENHAIN
                                 //取设备对应的状态
                                 m_ControlState = deviceList.Where(m => m.Key == hdhBody.MachineName).FirstOrDefault().Value;
                             }
-                            JHMachineInProcess Machine = machineList.Where(m => m.Key == hdhBody.MachineName).FirstOrDefault().Value; 
-
+                           
 
                             if (m_ControlState != null && m_ControlState.ToString() == "DNC_STATE_MACHINE_IS_AVAILABLE")
                             {
+                                JHMachineInProcess Machine = machineList.Where(m => m.Key == hdhBody.MachineName).FirstOrDefault().Value;
                                 JHError m_Error = Machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHERROR);
                                 JHErrorEntry2List errorsList = m_Error.GetErrorList();
                                 IJHErrorEntry2 pErrorEntry = null;
@@ -190,8 +193,8 @@ namespace HEIDENHAIN
                                     JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
 
                                     JHProcessData m_ProcessData = Machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHPROCESSDATA);
-                                    
-                                 
+
+
                                     object pFeed = new object();
                                     object pSpeed = new object();
                                     object pRapid = new object();
@@ -200,11 +203,11 @@ namespace HEIDENHAIN
                                     m_Automatic.GetOverrideInfo(ref pFeed, ref pSpeed, ref pRapid);
                                     m_Automatic.GetExecutionMode();
                                     DNC_STS_PROGRAM dncProgram = m_Automatic.GetProgramStatus();
-                                   
+
                                     RunDatasInfo runDatasInfo = new RunDatasInfo();
                                     runDatasInfo.feedRate = pFeed.ToString();
                                     runDatasInfo.spindleMagnification = pSpeed.ToString();
-                                    runDatasInfo.spindleSpeed = pRapid.ToString();                                    
+                                    runDatasInfo.spindleSpeed = pRapid.ToString();
 
                                     responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
 
@@ -229,7 +232,7 @@ namespace HEIDENHAIN
                                     JHFileSystem m_FileSystem = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHFILESYSTEM);
 
                                     JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
-                                  
+
                                     string sSelectedFile = Path.GetFileName(hdhBody.Path);
                                     string dncPath = null;
                                     string tempDncPath = RemotePath + "\\2.h";
@@ -249,7 +252,7 @@ namespace HEIDENHAIN
                                     {
                                     }
                                     //上传
-                                     m_FileSystem.TransmitFile(hdhBody.Path, dncPath);
+                                    m_FileSystem.TransmitFile(hdhBody.Path, dncPath);
                                     //设当前上传程序为主程序
                                     m_Automatic.SelectProgram(iChannel, dncPath);
                                 }
@@ -257,7 +260,7 @@ namespace HEIDENHAIN
                                 {
 
                                     JHFileSystem m_FileSystem = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHFILESYSTEM);
-                                   
+
                                     string dncPath = GenPath(RemotePath, hdhBody.Path);
                                     m_FileSystem.DeleteFile(dncPath);
                                 }
@@ -265,7 +268,7 @@ namespace HEIDENHAIN
                                 {
 
                                     JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
-                                    
+
                                     string dncPath = GenPath(RemotePath, hdhBody.Path);
                                     m_Automatic.SelectProgram(iChannel, dncPath);
                                 }
@@ -273,7 +276,7 @@ namespace HEIDENHAIN
                                 {
 
                                     JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
-                                    
+
                                     //m_Automatic.SelectProgram(iChannel, GenPath(RemotePath, hdhBody.Path));
                                     //Thread.Sleep(1000);
                                     m_Automatic.StartProgram(GenPath(RemotePath, hdhBody.Path));
@@ -312,11 +315,11 @@ namespace HEIDENHAIN
                                         toolsInfo.position = string.Join(".", pCode);
                                         toolsInfo.number = ToolCells[1].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
                                         toolsInfo.name = ToolCells[2].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
-                                                                                                                                                                                                     
 
-                                        if (!String.IsNullOrEmpty(toolsInfo.name) && !String.IsNullOrEmpty(toolsInfo.number) && pCode.Length>0 && pCode[1]>0)
+
+                                        if (!String.IsNullOrEmpty(toolsInfo.name) && !String.IsNullOrEmpty(toolsInfo.number) && pCode.Length > 0 && pCode[1] > 0)
                                         {
-                                            string ToolNumberAccessor = @"\TABLE\TOOL\T\"+ toolsInfo.number.ToString();
+                                            string ToolNumberAccessor = @"\TABLE\TOOL\T\" + toolsInfo.number.ToString();
                                             IJHDataEntry2List ToolList = dataAccess.GetDataEntry2(ToolNumberAccessor, DNC_DATA_UNIT_SELECT.DNC_DATA_UNIT_SELECT_METRIC, false).GetChildList();
 
                                             //报警期限
@@ -327,7 +330,7 @@ namespace HEIDENHAIN
                                             toolsInfo.curTime = ToolList[12].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
 
                                             toolsList.Add(toolsInfo);
-                                        }                                      
+                                        }
                                     }
                                     //获取海德汉的刀具寿命信息
                                     responseBody.toolsInfo = JsonConvert.SerializeObject(toolsList.Distinct().ToList());
@@ -337,36 +340,41 @@ namespace HEIDENHAIN
                             {
                                 responseBody.msg = m_ControlState.ToString();
                                 responseBody.result = false;
-                                deviceList.Remove(hdhBody.MachineName); 
+                                deviceList.Remove(hdhBody.MachineName);
+                                machineList.Remove(hdhBody.MachineName);
                             }
-                        }  
+                        }
+
+
+                    }
+                    catch (Exception opcex)
+                    {
+                        YG.Log.Instance.WriteLogAdd($"海德汉响应异常--->>" + opcex.Message);
+                        AddList(DateTime.Now.ToString(), "POST", request.Url.ToString(), opcex.Message);
+                        //发生异常,清空数组,重新连接
+                        deviceList = new Dictionary<string, DNC_STATE>();
+                        machineList = new Dictionary<string, JHMachineInProcess>();
+                    }
+                    finally {
 
-                        AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":响应数据:" + responseBody.toolsInfo, responseBody.result ? "OK" : "失败:"+ m_ControlState != null ? m_ControlState.ToString():"");
+                        AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":响应数据:" + responseBody.toolsInfo, responseBody.result ? "OK" : "失败:" + m_ControlState != null ? m_ControlState.ToString() : "");
 
                         response.ContentType = "application/json;charset=UTF-8";
                         response.ContentEncoding = Encoding.UTF8;
                         response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
                         string retJsonData = JsonConvert.SerializeObject(responseBody);
 
-
                         using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
                         {
                             YG.Log.Instance.WriteLogAdd($"海德汉响应结果--->>{JsonConvert.SerializeObject(JsonConvert.SerializeObject(responseBody))}--->>{body}\r\n");
-                       
+                            
                             writer.Write(JsonConvert.SerializeObject(responseBody));
                             writer.Close();
                             response.Close();
                         }
-
-                    }
-                    catch (Exception opcex)
-                    {
-                        YG.Log.Instance.WriteLogAdd($"海德汉响应异常--->>" + opcex.Message);
-                        AddList(DateTime.Now.ToString(), "POST", request.Url.ToString(), opcex.Message);
-                        //发生异常,清空数组,重新连接
-                        deviceList = new Dictionary<string, DNC_STATE>();
-                        machineList = new Dictionary<string, JHMachineInProcess>();
                     }
+
+
                 }
             }
             else if (request.HttpMethod == "GET")