using MWS.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.Core;
using HslCommunication.ModBus;
using HslCommunication.Profinet.Freedom;
using HslCommunication.Profinet.Siemens;
using MWS.Library.Enums;
using MWS.Library.Common;

namespace MWS.Domain.Dto
{
    public class GatewayClientDto: Gateway
    {
        public GatewayClientDto(int componentId,int gatewayType, string plcModel, string ipaddress = "127.0.0.1", int port = 0, string serialPortName = "", int serialBaudRate = 0, int serialDataBits = 0, int serialParity = 0, int serialStopBits = 0)
        {
            //ModbusTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["ModbusTimeOut"]);
            //PLCTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["PLCTimeOut"]);
            Id = componentId;
            if (ModbusTimeOut == 0)
            {
                ModbusTimeOut = 500;
            }

            if (PLCTimeOut == 0)
            {
                PLCTimeOut = 1000;
            }



            this.Type = (GatewayType)gatewayType;
            try
            {
                switch (Type)
                {
                    // PLC
                    case Library.Enums.GatewayType.SiemensS7PLC:
                        SiemensPLCS siemensPLC = plcModel.ToEnum<SiemensPLCS>();
                        SiemensS7Client = new SiemensS7Net(siemensPLC, ipaddress)
                        {
                            ConnectTimeOut = PLCTimeOut,
                            ReceiveTimeOut = PLCTimeOut,
                            Port = (port == 0 ? 102 : port)
                        };
                        ConnResult = SiemensS7Client.ConnectServer();


                        break;
                    case Library.Enums.GatewayType.ModbusTcp:
                        ModbusTcpClient = new ModbusTcpNet(ipaddress, (port == 0 ? 502 : port))
                        {
                            ConnectTimeOut = ModbusTimeOut,
                            ReceiveTimeOut = ModbusTimeOut,
                            IsStringReverse = true,
                            DataFormat = DataFormat.CDAB,
                            Port = (port == 0 ? 502 : port)
                            //DataFormat = DataFormat.DCBA

                            //AddressStartWithZero = false //默认为true，表示modbus从0位开始读，为false代办从1位开始读，如果从1位置开始读，记得地址表+1
                        };
                        ConnResult = ModbusTcpClient.ConnectServer();
                        //ConnResult = new OperateResult() { IsSuccess = true };
                        break;
                   
                }
                if (ConnResult.IsSuccess)
                {
                    this.IsOnLine = true;
                }
            }
            catch (Exception ex)
            {
                int a = 1;
                throw;
            }


        }


        public FreedomSerial SerialBase { get; set; }

        public SiemensS7Net SiemensS7Client { get; set; }

        public OperateResult ConnResult { get; set; }

        public ModbusTcpNet ModbusTcpClient { get; set; }
        public GatewayType Type { get; set; }
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnLine { get; set; }
        public int ModbusTimeOut { get; private set; }
        public int PLCTimeOut { get; private set; }
        public ModbusRtu ModbusRtuClient { get; private set; }
    }
}
