OpcUaServer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Collect.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. else
  40. {
  41. requestBody.serverUrl = "opc.tcp://" + requestBody.serverUrl + ":4840";
  42. YG.Log.Instance.WriteLogAdd(">>>===come in >>>>===");
  43. if (deviceList == null || (deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).Count() == 0))
  44. {
  45. YG.Log.Instance.WriteLogAdd(">>>===come in deviceList>>>>===");
  46. try
  47. {
  48. opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
  49. }
  50. catch (Exception e)
  51. {
  52. YG.Log.Instance.WriteLogAdd(">>>===come in deviceList1>>>>===" + e.Message);
  53. }
  54. YG.Log.Instance.WriteLogAdd(">>>===come in deviceList2>>>>===");
  55. if (opcServer.Session != null)
  56. {
  57. deviceList.Add(requestBody.serverUrl, opcServer);
  58. }
  59. }
  60. else
  61. {
  62. opcServer = deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).FirstOrDefault().Value;
  63. }
  64. if (opcServer == null || !opcServer.Session.Connected)
  65. {
  66. opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
  67. }
  68. //else
  69. //{
  70. // opcServer.Session.Reconnect();
  71. //}
  72. if (opcServer.Session.Connected)
  73. {
  74. responseBody.deviceState = ON_LINE_STATE;
  75. if (fun == ActionTypeEnum.Connect.ToString()) //连接状态
  76. {
  77. responseBody.result = true;
  78. }
  79. else if (fun == ActionTypeEnum.Collect.ToString()) //采集
  80. {
  81. List<string> addresses = new List<string>();
  82. addresses.Add("ns=2;s=/Channel/GeometricAxis/feedRateOvr"); //进给倍率
  83. addresses.Add("ns=2;s=/Channel/State/actFeedRateIpo"); //进给速度
  84. addresses.Add("ns=2;s=/Channel/Spindle/speedOvr");//主轴倍率
  85. addresses.Add("ns=2;s=/Channel/Spindle/actSpeed");//主轴实际速度
  86. //addresses.Add("ns=2;s=/Channel/Spindle/driveLoad");//主轴负载
  87. addresses.Add("ns=2;s=/Channel/ProgramInfo/selectedWorkPProg");//主程序
  88. addresses.Add("ns=2;s=/Nck/ChannelDiagnose/poweronTime");
  89. List<string> values = opcServer.ReadValues(addresses);
  90. RunDatasInfo runDatasInfo = new RunDatasInfo();
  91. for (int i = 0; i < values.Count; i++)
  92. {
  93. if (i == 0)
  94. {
  95. responseBody.feedRateOvr = values[i];
  96. responseBody.actFeed = values[i];
  97. }
  98. else if (i == 1)
  99. {
  100. responseBody.actFeed = values[i];
  101. }
  102. else if (i == 2)
  103. {
  104. responseBody.spindleMagnification = values[i];
  105. }
  106. else if (i == 3)
  107. {
  108. responseBody.actSpindle = values[i];
  109. }
  110. else if (i == 4)
  111. {
  112. responseBody.mainProg = values[i];
  113. }
  114. else if (i == 5)
  115. {
  116. responseBody.powerOnTime = values[i];
  117. }
  118. }
  119. // responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
  120. }
  121. else if (fun == ActionTypeEnum.ToolList.ToString())
  122. {
  123. //List<ToolsInfo> toolInfoList = new List<ToolsInfo>();
  124. //int index = 1;
  125. //List<string> stringList = this.readVariables(opcServer, new string[6]
  126. // {
  127. // "/Tool/Catalogue/toolNo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  128. // "/Tool/Catalogue/toolIdent[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  129. // "/Tool/Catalogue/toolInMag[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  130. // "/Tool/Catalogue/toolInPlace[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  131. // "/Tool/Catalogue/nrDuplo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  132. // "/Tool/Catalogue/numCuttEdges[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString())
  133. // }
  134. // );
  135. //ToolsInfo toolInfo = new ToolsInfo(); ;
  136. //toolInfo.number = Convert.ToUInt16(stringList[0]) + "";
  137. ////toolInfo.toolIdent = stringList[1];
  138. ////toolInfo.toolInMag = Convert.ToUInt16(stringList[2]);
  139. ////toolInfo.toolInPlace = Convert.ToUInt16(stringList[3]);
  140. ////toolInfo.nrDuplo = Convert.ToUInt16(stringList[4]);
  141. ////toolInfo.numCuttEdges = Convert.ToUInt16(stringList[5]);
  142. //toolInfoList.Add(toolInfo);
  143. // responseBody.toolsInfo = toolInfoList;
  144. }
  145. else if (fun == ActionTypeEnum.Read.ToString())
  146. {
  147. }
  148. else if (fun == ActionTypeEnum.Write.ToString())
  149. {
  150. }
  151. else if (fun == ActionTypeEnum.Upload.ToString())
  152. {
  153. string file = requestBody.path;
  154. string serverPath = "";
  155. //选择临时文件
  156. string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
  157. try
  158. {
  159. string extension = Path.GetExtension(file);
  160. if (string.IsNullOrWhiteSpace(extension))
  161. {
  162. responseBody.msg = "文件名不合法";
  163. }
  164. try
  165. {
  166. byte[] data = opcServer.ReadFile(file);
  167. serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
  168. Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
  169. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  170. {
  171. responseBody.msg = "上传文件成功";
  172. YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
  173. }
  174. else
  175. {
  176. responseBody.msg = "上传文件失败";
  177. YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
  178. }
  179. // }
  180. }
  181. catch (Exception ex)
  182. {
  183. responseBody.msg = "上传文件失败===>" + ex.Message;
  184. YG.Log.Instance.WriteLogAdd($"{ex.Message}");
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. responseBody.msg = "上传文件失败===>" + ex.Message;
  190. YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
  191. }
  192. //设位主程序
  193. status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
  194. }
  195. }
  196. else
  197. {
  198. responseBody.code = 0;
  199. responseBody.msg = "服务器离线,连不上opcUa";
  200. responseBody.result = false;
  201. }
  202. }
  203. }
  204. return responseBody;
  205. }
  206. }
  207. }