using HighWayIot.Repository.service.Impl; using HighWayIot.TouchSocket; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace RFIDSocket { public class ServerConnect { private static readonly Lazy lazy = new Lazy(() => new ServerConnect()); public static ServerConnect Instance => lazy.Value; BaseContentServiceImpl baseContentServiceImpl => new BaseContentServiceImpl(); private static List TcpClients = new List(); /// /// 获取配置键值对 /// public static Dictionary IPConfig { get { NameValueCollection config = ConfigurationManager.GetSection("IPConfigData") as NameValueCollection; Dictionary result = new Dictionary(); foreach (string key in config.AllKeys) { result.Add(key, config[key]); } return result; } } public bool State = false; public static string[] IPs = IPConfig.Values.ToArray(); public static string[] Names = IPConfig.Keys.ToArray(); public ServerConnect() { for(int i = 0; i < IPs.Length; i++) { TcpClients.Add(new RFIDTcpClient()); } } /// /// 连接所有服务端 /// /// /// /// /// 成功与否结果 public Dictionary ConnectAllServer(string clientIP, string clientPort, string serverPort) { int count = IPs.Length; Dictionary results = new Dictionary(); for (int i = 0; i < count; i++) { if (!TcpClients[i].State) { results.Add(Names[i], TcpClients[i].ClientStart(clientIP, clientPort, IPs[i], serverPort)); } } State = true; return results; } /// /// 暂停所有服务端 /// /// 成功与否结果 public Dictionary ConnectAllStop() { int count = IPs.Length; Dictionary results = new Dictionary(); for (int i = 0; i < count; i++) { if (TcpClients[i].State) { results.Add(Names[i], TcpClients[i].ClientStop()); } } State = false; return results; } /// /// 释放所有服务端 /// /// 成功与否结果 public Dictionary ConnectAllDispose() { int count = IPs.Length; Dictionary results = new Dictionary(); for (int i = 0; i < count; i++) { if (!TcpClients[i].State) { results.Add(Names[i], TcpClients[i].ClientDispose()); } } return results; } /// /// 查找数据表里所有IP /// /// public List GetAllIP() { var groups = baseContentServiceImpl.GetContentInfos().GroupBy(x => x.IP); List result = new List(); foreach (var group in groups) { result.Add(group.Key); } return result; } } }