110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TemperatureAlarm
{
internal class Program
{
private static void Main(string[] args)
{
IList<Detectors> detectorList = new List<Detectors>();
using (var db = new LGJKEntities())
{
var query = from p in db.Detectors
select p;
detectorList = query.ToList();
foreach (var item in detectorList)
{
Detector d = new Detector(item.ID);
d.DataChange += (new AlarmCenter()).Alarm;
d.DoMonitor();
Console.WriteLine(string.Format("Monitor Start by {0}",item.ID));
}
}
Console.ReadLine();
}
}
}
public class Detector
{
private int _detectorID = 0;
#region 构造函数
public Detector()
{
}
/// <summary>
/// 传入探头ID
/// </summary>
/// <param name="detectorID"></param>
public Detector(int detectorID)
{
this._detectorID = detectorID;
}
#endregion
#region 事件与委托定义
public Action<Object, MonitorEventArgs> DataChange;
/// <summary>
/// MonitorEventArgs类,用于传递参数
/// </summary>
public class MonitorEventArgs : EventArgs
{
public Detectors info;
public MonitorEventArgs(Detectors d)
{
this.info = d;
}
}
#endregion
#region 自定义事件
/// <summary>
/// 数据更新
/// </summary>
/// <param name="e"></param>
protected virtual void OnDataChange(MonitorEventArgs e)
{
if (DataChange != null)
{
DataChange(this, e);
}
}
#endregion
#region 公共方法
public void DoMonitor()
{
if (_detectorID > 0)
{
//开始任务
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 6000;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(GetDetectorInfoByID);
timer.Start();
}
else
{
Exception ex = new Exception("探头ID不正确");
ExceptionSave.Save("Detector.DoMonitor." + _detectorID.ToString(), ex);
}
}
#endregion
#region 私有方法
/// <summary>
/// 获取温湿度信息
/// </summary>
private void GetDetectorInfoByID(object source, ElapsedEventArgs e)
{
try
{
Detectors d = new Detectors();
using (var db = new LGJKEntities())
{
d = db.Detectors.First(p => p.ID == _detectorID);
}
MonitorEventArgs ev = new MonitorEventArgs(d);
OnDataChange(ev);
}
catch (Exception ex)
{
ExceptionSave.Save("Detector.GetDetectorInfoByID." + _detectorID.ToString(), ex);
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TemperatureAlarm
{
public class AlarmCenter
{
public void Alarm(Object sender, Detector.MonitorEventArgs e)
{
Detectors d = e.info;
//总开关
if (d.AlarmSP == "关")
{
return;
}
TAlarm(d);
HAlarm(d);
}
/// <summary>
/// 湿度报警判断
/// </summary>
/// <param name="d"></param>
private void HAlarm(Detectors d)
{
AlarmInfo alarmInfo = new AlarmInfo(Common.FormatDouble(d.Humidity), Common.FormatDouble(d.MinHumidity),
Common.FormatDouble(d.MaxHumidity), d.HumidityAlarm, d.HumidityCondition);
if (alarmInfo.IsAlarm)
{
//短信报警
if (d.SMSAlarm == "开")
{
SMSAlarm sms = new SMSAlarm();
sms.DoAlarm();
}
//设备报警
if (d.DeviceAlarm == "开")
{
DeviceAlarm device = new DeviceAlarm();
device.DoAlarm();
}
//服务器报警
if (d.ServerAlarm == "开")
{
ServerAlarm server = new ServerAlarm();
server.DoAlarm();
}
}
}
/// <summary>
/// 温度报警判断
/// </summary>
/// <param name="d"></param>
private void TAlarm(Detectors d)
{
AlarmInfo alarmInfo = new AlarmInfo(Common.FormatDouble(d.Temperature), Common.FormatDouble(d.MinTemperature),
Common.FormatDouble(d.MaxTemperature), d.TemperatureAlarm, d.TemperatureCondition);
if (alarmInfo.IsAlarm)
{
//短信报警
if (d.SMSAlarm == "开")
{
SMSAlarm sms = new SMSAlarm();
sms.DoAlarm();
}
//设备报警
if (d.DeviceAlarm == "开")
{
DeviceAlarm device = new DeviceAlarm();
device.DoAlarm();
}
//服务器报警
if (d.ServerAlarm == "开")
{
ServerAlarm server = new ServerAlarm();
server.DoAlarm();
}
}
}
}
/// <summary>
/// 短信报警
/// </summary>
public class SMSAlarm
{
public void DoAlarm()
{
}
}
/// <summary>
/// 设备上的警号报警
/// </summary>
public class DeviceAlarm
{
public void DoAlarm()
{
}
}
/// <summary>
/// 服务器上的警号报警
/// </summary>
public class ServerAlarm
{
public void DoAlarm()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TemperatureAlarm
{
/// <summary>
/// 警报类:负责判断传入的参数是否触发警报
/// </summary>
public class AlarmInfo
{
public double MaxValue;
public double MinValue;
public double Value;
public string AlarmSwitch;
public string AlarmCondition;
public int AlarmCode;
public bool IsAlarm;
public AlarmInfo()
{
}
public AlarmInfo(double _val, double _min, double _max, string _switch, string _condition)
{
this.MaxValue = _max;
this.MinValue = _min;
this.Value = _val;
this.AlarmSwitch = _switch;
this.AlarmCondition = _condition;
this.AlarmCode = GetAlarmCode();
this.IsAlarm = CheckAlarm();
}
/// <summary>
/// 判断是否需要报警
/// </summary>
/// <param name="alarmInfo"></param>
/// <returns></returns>
private bool CheckAlarm()
{
int iConditionValue = 0;
if (this.AlarmSwitch == "开")
{
switch (this.AlarmCondition)
{
case "上下都报":
iConditionValue = 3;
break;
case "上报":
iConditionValue = 2;
break;
case "下报":
iConditionValue = 1;
break;
case "不报":
iConditionValue = 0;
break;
default:
iConditionValue = 3;
break;
}
}
return ((this.AlarmCode & iConditionValue) > 0);
}
/// <summary>
/// 计算报警代码:11,10,01
/// 分别代表:上下都报,上报,下报
/// </summary>
/// <returns></returns>
private int GetAlarmCode()
{
int intAlarmCode = 0;
intAlarmCode += CalcValue(this.Value, this.MaxValue, 2);
intAlarmCode += CalcValue(this.MinValue, this.Value, 1);
return intAlarmCode;
}
/// <summary>
/// 当前者大于后者时,返回累加值
/// </summary>
/// <param name="firstNum"></param>
/// <param name="secondNum"></param>
/// <param name="plusValue"></param>
/// <returns></returns>
private int CalcValue(double firstNum, double secondNum, int plusValue)
{
if (firstNum <= secondNum)
{
plusValue = 0;
}
return plusValue;
}
}
}