日期:2014-05-18 浏览次数:20900 次
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 }