日期:2014-05-18  浏览次数:20885 次

温湿度监控报警程序,设计模式 求指点。
1、程序入口
C# code

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();
        }
    }
}



2、探头对象,拥有DataChange事件,主要负责监控探头当前各项参数
Detector.cs
C# code

    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
    }


3、报警中心,在DataChange事件触发时,交由此类中的Alarm进行处理,负责判断当前传入的探头参数是否应该触发警报,并根据警报开关进行选择性报警。<