using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;
using WCS.BLL.Config;
using WCS.BLL.HardWare;
using WCS.BLL.Tool;
using WCS.DAL.Db;
using WCS.DAL.DbModels;

namespace WCS.BLL.Manager
{
    public static class TCPClientManager
    {
        /// <summary>
        /// 货架类型的TCP连接管理
        /// </summary>
        public static List<TCPClient> TCPClients = new List<TCPClient>();



        //后台启动时给所有板子、警示灯发送复位操作 保持状态一致
        public static void InitStatus()
        {
            Task.Run(() =>
            {
                try
                {
                    Thread.Sleep(1000);
                    //给所有板子发复位
                    TCPClients.ForEach(tcpClient =>
                    {
                        //板子复位
                        new SmartShelfModule()
                        {
                            BoardId = 2047
                        }.Reset(tcpClient);
                        //报警灯复位
                        new WarningLight().CloseLight(tcpClient);
                    });
                }
                catch (Exception ex)
                {
                    Logs.Write($"启动时复位异常，{ex.Message}");
                }
            });
        }

        //后台启动时给所有板子、警示灯发送复位操作 保持状态一致
        public static void InitStatus(TCPClient tcpClient)
        {


            Task.Run(() =>
            {
                try
                {
                    Thread.Sleep(1000);

                    //板子复位
                    new SmartShelfModule()
                    {
                        BoardId = 2047
                    }.Reset(tcpClient);
                    //报警灯复位
                    new WarningLight().CloseLight(tcpClient);
                }
                catch (Exception ex)
                {
                    Logs.Write($"[{tcpClient.RemoteIPHost}]:启动时复位异常，{ex.Message}");
                }
            });
        }
        public static TCPClient GetTCPClientByIPHost(string IpHost)
        {
            return TCPClients.Where(t => t.RemoteIPHost == IpHost).FirstOrDefault();
        }
    }
}
