123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Prism.Mvvm;
- using System.Threading.Tasks;
- using Prism.Commands;
- using CommonUtils.Tool;
- using Microsoft.Xaml.Behaviors.Layout;
- using System.ComponentModel.DataAnnotations;
- using System.Collections.ObjectModel;
- using TcpClientModule.Model;
- using CommonUtils.Tcp.TcpSocket;
- using CommonUtils.ViewModelBase;
- using CommonUtils.Log4;
- using static CommonUtils.Enums.Enums;
- using CommonUtils.内部存储;
- namespace TcpClientModule.ViewModels
- {
- public class TcpClientSendViewModel:BaseViewModel
- {
- private ObservableCollection<TcpClientModel> tcpClientModels =new ObservableCollection<TcpClientModel>();
- public ObservableCollection<TcpClientModel> TcpClientModels
- {
- get
- {
- for ( int i = 0; i < tcpClientModels.Count; i++ )
- {
- if ( tcpClientModels[ i ].Ipstring ==Tcp_SendModel.Tcp_ClientModel.Ipstring &&
- tcpClientModels[ i ].Port == Tcp_SendModel.Tcp_ClientModel.Port )
- {
- Tcp_SendModel.Tcp_ClientModel = tcpClientModels[ i ];
- }
- }
- return tcpClientModels;
- }
- set
- {
- SetProperty(ref tcpClientModels, value);
- }
- }
- private List<YesNO> yesNolist = new List<YesNO>(){ YesNO.是, YesNO.否 };
- private string inputstr;
- public List<string> StorageTypes = InternalStorage.StorageTypes;
- public List<string> StorageIndexs = InternalStorage.StorageIndexs;
- public string Inputstr { get => inputstr; set { SetProperty(ref inputstr, value); } }
- private DelegateCommand _TcpSendOpencommand;
- public DelegateCommand TcpSendOpencommand =>
- _TcpSendOpencommand ?? ( _TcpSendOpencommand = new DelegateCommand(TcpSendCommand) );
- public List<YesNO> YesNolist { get => yesNolist; set => yesNolist = value ; }
- public TcpSendModel Tcp_SendModel { get => _TcpSendModel; set { SetProperty(ref _TcpSendModel, value); } }
- private TcpSendModel _TcpSendModel=new TcpSendModel();
-
- public TcpClientSendViewModel()
- {
- TcpClientViewModel.ItemAdded += TcpClientConfig_ItemAdded;
- TcpClientViewModel.ItemRemoved += TcpClientConfig_ItemRemoved;
-
- }
- private void TcpClientConfig_ItemRemoved(ObservableCollection<TcpClientModel> models)
- {
- TcpClientModels=models;
- }
- private void TcpClientConfig_ItemAdded(ObservableCollection<TcpClientModel> models)
- {
- TcpClientModels = models;
- }
- public void TcpSendCommand()
- {
- try
- {
- if ( TcpClientModels.Count != 0 )
- {
- HexUtils utils= new HexUtils();
- if (Tcp_SendModel.StorageType != "无" && Tcp_SendModel.StorageIndex != "无")
- {
- inputstr = InternalStorage.GetValue(Tcp_SendModel.StorageType+":"+ Tcp_SendModel.StorageIndex).ToString();
- }
- byte[]bytes=new byte[0];
- if ( Tcp_SendModel.Tcp_ClientModel.socketClient == null )
- {
- for ( int i = 0; i < TcpClientModels.Count; i++ )
- {
- if ( TcpClientModels[ i ].Ipstring == Tcp_SendModel.Tcp_ClientModel.Ipstring && TcpClientModels[ i ].Port == Tcp_SendModel.Tcp_ClientModel.Port )
- {
- if ( TcpClientModels[ i ].socketClient == null )
- {
- TcpClientModels[ i ].socketClient = new TcpSocketClient();
- TcpClientModels[ i ].socketClient.Connect(TcpClientModels[ i ].Ipstring, TcpClientModels[ i ].Port);
- }
- Tcp_SendModel.Tcp_ClientModel.socketClient = TcpClientModels[ i ].socketClient;
- }
- }
- }
- if ( Tcp_SendModel.Tcp_ClientModel.socketClient.Socket==null||!Tcp_SendModel.Tcp_ClientModel.socketClient.Socket.Connected )
- {
- Tcp_SendModel.Tcp_ClientModel.socketClient.Connect(Tcp_SendModel.Tcp_ClientModel.Ipstring, Tcp_SendModel.Tcp_ClientModel.Port);
- }
- if ( Tcp_SendModel.Tcp_ClientModel.socketClient.Socket.Connected )
- {
- if (Tcp_SendModel.IsHex == YesNO.是)
- {
-
- bytes = utils.HexToByte(inputstr);
- }
- else
- {
- bytes = System.Text.Encoding.GetEncoding(Tcp_SendModel.SendEncoding.BodyName.ToLower()).GetBytes(inputstr);
- }
- TcpClientModels[ 0 ].socketClient.Send(bytes);
- }
- }
- }
- catch ( Exception e )
- {
- LogHelper.Error("Tcp发送运行错误 " + e.Message);
- isErr = true;
- }
- }
-
- }
- }
|