213 lines
6.2 KiB
C#
213 lines
6.2 KiB
C#
using Prism.Mvvm;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Timers;
|
||
|
||
namespace OrpaonEMS.Model.MasterSlave
|
||
{
|
||
/// <summary>
|
||
/// EMS Client连接状态模型
|
||
/// 发送和接受的状态必须是OK的才能运行,否则停机
|
||
/// 连接的状态必须是OK才能运行,否则停机
|
||
/// </summary>
|
||
public class EMSClientConState:BindableBase
|
||
{
|
||
/// <summary>
|
||
/// 声明委托对象-判定跟EMS连接失败时触发事件
|
||
/// </summary>
|
||
public event EventHandler<EMSConErrEventArgs> EMSClientConErrEventHandler;
|
||
|
||
/// <summary>
|
||
/// 周期读取定时器
|
||
/// 周期发送到SignaIR Hub中
|
||
/// </summary>
|
||
private System.Timers.Timer? timer { get; set; }
|
||
|
||
public EMSClientConState()
|
||
{
|
||
timer = new System.Timers.Timer(1000);
|
||
timer.Elapsed -= CycleCheck;
|
||
timer.Elapsed += CycleCheck;
|
||
timer.AutoReset = true;
|
||
timer.Enabled = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 循环判定连接状态
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
private void CycleCheck(object? sender, ElapsedEventArgs e)
|
||
{
|
||
timer.Enabled = false;
|
||
|
||
//连接状态出错,立刻停止
|
||
if (!ClientState)
|
||
{
|
||
Msg = "连接出现错误";
|
||
ConResult = false;
|
||
//EMSClientConErrEventHandler(this, new EMSConErrEventArgs() { Msg = "连接出现错误" });
|
||
timer.Enabled = true;
|
||
return;
|
||
}
|
||
|
||
if (ClientSendState == false || TimeIsOk(ClientSendTime) == false)
|
||
{
|
||
Msg = "发送数据出现错误";
|
||
ConResult = false;
|
||
//EMSClientConErrEventHandler(this, new EMSConErrEventArgs() { Msg = "发送数据出现错误" });
|
||
timer.Enabled = true;
|
||
return;
|
||
}
|
||
|
||
if (ClientRecvState == false || TimeIsOk(ClientRecvTime) == false)
|
||
{
|
||
Msg = "接受数据出现错误";
|
||
ConResult = false;
|
||
//EMSClientConErrEventHandler(this, new EMSConErrEventArgs() { Msg = "接受数据出现错误" });
|
||
timer.Enabled = true;
|
||
return;
|
||
}
|
||
|
||
Msg = "";
|
||
ConResult = true;
|
||
timer.Enabled = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通信的消息
|
||
/// </summary>
|
||
private string Msg { get; set; }=string.Empty;
|
||
|
||
private bool _ConResult = true;
|
||
/// <summary>
|
||
/// 整体的通信状态结果
|
||
/// </summary>
|
||
public bool ConResult
|
||
{
|
||
get { return _ConResult; }
|
||
set
|
||
{
|
||
|
||
//if (_ConResult != value)//触发一次
|
||
//{
|
||
// //暂时的逻辑不需要触发事件了,由外部的函数获取这个状态判断信息
|
||
// //EMSClientConErrEventHandler(this, new EMSConErrEventArgs() { Msg = Msg, Result = value });
|
||
// _ConResult = value;
|
||
//}
|
||
//Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} -通信结果:{value}");
|
||
_ConResult = value;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private bool _ClientState;
|
||
/// <summary>
|
||
/// 通信连接状态
|
||
/// 来自通信的事件触发方法
|
||
/// </summary>
|
||
public bool ClientState
|
||
{
|
||
get { return _ClientState; }
|
||
set { _ClientState = value; }
|
||
}
|
||
|
||
|
||
private bool _ClientSendState;
|
||
/// <summary>
|
||
/// Client 发送数据的状态
|
||
/// </summary>
|
||
public bool ClientSendState
|
||
{
|
||
get { return _ClientSendState; }
|
||
set
|
||
{
|
||
_ClientSendState = value;
|
||
ClientSendTime = DateTime.Now;
|
||
if (value)
|
||
{
|
||
ClientSendStateMsg = "正常";
|
||
}
|
||
else
|
||
{
|
||
ClientSendStateMsg = "失败";
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 发送数据的时间
|
||
/// </summary>
|
||
private DateTime ClientSendTime { get; set; } = DateTime.Now;
|
||
|
||
|
||
private string _ClientSendStateMsg;
|
||
/// <summary>
|
||
/// Client 发送数据的状态 消息
|
||
/// </summary>
|
||
public string ClientSendStateMsg
|
||
{
|
||
get { return _ClientSendStateMsg; }
|
||
set { _ClientSendStateMsg = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
|
||
private bool _ClientRecvState;
|
||
/// <summary>
|
||
/// Client 接受数据的状态
|
||
/// </summary>
|
||
public bool ClientRecvState
|
||
{
|
||
get { return _ClientRecvState; }
|
||
set
|
||
{
|
||
_ClientRecvState = value;
|
||
ClientRecvTime = DateTime.Now;
|
||
if (value)
|
||
{
|
||
ClientRecvStateMsg = "正常";
|
||
}
|
||
else
|
||
{
|
||
ClientRecvStateMsg = "失败";
|
||
}
|
||
}
|
||
}
|
||
|
||
private string _ClientRecvStateMsg;
|
||
/// <summary>
|
||
/// Client 接受数据的状态 消息
|
||
/// </summary>
|
||
public string ClientRecvStateMsg
|
||
{
|
||
get { return _ClientRecvStateMsg; }
|
||
set { _ClientRecvStateMsg = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
///接受数据的时间
|
||
/// </summary>
|
||
private DateTime ClientRecvTime { get; set; } = DateTime.Now;
|
||
|
||
/// <summary>
|
||
/// 判断时间是否OK
|
||
/// 基于现在的时间
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private bool TimeIsOk(DateTime dateTime)
|
||
{
|
||
//大于1秒代表通信失败
|
||
if ((DateTime.Now - dateTime).TotalSeconds > 1.5)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
}
|