You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.0 KiB
C#

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<ServerConnect> lazy = new Lazy<ServerConnect>(() => new ServerConnect());
public static ServerConnect Instance => lazy.Value;
BaseContentServiceImpl baseContentServiceImpl => new BaseContentServiceImpl();
private static List<RFIDTcpClient> TcpClients = new List<RFIDTcpClient>();
/// <summary>
/// 获取配置键值对
/// </summary>
public static Dictionary<string, string> IPConfig
{
get
{
NameValueCollection config = ConfigurationManager.GetSection("IPConfigData") as NameValueCollection;
Dictionary<string, string> result = new Dictionary<string, string>();
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());
}
}
/// <summary>
/// 连接所有服务端
/// </summary>
/// <param name="serverPort"></param>
/// <param name="clientIP"></param>
/// <param name="clientPort"></param>
/// <returns>成功与否结果</returns>
public Dictionary<string, bool> ConnectAllServer(string clientIP, string clientPort, string serverPort)
{
int count = IPs.Length;
Dictionary<string, bool> results = new Dictionary<string, bool>();
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;
}
/// <summary>
/// 暂停所有服务端
/// </summary>
/// <returns>成功与否结果</returns>
public Dictionary<string, bool> ConnectAllStop()
{
int count = IPs.Length;
Dictionary<string, bool> results = new Dictionary<string, bool>();
for (int i = 0; i < count; i++)
{
if (TcpClients[i].State)
{
results.Add(Names[i], TcpClients[i].ClientStop());
}
}
State = false;
return results;
}
/// <summary>
/// 释放所有服务端
/// </summary>
/// <returns>成功与否结果</returns>
public Dictionary<string, bool> ConnectAllDispose()
{
int count = IPs.Length;
Dictionary<string, bool> results = new Dictionary<string, bool>();
for (int i = 0; i < count; i++)
{
if (!TcpClients[i].State)
{
results.Add(Names[i], TcpClients[i].ClientDispose());
}
}
return results;
}
/// <summary>
/// 查找数据表里所有IP
/// </summary>
/// <returns></returns>
public List<string> GetAllIP()
{
var groups = baseContentServiceImpl.GetContentInfos().GroupBy(x => x.IP);
List<string> result = new List<string>();
foreach (var group in groups)
{
result.Add(group.Key);
}
return result;
}
}
}