using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace JinYuan.Models
{
///
/// 通信设备实体类
///
public class Device
{
///
/// IP地址
///
public string IPAddress { get; set; }
///
/// 端口
///
public int Port { get; set; }
///
/// PLC类型
///
public string PLCType { get; set; }
///
/// 当前设备的连接状态
///
public bool IsConnected { get; set; }
///
/// 是否是首次连接
///
public bool IsFirstConnect { get; set; } = true;
///
/// 重连周期
///
public int ReConnectTime { get; set; } = 2000;
///
/// 休眠时间
///
public int SleepTime { get; set; } = 10;
///
/// 大小端
///
[JsonIgnore]
public int DataFormat { get; set; } = 0;
///
/// 出错次数
///
public int ErrorTimes { get; set; }
///
/// 容错次数
///
public int AllowErrorTimes { get; set; } = 3;
///
/// 多线程取消源标志位
///
public CancellationTokenSource Cts { get; set; }
///
/// 计时器
///
public Stopwatch Stopwatch { get; set; }
///
/// 所有归档变量的集合
///
public List StoreVarList { get; set; } = new List();
///
/// 按工位读取
///
public List WorkVarList { get; set; } = new List();
///
/// 通信组集合
///
public List GroupList { get; set; } = new List();
///
/// 所有变量的集合
///
public List VarList { get; set; } = new List();
///
/// 所有变量名称与值的集合
///
public Dictionary CurrentValue { get; set; } = new Dictionary();
///
/// 定义报警触发和消除事件
///
public event Action AlarmTrigEvent;
///
/// 变量初始化
///
public void Init()
{
foreach (var gp in GroupList)
{
if (gp.IsTiggerRead)
{
WorkVarList.Add(gp);
}
foreach (var variable in gp.VarList)
{
if (!gp.IsTiggerRead)
{
VarList.Add(variable);
if (CurrentValue.ContainsKey(variable.VarName))
{
CurrentValue[variable.VarName] = "NA";
}
else
{
CurrentValue.Add(variable.VarName, "NA");
}
}
}
}
}
///
/// 索引器,通过键名获取到数据
///
///
///
[JsonIgnore]
public object this[string key]
{
get
{
if (CurrentValue.ContainsKey(key))
{
return CurrentValue[key];
}
else
{
return null;
}
}
}
///
/// 实时更新
///
///
public void Update(Variable variable)
{
//更新处理
if (CurrentValue.ContainsKey(variable.VarName))
{
CurrentValue[variable.VarName] = variable.VarValue;
}
else
{
CurrentValue.Add(variable.VarName, variable.VarValue);
}
//报警处理
if (variable.HighAlarmEnable || variable.LowAlarmEnable)
{
float compareValue = 0.0f;
if (variable.DataType.ToLower() == "bool")
{
compareValue = Convert.ToBoolean(variable.VarValue) ? 1.0f : 0.0f;
}
else
{
compareValue = Convert.ToSingle(variable.VarValue);
}
int compareResult = 0;
//高报警时,触发事件
if (variable.HighAlarmEnable)
{
compareResult = Compare(compareValue, variable.HighAlarmValue, variable.HighAlarmCacheValue, true);
if (compareResult != 0)
{
//通过事件做通知
OnAlarmAckEvent(variable, new AlarmEventArgs()
{
Name = variable.VarName,
CurrentValue = variable.VarValue.ToString(),
SetValue = variable.HighAlarmValue.ToString(),
AlarmNote = variable.HighAlarmNote,
IsTrigger = compareResult == 1
});
}
variable.HighAlarmCacheValue = compareValue;
}
else
{
compareResult = Compare(compareValue, variable.LowAlarmValue, variable.LowAlarmCacheValue, false);
if (compareResult != 0)
{
//通过事件做通知
OnAlarmAckEvent(variable, new AlarmEventArgs()
{
Name = variable.VarName,
CurrentValue = variable.VarValue.ToString(),
SetValue = variable.LowAlarmValue.ToString(),
AlarmNote = variable.LowAlarmNote,
IsTrigger = compareResult == 1
});
}
variable.LowAlarmCacheValue = compareValue;
}
}
}
///
/// 报警触发的事件
///
//public event AlarmTriggerEventDelegate AlarmTriggerEvent;
///
/// 触发事件
///
///
///
private void OnAlarmAckEvent(object sender, AlarmEventArgs e)
{
//AlarmTriggerEvent?.Invoke(sender, e);
}
///
/// 报警比较
///
/// 当前值
/// 设置值
/// 缓存值
/// 上升沿还是下降沿
/// 比较结果,1表示触发,-1表示消除,0表示无变化
private int Compare(float current, float set, float last, bool isPositive)
{
if (isPositive)
{
if (current >= set && last < set)
{
return 1;
}
if (current < set && last >= set)
{
return -1;
}
}
else
{
if (current <= set && last > set)
{
return 1;
}
if (current > set && last <= set)
{
return -1;
}
}
return 0;
}
}
public class AlarmEventArgs : EventArgs
{
///
/// 变量名称
///
public string Name { get; set; }
///
/// 当前值
///
public string CurrentValue { get; set; }
///
/// 报警说明
///
public string AlarmNote { get; set; }
///
/// 报警设定值
///
public string SetValue { get; set; }
///
/// 是否为触发
///
public bool IsTrigger { get; set; }
}
}