using HslCommunication.ModBus;
using MWS.Library;
using MWS.Library.Common;
using System;
using System.Configuration;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;

namespace MWS.Domain.Common
{
    public class ModbusServerHelper
    {
        public static ModbusTcpServer StartModbusServer()
        {
            ModbusTcpServer busTcpServer = null;
            int.TryParse(ConfigurationManager.AppSettings["slave"], out int slave);
            var portName1 = ConfigurationManager.AppSettings["modbusServerPort"];
            var portName = ConfigurationManager.AppSettings["portName"];
            int.TryParse(ConfigurationManager.AppSettings["baudRate"], out int baudRate);
            int.TryParse(ConfigurationManager.AppSettings["dataBits"], out int dataBits);
            Enum.TryParse(ConfigurationManager.AppSettings["stopBits"], out StopBits stopBits);
            Enum.TryParse(ConfigurationManager.AppSettings["parity"], out Parity parity);

            SerialPort _tempPort = new SerialPort(portName);
            try
            {
                _tempPort.Open();
            }
            catch {
                LogHelper.WriteLog("打开串口 " + _tempPort.PortName + " 失败！");
                return null;
            }
            if (_tempPort.IsOpen)
            {
                _tempPort.Close();
            }
            else
            {
                portName = null;
                string[] ports = SerialPort.GetPortNames();
                foreach (var port in ports)
                {
                    _tempPort = new SerialPort(portName);
                    try
                    {
                        _tempPort.Open();
                    }
                    catch (Exception e)
                    {
                        LogHelper.WriteLog(e.Message);
                        return null;
                    }
                    if (_tempPort.IsOpen)
                    {
                        _tempPort.Close();
                        portName = port;
                        Tool.UpdateAppConfig("portName", _tempPort.PortName);
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            if (portName != null)
            {
                //busTcpServer = new HslCommunication.ModBus.ModbusTcpServer();
                //busTcpServer.LogNet = new HslCommunication.LogNet.LogNetSingle("logs.txt");
                ////busTcpServer.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile;
                //busTcpServer.ServerStart(port);
                //busTcpServer.Station = 1;
                busTcpServer = new ModbusTcpServer
                {
                    LogNet = new HslCommunication.LogNet.LogNetSingle(Path.Combine(Application.StartupPath, "logs.txt"))        // 配置日志信息
                };
                busTcpServer.ServerStart(Convert.ToInt32(portName1));
                busTcpServer.Station = slave;
                busTcpServer.StartModbusRtu(sp =>
                {
                    sp.PortName = portName;
                    sp.BaudRate = baudRate;
                    sp.DataBits = dataBits;
                    sp.StopBits = stopBits;
                    sp.Parity = parity;
                });
            }

            return busTcpServer;
        }
    }
}
