WarningSystem2.cs 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FObjBase;
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>
    public class WarningSystem2 : IWarningSystem2Service
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region  IWarningSystem2Service

        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }
        /// <summary>
        /// 正在响铃
        /// </summary>
        public bool IsRinging { get; private set; }
        /// <summary>
        /// 当前正在报警的!!!!!!
        /// </summary>
        public FlyData_WarningHistory[] ReasonList { get; private set; }

        /// <summary>
        /// 最后一条数据Id
        /// </summary>
        public long LastId { get; set; }



        #endregion

        /// <summary>
        /// ReasonList 的原始数据, errors 与 ReasonList 需要同步
        /// </summary>
        private List<FlyData_WarningHistory> errors = new List<FlyData_WarningHistory>();
        /// <summary>
        /// 保存到SQL数据库的集成工具
        /// </summary>
        BufferError errorBuffer;
        public WarningSystem2()
        {
            Enable = true;
        }

        public void Init(BufferError errorBuffer) 
        {
            this.errorBuffer = errorBuffer;
            Misc.BindingOperations.SetBinding(this.errorBuffer, nameof(this.errorBuffer.LastId), this, nameof(LastId));
        }

        public void Reset()
        {
65 66 67
            if (errors.RemoveAll(e => e.CanReset == true) > 0) {
                updateReasonList();
            }
68 69 70 71 72 73 74 75 76 77 78
        }

        public void Silence()
        {
            IsRinging = false;
        }

        #region IWarningServiceSimple

        

79
        public void Update(int errcode, string description, string accessory="", bool canReset=true)
80
        {
81 82 83 84
            Add(errcode, description, 
                accessory:accessory,
                isUpdate:true,
                canReset:canReset);
85 86
        }

87 88 89 90 91 92 93 94
        public void Add(ERRNO errno,bool canReset = true) 
        {
            Add(errno.Code, errno.Descrption,canReset:canReset);
        }
        public void Remove(ERRNO errno)
        {
            Remove(errno.Code);
        }
95 96 97 98 99 100 101 102
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errcode">报警码</param>
        /// <param name="description">描述</param>
        /// <param name="state">状态,ON or OFF</param>
        /// <param name="accessory">附加信息</param>
        /// <param name="isUpdate">就算已经存在,也有刷新</param>
103 104
        /// <param name="canReset">可以被Reset()清除</param>
        public void Add(int errcode, string description, ERR_STATE state = ERR_STATE.ON, string accessory = "", bool isUpdate=false, bool canReset=true)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        {
            if (!Enable)
                return;

            if (state == ERR_STATE.OFF)
            {
                Remove(errcode);
                return;
            }

            if (errors.Count > 0)
            {
                //查找是否已经存在
                var error = errors.Find(err => err.ErrCode == errcode);
                if (error != null) {
                    //已经有了
                    if (!isUpdate)
                    {
                        //不需要刷新
                        return;
                    }
                    else {
                        //需要刷新
                        //先把它删除
                        errors.Remove(error);
                    }
                }
            }
            {
                //这是新的记录
                FlyData_WarningHistory error = new FlyData_WarningHistory
                {
                    Time = DateTime.Now,

                    ErrCode = errcode,
                    Description = description,
                    State = state,
142 143
                    Accessory = accessory,
                    CanReset = canReset
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                };
                errors.Add(error);
                updateReasonList();

                errorBuffer.Add(error);
            }
        }

        void updateReasonList() {
            if (errors.Count() > 0)
                ReasonList = errors.ToArray();
            else
                ReasonList = null;

            IsRinging = errors.Count > 0;
        }
        
        public void Remove(int errcode) {
 

            FlyData_WarningHistory error = null;
            if (errors.Count()==0)
                return;

            //删除!!!!!
            error = errors.Find(err => err.ErrCode == errcode);
            if (error == null)
                return;
            errors.Remove(error);

            updateReasonList();

            errorBuffer.Add(new FlyData_WarningHistory
            {
                Time = DateTime.Now,
                ErrCode = errcode,
                Description = error.Description,
                State = ERR_STATE.OFF
            });
        }
        #endregion


        /// <summary>
        /// 获取纵向趋势图
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void GetTrend(
            Pack_GetTrendRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext)
        {
            errorBuffer.GetTrend(request, asyncDelegate, asyncContext);
        }
    }
}