OpcUaServer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. List<string> addresses = new List<string>();
  72. //addresses.Add("ns=2;s=/Channel/GeometricAxis/feedRateOvr"); //进给倍率
  73. addresses.Add("ns=2;s=/Channel/State/acFZ"); //进给速度
  74. addresses.Add("ns=2;s=/Channel/Spindle/speedOvr");//主轴倍率
  75. addresses.Add("ns=2;s=/Channel/Spindle/actSpeed");//主轴实际速度
  76. addresses.Add("ns=2;s=/Channel/Spindle/driveLoad");//主轴负载
  77. addresses.Add("ns=2;s=/Channel/ProgramInfo/selectedWorkPProg");//主程序
  78. addresses.Add("ns=2;s=/Nck/ChannelDiagnose/poweronTime");
  79. List<string> values = opcServer.ReadValues(addresses);
  80. RunDatasInfo runDatasInfo = new RunDatasInfo();
  81. for (int i = 0; i < values.Count; i++)
  82. {
  83. if (i == 0)
  84. {
  85. runDatasInfo.feedRate = values[i];
  86. responseBody.actFeed = values[i];
  87. }
  88. else if (i == 1)
  89. {
  90. runDatasInfo.spindleMagnification = values[i];
  91. responseBody.spindleMagnification = values[i];
  92. }
  93. else if (i == 2)
  94. {
  95. runDatasInfo.spindleSpeed = values[i];
  96. responseBody.actSpindle = values[i];
  97. }
  98. else if (i == 3)
  99. {
  100. runDatasInfo.spindleLoad = values[i];
  101. }
  102. else if (i == 4)
  103. {
  104. responseBody.mainProg = values[i];
  105. }
  106. else if (i == 5)
  107. {
  108. responseBody.powerOnTime = values[i];
  109. }
  110. }
  111. // responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
  112. }
  113. else if (fun == ActionTypeEnum.ToolList.ToString())
  114. {
  115. //List<ToolsInfo> toolInfoList = new List<ToolsInfo>();
  116. //int index = 1;
  117. //List<string> stringList = this.readVariables(opcServer, new string[6]
  118. // {
  119. // "/Tool/Catalogue/toolNo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  120. // "/Tool/Catalogue/toolIdent[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  121. // "/Tool/Catalogue/toolInMag[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  122. // "/Tool/Catalogue/toolInPlace[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  123. // "/Tool/Catalogue/nrDuplo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
  124. // "/Tool/Catalogue/numCuttEdges[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString())
  125. // }
  126. // );
  127. //ToolsInfo toolInfo = new ToolsInfo(); ;
  128. //toolInfo.number = Convert.ToUInt16(stringList[0]) + "";
  129. ////toolInfo.toolIdent = stringList[1];
  130. ////toolInfo.toolInMag = Convert.ToUInt16(stringList[2]);
  131. ////toolInfo.toolInPlace = Convert.ToUInt16(stringList[3]);
  132. ////toolInfo.nrDuplo = Convert.ToUInt16(stringList[4]);
  133. ////toolInfo.numCuttEdges = Convert.ToUInt16(stringList[5]);
  134. //toolInfoList.Add(toolInfo);
  135. // responseBody.toolsInfo = toolInfoList;
  136. }
  137. else if (fun == ActionTypeEnum.Read.ToString())
  138. {
  139. }
  140. else if (fun == ActionTypeEnum.Write.ToString())
  141. {
  142. }
  143. else if (fun == ActionTypeEnum.Upload.ToString())
  144. {
  145. string file = requestBody.path;
  146. string serverPath = "";
  147. //选择临时文件
  148. string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
  149. try
  150. {
  151. string extension = Path.GetExtension(file);
  152. if (string.IsNullOrWhiteSpace(extension))
  153. {
  154. responseBody.msg = "文件名不合法";
  155. }
  156. try
  157. {
  158. byte[] data = opcServer.ReadFile(file);
  159. serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
  160. Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
  161. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  162. {
  163. responseBody.msg = "上传文件成功";
  164. YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
  165. }
  166. else
  167. {
  168. responseBody.msg = "上传文件失败";
  169. YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
  170. }
  171. // }
  172. }
  173. catch (Exception ex)
  174. {
  175. responseBody.msg = "上传文件失败===>" + ex.Message;
  176. YG.Log.Instance.WriteLogAdd($"{ex.Message}");
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. responseBody.msg = "上传文件失败===>" + ex.Message;
  182. YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
  183. }
  184. //设位主程序
  185. status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
  186. }
  187. }
  188. else
  189. {
  190. responseBody.code = 0;
  191. responseBody.msg = "服务器离线,连不上opcUa";
  192. responseBody.result = false;
  193. }
  194. }
  195. return responseBody;
  196. }
  197. }
  198. }