using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper
{
public class AlarmSaveHelper
{
///
/// 报警保存事件定义
///
public static event EventHandler OnAlarmSave;
public static event EventHandler OnAlarmSaveExtend;
public static object sender = null;
public static object senderExtend = null;
private static int WordLength = 16; //定义字长
///
/// 保存报警信息
///
public static void Save()
{
List _alarmInfoList = Cache.CacheHelper.AlarmInfoList;
List _alarmBlockWordList = Cache.CacheHelper.BlockWordList;
#region 读取PLC的报警数据
foreach (Entity.AlarmInfo info in _alarmInfoList)
{
Int16[] buff = null;
if (BasePlcHelper.Instance.PlcRead(info.AlarmPLC, info.AlarmBlock, info.Start, info.Length, out buff))
{
foreach(Entity.SimpleAlarmInfo sai in _alarmBlockWordList)
{
if (sai.AlramPLC == info.AlarmPLC && sai.AlarmBlock == info.AlarmBlock) //匹配数据块
{
sai.AlarmData = buff[sai.AlarmAddress - info.Start]; //对起始字偏移,为当前字的报警值
}
}
}
else
{
ICSharpCode.Core.LoggingService.Warn(String.Format("从PLC读取报警数据失败:PLC={0},Block={1},Start={2},Len={3}", info.AlarmPLC, info.AlarmBlock, info.Start, info.Length));
}
}
#endregion
#region 保存报警数据
foreach(Entity.SimpleAlarmInfo sai in _alarmBlockWordList)
{
int[] binaryAlarmData = Mesnac.Basic.DataProcessor.ParseBinaryValue(sai.AlarmData, WordLength);
for (int i = 0; i < WordLength; i++)
{
if (sai.IsSaveAlarm[i] == true && binaryAlarmData[i] == 0)
{
//更新报警状态
Entity.PmtAlarmInfo alarmInfo = Cache.CacheHelper.GetPmtAlarmInfoFromCache(sai.AlramPLC, sai.AlarmBlock, sai.AlarmAddress, i);
if (alarmInfo != null)
{
Alarm.AlarmHelper.UpdateAlarmLogData(alarmInfo.Alarm_ID,DateTime.Now.ToString());
//清除显示的报警列表信息
if(Global.PublicVar.Instance.AlarmStrList.Exists(x => x == alarmInfo.AlarmName))
{
Global.PublicVar.Instance.AlarmStrList.Remove(alarmInfo.AlarmName);
Global.PublicVar.Instance.isFlashFlag = true;
Global.PublicVar.Instance.isFlashFlagExtend = true;
}
}
}
if (binaryAlarmData[i] == 0)
{
sai.IsSaveAlarm[i] = false;
}
if (sai.IsSaveAlarm[i] == false && binaryAlarmData[i] == 1)
{
sai.IsSaveAlarm[i] = true;
//数据保存
Entity.PmtAlarmInfo alarmInfo = Cache.CacheHelper.GetPmtAlarmInfoFromCache(sai.AlramPLC, sai.AlarmBlock, sai.AlarmAddress, i);
if (alarmInfo != null)
{
Entity.LR_Alarmlog alarmLogData = new Entity.LR_Alarmlog();
alarmLogData.Equip_Code = alarmInfo.Equip_Code;
alarmLogData.Alarm_ID = alarmInfo.Alarm_ID;
alarmLogData.Alarm_OccurTime = DateTime.Now.ToString();
alarmLogData.Alarm_ClearTime = String.Empty;
alarmLogData.Alarm_Status = 1;
alarmLogData.Alarm_GUID = Guid.NewGuid().ToString().ToUpper();
Alarm.AlarmHelper.InsertAlarmLogData(alarmLogData);
Global.PublicVar.Instance.AlarmStrList.Add(alarmInfo.AlarmName);
Global.PublicVar.Instance.isFlashFlag = true;
Global.PublicVar.Instance.isFlashFlagExtend = true;
}
}
}
}
#region 触发事件
if (OnAlarmSave != null && Global.PublicVar.Instance.isFlashFlag)
{
OnAlarmSave(sender, System.EventArgs.Empty);
}
if (OnAlarmSaveExtend != null && Global.PublicVar.Instance.isFlashFlagExtend)
{
OnAlarmSaveExtend(senderExtend, System.EventArgs.Empty);
}
#endregion
#endregion
}
}
}