GlobalProperties是一个全局的数据,类似CoreData:
public class GlobalProperties{public static Device Device { set; get; }public static Action<int, string> AddLog;public static SysAdmin CurrentAdmin;public static ModbusTCP Modbus { get; set; }///<summary>///确定大小端///</summary>public static DataFormat dataFormat = DataFormat.ABCD;/// <summary>///通过标签名称找对应的Variable对象///</summary>/// <param name="varName"></param>/// <returns></returns>private static Variable FindVariable(string varName){foreach (var item in Device.GroupList){var res = item.VarList.Find(c => c.VarName == varName);if (res != null){return res;}}return null;}public static bool CommonWrite(string varName, string varValue){var variable = FindVariable(varName);//第一步:先找到变量对象if (variable != null){// 第二步: 获取变量类型DataType dataType = (DataType)Enum.Parse(typeof(DataType), variable.DataType, true);// 第三步: 获取写入数据var result = MigrationLib.SetMigrationValue(varValue, dataType, variable.Scale.ToString(), variable.Offset.ToString());if (result.IsSuccess){// 第四步:写入数据switch (dataType){case DataType.Bool: return Modbus.PreSetSingleColls(variable.Start, Convert.ToBoolean(result.Content));/*return Modbus.PreSetSingleRegister(variable.Start, Convert.ToInt16(result.Content));*/case DataType.Short:return Modbus.PreSetSingleRegister(variable.Start, Convert.ToInt16(result.Content));case DataType.UShort:return Modbus.PreSetSingleRegister(variable.Start, Convert.ToUInt16(result.Content));case DataType.Int:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromInt(Convert.ToInt32(result.Content), dataFormat));case DataType.UInt:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromUInt(Convert.ToUInt32(result.Content), dataFormat));case DataType.Float:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromFloat(Convert.ToSingle(result.Content), dataFormat));case DataType.Double:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromDouble(Convert.ToDouble(result.Content), dataFormat));case DataType.Long:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromLong(Convert.ToInt64(result.Content), dataFormat));case DataType.ULong:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromULong(Convert.ToUInt64(result.Content), dataFormat));case DataType.String:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromString(result.Content, Encoding.ASCII));case DataType.ByteArray:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromHexString(result.Content));case DataType.HexString:return Modbus.PreSetMultiRegister(variable.Start, ByteArrayLib.GetByteArrayFromHexString(result.Content));default:break;}}}return false;}}
实体设备类,含有报警的检查和恢复触发:
public class Device
{/// <summary>/// ip地址/// </summary>public string IPAddress { get; set; }/// <summary>/// 端口号/// </summary>public int Port { get; set; }/// <summary>/// 当前配方/// </summary>public string CurrentRecipe { get; set; }/// <summary>/// 通信类集合/// </summary>public List<Group> GroupList { get; set; }/// <summary>/// 通信状态/// </summary>public bool IsConnected { get; set; }/// <summary>/// 重连时间/// </summary>public int ReConnectTime { get; set; } = 100;/// <summary>/// 重连标志/// </summary>public bool ReConnect { get; set; }/// <summary>/// slave实时读取数据/// </summary>public Dictionary<string, object> CurrentValue = new Dictionary<string, object>();/// <summary>/// 定义报警触发事件和消除事件/// </summary>public event Action<bool, Variable> AlarmTrigEvent;public void UpdateVariable(Variable variable){if (CurrentValue.ContainsKey(variable.VarName)){CurrentValue[variable.VarName] = variable.VarValue;}else{CurrentValue.Add(variable.VarName, variable.VarValue);}// 报警检测CheckAlarm(variable);}private void CheckAlarm(Variable variable){// 上升沿报警检测 只有相关报警变量才为Tif (variable.PosAlarm){bool currentValue = variable.VarValue.ToString() == "1"?true:false;if (variable.PosCacheValue == false && currentValue == true){// 检测到了报警触发 AlarmTrigEvent?.Invoke(true, variable);}if (variable.PosCacheValue == true && currentValue == false){// 检测到了报警消除 AlarmTrigEvent?.Invoke(false, variable);}variable.PosCacheValue = currentValue;}// 下降沿报警检测if (variable.NegAlarm){bool currentValue = variable.VarValue.ToString() == "True";if (variable.NegCacheValue == true & currentValue == false){// 检测到了报警触发AlarmTrigEvent?.Invoke(true, variable);}if (variable.NegCacheValue == false & currentValue == true){// 检测到了报警消除AlarmTrigEvent?.Invoke(false, variable);}variable.NegCacheValue = currentValue;}}public object this[string key]{get{if (CurrentValue.ContainsKey(key)){return CurrentValue[key];}else { return null; }}}}