You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
7.1 KiB
205 lines
7.1 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.ComponentModel;
|
||
|
using System.Data;
|
||
|
using System.Diagnostics;
|
||
|
using System.Linq;
|
||
|
using System.ServiceProcess;
|
||
|
using System.Text;
|
||
|
using System.Collections;
|
||
|
using QMFrameWork.Log;
|
||
|
|
||
|
namespace EquipmentAlarmMonitor
|
||
|
{
|
||
|
public partial class AlarmMonitor : ServiceBase
|
||
|
{
|
||
|
Dictionary<int,QMAPP.FJC.Entity.Equipment.AlarmTable> alarms;
|
||
|
Dictionary<int, OPCAutomation.OPCItem> opcitems;
|
||
|
List<OPCAutomation.OPCGroup> opcgroups;
|
||
|
OPCAutomation.OPCServerClass opcServer;
|
||
|
|
||
|
System.Threading.Timer timer;
|
||
|
|
||
|
int OPCUpdateRate = 1000;
|
||
|
int TimerPeriod = 120000;
|
||
|
|
||
|
const int TimerRefresh_TransactionID = 1000001;
|
||
|
|
||
|
public AlarmMonitor()
|
||
|
{
|
||
|
alarms = new Dictionary<int, QMAPP.FJC.Entity.Equipment.AlarmTable>();
|
||
|
opcitems = new Dictionary<int, OPCAutomation.OPCItem>();
|
||
|
opcgroups = new List<OPCAutomation.OPCGroup>();
|
||
|
timer = new System.Threading.Timer(TimerTick);
|
||
|
|
||
|
string strOPCUpdateRate = System.Configuration.ConfigurationManager.AppSettings["OPCUpdateRate"] + "";
|
||
|
string strTimerPeriod = System.Configuration.ConfigurationManager.AppSettings["TimerPeriod"] + "";
|
||
|
|
||
|
int iOPCUpdateRate=0;
|
||
|
if (int.TryParse(strOPCUpdateRate, out iOPCUpdateRate))
|
||
|
{
|
||
|
OPCUpdateRate = iOPCUpdateRate;
|
||
|
}
|
||
|
int iTimerPeriod = 0;
|
||
|
if(int.TryParse(strTimerPeriod,out iTimerPeriod))
|
||
|
{
|
||
|
TimerPeriod = iTimerPeriod;
|
||
|
}
|
||
|
|
||
|
InitializeComponent();
|
||
|
}
|
||
|
protected override void OnStart(string[] args)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
alarms.Clear();
|
||
|
opcitems.Clear();
|
||
|
opcgroups.Clear();
|
||
|
|
||
|
int ClientHandle = 1;
|
||
|
opcServer = new OPCAutomation.OPCServerClass();
|
||
|
string progid = System.Configuration.ConfigurationManager.AppSettings["OPCProgID"] + "";
|
||
|
string node = System.Configuration.ConfigurationManager.AppSettings["OPCNode"] + "";
|
||
|
opcServer.Connect(progid, node);
|
||
|
|
||
|
var alarmtable = new
|
||
|
QMAPP.FJC.BLL.Equipment.AlarmTableBLL().GetAlarmTable();
|
||
|
var eqptgroup = alarmtable.GroupBy(p => p.EQPT_CODE);
|
||
|
foreach (var eqpt in eqptgroup)
|
||
|
{
|
||
|
var group = opcServer.OPCGroups.Add(eqpt.Key);
|
||
|
opcgroups.Add(group);
|
||
|
foreach (var alarm in eqpt)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
var alarmitem = group.OPCItems.AddItem(alarm.OPCITEM_ID, ClientHandle);
|
||
|
opcitems.Add(ClientHandle, alarmitem);
|
||
|
alarms.Add(ClientHandle, alarm);
|
||
|
}
|
||
|
catch(Exception e)
|
||
|
{
|
||
|
LogManager.LogHelper.Error(new LogInfo()
|
||
|
{
|
||
|
Info = "报警监控服务-报警信号位载入失败:" + alarm.OPCITEM_ID
|
||
|
});
|
||
|
}
|
||
|
ClientHandle++;
|
||
|
}
|
||
|
group.IsSubscribed = true;
|
||
|
group.IsActive = true;
|
||
|
//group.AsyncRefresh(
|
||
|
group.UpdateRate = OPCUpdateRate;
|
||
|
group.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(group_DataChange);
|
||
|
}
|
||
|
opcServer.ServerShutDown += new OPCAutomation.DIOPCServerEvent_ServerShutDownEventHandler(opcServer_ServerShutDown);
|
||
|
timer.Change(TimerPeriod, TimerPeriod);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
LogManager.LogHelper.Error(new LogInfo()
|
||
|
{
|
||
|
ErrorInfo = e,
|
||
|
Tag = e.StackTrace,
|
||
|
Info = "报警监控服务-OPC加载失败"
|
||
|
});
|
||
|
throw e;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void opcServer_ServerShutDown(string Reason)
|
||
|
{
|
||
|
LogManager.LogHelper.Error(new LogInfo()
|
||
|
{
|
||
|
ErrorInfo = new Exception("OPC服务被关闭!" + Reason),
|
||
|
//Tag = e.StackTrace,
|
||
|
Info = "报警监控服务-OPC服务被关闭"
|
||
|
});
|
||
|
this.ExitCode = 11;
|
||
|
this.Stop();
|
||
|
|
||
|
}
|
||
|
|
||
|
void group_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
|
||
|
{
|
||
|
if (TransactionID == TimerRefresh_TransactionID)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (int i = 1; i <= NumItems; i++)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
int clienthandle = (int)ClientHandles.GetValue(i);
|
||
|
bool? alarmsignal = ItemValues.GetValue(i) as Nullable<bool>;
|
||
|
if (alarmsignal.HasValue && alarmsignal.Value)
|
||
|
{
|
||
|
var alarm = alarms[clienthandle];
|
||
|
Console.WriteLine(alarm.EQPT_CODE + "\t" + alarm.ALARM_CODE + "\t" + alarm.ALARM_TEXT);
|
||
|
QMAPP.FJC.BLL.Equipment.AlarmTableBLL bll = new QMAPP.FJC.BLL.Equipment.AlarmTableBLL();
|
||
|
bll.SaveAlarm(alarm, DateTime.Now);
|
||
|
}
|
||
|
}
|
||
|
catch(Exception e)
|
||
|
{
|
||
|
LogManager.LogHelper.Error(new LogInfo()
|
||
|
{
|
||
|
ErrorInfo = e,
|
||
|
Tag = e.StackTrace,
|
||
|
Info = "报警监控服务-报警信息获取失败!"
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private void TimerTick(object obj)
|
||
|
{
|
||
|
foreach (var group in opcgroups)
|
||
|
{
|
||
|
int cancelid=0;
|
||
|
group.AsyncRefresh(1, TimerRefresh_TransactionID, out cancelid);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
internal void TestStart()
|
||
|
{
|
||
|
this.OnStart(null);
|
||
|
}
|
||
|
internal void TestStop()
|
||
|
{
|
||
|
OnStop();
|
||
|
}
|
||
|
|
||
|
protected override void OnStop()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (timer != null)
|
||
|
{
|
||
|
timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
|
||
|
}
|
||
|
|
||
|
if (opcServer != null)
|
||
|
{
|
||
|
if (opcServer.ServerState == (int)OPCAutomation.OPCServerState.OPCRunning)
|
||
|
{
|
||
|
opcServer.Disconnect();
|
||
|
}
|
||
|
opcServer = null;
|
||
|
}
|
||
|
}
|
||
|
catch(Exception e)
|
||
|
{
|
||
|
LogManager.LogHelper.Error(new LogInfo()
|
||
|
{
|
||
|
ErrorInfo = e,
|
||
|
Tag = e.StackTrace,
|
||
|
Info = "报警监控服务-服务停止时发生异常!"
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|