OpcUaServer.cs 10 KB

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