ErrorConf.cs 6.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FObjBase;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server
{
    public class ErrorConf
    {
        #region 延时推送 MARKNO
        const int MARKNO_DELAY_ISCONNECTED = 4;
        #endregion
19
        string plcName;
20 21 22 23 24 25 26 27
        /// <summary>
        /// 需要设置
        /// </summary>
        IPLCProxySystemService PLCos;
        /// <summary>
        /// 报警系统
        /// </summary>
        WarningSystem mWarning;
28
        public byte ErrCode=0;
29
        public ErrorConf(IPLCProxySystemService PLCos, WarningSystem mWarning, string plcName)
30 31 32
        {
            this.PLCos = PLCos;
            this.mWarning = mWarning;
33
            this.plcName = plcName;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        }

        #region 报警
        public class ErrorAction
        {
            public List<ErrorInfo> error_property;
            public object state;
            public delegate void ErrorHandler(ref string msg, object state);
            public ErrorHandler action;
        }
        public class ErrorInfo
        {
            public string property;
            public byte code;
            public string msg;
            public bool offIsError;
        }
        Dictionary<INotifyPropertyChanged, ErrorAction> obj_error = new Dictionary<INotifyPropertyChanged, ErrorAction>();

53
        public void AddErrorAction(INotifyPropertyChanged sender, ErrorAction.ErrorHandler errorHandler = null, object state = null)
54 55 56 57 58 59 60 61 62
        {
            //反射找出全部是报警的property
            List<ErrorInfo> error_property = new List<ErrorInfo>();
            foreach (var propertyInfo in sender.GetType().GetProperties())
            {
                if (propertyInfo.GetCustomAttributes(typeof(IsErrorAttribute), false).Count() > 0)
                {
                    //肯定有,没有就让它出错!!!
                    string desp = (propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).First() as DescriptionAttribute).Description;
63
                    //bool offIsError = (propertyInfo.GetCustomAttributes(typeof(IsErrorAttribute), false).First() as IsErrorAttribute).OffIsError;
64 65 66 67 68 69


                    ErrorInfo errorInfo = new ErrorInfo()
                    {
                        property = propertyInfo.Name,
                        msg = desp,
70 71
                        //offIsError = offIsError,
                        code = ErrCode
72 73
                    };
                    error_property.Add(errorInfo);
74
                    ErrCode++;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                }
            }
            obj_error.Add(sender, new ErrorAction()
            {
                error_property = error_property,
                action = errorHandler,
                state = state
            });
        }

        /// <summary>
        /// 执行之前,需要调用 AddErrorAction, 向 obj_error 添加报警寄存器操作
        /// </summary>
        public void InitError()
        {
            foreach (var obj in obj_error.Keys)
            {
                obj.PropertyChanged += Obj_PropertyChanged_ForError;
            }

            //--------------------------------------------------------------------------------
            //添加任务
            foreach (var kv in obj_error)
            {
                string objname = PLCos.ObjNames.First(_kv => _kv.Value == kv.Key).Key;
                PLCos.SetPlan(objname, kv.Value.error_property.Select(ei => ei.property).ToArray(), 0);
            }

            //--------------------------------------------------------------------------------
            //连接断开事件
            FObjBase.PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
                {
                    Misc.BindingOperations.SetBinding(PLCos, "IsConnectedWithPLC", () =>
                    {
                        bool b = !PLCos.IsConnectedWithPLC;

                        ERR_STATE state = b ? ERR_STATE.ON : ERR_STATE.OFF;

                        ERRNO errno = PlcErrNos.ERRNO_PLC_DISCONNECTED;
                        byte errcode = errno.Code;
116
                        string description = $"{plcName} "+errno.Descrption;
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 142 143 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
                        mWarning.Add(errcode, description, state);
                    });
                }, TimeSpan.FromSeconds(3), true, false, this, MARKNO_DELAY_ISCONNECTED, true);

            //--------------------------------------------------------------------------------
            //把全部报警状态取反,也就是不报警!!!
            foreach (var kv in obj_error)
            {
                object sender = kv.Key;
                foreach (var ei in kv.Value.error_property)
                {
                    if (ei.offIsError)
                    {
                        Misc.PropertiesManager.SetValue(sender, ei.property, true);
                    }
                    //false 不用写,默认值就是
                }
            }

        }


        void Obj_PropertyChanged_ForError(object sender, PropertyChangedEventArgs e)
        {
            ErrorAction errorAction = obj_error[sender as INotifyPropertyChanged];

            var eis = from ei in errorAction.error_property where ei.property == e.PropertyName select ei;
            if (eis.Count() > 0)
            {
                var ei = eis.First();

                //获取描述
                var type = sender.GetType();
                var property = type.GetProperty(ei.property);
                bool b = (bool)property.GetValue(sender, null);
                string desp = ei.msg;
                bool offIsError = ei.offIsError;

                ERR_STATE state;
                if (offIsError)
                    state = !b ? ERR_STATE.ON : ERR_STATE.OFF;
                else
                    state = b ? ERR_STATE.ON : ERR_STATE.OFF;

                byte errcode = ei.code;//每个报警必须不一样!!
                string description = desp;

                errorAction.action?.Invoke(ref description, errorAction.state);

                mWarning.Add(errcode, description, state);
            }
        }
        #endregion     
    }
}