20240301_JSEQ_upperpc/JiangsuEarthquakeJM/JiangsuEarthquake/Models/ClientModel.cs
春风过客 d7f51483a7 新增功能:
1 将地震仪数据读取功能单独成一个页面;
2024-05-13 18:51:05 +08:00

214 lines
7.0 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();
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.Connect();
if (IsConnected)
{
_client.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(Sockets sks)
{
if (sks.ex != null)
{
if (sks.ClientDispose == true)
{
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
//SetClientState(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex));
}
else
{
//SetClientState(string.Format("异常消息:{0}\r\n", sks.ex));
}
//timerConnect.Enabled = true;
}
else if (sks.Offset == 0)
{
//客户端主动下线
// SetClientState("客户端下线.!");
}
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("DZY_Client_IP1") && SI.Port == int.Parse(Tools.GetAppSetting("DZY_Client_Port1")))
{
stationName = 1;
seismographModel.ParsingData(new List<byte>(buffer), stationName);
}
else if (SI.IP == Tools.GetAppSetting("DZY_Client_IP2") && SI.Port == int.Parse(Tools.GetAppSetting("DZY_Client_Port2")))
{
stationName = 2;
seismographModel.ParsingData(new List<byte>(buffer), stationName);
}
}
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();
//等待2秒钟
Thread.Sleep(2000);
//tools.Logging("心跳包超时,开始初始化并重新链接!");
//重新链接
while (!DoConnect(SI))
{
//tools.Logging("心跳包超时,重新链接失败!");
Thread.Sleep(30000);
}
//tools.Logging("心跳包超时,重新链接成功!");
});
}
}
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(); }
}
}
}