DeviceOPCUat.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. using IMCS.CCS.Entitys;
  2. using IMCS.CCS;
  3. using IMCS_CCS.Utils;
  4. using Opc.Ua;
  5. using Opc.Ua.Client;
  6. using SinumerikOpcUaAPI;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Org.BouncyCastle.Utilities.Encoders;
  13. namespace YG.Device
  14. {
  15. public class DeviceOPCUat
  16. {
  17. Server opcserver;
  18. EndpointDescription endpointDescription;
  19. public bool IsOn { get; set; } = false;
  20. public DeviceOPCUat(string url, string username = "OpcUaClient", string pwd = "12345678")
  21. {
  22. if (opcserver == null || !opcserver.Session.Connected)
  23. {
  24. IsOn = OpcUa_Connection(url, username, pwd);
  25. Log.Instance.WriteLogAdd("OPCUA 连接是否成功===>>" + IsOn,
  26. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  27. }
  28. }
  29. public bool IsConnection { get; set; } = false;
  30. private void Notification_KeepAlive(Session sender, KeepAliveEventArgs e)
  31. {
  32. try
  33. {
  34. if (sender != this.opcserver.Session || !ServiceResult.IsBad(e.Status))
  35. return;
  36. this.opcserver.Session.Reconnect();
  37. Log.Instance.WriteLogAdd($"35----->>----->>----->>----->>----->>----->>----->>----->>----->>----->>----->>Notification_KeepAlive",
  38. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  39. IsConnection = true;
  40. }
  41. catch (Exception ex)
  42. {
  43. IsConnection = false;
  44. Log.Instance.WriteLogAdd($"41-->>{ex.Message}",
  45. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  46. }
  47. }
  48. /// <summary>
  49. /// 获取NC下边的文件列表
  50. /// </summary>
  51. /// <returns></returns>
  52. public ReferenceDescriptionCollection OpcUa_BrowseNode()
  53. {
  54. return opcserver.BrowseNode(new NodeId("Sinumerik/FileSystem/NCExtend/webApiMESFILE/", (ushort)2).ToString());
  55. }
  56. #region 终止连接
  57. public void OpcUa_Close()
  58. {
  59. if (OpcUa_ConState())
  60. {
  61. opcserver.Disconnect();
  62. }
  63. }
  64. private bool OpcUa_ConState()
  65. {
  66. if (opcserver != null && opcserver.Session.Connected)
  67. {
  68. return true;
  69. }
  70. else
  71. {
  72. Log.Instance.WriteLogAdd("OPC没有连接",
  73. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  74. }
  75. return false;
  76. }
  77. #endregion
  78. #region 写入
  79. public bool OpcUa_Write(string address, object value)
  80. {
  81. try
  82. {
  83. if (OpcUa_ConState())
  84. {
  85. opcserver.WriteValues(new List<string>() { value.ToString() }, new List<string>() { address });
  86. return true;
  87. }
  88. return false;
  89. }
  90. catch (Exception ex)
  91. {
  92. Log.Instance.WriteLogAdd($"92-->{ex.Message}",
  93. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  94. return false;
  95. }
  96. }
  97. public bool OpcUa_WriteValue(string address, object value)
  98. {
  99. try
  100. {
  101. string str = new NodeId(address, (ushort)2).ToString();
  102. if (OpcUa_ConState())
  103. {
  104. opcserver.WriteValues(new List<string>() { value.ToString() }, new List<string>() { str });
  105. return true;
  106. }
  107. return false;
  108. }
  109. catch (Exception ex)
  110. {
  111. Log.Instance.WriteLogAdd($"110->{ex.Message}",
  112. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  113. return false;
  114. }
  115. }
  116. public bool OpcUa_WriteValue(List<string> addresses, List<string> values)
  117. {
  118. try
  119. {
  120. if (OpcUa_ConState())
  121. {
  122. opcserver.WriteValues(addresses, values);
  123. return true;
  124. }
  125. return false;
  126. }
  127. catch (Exception ex)
  128. {
  129. Log.Instance.WriteLogAdd($"110->{ex.Message}",
  130. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  131. return false;
  132. }
  133. }
  134. #endregion
  135. #region 选中
  136. public bool OpcUa_Select(string filename)
  137. {
  138. bool result = false;
  139. try
  140. {
  141. if (OpcUa_ConState())
  142. {
  143. string extension = System.IO.Path.GetExtension(filename);
  144. if (string.IsNullOrWhiteSpace(extension) || !extension.ToUpper().Equals(".MPF"))
  145. {
  146. // System.Windows.Forms.MessageBox.Show("请选择要上传的文件");
  147. return false;
  148. }
  149. try
  150. {
  151. string value = opcserver.MethodCallSelectProgram(filename, Convert.ToUInt32(1)).status;
  152. if (value.Equals("Good"))
  153. {
  154. result = true;
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  160. Log.Instance.WriteLogAdd($"{ex.Message}",
  161. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  162. }
  163. }
  164. return result;
  165. }
  166. catch (Exception ex)
  167. {
  168. Log.Instance.WriteLogAdd($"154--->{ex.Message}",
  169. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  170. return false;
  171. }
  172. }
  173. public bool OpcUa_Select(string path, string filename)
  174. {
  175. bool result = false;
  176. try
  177. {
  178. if (OpcUa_ConState())
  179. {
  180. string extension = System.IO.Path.GetExtension(filename);
  181. if (string.IsNullOrWhiteSpace(extension) || !extension.ToUpper().Equals(".MPF"))
  182. {
  183. //System.Windows.Forms.MessageBox.Show("请选择要上传的文件");
  184. return false;
  185. }
  186. try
  187. {
  188. string value = opcserver.MethodCallSelectProgram(path + filename, Convert.ToUInt32(1)).status;
  189. if (value.Equals("Good"))
  190. {
  191. result = true;
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  197. Log.Instance.WriteLogAdd($"182-->{ex.Message}",
  198. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  199. }
  200. }
  201. return result;
  202. }
  203. catch (Exception ex)
  204. {
  205. Log.Instance.WriteLogAdd($"192-->{ex.Message}",
  206. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  207. return false;
  208. }
  209. }
  210. #endregion
  211. #region 读取
  212. public bool OpcUa_Read(string address, out string value)
  213. {
  214. bool result = false;
  215. value = "";
  216. if (OpcUa_ConState())
  217. {
  218. try
  219. {
  220. string itemaddress = $"/Plc/{address}";
  221. string str = new NodeId(itemaddress, (ushort)2).ToString();
  222. if (opcserver != null && opcserver.Session.Connected)
  223. {
  224. value = opcserver.ReadValues(new List<string>() { str })[0];
  225. }
  226. result = true;
  227. }
  228. catch (Exception ex)
  229. {
  230. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  231. Log.Instance.WriteLogAdd($"{ex.Message}",
  232. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  233. }
  234. }
  235. return result;
  236. }
  237. /// <summary>
  238. ///
  239. /// </summary>
  240. /// <param name="address">list里边的地址格式为I2.0,I2.1,DB2.DBX35.0等格式</param>
  241. /// <param name="value">查询出来的值和上面的值是一一对应的关系</param>
  242. /// <returns></returns>
  243. public bool OpcUa_Read(List<string> address, out List<string> value)
  244. {
  245. value = new List<string>();
  246. try
  247. {
  248. List<string> nodeIdStrings = new List<string>();
  249. foreach (string s in address)
  250. {
  251. nodeIdStrings.Add(new NodeId($"/Plc/{s}", (ushort)2).ToString());
  252. }
  253. if (opcserver != null && opcserver.Session.Connected)
  254. {
  255. value = opcserver.ReadValues(nodeIdStrings);
  256. }
  257. return true;
  258. }
  259. catch (Exception ex)
  260. {
  261. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  262. Log.Instance.WriteLogAdd($"{ex.Message}",
  263. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  264. return false;
  265. }
  266. }
  267. public List<string> OpcUa_Read(List<string> address)
  268. {
  269. List<string> value = new List<string>();
  270. try
  271. {
  272. List<string> nodeIdStrings = new List<string>();
  273. if (opcserver != null && opcserver.Session != null && opcserver.Session.Connected)
  274. {
  275. value = opcserver.ReadValues(nodeIdStrings);
  276. }
  277. return value;
  278. }
  279. catch (Exception ex)
  280. {
  281. try
  282. {
  283. OpcUa_Connection(opcserver.Session.ConfiguredEndpoint.ToString());
  284. }
  285. catch (Exception esx)
  286. {
  287. Log.Instance.WriteLogAdd($"284------->>esx---->{esx.Message} {ex.Message}--->>>",
  288. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  289. }
  290. return value;
  291. }
  292. }
  293. public bool OpcUa_Read(List<string> address, out Dictionary<string, string> value)
  294. {
  295. value = new Dictionary<string, string>();
  296. try
  297. {
  298. List<string> nodeIdStrings = new List<string>();
  299. foreach (string s in address)
  300. {
  301. nodeIdStrings.Add(new NodeId($"/Plc/{s}", (ushort)2).ToString());
  302. value.Add(s, "");
  303. }
  304. if (opcserver != null && opcserver.Session.Connected)
  305. {
  306. List<string> Keys = value.Keys.ToList();
  307. List<string> items = opcserver.ReadValues(nodeIdStrings);
  308. for (int i = 0; i < items.Count; i++)
  309. {
  310. value[Keys[i]] = items[i];
  311. }
  312. }
  313. return true;
  314. }
  315. catch (Exception ex)
  316. {
  317. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  318. Log.Instance.WriteLogAdd($"{ex.Message}",
  319. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  320. return false;
  321. }
  322. }
  323. #endregion
  324. #region OPCUA连接
  325. /// <summary>
  326. /// 连接opcua
  327. /// </summary>
  328. /// <param name="url"></param>
  329. /// <param name="username"></param>
  330. /// <param name="pwd"></param>
  331. public bool OpcUa_Connection(string url, string username = "OpcUaClient", string pwd = "12345678")
  332. {
  333. bool result = false;
  334. try
  335. {
  336. opcserver = new Server();
  337. ApplicationDescriptionCollection applicationDescriptions = opcserver.FindServers(url);
  338. if (applicationDescriptions.Count == 0)
  339. {
  340. Log.Instance.WriteLogAdd("没有找到服务!",
  341. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  342. }
  343. else if (applicationDescriptions.Count == 1)
  344. {
  345. applicationDescriptions[0].DiscoveryUrls.Where(m => m.Equals(url)).FirstOrDefault().FirstOrDefaultYG(
  346. (t) =>
  347. {
  348. try
  349. {
  350. var cc = opcserver.GetEndpoints(t);
  351. opcserver.GetEndpoints(t).Where(n => n.EndpointUrl.Equals(url)).FirstOrDefault().FirstOrDefaultYG((tt) =>
  352. {
  353. try
  354. {
  355. endpointDescription = tt;
  356. opcserver.KeepAliveNotification += new KeepAliveEventHandler(this.Notification_KeepAlive);
  357. opcserver.Connect(tt, true, username, pwd);
  358. if (opcserver.Session.Connected)
  359. {
  360. IsConnection = true;
  361. result = true;
  362. }
  363. else
  364. {
  365. result = false;
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. Log.Instance.WriteLogAdd($"355-->{ex.Message}",
  371. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  372. result = false;
  373. }
  374. });
  375. }
  376. catch (Exception ex)
  377. {
  378. Log.Instance.WriteLogAdd($"362-->{ex.Message}",
  379. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  380. }
  381. });
  382. }
  383. }
  384. catch (Exception eex) {
  385. Log.Instance.WriteLogAdd($"368-->{eex.Message}-->{url}",
  386. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  387. }
  388. return result;
  389. }
  390. /// <summary>
  391. /// 连接opcua
  392. /// </summary>
  393. /// <param name="url"></param>
  394. /// <param name="username"></param>
  395. /// <param name="pwd"></param>
  396. public static Server OpcUa_Connection1(string url, string username = "OpcUaClient", string pwd = "12345678")
  397. {
  398. Server serverOpcUa = new Server();
  399. try
  400. {
  401. ApplicationDescriptionCollection applicationDescriptions = serverOpcUa.FindServers(url);
  402. if (applicationDescriptions.Count == 0)
  403. {
  404. Log.Instance.WriteLogAdd("没有找到服务!",
  405. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  406. }
  407. else if (applicationDescriptions.Count == 1)
  408. {
  409. applicationDescriptions[0].DiscoveryUrls.Where(m => m.Equals(url)).FirstOrDefault().FirstOrDefaultYG(
  410. (t) =>
  411. {
  412. try
  413. {
  414. var cc = serverOpcUa.GetEndpoints(t);
  415. serverOpcUa.GetEndpoints(t).Where(n => n.EndpointUrl.Equals(url)).FirstOrDefault().FirstOrDefaultYG((tt) =>
  416. {
  417. try
  418. {
  419. serverOpcUa.Connect(tt, true, username, pwd);
  420. }
  421. catch (Exception ex)
  422. {
  423. Log.Instance.WriteLogAdd($"355-->{ex.Message}",
  424. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  425. }
  426. });
  427. }
  428. catch (Exception ex)
  429. {
  430. Log.Instance.WriteLogAdd($"362-->{ex.Message}",
  431. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  432. }
  433. });
  434. }
  435. }
  436. catch (Exception eex) {
  437. Log.Instance.WriteLogAdd($"368-->{eex.Message}-->{url}",
  438. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  439. }
  440. return serverOpcUa;
  441. }
  442. #endregion
  443. #region 添加用户权限
  444. /// <summary>
  445. /// 添加用户权限
  446. /// </summary>
  447. /// <param name="username"></param>
  448. public bool OpcUa_Access(string username = "OpcUaClient")
  449. {
  450. bool resultb = true;
  451. if (OpcUa_ConState())
  452. {
  453. try
  454. {
  455. string result = "";
  456. string strValue = "StateRead,StateWrite,FrameRead,FrameWrite,SeaRead,SeaWrite,TeaRead,TeaWrite,ToolRead,ToolWrite,DriveRead,DriveWrite,GudRead,GudWrite,PlcRead,PlcWrite,AlarmRead,FsRead,FsWrite,ApWrite,CsomReadx,CsomWritex,PlcReadDBx,PlcWriteDBx,SinuReadAll,SinuWriteAll";
  457. {
  458. string[] vs = strValue.Split(new string[] { "," }, StringSplitOptions.None);
  459. foreach (string s in vs)
  460. {
  461. Server.MethodCallResult methodCallResult = opcserver.MethodCall("/Methods/GiveUserAccess", username, s);
  462. if (methodCallResult != null && !methodCallResult.status.Equals("Good"))
  463. {
  464. result += s + methodCallResult.status;
  465. }
  466. }
  467. }
  468. if (result.Length > 0)
  469. {
  470. resultb = false;
  471. Log.Instance.WriteLogAdd($"执行权限添加是出现异常:{result}",
  472. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  473. }
  474. }
  475. catch (Exception ex)
  476. {
  477. resultb = false;
  478. Log.Instance.WriteLogAdd($"409-->{ex.Message}",
  479. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  480. }
  481. }
  482. return resultb;
  483. }
  484. #endregion
  485. #region 上传文件
  486. /// <summary>
  487. /// 上传文件,不能是文件夹,只能是一个文件,已经默认了一个文件位置, 如果有其他需求后期做调整
  488. /// </summary>
  489. /// <param name="file">需要上传的文件的名字,这个名字是有后缀的,</param>
  490. public void OpcUa_UpLoadFile(string file)
  491. {
  492. try
  493. {
  494. string extension = System.IO.Path.GetExtension(file);
  495. if (string.IsNullOrWhiteSpace(extension))
  496. {
  497. //System.Windows.Forms.MessageBox.Show("请选择要上传的文件");
  498. return;
  499. }
  500. try
  501. {
  502. opcserver.MethodCallCreateNewFile("Sinumerik/FileSystem/NCExtend/webApiMESFILE/", System.IO.Path.GetFileName(file), true);
  503. byte[] data = opcserver.ReadFile(file);
  504. int copylen = 5000;
  505. bool isover = true;
  506. byte[] bt = new byte[copylen];
  507. if (data.Length > copylen)
  508. {
  509. int runcount = data.Length / copylen;
  510. for (int i = 0; i < runcount; i++)
  511. {
  512. Array.Copy(data, i * copylen, bt, 0, copylen);
  513. if (i > 0)
  514. {
  515. isover = false;
  516. }
  517. var cc = opcserver.MethodCallCopyFileToServer($"/Methods/CopyFileToServer", $"Sinumerik/FileSystem/NCExtend/webApiMESFILE/{System.IO.Path.GetFileName(file)}", data, isover);
  518. System.Console.WriteLine($"当前循环到的位置:{i}-->>{cc.status}");
  519. }
  520. int lastleng = data.Length % copylen;
  521. bt = new byte[lastleng];
  522. Array.Copy(data, runcount * copylen, bt, 0, lastleng);
  523. Server.MethodCallResult methodCallResult = opcserver.MethodCallCopyFileToServer($"/Methods/CopyFileToServer", $"Sinumerik/FileSystem/NCExtend/webApiMESFILE/{System.IO.Path.GetFileName(file)}", data, isover);
  524. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  525. {
  526. Log.Instance.WriteLogAdd("上传文件成功",
  527. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  528. }
  529. else
  530. {
  531. Log.Instance.WriteLogAdd("上传文件失败",
  532. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  533. }
  534. }
  535. else
  536. {
  537. Server.MethodCallResult methodCallResult = opcserver.MethodCallCopyFileToServer($"/Methods/CopyFileToServer", $"Sinumerik/FileSystem/NCExtend/webApiMESFILE/{System.IO.Path.GetFileName(file)}", data, isover);
  538. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  539. {
  540. Log.Instance.WriteLogAdd("上传文件成功",
  541. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  542. }
  543. else
  544. {
  545. Log.Instance.WriteLogAdd("上传文件失败",
  546. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  547. }
  548. }
  549. }
  550. catch (Exception ex)
  551. {
  552. Log.Instance.WriteLogAdd($"{ex.Message}",
  553. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  554. //System.Windows.Forms.MessageBox.Show($"{ex.Message}");
  555. }
  556. }
  557. catch (Exception ex)
  558. {
  559. Log.Instance.WriteLogAdd($"493-->{ex.Message}",
  560. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  561. }
  562. }
  563. #endregion
  564. #region 删除文件
  565. /// <summary>
  566. /// 删除文件,只能是一个文件,已经默认了一个文件位置, 如果有其他需求后期做调整
  567. /// </summary>
  568. /// <param name="file">需要删除的文件的名字,这个名字是有后缀的</param>
  569. public void OpcUa_DeleteFile(string file)
  570. {
  571. try
  572. {
  573. Server.MethodCallResult methodCallResult = opcserver.MethodCallDeleteFile($"Sinumerik/FileSystem/NCExtend/webApiMESFILE/{file}");
  574. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  575. {
  576. Log.Instance.WriteLogAdd("删除文件成功",
  577. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  578. }
  579. else
  580. {
  581. Log.Instance.WriteLogAdd("删除文件失败",
  582. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  583. }
  584. }
  585. catch (Exception ex)
  586. {
  587. Log.Instance.WriteLogAdd($"519-->{ex.Message}",
  588. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  589. }
  590. }
  591. public string OpcUa_DeleteFile(string path, string file)
  592. {
  593. string status = "";
  594. try
  595. {
  596. status = opcserver.MethodCallDeleteFile(path + file).status;
  597. if (status.ToUpper().Equals("GOOD"))
  598. {
  599. Log.Instance.WriteLogAdd("删除文件成功",
  600. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  601. }
  602. else
  603. {
  604. Log.Instance.WriteLogAdd("删除文件失败",
  605. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  606. }
  607. }
  608. catch (Exception ex)
  609. {
  610. Log.Instance.WriteLogAdd($"539-->{ex.Message}",
  611. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  612. }
  613. return status;
  614. }
  615. #endregion
  616. #region 创建文件目录
  617. /// <summary>
  618. /// 创建文件目录,,
  619. /// </summary>
  620. /// <param name="file">类似于文件夹名称</param>
  621. public void OpcUa_CreateNewFile(string file = "webApiMESFILE")
  622. {
  623. try
  624. {
  625. Server.MethodCallResult methodCallResult = opcserver.MethodCallCreateNewDir($"Sinumerik/FileSystem/NCExtend", file);
  626. if (methodCallResult.status.ToUpper().Equals("GOOD"))
  627. {
  628. Log.Instance.WriteLogAdd("删除文件成功",
  629. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  630. }
  631. else
  632. {
  633. Log.Instance.WriteLogAdd("删除文件失败",
  634. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  635. }
  636. }
  637. catch (Exception ex)
  638. {
  639. Log.Instance.WriteLogAdd($"565-->{ex.Message}",
  640. EnumHelper.GetDescription(ActionTypeEnum.UploadFile));
  641. }
  642. }
  643. #endregion
  644. public void disConnect()
  645. {
  646. if (opcserver.Session.Connected)
  647. {
  648. opcserver.Disconnect();
  649. }
  650. }
  651. }
  652. }