OpcUaServer.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using OPCUA.Device;
  2. using RequestServer.HttpServer;
  3. using ResponseServer.HttpServer;
  4. using SinumerikOpcUaAPI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.NetworkInformation;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Fanuc_HttpServer.opcuaserver
  13. {
  14. class OpcUaServer
  15. {
  16. public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/TEST1/";
  17. //public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/";
  18. public string CHANNEL_NO = "1";
  19. public string TEMP_NC_NAME = "7999.MPF";
  20. private uint currentToolArea = 1;
  21. private static string ON_LINE_STATE = "在线";
  22. Server opcServer = null;
  23. public static Dictionary<string, Server> deviceList { get; set; } = new Dictionary<string, Server>();
  24. public ResponseBody requestHttpServer(RequestBody requestBody)
  25. {
  26. string ip = requestBody.serverUrl;
  27. string port = requestBody.port;
  28. string fun = requestBody.type;
  29. ResponseBody responseBody = new ResponseBody();
  30. if (fun == ActionTypeEnum.Connect.ToString())
  31. {
  32. Ping pingSender = new Ping();
  33. PingReply reply = pingSender.Send(requestBody.serverUrl);
  34. if (reply.Status != IPStatus.Success)
  35. {
  36. //responseBody.result = false;
  37. responseBody.msg = "调用失败";
  38. }
  39. }
  40. else
  41. {
  42. if (deviceList == null || (deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).Count() == 0))
  43. {
  44. opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
  45. if (opcServer.Session != null)
  46. {
  47. deviceList.Add(requestBody.serverUrl, opcServer);
  48. }
  49. }
  50. else
  51. {
  52. opcServer = deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).FirstOrDefault().Value;
  53. }
  54. if (opcServer == null || !opcServer.Session.Connected)
  55. {
  56. opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
  57. }
  58. //else
  59. //{
  60. // opcServer.Session.Reconnect();
  61. //}
  62. if (opcServer.Session.Connected)
  63. {
  64. responseBody.deviceState = ON_LINE_STATE;
  65. if (fun == ActionTypeEnum.Connect.ToString()) //连接状态
  66. {
  67. responseBody.result = true;
  68. }
  69. else if (fun == ActionTypeEnum.Collect.ToString()) //采集
  70. {
  71. }
  72. else if (fun == ActionTypeEnum.ToolList.ToString())
  73. {
  74. //List<ToolsInfo> toolInfoList = new List<ToolsInfo>();
  75. //int index = 1;
  76. //List<string> stringList = this.readVariables(opcServer, new string[6]
  77. // {
  78. // "/Tool/Catalogue/toolNo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  79. // "/Tool/Catalogue/toolIdent[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  80. // "/Tool/Catalogue/toolInMag[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  81. // "/Tool/Catalogue/toolInPlace[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  82. // "/Tool/Catalogue/nrDuplo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  83. // "/Tool/Catalogue/numCuttEdges[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString())
  84. // }
  85. // );
  86. //ToolsInfo toolInfo = new ToolsInfo(); ;
  87. //toolInfo.number = Convert.ToUInt16(stringList[0]) + "";
  88. ////toolInfo.toolIdent = stringList[1];
  89. ////toolInfo.toolInMag = Convert.ToUInt16(stringList[2]);
  90. ////toolInfo.toolInPlace = Convert.ToUInt16(stringList[3]);
  91. ////toolInfo.nrDuplo = Convert.ToUInt16(stringList[4]);
  92. ////toolInfo.numCuttEdges = Convert.ToUInt16(stringList[5]);
  93. //toolInfoList.Add(toolInfo);
  94. // responseBody.toolsInfo = toolInfoList;
  95. }
  96. else if (fun == ActionTypeEnum.Read.ToString())
  97. {
  98. }
  99. else if (fun == ActionTypeEnum.Write.ToString())
  100. {
  101. }
  102. else if (fun == ActionTypeEnum.Upload.ToString())
  103. {
  104. string file = requestBody.path;
  105. string serverPath = "";
  106. //选择临时文件
  107. string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
  108. try
  109. {
  110. string extension = Path.GetExtension(file);
  111. if (string.IsNullOrWhiteSpace(extension))
  112. {
  113. responseBody.msg = "文件名不合法";
  114. }
  115. try
  116. {
  117. byte[] data = opcServer.ReadFile(file);
  118. serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
  119. Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
  120. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  121. {
  122. responseBody.msg = "上传文件成功";
  123. YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
  124. }
  125. else
  126. {
  127. responseBody.msg = "上传文件失败";
  128. YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
  129. }
  130. // }
  131. }
  132. catch (Exception ex)
  133. {
  134. responseBody.msg = "上传文件失败===>" + ex.Message;
  135. YG.Log.Instance.WriteLogAdd($"{ex.Message}");
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. responseBody.msg = "上传文件失败===>" + ex.Message;
  141. YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
  142. }
  143. //设位主程序
  144. status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
  145. }
  146. }
  147. else
  148. {
  149. responseBody.code = 0;
  150. responseBody.msg = "服务器离线,连不上opcUa";
  151. responseBody.result = false;
  152. }
  153. }
  154. return responseBody;
  155. }
  156. }
  157. }