OpcUaServer.cs 10 KB

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