WinderSystem.cs 9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
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 65 66 67
using FLY.Modbus;
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FLY.Winder.Common;
using FLY.Winder.IService;
using FObjBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace FLY.Winder.Server
{
    public class WinderSystem : IWinderSystemService
    {
        #region 延时推送 MARKNO
        const int MARKNO_SAVE = 1;
        const int MARKNO_DELAY_ISCONNECTED = 4;
        #endregion
        /// <summary>
        /// 收卷其它附件
        /// </summary>
        public WinderAccessory Accessory { get; private set; }

        /// <summary>
        /// 内收卷 外收卷
        /// </summary>
        public List<WinderInsideOutside> Items { get; private set; }

        private PLCProxySystem plcos = new PLCProxySystem();
        /// <summary>
        /// PLC代理系统
        /// </summary>
        public IPLCProxySystemService PLCos { get { return plcos; } }

        /// <summary>
        /// 报警系统
        /// </summary>
        public WarningSystem mWarning;

        static WinderSystem()
        {
            //Misc.SaveToXmlHepler.Regist(typeof(WinderSystemParams));
        }
        public WinderSystem()
        {
            Items = new List<WinderInsideOutside>();
            for (int i = 0; i < 2; i++)
            {
                Items.Add(new WinderInsideOutside());
            }
            Accessory = new WinderAccessory();


    
            mWarning = new WarningSystem();

            AddConfigFile("WS.xml");


            //--------------------------------------------------------------------------------
            //报警配置
            InitError();

            plcos.Init();
68

潘栩锋's avatar
潘栩锋 committed
69 70 71 72 73 74 75 76 77 78 79 80 81
        }
        #region 报警
        class ErrorAction
        {
            public Dictionary<string, ERRNO> error_property;
            public object state;
            public delegate void ErrorHandler(ref byte errcode, ref string msg, object state);
            public ErrorHandler action;
        }
        Dictionary<INotifyPropertyChanged, ErrorAction> obj_error = new Dictionary<INotifyPropertyChanged, ErrorAction>();

        void InitError()
        {
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
            Dictionary<string, ERRNO> error_winder_property = new Dictionary<string, ERRNO>
            {
                { "IsError_VFD", ERRNOs.ERRNO_WINDER_VFD },
                { "IsError_Fan", ERRNOs.ERRNO_WINDER_FAN },
                { "IsError_Turnover", ERRNOs.ERRNO_WINDER_TURNOVER },
                { "IsError_Scram", ERRNOs.ERRNO_WINDER_SCRAM },
                { "IsError_AirRollerNoReady", ERRNOs.ERRNO_WINDER_AIRROLLERNOREADY },
                { "IsError_MeasurePreWarning", ERRNOs.ERRNO_WINDER_MEASUREPREWARNING },
                { "IsError_ChangeRoll", ERRNOs.ERRNO_WINDER_CHANGEROLL },
                { "IsError_UnloadArm", ERRNOs.ERRNO_WINDER_UNLOADARM }
            };


            Dictionary<string, ERRNO> error_accessory_property = new Dictionary<string, ERRNO>
            {
                { "IsError_Traction1Fan", ERRNOs.ERRNO_TRACTION1FAN },
                { "IsError_Traction1VFD", ERRNOs.ERRNO_TRACTION1VFD },
                { "IsError_Traction2VFD", ERRNOs.ERRNO_TRACTION2VFD },
                { "IsError_Traction2Fan", ERRNOs.ERRNO_TRACTION2FAN },
                { "IsError_RotaryVFD", ERRNOs.ERRNO_ROTARYVFD },
                { "IsError_RotaryFan", ERRNOs.ERRNO_ROTARYFAN },
                { "IsError_Traction2Scram", ERRNOs.ERRNO_TRACTION2SCRAM },
                { "IsError_Scram", ERRNOs.ERRNO_SCRAM },
                { "IsError_Traction1Scram", ERRNOs.ERRNO_TRACTION1SCRAM },
                { "IsError_CustomerScram", ERRNOs.ERRNO_CUSTOMERSCRAM },
                { "IsError_RotaryForwLimit", ERRNOs.ERRNO_ROTARYFORWLIMIT },
                { "IsError_RotaryBackwLimit", ERRNOs.ERRNO_ROTARYBACKWLIMIT },
                { "IsError_RotaryForwLock", ERRNOs.ERRNO_ROTARYFORWLOCK },
                { "IsError_RotaryBackwLock", ERRNOs.ERRNO_ROTARYBACKWLOCK }
            };
潘栩锋's avatar
潘栩锋 committed
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 142

            obj_error.Add(Accessory, new ErrorAction()
            {
                error_property = error_accessory_property
            });

            obj_error.Add(Items[0], new ErrorAction()
            {
                error_property = error_winder_property,
                action = (ref byte code, ref string description, object state) => {
                    description = "内" + description;
                }
            });

            obj_error.Add(Items[1], new ErrorAction()
            {
                error_property = error_winder_property,
                action = (ref byte code, ref string description, object state) => {
                    description = "外" + description;
                    code += 10;
                }
            });


            foreach (var obj in obj_error.Keys)
            {
                obj.PropertyChanged += Obj_PropertyChanged_ForError;
            }

            //--------------------------------------------------------------------------------
            //添加任务
143
            foreach (var kv in obj_error)
潘栩锋's avatar
潘栩锋 committed
144
            {
145 146 147
                string objname = PLCos.ObjNames.First(_kv => _kv.Value == kv.Key).Key;
                PLCos.SetPlan(objname, kv.Value.error_property.Keys.ToArray(), 0);
            }
潘栩锋's avatar
潘栩锋 committed
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

            //--------------------------------------------------------------------------------
            //连接断开事件
            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 = ERRNOs.ERRNO_PLC_DISCONNECTED;
                        byte errcode = errno.Code;
                        string description = errno.Descrption;
                        mWarning.Add(errcode, description, state);

                        if (PLCos.IsConnectedWithPLC)
                        {
                            //刚连上
                            //全部报警触发一次
                            foreach (var kv in obj_error)
                            {
                                object sender = kv.Key;
                                foreach (string propertyname in kv.Value.error_property.Keys)
                                {
                                    Obj_PropertyChanged_ForError(sender, new PropertyChangedEventArgs(propertyname));
                                }
                            }
                        }
                    });
                }, TimeSpan.FromSeconds(3), true, false, this, MARKNO_DELAY_ISCONNECTED, true);

        }


        void Obj_PropertyChanged_ForError(object sender, PropertyChangedEventArgs e)
        {
            ErrorAction errorAction = obj_error[sender as INotifyPropertyChanged];
 
            if (errorAction.error_property.ContainsKey(e.PropertyName))
            {
                bool b = (bool)Misc.PropertiesManager.GetValue(sender, e.PropertyName);

                ERRNO errno = errorAction.error_property[e.PropertyName];
                ERR_STATE state;
194
                if(errno.OffIsError)
195
                    state = !b ? ERR_STATE.ON : ERR_STATE.OFF; 
潘栩锋's avatar
潘栩锋 committed
196
                else
197
                    state = b ? ERR_STATE.ON : ERR_STATE.OFF;
潘栩锋's avatar
潘栩锋 committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

                byte errcode = errno.Code;
                string description = errno.Descrption;

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

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


        void AddConfigFile(string configfile)
        {
            PLCGroup plcgroup = new PLCGroup();
            plcgroup.Load(configfile);

            foreach (PLCGroup.PLCDevice device in plcgroup.Devices)
            {
217
                var plc = new Modbus.WithThread.ModbusMapper_Client(device.EP);
潘栩锋's avatar
潘栩锋 committed
218 219 220 221 222 223
                plcos.PLCs.Add(plc);
            }

            //objname 转 obj
            PLCos.ObjNames.Add("Accessory", Accessory);
            for (int i = 0; i < Items.Count(); i++)
224
                PLCos.ObjNames.Add($"Items[{i}]", Items[i]);
潘栩锋's avatar
潘栩锋 committed
225 226 227 228 229 230 231


            foreach (PLCGroup.PLCVariable var in plcgroup.Variables)
            {
                if (var.DeviceIndex < 0 || var.DeviceIndex >= plcos.PLCs.Count)
                    continue;

232 233
                List<Modbus.DataToRegs> drs = plcos.DRMap;
                var plc = plcos.PLCs[var.DeviceIndex];
潘栩锋's avatar
潘栩锋 committed
234 235


236
                Modbus.DataToRegs dr = plc.MapDataToRegs(
潘栩锋's avatar
潘栩锋 committed
237 238 239 240 241 242 243 244 245
                    ModbusMapper.TranslateToPLCAddressArea(var.Mode),
                    var.Addr,
                    ModbusMapper.TranslateToREG_TYPE(var.Type),
                    var.Scale,
                    PLCos.ObjNames[var.OwnerName],
                    var.PropertyName);
                if (dr != null)
                    drs.Add(dr);
            }
246 247 248

            foreach (var plc in plcos.PLCs)
                plc.Build();
潘栩锋's avatar
潘栩锋 committed
249 250 251 252 253
        }


    }
}