SchneiderRobot.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using TeamAAS_VP.Enums;
  10. using TeamAAS_VP.Interfaces;
  11. using TeamAAS_VP.Models;
  12. using TeamAAS_VP;
  13. using TouchSocket.Core;
  14. using TouchSocket.Sockets;
  15. using Opc.Ua;
  16. using TeamAAS_VP.Models.Product;
  17. namespace TeamAAS_VP.Core.Robots
  18. {
  19. public class SchneiderRobot : IRobot, INotifyPropertyChanged
  20. {
  21. private IWaitingClient<ITcpClient, IReceiverResult> WaitClient;
  22. private IWaitingClient<ITcpSessionClient, IReceiverResult> WaitClientServer;
  23. //private bool isFirstConnect = true;
  24. private Task bgConnectTask;
  25. public event Action<Guid, object, ConnectedEventArgs> ConnectedEvent;
  26. public event Action<Guid, object, ClosedEventArgs> DisconnectedEvent;
  27. public event Action<Guid, object, ReceivedDataEventArgs> ReceivedEvent;
  28. public event Action<Guid, object, string> SendEvent;
  29. public SchneiderRobot(RobotInfo robot)
  30. {
  31. Id = robot.Id;
  32. Name = robot.RobotName;
  33. RobotNo = robot.RobotNo;
  34. RobotIp = robot.IP;
  35. RobotPort = robot.Port;
  36. ConnectType = robot.ConnectType;
  37. Terminator = robot.Terminator;
  38. DataEncoding = robot.DataEncoding;
  39. Brand = robot.RobotBrand;
  40. if (ConnectType == TCPConnectType.Client)
  41. {
  42. TcpClient = new TouchSocket.Sockets.TcpClient();
  43. var config = new TouchSocketConfig();
  44. config.SetRemoteIPHost(new IPHost(IPAddress.Parse(RobotIp), RobotPort));
  45. config.ConfigurePlugins(a => { a.UseTcpReconnection(); }); ////如需永远尝试连接,tryCount设置为-1即可。
  46. ////设置结束符
  47. if (Terminator == Terminator.None)
  48. {
  49. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  50. }
  51. else if (Terminator == Terminator.CR)
  52. {
  53. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  54. }
  55. else if (Terminator == Terminator.LF)
  56. {
  57. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  58. }
  59. else if (Terminator == Terminator.CRLF)
  60. {
  61. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  62. }
  63. //载入配置
  64. TcpClient.Setup(config);
  65. ////调用CreateWaitingClient获取到IWaitingClient的对象。
  66. WaitClient = TcpClient.CreateWaitingClient(new WaitingOptions()
  67. {
  68. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  69. {
  70. return true;
  71. //if (response.Data.Length == 1)
  72. //{
  73. // return true;
  74. //}
  75. //return false;
  76. }
  77. });
  78. TcpClient.Connected = ConnectedVoid;//成功连接到服务器
  79. //有客户端断开连接
  80. TcpClient.Closed = DisconnectedVoid;
  81. //从客户端收到信息
  82. TcpClient.Received = ReceivedVoid;
  83. }
  84. else
  85. {
  86. TcpService = new TcpService();
  87. var config = new TouchSocketConfig();
  88. config.SetListenIPHosts(new IPHost[] { new IPHost($"{RobotIp}:{RobotPort}"), new IPHost(RobotPort + 1) }); //同时监听两个地址
  89. ////设置结束符
  90. if (Terminator == Terminator.None)
  91. {
  92. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  93. }
  94. else if (Terminator == Terminator.CR)
  95. {
  96. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  97. }
  98. else if (Terminator == Terminator.LF)
  99. {
  100. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  101. }
  102. else if (Terminator == Terminator.CRLF)
  103. {
  104. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  105. }
  106. //载入配置
  107. TcpService.Setup(config);
  108. /////有客户端成功连接
  109. TcpService.Connected = ConnectedVoid;
  110. //有客户端断开连接
  111. TcpService.Closed = DisconnectedVoid;
  112. //从客户端收到信息
  113. TcpService.Received = ReceivedVoid;
  114. }
  115. }
  116. #region 属性
  117. public TcpClient TcpClient { get; private set; }
  118. public TcpService TcpService { get; private set; }
  119. public Guid Id { get; set; }
  120. public string Name { get; set; }
  121. /// <summary>
  122. /// 机器人编号
  123. /// </summary>
  124. public int RobotNo { get; set; }
  125. public int RobotPort { get; private set; }
  126. public string RobotIp { get; private set; }
  127. public TCPConnectType ConnectType { get; private set; }
  128. public Terminator Terminator { get; private set; }
  129. public DataEncoding DataEncoding { get; private set; }
  130. public bool IsConnected
  131. {
  132. get
  133. {
  134. if (ConnectType == TCPConnectType.Client)
  135. {
  136. return TcpClient.Online;
  137. }
  138. else
  139. {
  140. if (TcpService.Count > 0)
  141. return true;
  142. else return false;
  143. }
  144. }
  145. }
  146. public int Timeout { get; set; } = 5000;
  147. private bool _CanExecute = false;
  148. public bool CanExecute
  149. {
  150. get { return _CanExecute; }
  151. set { SetProperty(ref _CanExecute, value); }
  152. }
  153. public int SelectedTool { get; private set; } = 0;
  154. public RobotBrand Brand { get; private set; }
  155. /// <summary>
  156. /// 进入调试模式
  157. /// </summary>
  158. /// <returns></returns>
  159. public bool EnterDebugMode { get; set; }
  160. private RPoint _CurrentPosition = new RPoint();
  161. /// <summary>
  162. /// 当前位置
  163. /// </summary>
  164. public RPoint CurrentPosition
  165. {
  166. get { return _CurrentPosition; }
  167. set { SetProperty(ref _CurrentPosition, value); }
  168. }
  169. #endregion
  170. #region 事件接收
  171. //-----------------------------------------------服务器---------------------------------------------
  172. /// <summary>
  173. /// 有客户端成功连接
  174. /// </summary>
  175. /// <param name="client"></param>
  176. /// <param name="e"></param>
  177. /// <returns></returns>
  178. private Task ConnectedVoid(ITcpSessionClient client, ConnectedEventArgs e)
  179. {
  180. CanExecute = true;
  181. //调用CreateWaitingClient获取到IWaitingClient的对象。
  182. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  183. {
  184. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  185. {
  186. return true;
  187. //if (response.Data.Length == 1)
  188. //{
  189. // return true;
  190. //}
  191. //return false;
  192. }
  193. });
  194. ConnectedEvent?.Invoke(this.Id, client, e);
  195. return EasyTask.CompletedTask;
  196. }
  197. /// <summary>
  198. /// 有客户端断开连接
  199. /// </summary>
  200. /// <param name="client"></param>
  201. /// <param name="e"></param>
  202. /// <returns></returns>
  203. private Task DisconnectedVoid(ITcpSessionClient client, ClosedEventArgs e)
  204. {
  205. if (TcpService.Count < 1)
  206. {
  207. CanExecute = false;
  208. }
  209. DisconnectedEvent?.Invoke(this.Id, client, e);
  210. if (WaitClientServer != null)
  211. {
  212. if (WaitClientServer.Client.Id == client.Id)
  213. {
  214. if (TcpService.Count > 0)
  215. {
  216. WaitClientServer = ((ITcpSessionClient)(TcpService.Clients.First())).CreateWaitingClient(new WaitingOptions()
  217. {
  218. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  219. {
  220. return true;
  221. //if (response.Data.Length == 1)
  222. //{
  223. // return true;
  224. //}
  225. //return false;
  226. }
  227. });
  228. }
  229. }
  230. }
  231. return EasyTask.CompletedTask;
  232. }
  233. /// <summary>
  234. /// 从客户端收到信息
  235. /// </summary>
  236. /// <param name="client"></param>
  237. /// <param name="e"></param>
  238. /// <returns></returns>
  239. private Task ReceivedVoid(ITcpSessionClient client, ReceivedDataEventArgs e)
  240. {
  241. ReceivedEvent?.Invoke(this.Id, client, e);
  242. //调用CreateWaitingClient获取到IWaitingClient的对象。
  243. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  244. {
  245. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  246. {
  247. return true;
  248. //if (response.Data.Length == 1)
  249. //{
  250. // return true;
  251. //}
  252. //return false;
  253. }
  254. });
  255. return EasyTask.CompletedTask;
  256. }
  257. //---------------------------------------------客户端--------------------------------------------
  258. /// <summary>
  259. /// 连接到服务器时
  260. /// </summary>
  261. /// <param name="client"></param>
  262. /// <param name="e"></param>
  263. /// <returns></returns>
  264. private Task ConnectedVoid(ITcpClient client, ConnectedEventArgs e)
  265. {
  266. CanExecute = true;
  267. ConnectedEvent?.Invoke(this.Id, client, e);
  268. return EasyTask.CompletedTask;
  269. }
  270. /// <summary>
  271. /// 有客户端断开连接
  272. /// </summary>
  273. /// <param name="client"></param>
  274. /// <param name="e"></param>
  275. /// <returns></returns>
  276. private Task DisconnectedVoid(ITcpClient client, ClosedEventArgs e)
  277. {
  278. CanExecute = false;
  279. DisconnectedEvent?.Invoke(this.Id, client, e);
  280. return EasyTask.CompletedTask;
  281. }
  282. /// <summary>
  283. /// 从客户端收到信息
  284. /// </summary>
  285. /// <param name="client"></param>
  286. /// <param name="e"></param>
  287. /// <returns></returns>
  288. private Task ReceivedVoid(ITcpClient client, ReceivedDataEventArgs e)
  289. {
  290. ReceivedEvent?.Invoke(this.Id, client, e);
  291. return EasyTask.CompletedTask;
  292. }
  293. #endregion
  294. #region 连接
  295. public void Connect()
  296. {
  297. if (ConnectType == TCPConnectType.Client)
  298. {
  299. try
  300. {
  301. TcpClient.Connect();
  302. //isFirstConnect = false;
  303. }
  304. catch (Exception)
  305. {
  306. if (bgConnectTask == null)
  307. {
  308. bgConnectTask = Task.Run(async () =>
  309. {
  310. while (true)
  311. {
  312. try
  313. {
  314. TcpClient.Connect();
  315. //isFirstConnect = false;
  316. return;
  317. }
  318. catch (Exception)
  319. {
  320. await Task.Delay(1000);
  321. }
  322. }
  323. });
  324. }
  325. throw;
  326. }
  327. }
  328. else
  329. {
  330. TcpService.Start();
  331. }
  332. }
  333. public async Task ConnectAsync()
  334. {
  335. if (ConnectType == TCPConnectType.Client)
  336. {
  337. try
  338. {
  339. await TcpClient.ConnectAsync();
  340. //isFirstConnect = false;
  341. }
  342. catch (Exception)
  343. {
  344. if (bgConnectTask == null)
  345. {
  346. bgConnectTask = Task.Run(async () =>
  347. {
  348. while (true)
  349. {
  350. try
  351. {
  352. TcpClient.Connect();
  353. //isFirstConnect = false;
  354. return;
  355. }
  356. catch (Exception)
  357. {
  358. await Task.Delay(1000);
  359. }
  360. }
  361. });
  362. }
  363. throw;
  364. }
  365. }
  366. else
  367. {
  368. await TcpService.StartAsync();
  369. }
  370. }
  371. public void Disconnect()
  372. {
  373. if (TcpClient != null)
  374. {
  375. TcpClient.Close();
  376. }
  377. if (TcpService != null)
  378. {
  379. TcpService.Stop();
  380. }
  381. }
  382. public void Dispose()
  383. {
  384. if (TcpClient != null)
  385. {
  386. TcpClient.Dispose();
  387. }
  388. if (TcpService != null)
  389. {
  390. TcpService.Dispose();
  391. }
  392. }
  393. #endregion
  394. #region 控制
  395. public bool Reset()
  396. {
  397. try
  398. {
  399. CanExecute = false;
  400. string rev = SendAndReceive($"Reset");
  401. if (rev.Contains("Reset"))
  402. return true;
  403. else
  404. return false;
  405. }
  406. catch (Exception ex)
  407. {
  408. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  409. CanExecute = true;
  410. throw ex;
  411. }
  412. finally
  413. {
  414. CanExecute = true;
  415. }
  416. }
  417. public async Task<bool> ResetAsync()
  418. {
  419. try
  420. {
  421. CanExecute = false;
  422. string rev = await SendAndReceiveAsync($"Reset");
  423. if (rev.Contains("Reset"))
  424. return true;
  425. else
  426. return false;
  427. }
  428. catch (Exception ex)
  429. {
  430. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  431. CanExecute = true;
  432. throw ex;
  433. }
  434. finally
  435. {
  436. CanExecute = true;
  437. }
  438. }
  439. public bool Motor(bool state)
  440. {
  441. try
  442. {
  443. CanExecute = false;
  444. int v = state ? 1 : 0;
  445. string rev = SendAndReceive($"Motor,{v}");
  446. if (rev.Contains("Motor"))
  447. return true;
  448. else
  449. return false;
  450. }
  451. catch (Exception ex)
  452. {
  453. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  454. CanExecute = true;
  455. throw ex;
  456. }
  457. finally
  458. {
  459. CanExecute = true;
  460. }
  461. }
  462. public async Task<bool> MotorAsync(bool state)
  463. {
  464. try
  465. {
  466. CanExecute = false;
  467. int v = state ? 1 : 0;
  468. string rev = await SendAndReceiveAsync($"Motor,{v}");
  469. if (rev.Contains("Motor"))
  470. return true;
  471. else
  472. return false;
  473. }
  474. catch (Exception ex)
  475. {
  476. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  477. CanExecute = true;
  478. throw ex;
  479. }
  480. finally
  481. {
  482. CanExecute = true;
  483. }
  484. }
  485. public bool Power(bool state)
  486. {
  487. try
  488. {
  489. CanExecute = false;
  490. int v = state ? 1 : 0;
  491. string rev = SendAndReceive($"Power,{v}");
  492. if (rev.Contains("Power"))
  493. return true;
  494. else
  495. return false;
  496. }
  497. catch (Exception ex)
  498. {
  499. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  500. CanExecute = true;
  501. throw ex;
  502. }
  503. finally
  504. {
  505. CanExecute = true;
  506. }
  507. }
  508. public async Task<bool> PowerAsync(bool state)
  509. {
  510. try
  511. {
  512. CanExecute = false;
  513. int v = state ? 1 : 0;
  514. string rev = await SendAndReceiveAsync($"Power,{v}");
  515. if (rev.Contains("Power"))
  516. return true;
  517. else
  518. return false;
  519. }
  520. catch (Exception ex)
  521. {
  522. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  523. CanExecute = true;
  524. throw ex;
  525. }
  526. finally
  527. {
  528. CanExecute = true;
  529. }
  530. }
  531. public bool Speed(int value)
  532. {
  533. try
  534. {
  535. CanExecute = false;
  536. string rev = SendAndReceive($"Speed,{value}");
  537. if (rev.Contains("Speed"))
  538. return true;
  539. else
  540. return false;
  541. }
  542. catch (Exception ex)
  543. {
  544. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  545. CanExecute = true;
  546. throw ex;
  547. }
  548. finally
  549. {
  550. CanExecute = true;
  551. }
  552. }
  553. public async Task<bool> SpeedAsync(int value)
  554. {
  555. try
  556. {
  557. CanExecute = false;
  558. string rev = await SendAndReceiveAsync($"Speed,{value}");
  559. if (rev.Contains("Speed"))
  560. return true;
  561. else
  562. return false;
  563. }
  564. catch (Exception ex)
  565. {
  566. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  567. CanExecute = true;
  568. throw ex;
  569. }
  570. finally
  571. {
  572. CanExecute = true;
  573. }
  574. }
  575. public bool Speedfactor(int value)
  576. {
  577. try
  578. {
  579. CanExecute = false;
  580. string rev = SendAndReceive($"Speedfactor,{value}");
  581. if (rev.Contains("Speedfactor"))
  582. return true;
  583. else
  584. return false;
  585. }
  586. catch (Exception ex)
  587. {
  588. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  589. CanExecute = true;
  590. throw ex;
  591. }
  592. finally
  593. {
  594. CanExecute = true;
  595. }
  596. }
  597. public async Task<bool> SpeedfactorAsync(int value)
  598. {
  599. try
  600. {
  601. CanExecute = false;
  602. string rev = await SendAndReceiveAsync($"Speedfactor,{value}");
  603. if (rev.Contains("Speedfactor"))
  604. return true;
  605. else
  606. return false;
  607. }
  608. catch (Exception ex)
  609. {
  610. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  611. CanExecute = true;
  612. throw ex;
  613. }
  614. finally
  615. {
  616. CanExecute = true;
  617. }
  618. }
  619. public bool Speeds(double value)
  620. {
  621. try
  622. {
  623. CanExecute = false;
  624. string rev = SendAndReceive($"Speeds,{value}");
  625. if (rev.Contains("Speeds"))
  626. return true;
  627. else
  628. return false;
  629. }
  630. catch (Exception ex)
  631. {
  632. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  633. CanExecute = true;
  634. throw ex;
  635. }
  636. finally
  637. {
  638. CanExecute = true;
  639. }
  640. }
  641. public async Task<bool> SpeedsAsync(double value)
  642. {
  643. try
  644. {
  645. CanExecute = false;
  646. string rev = await SendAndReceiveAsync($"Speeds,{value}");
  647. if (rev.Contains("Speeds"))
  648. return true;
  649. else
  650. return false;
  651. }
  652. catch (Exception ex)
  653. {
  654. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  655. CanExecute = true;
  656. throw ex;
  657. }
  658. finally
  659. {
  660. CanExecute = true;
  661. }
  662. }
  663. public bool Accel(int value)
  664. {
  665. try
  666. {
  667. CanExecute = false;
  668. string rev = SendAndReceive($"Accel,{value},{value}");
  669. if (rev.Contains("Accel"))
  670. return true;
  671. else
  672. return false;
  673. }
  674. catch (Exception ex)
  675. {
  676. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  677. CanExecute = true;
  678. throw ex;
  679. }
  680. finally
  681. {
  682. CanExecute = true;
  683. }
  684. }
  685. public async Task<bool> AccelAsync(int value)
  686. {
  687. try
  688. {
  689. CanExecute = false;
  690. string rev = await SendAndReceiveAsync($"Accel,{value},{value}");
  691. if (rev.Contains("Accel"))
  692. return true;
  693. else
  694. return false;
  695. }
  696. catch (Exception ex)
  697. {
  698. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  699. CanExecute = true;
  700. throw ex;
  701. }
  702. finally
  703. {
  704. CanExecute = true;
  705. }
  706. }
  707. public bool Accels(double value)
  708. {
  709. try
  710. {
  711. CanExecute = false;
  712. string rev = SendAndReceive($"Accels,{value},{value}");
  713. if (rev.Contains("Accels"))
  714. return true;
  715. else
  716. return false;
  717. }
  718. catch (Exception ex)
  719. {
  720. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  721. CanExecute = true;
  722. throw ex;
  723. }
  724. finally
  725. {
  726. CanExecute = true;
  727. }
  728. }
  729. public async Task<bool> AccelsAsync(double value)
  730. {
  731. try
  732. {
  733. CanExecute = false;
  734. string rev = await SendAndReceiveAsync($"Accels,{value},{value}");
  735. if (rev.Contains("Accels"))
  736. return true;
  737. else
  738. return false;
  739. }
  740. catch (Exception ex)
  741. {
  742. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  743. CanExecute = true;
  744. throw ex;
  745. }
  746. finally
  747. {
  748. CanExecute = true;
  749. }
  750. }
  751. public RPoint GetRobotPos()
  752. {
  753. try
  754. {
  755. CanExecute = false;
  756. string rev = SendAndReceive($"GetRobotPos");
  757. if (rev.Contains("GetRobotPos"))
  758. {
  759. RPoint point = new RPoint();
  760. var items = rev.Split(',');
  761. point.X = Convert.ToSingle(items[1]);
  762. point.Y = Convert.ToSingle(items[2]);
  763. point.Z = Convert.ToSingle(items[3]);
  764. point.U = Convert.ToSingle(items[4]);
  765. point.V = Convert.ToSingle(items[5]);
  766. point.W = Convert.ToSingle(items[6]);
  767. point.Hand = (RobotHand)Convert.ToInt32(items[7]);
  768. point.Local = Convert.ToInt32(items[8]);
  769. point.Tool = Convert.ToInt32(items[9]);
  770. CurrentPosition = point.Clone();
  771. return point;
  772. }
  773. else
  774. return null;
  775. }
  776. catch (Exception ex)
  777. {
  778. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  779. CanExecute = true;
  780. throw ex;
  781. }
  782. finally
  783. {
  784. CanExecute = true;
  785. }
  786. }
  787. public async Task<RPoint> GetRobotPosAsync()
  788. {
  789. try
  790. {
  791. CanExecute = false;
  792. string rev = await SendAndReceiveAsync($"GetRobotPos");
  793. if (rev.Contains("GetRobotPos"))
  794. {
  795. RPoint point = new RPoint();
  796. var items = rev.Split(',');
  797. point.X = Convert.ToSingle(items[1]);
  798. point.Y = Convert.ToSingle(items[2]);
  799. point.Z = Convert.ToSingle(items[3]);
  800. point.U = Convert.ToSingle(items[4]);
  801. point.V = Convert.ToSingle(items[5]);
  802. point.W = Convert.ToSingle(items[6]);
  803. point.Hand = (RobotHand)Convert.ToInt32(items[7]);
  804. point.Local = Convert.ToInt32(items[8]);
  805. point.Tool = Convert.ToInt32(items[9]);
  806. CurrentPosition = point.Clone();
  807. return point;
  808. }
  809. else
  810. return null;
  811. }
  812. catch (Exception ex)
  813. {
  814. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  815. CanExecute = true;
  816. throw ex;
  817. }
  818. finally
  819. {
  820. CanExecute = true;
  821. }
  822. }
  823. public bool Jog(string axis, double distance)
  824. {
  825. try
  826. {
  827. CanExecute = false;
  828. string rev = SendAndReceive($"Jog,{axis},{distance}");
  829. if (rev.Contains("Jog"))
  830. return true;
  831. else
  832. return false;
  833. }
  834. catch (Exception ex)
  835. {
  836. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  837. CanExecute = true;
  838. throw ex;
  839. }
  840. finally
  841. {
  842. CanExecute = true;
  843. }
  844. }
  845. public async Task<bool> JogAsync(string axis, double distance)
  846. {
  847. try
  848. {
  849. CanExecute = false;
  850. string rev = await SendAndReceiveAsync($"Jog,{axis},{distance}");
  851. if (rev.Contains("Jog"))
  852. return true;
  853. else
  854. return false;
  855. }
  856. catch (Exception ex)
  857. {
  858. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  859. CanExecute = true;
  860. throw ex;
  861. }
  862. finally
  863. {
  864. CanExecute = true;
  865. }
  866. }
  867. public bool Joint(int joint, double distance)
  868. {
  869. try
  870. {
  871. CanExecute = false;
  872. string rev = SendAndReceive($"Joint,{joint},{distance}");
  873. if (rev.Contains("Joint"))
  874. return true;
  875. else
  876. return false;
  877. }
  878. catch (Exception ex)
  879. {
  880. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  881. CanExecute = true;
  882. throw ex;
  883. }
  884. finally
  885. {
  886. CanExecute = true;
  887. }
  888. }
  889. public async Task<bool> JointAsync(int joint, double distance)
  890. {
  891. try
  892. {
  893. CanExecute = false;
  894. string rev = await SendAndReceiveAsync($"Joint,{joint},{distance}");
  895. if (rev.Contains("Joint"))
  896. return true;
  897. else
  898. return false;
  899. }
  900. catch (Exception ex)
  901. {
  902. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  903. CanExecute = true;
  904. throw ex;
  905. }
  906. finally
  907. {
  908. CanExecute = true;
  909. }
  910. }
  911. public bool Go(RPoint position)
  912. {
  913. try
  914. {
  915. CanExecute = false;
  916. string rev = SendAndReceive($"Go,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  917. if (rev.Contains("Go"))
  918. return true;
  919. else
  920. return false;
  921. }
  922. catch (Exception ex)
  923. {
  924. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  925. CanExecute = true;
  926. throw ex;
  927. }
  928. finally
  929. {
  930. CanExecute = true;
  931. }
  932. }
  933. public async Task<bool> GoAsync(RPoint position)
  934. {
  935. try
  936. {
  937. CanExecute = false;
  938. string rev = await SendAndReceiveAsync($"Go,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool}");
  939. if (rev.Contains("Go"))
  940. return true;
  941. else
  942. return false;
  943. }
  944. catch (Exception ex)
  945. {
  946. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  947. CanExecute = true;
  948. throw ex;
  949. }
  950. finally
  951. {
  952. CanExecute = true;
  953. }
  954. }
  955. public bool Jump(RPoint position, double? LimZ)
  956. {
  957. try
  958. {
  959. CanExecute = false;
  960. double limz = LimZ == null ? 0 : (double)LimZ;
  961. string rev = SendAndReceive($"Jump,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz, 3)}");
  962. if (rev.Contains("Jump"))
  963. return true;
  964. else
  965. return false;
  966. }
  967. catch (Exception ex)
  968. {
  969. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  970. CanExecute = true;
  971. throw ex;
  972. }
  973. finally
  974. {
  975. CanExecute = true;
  976. }
  977. }
  978. public async Task<bool> JumpAsync(RPoint position, double? LimZ)
  979. {
  980. try
  981. {
  982. CanExecute = false;
  983. double limz = LimZ == null ? 0 : (double)LimZ;
  984. string rev = await SendAndReceiveAsync($"Jump,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz, 3)}");
  985. if (rev.Contains("Jump"))
  986. return true;
  987. else
  988. return false;
  989. }
  990. catch (Exception ex)
  991. {
  992. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  993. CanExecute = true;
  994. throw ex;
  995. }
  996. finally
  997. {
  998. CanExecute = true;
  999. }
  1000. }
  1001. public bool Move(RPoint position)
  1002. {
  1003. try
  1004. {
  1005. CanExecute = false;
  1006. string rev = SendAndReceive($"Move,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  1007. if (rev.Contains("Move"))
  1008. return true;
  1009. else
  1010. return false;
  1011. }
  1012. catch (Exception ex)
  1013. {
  1014. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1015. CanExecute = true;
  1016. throw ex;
  1017. }
  1018. finally
  1019. {
  1020. CanExecute = true;
  1021. }
  1022. }
  1023. public async Task<bool> MoveAsync(RPoint position)
  1024. {
  1025. try
  1026. {
  1027. CanExecute = false;
  1028. string rev = await SendAndReceiveAsync($"Move,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool}");
  1029. if (rev.Contains("Move"))
  1030. return true;
  1031. else
  1032. return false;
  1033. }
  1034. catch (Exception ex)
  1035. {
  1036. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1037. CanExecute = true;
  1038. throw ex;
  1039. }
  1040. finally
  1041. {
  1042. CanExecute = true;
  1043. }
  1044. }
  1045. public bool SFree()
  1046. {
  1047. try
  1048. {
  1049. CanExecute = false;
  1050. string rev = SendAndReceive($"SFree");
  1051. if (rev.Contains("SFree"))
  1052. return true;
  1053. else
  1054. return false;
  1055. }
  1056. catch (Exception ex)
  1057. {
  1058. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1059. CanExecute = true;
  1060. throw ex;
  1061. }
  1062. finally
  1063. {
  1064. CanExecute = true;
  1065. }
  1066. }
  1067. public async Task<bool> SFreeAsync()
  1068. {
  1069. try
  1070. {
  1071. CanExecute = false;
  1072. string rev = await SendAndReceiveAsync($"SFree");
  1073. if (rev.Contains("SFree"))
  1074. return true;
  1075. else
  1076. return false;
  1077. }
  1078. catch (Exception ex)
  1079. {
  1080. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1081. CanExecute = true;
  1082. throw ex;
  1083. }
  1084. finally
  1085. {
  1086. CanExecute = true;
  1087. }
  1088. }
  1089. public bool SLock()
  1090. {
  1091. try
  1092. {
  1093. CanExecute = false;
  1094. string rev = SendAndReceive($"SLock");
  1095. if (rev.Contains("SLock"))
  1096. return true;
  1097. else
  1098. return false;
  1099. }
  1100. catch (Exception ex)
  1101. {
  1102. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1103. CanExecute = true;
  1104. throw ex;
  1105. }
  1106. finally
  1107. {
  1108. CanExecute = true;
  1109. }
  1110. }
  1111. public async Task<bool> SLockAsync()
  1112. {
  1113. try
  1114. {
  1115. CanExecute = false;
  1116. string rev = await SendAndReceiveAsync($"SLock");
  1117. if (rev.Contains("SLock"))
  1118. return true;
  1119. else
  1120. return false;
  1121. }
  1122. catch (Exception ex)
  1123. {
  1124. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1125. CanExecute = true;
  1126. throw ex;
  1127. }
  1128. finally
  1129. {
  1130. CanExecute = true;
  1131. }
  1132. }
  1133. public bool CalibMotion(RPoint position, double? LimZ)
  1134. {
  1135. try
  1136. {
  1137. CanExecute = false;
  1138. double limz = LimZ == null ? 0 : (double)LimZ;
  1139. string rev = SendAndReceive($"CalibMotion,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool},{limz}");
  1140. if (rev.Contains("CalibMotion"))
  1141. return true;
  1142. else
  1143. return false;
  1144. }
  1145. catch (Exception ex)
  1146. {
  1147. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1148. CanExecute = true;
  1149. throw ex;
  1150. }
  1151. finally
  1152. {
  1153. CanExecute = true;
  1154. }
  1155. }
  1156. public async Task<bool> CalibMotionAsync(RPoint position, double? LimZ)
  1157. {
  1158. try
  1159. {
  1160. CanExecute = false;
  1161. double limz = LimZ == null ? 0 : (double)LimZ;
  1162. string rev = await SendAndReceiveAsync($"CalibMotion,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz, 2)}");
  1163. if (rev.Contains("CalibMotion"))
  1164. return true;
  1165. else
  1166. return false;
  1167. }
  1168. catch (Exception ex)
  1169. {
  1170. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1171. CanExecute = true;
  1172. throw ex;
  1173. }
  1174. finally
  1175. {
  1176. CanExecute = true;
  1177. }
  1178. }
  1179. public bool CalibOutIO(bool state)
  1180. {
  1181. try
  1182. {
  1183. CanExecute = false;
  1184. int va = state ? 1 : 0;
  1185. string rev = SendAndReceive($"CalibOutIO,{va}");
  1186. if (rev.Contains("CalibOutIO"))
  1187. return true;
  1188. else
  1189. return false;
  1190. }
  1191. catch (Exception ex)
  1192. {
  1193. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1194. CanExecute = true;
  1195. throw ex;
  1196. }
  1197. finally
  1198. {
  1199. CanExecute = true;
  1200. }
  1201. }
  1202. public async Task<bool> CalibOutIOAsync(bool state)
  1203. {
  1204. try
  1205. {
  1206. CanExecute = false;
  1207. int va = state ? 1 : 0;
  1208. string rev = await SendAndReceiveAsync($"CalibOutIO,{va}");
  1209. if (rev.Contains("CalibOutIO"))
  1210. return true;
  1211. else
  1212. return false;
  1213. }
  1214. catch (Exception ex)
  1215. {
  1216. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1217. CanExecute = true;
  1218. throw ex;
  1219. }
  1220. finally
  1221. {
  1222. CanExecute = true;
  1223. }
  1224. }
  1225. public bool CalibParame(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1226. {
  1227. try
  1228. {
  1229. CanExecute = false;
  1230. int va = Power ? 1 : 0;
  1231. string rev = SendAndReceive($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  1232. if (rev.Contains("CalibParame"))
  1233. return true;
  1234. else
  1235. return false;
  1236. }
  1237. catch (Exception ex)
  1238. {
  1239. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1240. CanExecute = true;
  1241. throw ex;
  1242. }
  1243. finally
  1244. {
  1245. CanExecute = true;
  1246. }
  1247. }
  1248. public async Task<bool> CalibParameAsync(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1249. {
  1250. try
  1251. {
  1252. CanExecute = false;
  1253. int va = Power ? 1 : 0;
  1254. string rev = await SendAndReceiveAsync($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  1255. if (rev.Contains("CalibParame"))
  1256. return true;
  1257. else
  1258. return false;
  1259. }
  1260. catch (Exception ex)
  1261. {
  1262. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1263. CanExecute = true;
  1264. throw ex;
  1265. }
  1266. finally
  1267. {
  1268. CanExecute = true;
  1269. }
  1270. }
  1271. /// <summary>
  1272. /// 设置工具坐标
  1273. /// </summary>
  1274. /// <param name="index"></param>
  1275. /// <param name="x"></param>
  1276. /// <param name="y"></param>
  1277. /// <returns></returns>
  1278. public bool SetTool(int index, double x, double y)
  1279. {
  1280. try
  1281. {
  1282. CanExecute = false;
  1283. string rev = SendAndReceive($"SetTool,{index},{x},{y}");
  1284. if (rev.Contains("SetTool"))
  1285. return true;
  1286. else
  1287. return false;
  1288. }
  1289. catch (Exception ex)
  1290. {
  1291. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1292. CanExecute = true;
  1293. throw ex;
  1294. }
  1295. finally
  1296. {
  1297. CanExecute = true;
  1298. }
  1299. }
  1300. /// <summary>
  1301. /// 设置工具坐标
  1302. /// </summary>
  1303. /// <param name="index"></param>
  1304. /// <param name="x"></param>
  1305. /// <param name="y"></param>
  1306. /// <returns></returns>
  1307. public async Task<bool> SetToolAsync(int index, double x, double y)
  1308. {
  1309. try
  1310. {
  1311. CanExecute = false;
  1312. string rev = await SendAndReceiveAsync($"SetTool,{index},{Math.Round(x, 3)},{Math.Round(y, 3)}");
  1313. if (rev.Contains("SetTool"))
  1314. return true;
  1315. else
  1316. return false;
  1317. }
  1318. catch (Exception ex)
  1319. {
  1320. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1321. CanExecute = true;
  1322. throw ex;
  1323. }
  1324. finally
  1325. {
  1326. CanExecute = true;
  1327. }
  1328. }
  1329. /// <summary>
  1330. /// 选择工具坐标
  1331. /// </summary>
  1332. /// <param name="index"></param>
  1333. /// <returns></returns>
  1334. public bool SelectTool(int index)
  1335. {
  1336. try
  1337. {
  1338. SelectedTool = index;
  1339. CanExecute = false;
  1340. string rev = SendAndReceive($"SelectTool,{index}");
  1341. if (rev.Contains("SetTool"))
  1342. return true;
  1343. else
  1344. return false;
  1345. }
  1346. catch (Exception ex)
  1347. {
  1348. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1349. CanExecute = true;
  1350. throw ex;
  1351. }
  1352. finally
  1353. {
  1354. CanExecute = true;
  1355. }
  1356. }
  1357. /// <summary>
  1358. /// 选择工具坐标
  1359. /// </summary>
  1360. /// <param name="index"></param>
  1361. /// <returns></returns>
  1362. public async Task<bool> SelectToolAsync(int index)
  1363. {
  1364. {
  1365. try
  1366. {
  1367. SelectedTool = index;
  1368. CanExecute = false;
  1369. string rev = await SendAndReceiveAsync($"SelectTool,{index}");
  1370. if (rev.Contains("SetTool"))
  1371. return true;
  1372. else
  1373. return false;
  1374. }
  1375. catch (Exception ex)
  1376. {
  1377. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1378. CanExecute = true;
  1379. throw ex;
  1380. }
  1381. finally
  1382. {
  1383. CanExecute = true;
  1384. }
  1385. }
  1386. }
  1387. #endregion
  1388. #region 收发数据
  1389. /// <summary>
  1390. /// 发送并接收数据
  1391. /// </summary>
  1392. /// <param name="send"></param>
  1393. /// <returns></returns>
  1394. public string SendAndReceive(string send)
  1395. {
  1396. if (ConnectType == TCPConnectType.Client)
  1397. {
  1398. Encoding encoding = GetEncoding();
  1399. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1400. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  1401. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1402. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1403. return encoding.GetString(returnData);
  1404. }
  1405. else
  1406. {
  1407. Encoding encoding = GetEncoding();
  1408. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1409. var returnData = WaitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  1410. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1411. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1412. return encoding.GetString(returnData);
  1413. }
  1414. }
  1415. /// <summary>
  1416. /// 发送并接收数据
  1417. /// </summary>
  1418. /// <param name="send"></param>
  1419. /// <returns></returns>
  1420. public async Task<string> SendAndReceiveAsync(string send)
  1421. {
  1422. if (ConnectType == TCPConnectType.Client)
  1423. {
  1424. Encoding encoding = GetEncoding();
  1425. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1426. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1427. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1428. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1429. return encoding.GetString(returnData);
  1430. }
  1431. else
  1432. {
  1433. Encoding encoding = GetEncoding();
  1434. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1435. var returnData = await WaitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1436. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1437. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1438. return encoding.GetString(returnData);
  1439. }
  1440. }
  1441. /// <summary>
  1442. /// 发送并接收数据
  1443. /// </summary>
  1444. /// <param name="send"></param>
  1445. /// <returns></returns>
  1446. public string SendAndReceive(ITcpSessionClient client, string send)
  1447. {
  1448. if (ConnectType == TCPConnectType.Client)
  1449. {
  1450. Encoding encoding = GetEncoding();
  1451. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1452. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  1453. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1454. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1455. return encoding.GetString(returnData);
  1456. }
  1457. else
  1458. {
  1459. //调用CreateWaitingClient获取到IWaitingClient的对象。
  1460. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  1461. {
  1462. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  1463. {
  1464. return true;
  1465. //if (response.Data.Length == 1)
  1466. //{
  1467. // return true;
  1468. //}
  1469. //return false;
  1470. }
  1471. });
  1472. Encoding encoding = GetEncoding();
  1473. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1474. var returnData = waitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  1475. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1476. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1477. return encoding.GetString(returnData);
  1478. }
  1479. }
  1480. /// <summary>
  1481. /// 发送并接收数据
  1482. /// </summary>
  1483. /// <param name="send"></param>
  1484. /// <returns></returns>
  1485. public async Task<string> SendAndReceiveAsync(ITcpSessionClient client, string send)
  1486. {
  1487. if (ConnectType == TCPConnectType.Client)
  1488. {
  1489. Encoding encoding = GetEncoding();
  1490. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1491. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1492. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1493. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1494. return encoding.GetString(returnData);
  1495. }
  1496. else
  1497. {
  1498. //调用CreateWaitingClient获取到IWaitingClient的对象。
  1499. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  1500. {
  1501. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  1502. {
  1503. return true;
  1504. //if (response.Data.Length == 1)
  1505. //{
  1506. // return true;
  1507. //}
  1508. //return false;
  1509. }
  1510. });
  1511. Encoding encoding = GetEncoding();
  1512. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1513. var returnData = await waitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1514. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1515. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1516. return encoding.GetString(returnData);
  1517. }
  1518. }
  1519. public void Send(string send)
  1520. {
  1521. if (ConnectType == TCPConnectType.Client)
  1522. {
  1523. Encoding encoding = GetEncoding();
  1524. SendEvent?.Invoke(this.Id, TcpClient, send);
  1525. TcpClient.Send(encoding.GetBytes(send));
  1526. }
  1527. else
  1528. {
  1529. Encoding encoding = GetEncoding();
  1530. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1531. TcpService.Send(WaitClientServer.Client.Id, encoding.GetBytes(send));
  1532. }
  1533. }
  1534. public async Task SendAsync(string send)
  1535. {
  1536. if (ConnectType == TCPConnectType.Client)
  1537. {
  1538. Encoding encoding = GetEncoding();
  1539. SendEvent?.Invoke(this.Id, TcpClient, send);
  1540. await TcpClient.SendAsync(encoding.GetBytes(send));
  1541. }
  1542. else
  1543. {
  1544. Encoding encoding = GetEncoding();
  1545. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1546. await TcpService.SendAsync(WaitClientServer.Client.Id, encoding.GetBytes(send));
  1547. }
  1548. }
  1549. public void Send(ITcpSessionClient client, string send)
  1550. {
  1551. try
  1552. {
  1553. if (ConnectType == TCPConnectType.Client)
  1554. {
  1555. Encoding encoding = GetEncoding();
  1556. SendEvent?.Invoke(this.Id, TcpClient, send);
  1557. TcpClient.Send(encoding.GetBytes(send));
  1558. }
  1559. else
  1560. {
  1561. Encoding encoding = GetEncoding();
  1562. SendEvent?.Invoke(this.Id, client, send);
  1563. TcpService.Send(client.Id, encoding.GetBytes(send));
  1564. }
  1565. }
  1566. catch (Exception ex)
  1567. {
  1568. LogHelper.WriteLogError("给机器人发送数据时出错", ex);
  1569. }
  1570. }
  1571. public async Task SendAsync(ITcpSessionClient client, string send)
  1572. {
  1573. try
  1574. {
  1575. if (ConnectType == TCPConnectType.Client)
  1576. {
  1577. Encoding encoding = GetEncoding();
  1578. SendEvent?.Invoke(this.Id, TcpClient, send);
  1579. await TcpClient.SendAsync(encoding.GetBytes(send));
  1580. }
  1581. else
  1582. {
  1583. Encoding encoding = GetEncoding();
  1584. SendEvent?.Invoke(this.Id, client, send);
  1585. await TcpService.SendAsync(client.Id, encoding.GetBytes(send));
  1586. }
  1587. }
  1588. catch (Exception ex)
  1589. {
  1590. LogHelper.WriteLogError("给机器人发送数据时出错", ex);
  1591. }
  1592. }
  1593. /// <summary>
  1594. /// 获取编码
  1595. /// </summary>
  1596. /// <param name="dataEncoding"></param>
  1597. /// <returns></returns>
  1598. public Encoding GetEncoding()
  1599. {
  1600. DataEncoding dataEncoding = this.DataEncoding;
  1601. Encoding encoding;
  1602. if (dataEncoding == DataEncoding.Default)
  1603. {
  1604. encoding = Encoding.Default;
  1605. }
  1606. else if (dataEncoding == DataEncoding.ASCII)
  1607. {
  1608. encoding = Encoding.ASCII;
  1609. }
  1610. else if (dataEncoding == DataEncoding.UTF7)
  1611. {
  1612. encoding = Encoding.UTF7;
  1613. }
  1614. else if (dataEncoding == DataEncoding.UTF8)
  1615. {
  1616. encoding = Encoding.UTF8;
  1617. }
  1618. else if (dataEncoding == DataEncoding.UTF32)
  1619. {
  1620. encoding = Encoding.UTF32;
  1621. }
  1622. else if (dataEncoding == DataEncoding.Unicode)
  1623. {
  1624. encoding = Encoding.Unicode;
  1625. }
  1626. else if (dataEncoding == DataEncoding.BigEndianUnicode)
  1627. {
  1628. encoding = Encoding.BigEndianUnicode;
  1629. }
  1630. else if (dataEncoding == DataEncoding.GB2312)
  1631. {
  1632. encoding = Encoding.GetEncoding("gb2312");
  1633. }
  1634. else
  1635. {
  1636. encoding = Encoding.Default;
  1637. }
  1638. return encoding;
  1639. }
  1640. #endregion
  1641. #region 属性通知
  1642. /// <summary>
  1643. /// Occurs when a property value changes.
  1644. /// </summary>
  1645. public event PropertyChangedEventHandler PropertyChanged;
  1646. /// <summary>
  1647. /// Checks if a property already matches a desired value. Sets the property and
  1648. /// notifies listeners only when necessary.
  1649. /// </summary>
  1650. /// <typeparam name="T">Type of the property.</typeparam>
  1651. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1652. /// <param name="value">Desired value for the property.</param>
  1653. /// <param name="propertyName">Name of the property used to notify listeners. This
  1654. /// value is optional and can be provided automatically when invoked from compilers that
  1655. /// support CallerMemberName.</param>
  1656. /// <returns>True if the value was changed, false if the existing value matched the
  1657. /// desired value.</returns>
  1658. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  1659. {
  1660. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1661. storage = value;
  1662. RaisePropertyChanged(propertyName);
  1663. return true;
  1664. }
  1665. /// <summary>
  1666. /// Checks if a property already matches a desired value. Sets the property and
  1667. /// notifies listeners only when necessary.
  1668. /// </summary>
  1669. /// <typeparam name="T">Type of the property.</typeparam>
  1670. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1671. /// <param name="value">Desired value for the property.</param>
  1672. /// <param name="propertyName">Name of the property used to notify listeners. This
  1673. /// value is optional and can be provided automatically when invoked from compilers that
  1674. /// support CallerMemberName.</param>
  1675. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  1676. /// <returns>True if the value was changed, false if the existing value matched the
  1677. /// desired value.</returns>
  1678. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  1679. {
  1680. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1681. storage = value;
  1682. onChanged?.Invoke();
  1683. RaisePropertyChanged(propertyName);
  1684. return true;
  1685. }
  1686. /// <summary>
  1687. /// Raises this object's PropertyChanged event.
  1688. /// </summary>
  1689. /// <param name="propertyName">Name of the property used to notify listeners. This
  1690. /// value is optional and can be provided automatically when invoked from compilers
  1691. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  1692. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  1693. {
  1694. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  1695. }
  1696. /// <summary>
  1697. /// Raises this object's PropertyChanged event.
  1698. /// </summary>
  1699. /// <param name="args">The PropertyChangedEventArgs</param>
  1700. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  1701. {
  1702. PropertyChanged?.Invoke(this, args);
  1703. }
  1704. public List<DataValue> GetNodesValue()
  1705. {
  1706. throw new NotImplementedException();
  1707. }
  1708. #endregion
  1709. }
  1710. }