20240301_JSEQ_upperpc/JiangsuEarthquake_test/JiangsuEarthquake/Models/ClientModel.cs
XuMin 3b6c570800 1 经过电控腔测试和升压站测试;
2 解决了后一个通信连接会影响前一个通信连接的问题;
3 测试过程中存在的问题修改;
2024-09-03 16:30:34 +08:00

230 lines
7.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JiangsuEarthquake.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiangsuEarthquake.Models
{
public class ClientModel : NotifyBase
{
SocketInfo SI = new SocketInfo();
//升压站数据解析
private VolCurCollectorModel volCurCollectorModel { get; set; } = new VolCurCollectorModel();
//地震仪数据解析
private SeismographModel seismographModel { get; set; } = new SeismographModel();
//public Client client { set; get; } = new Client();
SocketHelper.TcpClients client = new SocketHelper.TcpClients();
private bool isConnected;
/// <summary>
/// Socket通信是否连接
/// </summary>
public bool IsConnected
{
get { return isConnected; }
set { isConnected = value; this.DoNotify(); }
}
/// <summary>
/// 打开客户端连接
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool DoConnect(SocketInfo socketInfo)
{
//if (!tools.TelnetPort(socketInfo.IP, socketInfo.Port))
// return false;
SI.IP = socketInfo.IP;
SI.Port = socketInfo.Port;
client.InitSocket(socketInfo.IP, socketInfo.Port); //初始化设备的IP和端口号
IsConnected = client.Start();
if (IsConnected)
{
SocketHelper.pushSockets = new SocketHelper.PushSockets(ReceiveMess);
//tools.Logging("连接客户端成功,IP:" + socketInfo.IP + "端口号:" + socketInfo.Port);
//开始进行心跳包检查
//将是否接收到心跳包判断位 置于false
IsReturnMsg = System.DateTime.Now;
//检测当前连接状态
//CheckConnect();
}
else
{
//tools.Logging("连接客户端失败,IP:" + socketInfo.IP + "端口号:" + socketInfo.Port);
}
return IsConnected;
}
public bool DisConnect()
{
client.Stop();
IsConnected = false;
return IsConnected;
}
private void ReceiveMess(SocketHelper.Sockets sks)
{
if (sks.ex != null)
{
if (sks.ClientDispose == true)
{
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
//SetClientState(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex));
IsConnected = false;
client.Stop();
}
}
else if (sks.Offset == 0)
{
//客户端主动下线
// SetClientState("客户端下线.!");
IsConnected = false;
client.Stop();
}
else
{
byte[] buffer = new byte[sks.Offset];
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
try
{
int stationName = 0;
if (SI.IP == Tools.GetAppSetting("SYZ_Client_IP1") && SI.Port == int.Parse(Tools.GetAppSetting("SYZ_Client_Port1")))
{
stationName = 1;
volCurCollectorModel.ParsingData(new List<byte>(buffer), stationName);
}
else if (SI.IP == Tools.GetAppSetting("SYZ_Client_IP2") && SI.Port == int.Parse(Tools.GetAppSetting("SYZ_Client_Port2")))
{
stationName = 2;
volCurCollectorModel.ParsingData(new List<byte>(buffer), stationName);
}
else if (SI.IP == Tools.GetAppSetting("MainSeis_Client_IP1") && SI.Port == int.Parse(Tools.GetAppSetting("MainSeis_Client_Port1")))
{
stationName = 1;
seismographModel.ParsingData(new List<byte>(buffer), stationName, true);
}
else if (SI.IP == Tools.GetAppSetting("MainSeis_Client_IP2") && SI.Port == int.Parse(Tools.GetAppSetting("MainSeis_Client_Port2")))
{
stationName = 2;
seismographModel.ParsingData(new List<byte>(buffer), stationName, true);
}
else if (SI.IP == Tools.GetAppSetting("BackupSeis_Client_IP1") && SI.Port == int.Parse(Tools.GetAppSetting("BackupSeis_Client_IP1")))
{
stationName = 1;
seismographModel.ParsingData(new List<byte>(buffer), stationName, false);
}
else if (SI.IP == Tools.GetAppSetting("BackupSeis_Client_IP2") && SI.Port == int.Parse(Tools.GetAppSetting("BackupSeis_Client_IP2")))
{
stationName = 2;
seismographModel.ParsingData(new List<byte>(buffer), stationName, false);
}
}
catch (Exception ex)
{
return;
}
}
}
/// <summary>
/// 发送消息
/// </summary>
public bool SendMessage(byte[] SendData)
{
return client.SendData(SendData);
}
//用于记录心跳包的返回时间
public DateTime IsReturnMsg = System.DateTime.Now;
//用于记录当前是否在检查连接状态 初始值为false
bool IsCheckConnect = false;
/// <summary>
/// 用于检查当前连接状态
/// </summary>
private async void CheckConnect()
{
await Task.Run(() =>
{
//将检查连接状态置为开
IsCheckConnect = true;
//连接成功等待10秒后 发送心跳包
Thread.Sleep(10000);
//tools.Logging("开始发送心跳包");
while ((System.DateTime.Now - IsReturnMsg).TotalSeconds < 150)
{
//发送一次Get
SendMessage(new byte[] { (byte)'G', (byte)'E', (byte)'T' });
//tools.Logging("发送一次心跳包GET,上一次收到心跳包的时间差为" + (System.DateTime.Now - IsReturnMsg).TotalSeconds + "秒");
//等待30秒钟
Thread.Sleep(30000);
}
//tools.Logging("心跳包超时主动断开链接停止发送心跳包GET");
//先断开链接
DisConnect();
IsConnected = false;
//等待2秒钟
Thread.Sleep(2000);
//tools.Logging("心跳包超时,开始初始化并重新链接!");
//重新链接
while (!DoConnect(SI))
{
//tools.Logging("心跳包超时,重新链接失败!");
Thread.Sleep(30000);
IsConnected = false;
}
//tools.Logging("心跳包超时,重新链接成功!");
IsConnected = true;
});
}
}
public class SocketInfo : NotifyBase
{
private string ip;
/// <summary>
/// IP
/// </summary>
public string IP
{
get { return ip; }
set { ip = value; this.DoNotify(); }
}
private int port;
/// <summary>
/// 端口号
/// </summary>
public int Port
{
get { return port; }
set { port = value; this.DoNotify(); }
}
}
}