NlsScanner.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. using Opc.Ua;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using TeamAAS_VP.Enums;
  11. using TeamAAS_VP.Interfaces;
  12. using TeamAAS_VP.Models;
  13. using TeamAAS_VP.Models.Scanner;
  14. using TouchSocket.Core;
  15. using TouchSocket.Sockets;
  16. namespace TeamAAS_VP.Core.Scanners
  17. {
  18. public class NlsScanner : IScanner, INotifyPropertyChanged
  19. {
  20. private IWaitingClient<ITcpClient, IReceiverResult> WaitClient;
  21. private IWaitingClient<ITcpSessionClient, IReceiverResult> WaitClientServer;
  22. //private bool isFirstConnect=true;
  23. private Task bgConnectTask;
  24. public event Action<Guid, object, ConnectedEventArgs> ConnectedEvent;
  25. public event Action<Guid, object, ClosedEventArgs> DisconnectedEvent;
  26. public event Action<Guid, object, ReceivedDataEventArgs> ReceivedEvent;
  27. public event Action<Guid, object, string> SendEvent;
  28. public NlsScanner(ScannerInfo scanner)
  29. {
  30. Id = scanner.Id;
  31. Name = scanner.ScannerName;
  32. ScannerNo = scanner.ScannerNo;
  33. ScannerIp = scanner.IP;
  34. ScannerPort = scanner.Port;
  35. ConnectType = scanner.ConnectType;
  36. Terminator = scanner.Terminator;
  37. DataEncoding = scanner.DataEncoding;
  38. //Brand = scanner.ScannerBrand;
  39. if (ConnectType == TCPConnectType.Client)
  40. {
  41. TcpClient = new TouchSocket.Sockets.TcpClient();
  42. var config = new TouchSocketConfig();
  43. config.SetRemoteIPHost(new IPHost(IPAddress.Parse(ScannerIp), ScannerPort));
  44. config.ConfigurePlugins(a => { a.UseTcpReconnection(); }); ////如需永远尝试连接,tryCount设置为-1即可。
  45. ////设置结束符
  46. if (Terminator == Terminator.None)
  47. {
  48. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  49. }
  50. else if (Terminator == Terminator.CR)
  51. {
  52. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  53. }
  54. else if (Terminator == Terminator.LF)
  55. {
  56. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  57. }
  58. else if (Terminator == Terminator.CRLF)
  59. {
  60. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  61. }
  62. //载入配置
  63. TcpClient.Setup(config);
  64. ////调用CreateWaitingClient获取到IWaitingClient的对象。
  65. WaitClient = TcpClient.CreateWaitingClient(new WaitingOptions()
  66. {
  67. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  68. {
  69. return true;
  70. //if (response.Data.Length == 1)
  71. //{
  72. // return true;
  73. //}
  74. //return false;
  75. }
  76. });
  77. TcpClient.Connected = ConnectedVoid;//成功连接到服务器
  78. //有客户端断开连接
  79. TcpClient.Closed = DisconnectedVoid;
  80. //从客户端收到信息
  81. TcpClient.Received = ReceivedVoid;
  82. }
  83. else
  84. {
  85. TcpService = new TcpService();
  86. var config = new TouchSocketConfig();
  87. config.SetListenIPHosts(new IPHost[] { new IPHost($"{ScannerIp}:{ScannerPort}"), new IPHost(ScannerPort + 1) }); //同时监听两个地址
  88. ////设置结束符
  89. if (Terminator == Terminator.None)
  90. {
  91. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  92. }
  93. else if (Terminator == Terminator.CR)
  94. {
  95. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  96. }
  97. else if (Terminator == Terminator.LF)
  98. {
  99. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  100. }
  101. else if (Terminator == Terminator.CRLF)
  102. {
  103. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  104. }
  105. //载入配置
  106. TcpService.Setup(config);
  107. /////有客户端成功连接
  108. TcpService.Connected = ConnectedVoid;
  109. //有客户端断开连接
  110. TcpService.Closed = DisconnectedVoid;
  111. //从客户端收到信息
  112. TcpService.Received = ReceivedVoid;
  113. }
  114. }
  115. #region 属性
  116. public TcpClient TcpClient { get; private set; }
  117. public TcpService TcpService { get; private set; }
  118. public Guid Id { get; set; }
  119. public string Name { get; set; }
  120. /// <summary>
  121. /// 扫码枪编号
  122. /// </summary>
  123. public int ScannerNo { get; set; }
  124. public int ScannerPort { get; private set; }
  125. public string ScannerIp { get; private set; }
  126. public TCPConnectType ConnectType { get; private set; }
  127. public Terminator Terminator { get; private set; }
  128. public DataEncoding DataEncoding { get; private set; }
  129. public bool IsConnected
  130. {
  131. get
  132. {
  133. if (ConnectType == TCPConnectType.Client)
  134. {
  135. return TcpClient.Online;
  136. }
  137. else
  138. {
  139. if (TcpService.Count > 0)
  140. return true;
  141. else return false;
  142. }
  143. }
  144. }
  145. public int Timeout { get; set; } = 5000;
  146. private bool _CanExecute = false;
  147. public bool CanExecute
  148. {
  149. get { return _CanExecute; }
  150. set { SetProperty(ref _CanExecute, value); }
  151. }
  152. public int SelectedTool { get; private set; } = 0;
  153. public ScannerBrand Brand { get; private set; }
  154. /// <summary>
  155. /// 进入调试模式
  156. /// </summary>
  157. /// <returns></returns>
  158. public bool EnterDebugMode { get; set; }
  159. private RPoint _CurrentPosition = new RPoint();
  160. /// <summary>
  161. /// 当前位置
  162. /// </summary>
  163. public RPoint CurrentPosition
  164. {
  165. get { return _CurrentPosition; }
  166. set { SetProperty(ref _CurrentPosition, value); }
  167. }
  168. #endregion
  169. #region 事件接收
  170. //-----------------------------------------------服务器---------------------------------------------
  171. /// <summary>
  172. /// 有客户端成功连接
  173. /// </summary>
  174. /// <param name="client"></param>
  175. /// <param name="e"></param>
  176. /// <returns></returns>
  177. private Task ConnectedVoid(ITcpSessionClient client, ConnectedEventArgs e)
  178. {
  179. CanExecute = true;
  180. //调用CreateWaitingClient获取到IWaitingClient的对象。
  181. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  182. {
  183. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  184. {
  185. return true;
  186. //if (response.Data.Length == 1)
  187. //{
  188. // return true;
  189. //}
  190. //return false;
  191. }
  192. });
  193. ConnectedEvent?.Invoke(this.Id, client, e);
  194. return EasyTask.CompletedTask;
  195. }
  196. /// <summary>
  197. /// 有客户端断开连接
  198. /// </summary>
  199. /// <param name="client"></param>
  200. /// <param name="e"></param>
  201. /// <returns></returns>
  202. private Task DisconnectedVoid(ITcpSessionClient client, ClosedEventArgs e)
  203. {
  204. if (TcpService.Count < 1)
  205. {
  206. CanExecute = false;
  207. }
  208. DisconnectedEvent?.Invoke(this.Id, client, e);
  209. if (WaitClientServer != null)
  210. {
  211. if (WaitClientServer.Client.Id == client.Id)
  212. {
  213. if (TcpService.Count > 0)
  214. {
  215. WaitClientServer = ((ITcpSessionClient)(TcpService.Clients.First())).CreateWaitingClient(new WaitingOptions()
  216. {
  217. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  218. {
  219. return true;
  220. //if (response.Data.Length == 1)
  221. //{
  222. // return true;
  223. //}
  224. //return false;
  225. }
  226. });
  227. }
  228. }
  229. }
  230. return EasyTask.CompletedTask;
  231. }
  232. /// <summary>
  233. /// 从客户端收到信息
  234. /// </summary>
  235. /// <param name="client"></param>
  236. /// <param name="e"></param>
  237. /// <returns></returns>
  238. private Task ReceivedVoid(ITcpSessionClient client, ReceivedDataEventArgs e)
  239. {
  240. ReceivedEvent?.Invoke(this.Id, client, e);
  241. //调用CreateWaitingClient获取到IWaitingClient的对象。
  242. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  243. {
  244. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  245. {
  246. return true;
  247. //if (response.Data.Length == 1)
  248. //{
  249. // return true;
  250. //}
  251. //return false;
  252. }
  253. });
  254. return EasyTask.CompletedTask;
  255. }
  256. //---------------------------------------------客户端--------------------------------------------
  257. /// <summary>
  258. /// 连接到服务器时
  259. /// </summary>
  260. /// <param name="client"></param>
  261. /// <param name="e"></param>
  262. /// <returns></returns>
  263. private Task ConnectedVoid(ITcpClient client, ConnectedEventArgs e)
  264. {
  265. CanExecute = true;
  266. ConnectedEvent?.Invoke(this.Id, client, e);
  267. return EasyTask.CompletedTask;
  268. }
  269. /// <summary>
  270. /// 有客户端断开连接
  271. /// </summary>
  272. /// <param name="client"></param>
  273. /// <param name="e"></param>
  274. /// <returns></returns>
  275. private Task DisconnectedVoid(ITcpClient client, ClosedEventArgs e)
  276. {
  277. CanExecute = false;
  278. DisconnectedEvent?.Invoke(this.Id, client, e);
  279. return EasyTask.CompletedTask;
  280. }
  281. /// <summary>
  282. /// 从客户端收到信息
  283. /// </summary>
  284. /// <param name="client"></param>
  285. /// <param name="e"></param>
  286. /// <returns></returns>
  287. private Task ReceivedVoid(ITcpClient client, ReceivedDataEventArgs e)
  288. {
  289. ReceivedEvent?.Invoke(this.Id, client, e);
  290. return EasyTask.CompletedTask;
  291. }
  292. #endregion
  293. #region 连接
  294. public void Connect()
  295. {
  296. if (ConnectType == TCPConnectType.Client)
  297. {
  298. try
  299. {
  300. TcpClient.Connect();
  301. //isFirstConnect = false;
  302. }
  303. catch (Exception)
  304. {
  305. if (bgConnectTask == null)
  306. {
  307. bgConnectTask = Task.Run(async () =>
  308. {
  309. while (true)
  310. {
  311. try
  312. {
  313. TcpClient.Connect();
  314. //isFirstConnect = false;
  315. return;
  316. }
  317. catch (Exception)
  318. {
  319. await Task.Delay(1000);
  320. }
  321. }
  322. });
  323. }
  324. throw;
  325. }
  326. }
  327. else
  328. {
  329. TcpService.Start();
  330. }
  331. }
  332. public async Task ConnectAsync()
  333. {
  334. if (ConnectType == TCPConnectType.Client)
  335. {
  336. try
  337. {
  338. await TcpClient.ConnectAsync();
  339. //isFirstConnect = false;
  340. }
  341. catch (Exception)
  342. {
  343. if (bgConnectTask == null)
  344. {
  345. bgConnectTask = Task.Run(async () =>
  346. {
  347. while (true)
  348. {
  349. try
  350. {
  351. TcpClient.Connect();
  352. //isFirstConnect = false;
  353. return;
  354. }
  355. catch (Exception)
  356. {
  357. await Task.Delay(1000);
  358. }
  359. }
  360. });
  361. }
  362. throw;
  363. }
  364. }
  365. else
  366. {
  367. await TcpService.StartAsync();
  368. }
  369. }
  370. public void Disconnect()
  371. {
  372. if (TcpClient != null)
  373. {
  374. TcpClient.Close();
  375. }
  376. if (TcpService != null)
  377. {
  378. TcpService.Stop();
  379. }
  380. }
  381. public void Dispose()
  382. {
  383. if (TcpClient != null)
  384. {
  385. TcpClient.Dispose();
  386. }
  387. if (TcpService != null)
  388. {
  389. TcpService.Dispose();
  390. }
  391. }
  392. #endregion
  393. #region 收发数据
  394. /// <summary>
  395. /// 发送并接收数据
  396. /// </summary>
  397. /// <param name="send"></param>
  398. /// <returns></returns>
  399. public string SendAndReceive(string send)
  400. {
  401. if (ConnectType == TCPConnectType.Client)
  402. {
  403. Encoding encoding = GetEncoding();
  404. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  405. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  406. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  407. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  408. return encoding.GetString(returnData);
  409. }
  410. else
  411. {
  412. Encoding encoding = GetEncoding();
  413. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  414. var returnData = WaitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  415. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  416. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  417. return encoding.GetString(returnData);
  418. }
  419. }
  420. /// <summary>
  421. /// 发送并接收数据
  422. /// </summary>
  423. /// <param name="send"></param>
  424. /// <returns></returns>
  425. public async Task<string> SendAndReceiveAsync(string send)
  426. {
  427. if (ConnectType == TCPConnectType.Client)
  428. {
  429. Encoding encoding = GetEncoding();
  430. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  431. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  432. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  433. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  434. return encoding.GetString(returnData);
  435. }
  436. else
  437. {
  438. Encoding encoding = GetEncoding();
  439. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  440. var returnData = await WaitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  441. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  442. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  443. return encoding.GetString(returnData);
  444. }
  445. }
  446. /// <summary>
  447. /// 发送并接收数据
  448. /// </summary>
  449. /// <param name="send"></param>
  450. /// <returns></returns>
  451. public string SendAndReceive(ITcpSessionClient client, string send)
  452. {
  453. if (ConnectType == TCPConnectType.Client)
  454. {
  455. Encoding encoding = GetEncoding();
  456. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  457. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  458. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  459. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  460. return encoding.GetString(returnData);
  461. }
  462. else
  463. {
  464. //调用CreateWaitingClient获取到IWaitingClient的对象。
  465. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  466. {
  467. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  468. {
  469. return true;
  470. //if (response.Data.Length == 1)
  471. //{
  472. // return true;
  473. //}
  474. //return false;
  475. }
  476. });
  477. Encoding encoding = GetEncoding();
  478. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  479. var returnData = waitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  480. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  481. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  482. return encoding.GetString(returnData);
  483. }
  484. }
  485. /// <summary>
  486. /// 发送并接收数据
  487. /// </summary>
  488. /// <param name="send"></param>
  489. /// <returns></returns>
  490. public async Task<string> SendAndReceiveAsync(ITcpSessionClient client, string send)
  491. {
  492. if (ConnectType == TCPConnectType.Client)
  493. {
  494. Encoding encoding = GetEncoding();
  495. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  496. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  497. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  498. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  499. return encoding.GetString(returnData);
  500. }
  501. else
  502. {
  503. //调用CreateWaitingClient获取到IWaitingClient的对象。
  504. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  505. {
  506. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  507. {
  508. return true;
  509. //if (response.Data.Length == 1)
  510. //{
  511. // return true;
  512. //}
  513. //return false;
  514. }
  515. });
  516. Encoding encoding = GetEncoding();
  517. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  518. var returnData = await waitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  519. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  520. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  521. return encoding.GetString(returnData);
  522. }
  523. }
  524. public void Send(string send)
  525. {
  526. if (ConnectType == TCPConnectType.Client)
  527. {
  528. Encoding encoding = GetEncoding();
  529. SendEvent?.Invoke(this.Id, TcpClient, send);
  530. TcpClient.Send(encoding.GetBytes(send));
  531. }
  532. else
  533. {
  534. Encoding encoding = GetEncoding();
  535. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  536. TcpService.Send(WaitClientServer.Client.Id, encoding.GetBytes(send));
  537. }
  538. }
  539. public async Task SendAsync(string send)
  540. {
  541. if (ConnectType == TCPConnectType.Client)
  542. {
  543. Encoding encoding = GetEncoding();
  544. SendEvent?.Invoke(this.Id, TcpClient, send);
  545. await TcpClient.SendAsync(encoding.GetBytes(send));
  546. }
  547. else
  548. {
  549. Encoding encoding = GetEncoding();
  550. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  551. await TcpService.SendAsync(WaitClientServer.Client.Id, encoding.GetBytes(send));
  552. }
  553. }
  554. public void Send(ITcpSessionClient client, string send)
  555. {
  556. try
  557. {
  558. if (ConnectType == TCPConnectType.Client)
  559. {
  560. Encoding encoding = GetEncoding();
  561. SendEvent?.Invoke(this.Id, TcpClient, send);
  562. TcpClient.Send(encoding.GetBytes(send));
  563. }
  564. else
  565. {
  566. Encoding encoding = GetEncoding();
  567. SendEvent?.Invoke(this.Id, client, send);
  568. TcpService.Send(client.Id, encoding.GetBytes(send));
  569. }
  570. }
  571. catch (Exception ex)
  572. {
  573. LogHelper.WriteLogError("给机器人发送数据时出错", ex);
  574. }
  575. }
  576. public async Task SendAsync(ITcpSessionClient client, string send)
  577. {
  578. try
  579. {
  580. if (ConnectType == TCPConnectType.Client)
  581. {
  582. Encoding encoding = GetEncoding();
  583. SendEvent?.Invoke(this.Id, TcpClient, send);
  584. await TcpClient.SendAsync(encoding.GetBytes(send));
  585. }
  586. else
  587. {
  588. Encoding encoding = GetEncoding();
  589. SendEvent?.Invoke(this.Id, client, send);
  590. await TcpService.SendAsync(client.Id, encoding.GetBytes(send));
  591. }
  592. }
  593. catch (Exception ex)
  594. {
  595. LogHelper.WriteLogError("给机器人发送数据时出错", ex);
  596. }
  597. }
  598. /// <summary>
  599. /// 获取编码
  600. /// </summary>
  601. /// <param name="dataEncoding"></param>
  602. /// <returns></returns>
  603. public Encoding GetEncoding()
  604. {
  605. DataEncoding dataEncoding = this.DataEncoding;
  606. Encoding encoding;
  607. if (dataEncoding == DataEncoding.Default)
  608. {
  609. encoding = Encoding.Default;
  610. }
  611. else if (dataEncoding == DataEncoding.ASCII)
  612. {
  613. encoding = Encoding.ASCII;
  614. }
  615. else if (dataEncoding == DataEncoding.UTF7)
  616. {
  617. encoding = Encoding.UTF7;
  618. }
  619. else if (dataEncoding == DataEncoding.UTF8)
  620. {
  621. encoding = Encoding.UTF8;
  622. }
  623. else if (dataEncoding == DataEncoding.UTF32)
  624. {
  625. encoding = Encoding.UTF32;
  626. }
  627. else if (dataEncoding == DataEncoding.Unicode)
  628. {
  629. encoding = Encoding.Unicode;
  630. }
  631. else if (dataEncoding == DataEncoding.BigEndianUnicode)
  632. {
  633. encoding = Encoding.BigEndianUnicode;
  634. }
  635. else if (dataEncoding == DataEncoding.GB2312)
  636. {
  637. encoding = Encoding.GetEncoding("gb2312");
  638. }
  639. else
  640. {
  641. encoding = Encoding.Default;
  642. }
  643. return encoding;
  644. }
  645. #endregion
  646. #region 属性通知
  647. /// <summary>
  648. /// Occurs when a property value changes.
  649. /// </summary>
  650. public event PropertyChangedEventHandler PropertyChanged;
  651. /// <summary>
  652. /// Checks if a property already matches a desired value. Sets the property and
  653. /// notifies listeners only when necessary.
  654. /// </summary>
  655. /// <typeparam name="T">Type of the property.</typeparam>
  656. /// <param name="storage">Reference to a property with both getter and setter.</param>
  657. /// <param name="value">Desired value for the property.</param>
  658. /// <param name="propertyName">Name of the property used to notify listeners. This
  659. /// value is optional and can be provided automatically when invoked from compilers that
  660. /// support CallerMemberName.</param>
  661. /// <returns>True if the value was changed, false if the existing value matched the
  662. /// desired value.</returns>
  663. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  664. {
  665. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  666. storage = value;
  667. RaisePropertyChanged(propertyName);
  668. return true;
  669. }
  670. /// <summary>
  671. /// Checks if a property already matches a desired value. Sets the property and
  672. /// notifies listeners only when necessary.
  673. /// </summary>
  674. /// <typeparam name="T">Type of the property.</typeparam>
  675. /// <param name="storage">Reference to a property with both getter and setter.</param>
  676. /// <param name="value">Desired value for the property.</param>
  677. /// <param name="propertyName">Name of the property used to notify listeners. This
  678. /// value is optional and can be provided automatically when invoked from compilers that
  679. /// support CallerMemberName.</param>
  680. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  681. /// <returns>True if the value was changed, false if the existing value matched the
  682. /// desired value.</returns>
  683. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  684. {
  685. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  686. storage = value;
  687. onChanged?.Invoke();
  688. RaisePropertyChanged(propertyName);
  689. return true;
  690. }
  691. /// <summary>
  692. /// Raises this object's PropertyChanged event.
  693. /// </summary>
  694. /// <param name="propertyName">Name of the property used to notify listeners. This
  695. /// value is optional and can be provided automatically when invoked from compilers
  696. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  697. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  698. {
  699. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  700. }
  701. /// <summary>
  702. /// Raises this object's PropertyChanged event.
  703. /// </summary>
  704. /// <param name="args">The PropertyChangedEventArgs</param>
  705. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  706. {
  707. PropertyChanged?.Invoke(this, args);
  708. }
  709. public List<DataValue> GetNodesValue()
  710. {
  711. throw new NotImplementedException();
  712. }
  713. #endregion
  714. }
  715. }