TcpClientSendViewModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Prism.Mvvm;
  6. using System.Threading.Tasks;
  7. using Prism.Commands;
  8. using CommonUtils.Tool;
  9. using Microsoft.Xaml.Behaviors.Layout;
  10. using System.ComponentModel.DataAnnotations;
  11. using System.Collections.ObjectModel;
  12. using TcpClientModule.Model;
  13. using CommonUtils.Tcp.TcpSocket;
  14. using CommonUtils.ViewModelBase;
  15. using CommonUtils.Log4;
  16. using static CommonUtils.Enums.Enums;
  17. using CommonUtils.内部存储;
  18. namespace TcpClientModule.ViewModels
  19. {
  20. public class TcpClientSendViewModel:BaseViewModel
  21. {
  22. private ObservableCollection<TcpClientModel> tcpClientModels =new ObservableCollection<TcpClientModel>();
  23. public ObservableCollection<TcpClientModel> TcpClientModels
  24. {
  25. get
  26. {
  27. for ( int i = 0; i < tcpClientModels.Count; i++ )
  28. {
  29. if ( tcpClientModels[ i ].Ipstring ==Tcp_SendModel.Tcp_ClientModel.Ipstring &&
  30. tcpClientModels[ i ].Port == Tcp_SendModel.Tcp_ClientModel.Port )
  31. {
  32. Tcp_SendModel.Tcp_ClientModel = tcpClientModels[ i ];
  33. }
  34. }
  35. return tcpClientModels;
  36. }
  37. set
  38. {
  39. SetProperty(ref tcpClientModels, value);
  40. }
  41. }
  42. private List<YesNO> yesNolist = new List<YesNO>(){ YesNO.是, YesNO.否 };
  43. private string inputstr;
  44. public List<string> StorageTypes = InternalStorage.StorageTypes;
  45. public List<string> StorageIndexs = InternalStorage.StorageIndexs;
  46. public string Inputstr { get => inputstr; set { SetProperty(ref inputstr, value); } }
  47. private DelegateCommand _TcpSendOpencommand;
  48. public DelegateCommand TcpSendOpencommand =>
  49. _TcpSendOpencommand ?? ( _TcpSendOpencommand = new DelegateCommand(TcpSendCommand) );
  50. public List<YesNO> YesNolist { get => yesNolist; set => yesNolist = value ; }
  51. public TcpSendModel Tcp_SendModel { get => _TcpSendModel; set { SetProperty(ref _TcpSendModel, value); } }
  52. private TcpSendModel _TcpSendModel=new TcpSendModel();
  53. public TcpClientSendViewModel()
  54. {
  55. TcpClientViewModel.ItemAdded += TcpClientConfig_ItemAdded;
  56. TcpClientViewModel.ItemRemoved += TcpClientConfig_ItemRemoved;
  57. }
  58. private void TcpClientConfig_ItemRemoved(ObservableCollection<TcpClientModel> models)
  59. {
  60. TcpClientModels=models;
  61. }
  62. private void TcpClientConfig_ItemAdded(ObservableCollection<TcpClientModel> models)
  63. {
  64. TcpClientModels = models;
  65. }
  66. public void TcpSendCommand()
  67. {
  68. try
  69. {
  70. if ( TcpClientModels.Count != 0 )
  71. {
  72. HexUtils utils= new HexUtils();
  73. if (Tcp_SendModel.StorageType != "无" && Tcp_SendModel.StorageIndex != "无")
  74. {
  75. inputstr = InternalStorage.GetValue(Tcp_SendModel.StorageType+":"+ Tcp_SendModel.StorageIndex).ToString();
  76. }
  77. byte[]bytes=new byte[0];
  78. if ( Tcp_SendModel.Tcp_ClientModel.socketClient == null )
  79. {
  80. for ( int i = 0; i < TcpClientModels.Count; i++ )
  81. {
  82. if ( TcpClientModels[ i ].Ipstring == Tcp_SendModel.Tcp_ClientModel.Ipstring && TcpClientModels[ i ].Port == Tcp_SendModel.Tcp_ClientModel.Port )
  83. {
  84. if ( TcpClientModels[ i ].socketClient == null )
  85. {
  86. TcpClientModels[ i ].socketClient = new TcpSocketClient();
  87. TcpClientModels[ i ].socketClient.Connect(TcpClientModels[ i ].Ipstring, TcpClientModels[ i ].Port);
  88. }
  89. Tcp_SendModel.Tcp_ClientModel.socketClient = TcpClientModels[ i ].socketClient;
  90. }
  91. }
  92. }
  93. if ( Tcp_SendModel.Tcp_ClientModel.socketClient.Socket==null||!Tcp_SendModel.Tcp_ClientModel.socketClient.Socket.Connected )
  94. {
  95. Tcp_SendModel.Tcp_ClientModel.socketClient.Connect(Tcp_SendModel.Tcp_ClientModel.Ipstring, Tcp_SendModel.Tcp_ClientModel.Port);
  96. }
  97. if ( Tcp_SendModel.Tcp_ClientModel.socketClient.Socket.Connected )
  98. {
  99. if (Tcp_SendModel.IsHex == YesNO.是)
  100. {
  101. bytes = utils.HexToByte(inputstr);
  102. }
  103. else
  104. {
  105. bytes = System.Text.Encoding.GetEncoding(Tcp_SendModel.SendEncoding.BodyName.ToLower()).GetBytes(inputstr);
  106. }
  107. TcpClientModels[ 0 ].socketClient.Send(bytes);
  108. }
  109. }
  110. }
  111. catch ( Exception e )
  112. {
  113. LogHelper.Error("Tcp发送运行错误 " + e.Message);
  114. isErr = true;
  115. }
  116. }
  117. }
  118. }