WarningSystem.cs 5.39 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server
{
    /// <summary>
    /// 报警管理系统
    /// </summary>
15
    [Obsolete("已经作废,应该使用WarningSystem2")]
潘栩锋's avatar
潘栩锋 committed
16 17 18 19 20 21 22 23
    public class WarningSystem : INotifyPropertyChanged, IWarningService
    {
        #region IWarningService

        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }
24 25 26 27
        /// <summary>
        /// 正在响铃
        /// </summary>
        public bool IsRinging { get; private set; }
潘栩锋's avatar
潘栩锋 committed
28 29 30
        /// <summary>
        /// 当前正在报警的!!!!!!
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
31
        public IBuffer<FlyData_WarningHistory> ReasonList { get; private set; }
潘栩锋's avatar
潘栩锋 committed
32 33 34 35

        /// <summary>
        /// 最近报警列表
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
36
        public IBuffer<FlyData_WarningHistory> NewestList { get; private set; }
潘栩锋's avatar
潘栩锋 committed
37 38 39 40 41 42 43 44 45

        #endregion



        public WarningSystem()
        {
            Enable = true;
            ReasonList = new Buffer<FlyData_WarningHistory>();
潘栩锋's avatar
潘栩锋 committed
46 47 48
        }
        public void Init() 
        {
潘栩锋's avatar
潘栩锋 committed
49 50
            NewestList = new BufferStorage<FlyData_WarningHistory>("warning_newest.csv");
        }
潘栩锋's avatar
潘栩锋 committed
51
        public void Init(IBufferAdd<FlyData_WarningHistory> newestList) 
52 53 54
        {
            NewestList = newestList;
        }
潘栩锋's avatar
潘栩锋 committed
55 56
        public void Reset()
        {
潘栩锋's avatar
潘栩锋 committed
57
            ReasonList.Reset();
58 59 60 61 62 63 64
            IsRinging = false;
        }


        public void Silence()
        {
            IsRinging = false;
潘栩锋's avatar
潘栩锋 committed
65
        }
潘栩锋's avatar
潘栩锋 committed
66

潘栩锋's avatar
潘栩锋 committed
67
        #region IWarningServiceSimple
68 69 70 71
        public void Add(int errcode, string description)
        {
            Add(errcode, description, ERR_STATE.ON, "");
        }
72
        public void Add(int errcode, string description, ERR_STATE state)
潘栩锋's avatar
潘栩锋 committed
73
        {
74 75 76 77 78 79 80 81
            Add(errcode, description, state, "");
        }

        public void Add(int errcode, string description, ERR_STATE state, string accessory)
        {
            if (!Enable)
                return;

潘栩锋's avatar
潘栩锋 committed
82 83 84 85 86 87
            FlyData_WarningHistory reason = new FlyData_WarningHistory();
            reason.Time = DateTime.Now;

            reason.ErrCode = errcode;
            reason.Description = description;
            reason.State = state;
88
            reason.Accessory = accessory;
潘栩锋's avatar
潘栩锋 committed
89 90 91

            FlyData_WarningHistory error = null;
            Buffer<FlyData_WarningHistory> reasonList = ReasonList as Buffer<FlyData_WarningHistory>;
92
            int error_id = 0;
潘栩锋's avatar
潘栩锋 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106
            if (reasonList.Count > 0)
            {
                int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
                if (idx >= 0)
                {
                    error_id = reasonList.GetID(idx);
                    error = reasonList.list[idx];
                }
            }
            switch (state)
            {
                case ERR_STATE.ON:
                    if (error == null)
                    {
107
                        reasonList.Add(reason.Clone());
潘栩锋's avatar
潘栩锋 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

                    }
                    else
                    {
                        //已经有了,再发生,不管它。
                        return;
                    }
                    break;
                case ERR_STATE.OFF:
                    if (error == null)
                    {
                        //没有在报警,不管它。
                        return;
                    }
                    else
                    {
                        error.State = state;
125
                        reasonList.Remove(error_id);
潘栩锋's avatar
潘栩锋 committed
126 127 128 129

                    }
                    break;
            }
130 131 132 133
            var newestList = NewestList as IBufferAdd<FlyData_WarningHistory>;
            newestList.Add(reason);


潘栩锋's avatar
潘栩锋 committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            if (reasonList.Count > 0)
            {
                IsRinging = true;
            }
            else
            {
                Reset();
            }
        }

        public void Remove(int errcode) {
 

            FlyData_WarningHistory error = null;
            Buffer<FlyData_WarningHistory> reasonList = ReasonList as Buffer<FlyData_WarningHistory>;

            if (reasonList.Count == 0)
                return;

            int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
            if (idx < 0)
                return;

            int error_id = reasonList.GetID(idx);
            error = reasonList.list[idx];
            error.State = ERR_STATE.OFF;
            reasonList.Remove(error_id);

            var newestList = NewestList as IBufferAdd<FlyData_WarningHistory>;
163 164 165 166 167 168 169 170

            newestList.Add(new FlyData_WarningHistory
            {
                Time = DateTime.Now,
                ErrCode = errcode,
                Description = error.Description,
                State = ERR_STATE.OFF
            });
潘栩锋's avatar
潘栩锋 committed
171 172


潘栩锋's avatar
潘栩锋 committed
173 174
            if (reasonList.Count > 0)
            {
175
                IsRinging = true;
潘栩锋's avatar
潘栩锋 committed
176 177 178 179 180
            }
            else
            {
                Reset();
            }
潘栩锋's avatar
潘栩锋 committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194
        }
        #endregion

        /// <summary>
        /// 获取能被交易的属性
        /// </summary>
        /// <returns></returns>
        public string[] GetPropertys()
        {
            return new string[] {
                "Enable"
            };
        }
        #region INotifyPropertyChanged 成员
195

潘栩锋's avatar
潘栩锋 committed
196 197 198 199 200 201
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}