TcpClientCommunication.cs 3.1 KB
using DataAcquisition.Models;
using HslCommunication;
using System.Net.Sockets;
using System.Text;

namespace DataAcquisition.Common.Communications
{
    public class TcpClientCommunication : ICommunication
    {
        public int CommunicationId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public string IpAddress => _ipAddress;

        protected TcpClient tcpClient = new();

        protected string _ipAddress = null!;

        protected int _port;

        protected byte[] _data = new byte[1024 * 8];

        protected TcpClientCommunication() { }

        public TcpClientCommunication(int communicationId, string ip, int port) : this()
        {
            CommunicationId = communicationId;
            _ipAddress = ip;
            _port = port;
        }

        public OperateResult ConnectClose()
        {
            try
            {
                tcpClient.Close();
                return OperateResult.CreateSuccessResult();
            }
            catch (Exception ex)
            {
                return new OperateResult
                {
                    ErrorCode = 500,
                    IsSuccess = false,
                    Message = ex.Message,
                };
            }
        }

        public OperateResult ConnectServer()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(_ipAddress, _port);
                Task.Run(async () =>
                {
                    try
                    {
                        while (tcpClient.Connected)
                        {
                            var stream = tcpClient.GetStream();
                            var buffer = new byte[_data.Length];
                            stream.Read(buffer);
                            var bufferString = Encoding.Default.GetString(buffer).TrimEnd('\0');
                            if (!string.IsNullOrWhiteSpace(bufferString))
                            {
                                _data = buffer;
                            }
                            await Task.Delay(100);
                        }
                    }
                    catch (Exception) { }
                });
                return OperateResult.CreateSuccessResult();
            }
            catch (Exception ex)
            {
                return new OperateResult
                {
                    ErrorCode = 500,
                    IsSuccess = false,
                    Message = ex.Message,
                };
            }
        }

        public virtual void Read(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            throw new NotImplementedException();
        }

        public void Read(EquipmentProperty equipmentProperty)
        {
            Read([equipmentProperty]);
        }

        public virtual void Write(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            throw new NotImplementedException();
        }

        public void Write(EquipmentProperty equipmentProperty)
        {
            Write([equipmentProperty]);
        }
    }
}