IBCSystem.cs 10.5 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using FLY.IBC.Common;
using FLY.IBC.IService;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.IBC.Server.Model;
潘栩锋's avatar
潘栩锋 committed
4 5 6 7 8
using FLY.Modbus;
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FObjBase;
潘栩锋's avatar
潘栩锋 committed
9
using Newtonsoft.Json;
潘栩锋's avatar
潘栩锋 committed
10 11
using System;
using System.Collections.Generic;
潘栩锋's avatar
潘栩锋 committed
12
using System.Collections.ObjectModel;
潘栩锋's avatar
潘栩锋 committed
13 14
using System.ComponentModel;
using System.Diagnostics;
潘栩锋's avatar
潘栩锋 committed
15
using System.IO;
潘栩锋's avatar
潘栩锋 committed
16 17
using System.Linq;
using System.Text;
潘栩锋's avatar
潘栩锋 committed
18
using System.Text.RegularExpressions;
潘栩锋's avatar
潘栩锋 committed
19 20 21 22
using System.Threading.Tasks;

namespace FLY.IBC.Server
{
潘栩锋's avatar
潘栩锋 committed
23
    [JsonObject(MemberSerialization.OptIn)]
潘栩锋's avatar
潘栩锋 committed
24
    public class IBCSystem : IIbcSystemService, IPropertyOpt
潘栩锋's avatar
潘栩锋 committed
25 26
    {
        #region IIBCSystem
潘栩锋's avatar
潘栩锋 committed
27 28 29 30

        #region IBC
        public bool HasIbc { get; private set; } = true;

潘栩锋's avatar
潘栩锋 committed
31 32 33 34
        /// <summary>
        /// 数据
        /// </summary>
        public IBCData Item { get; } = new IBCData();
潘栩锋's avatar
潘栩锋 committed
35

潘栩锋's avatar
潘栩锋 committed
36
        #endregion
潘栩锋's avatar
潘栩锋 committed
37

潘栩锋's avatar
潘栩锋 committed
38
        #region 温控
潘栩锋's avatar
潘栩锋 committed
39 40 41 42
        /// <summary>
        /// 层数
        /// </summary>
        public int TAreasCnt { get; private set; } = 0;
潘栩锋's avatar
潘栩锋 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        public ObservableCollection<TempArea> TAreas { get; } = new ObservableCollection<TempArea>();
        #endregion

        #region 吸料
        /// <summary>
        /// 吸料机层数
        /// </summary>
        public int FeederCnt { get; private set; } = 0;

        /// <summary>
        /// 吸料
        /// </summary>
        public ObservableCollection<FeederData> FeederDatas { get; } = new ObservableCollection<FeederData>();

        /// <summary>
        /// 吸料报警
        /// </summary>
        public FeederWarningData FeederWarningData { get; } = new FeederWarningData();

        #endregion
潘栩锋's avatar
潘栩锋 committed
63

潘栩锋's avatar
潘栩锋 committed
64
        public PLCProxySystem plcos = new PLCProxySystem();
潘栩锋's avatar
潘栩锋 committed
65 66 67 68
        /// <summary>
        /// PLC代理系统
        /// </summary>
        public IPLCProxySystemService PLCos { get { return plcos; } }
潘栩锋's avatar
潘栩锋 committed
69

潘栩锋's avatar
潘栩锋 committed
70 71 72
        #endregion


潘栩锋's avatar
潘栩锋 committed
73 74 75
        /// <summary>
        /// 报警系统
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
76
        WarningSystem2 warning;
潘栩锋's avatar
潘栩锋 committed
77 78 79 80 81 82 83 84 85 86 87 88
        /// <summary>
        /// 记录到数据库
        /// </summary>
        HistoryDb historyDb;
        /// <summary>
        /// 报警配置
        /// </summary>
        ErrorConf errorConf;
        /// <summary>
        /// 周期保存Ibc数据
        /// </summary>
        PeriodicallySaveData<Db_Width> psdWidth;
潘栩锋's avatar
潘栩锋 committed
89

潘栩锋's avatar
潘栩锋 committed
90 91
        PLCGroup plcgroup;

潘栩锋's avatar
潘栩锋 committed
92 93 94 95
        public IBCSystem()
        {
            Load();

潘栩锋's avatar
潘栩锋 committed
96 97 98
            //--------------------------------------------------------------------------------
            //step 1 加载PLC寄存器 文件
            AddConfigFile();
潘栩锋's avatar
潘栩锋 committed
99
        }
潘栩锋's avatar
潘栩锋 committed
100
        public void Init(HistoryDb historyDb ,WarningSystem2 warning)
潘栩锋's avatar
潘栩锋 committed
101 102 103
        {
            this.historyDb = historyDb;
            this.warning = warning;
潘栩锋's avatar
潘栩锋 committed
104

105 106
            //--------------------------------------------------------------------------------
            //step 2 报警配置
潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111
            errorConf = new ErrorConf(plcos, this.warning, plcgroup.Devices.Select(d => d.PlcName).ToArray());
            if(HasIbc)
                errorConf.AddErrorAction(Item);

            if (TAreasCnt > 0)
潘栩锋's avatar
潘栩锋 committed
112
            {
潘栩锋's avatar
潘栩锋 committed
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
                for (int i = 0; i < TAreas.Count(); i++)
                {
                    errorConf.AddErrorAction(
                        TAreas[i],
                            (ref string description, object state) =>
                            {
                                int idx = (int)state;
                                description = TAreas[idx].Number + "层 " + description;
                            },
                            i);
                }
            }

            if (FeederCnt > 0) {
                errorConf.AddErrorAction(FeederWarningData);

                for (int i = 0; i < FeederDatas.Count(); i++)
                {
                    errorConf.AddErrorAction(
                        FeederDatas[i],
                            (ref string description, object state) =>
                            {
                                int idx = (int)state;
                                description = FeederDatas[idx].Number + "层 " + description;
                            },
                            i);
                }
潘栩锋's avatar
潘栩锋 committed
140
            }
潘栩锋's avatar
潘栩锋 committed
141
            
潘栩锋's avatar
潘栩锋 committed
142
            errorConf.InitError();
潘栩锋's avatar
潘栩锋 committed
143

潘栩锋's avatar
潘栩锋 committed
144 145 146 147 148 149
            Misc.BindingOperations.SetBinding(this.warning, nameof(this.warning.IsRinging), () =>
            {
                if (!this.warning.IsRinging)
                {
                    Item.IsErrorReset = false;
                    Item.IsErrorReset = true;
150 151

                    //把全部报警都复位, 当复位无效,该属性还是true,下次查寄存器时,还是能触发报警
潘栩锋's avatar
潘栩锋 committed
152
                    //errorConf.ResetError();
潘栩锋's avatar
潘栩锋 committed
153 154 155 156
                }
            });


潘栩锋's avatar
潘栩锋 committed
157
            //--------------------------------------------------------------------------------
158 159
            //step 3 历史数据记录

潘栩锋's avatar
潘栩锋 committed
160
            //添加任务
潘栩锋's avatar
潘栩锋 committed
161 162 163
            if (HasIbc)
            {
                PLCos.SetPlan(nameof(Item), new string[] {
潘栩锋's avatar
潘栩锋 committed
164 165 166 167
                        nameof(IBCData.FilmWidth),
                        nameof(IBCData.InletAirFreq),
                        nameof(IBCData.OutletAirFreq),
                        //nameof(IBCData.ExAirFreq)
潘栩锋's avatar
潘栩锋 committed
168
                    }, 0);
潘栩锋's avatar
潘栩锋 committed
169

潘栩锋's avatar
潘栩锋 committed
170 171 172 173 174 175 176 177
                psdWidth = new PeriodicallySaveData<Db_Width>();
                psdWidth.Init(
                    10,
                    this.historyDb.AddWidthRange,
                    () =>
                    {
                        if (!PLCos.IsConnectedWithPLC)
                            return null;
潘栩锋's avatar
潘栩锋 committed
178

潘栩锋's avatar
潘栩锋 committed
179
                        if (Item.InletAirFreq < 2 && Item.OutletAirFreq < 2)//没有工作!!!!
潘栩锋's avatar
潘栩锋 committed
180
                        return null;
潘栩锋's avatar
潘栩锋 committed
181

潘栩锋's avatar
潘栩锋 committed
182 183
                    //记录数据
                    return new Db_Width()
潘栩锋's avatar
潘栩锋 committed
184 185 186 187 188 189 190 191
                        {
                            Time = DateTime.Now,
                            FilmWidth = Math.Round(Item.FilmWidth, 1),
                            InletAirFreq = Math.Round(Item.InletAirFreq, 1),
                            OutletAirFreq = Math.Round(Item.OutletAirFreq, 1)
                        };
                    });
            }
潘栩锋's avatar
潘栩锋 committed
192
            //--------------------------------------------------------------------------------
193
            //last step  PLC 更新计划服务初始化
潘栩锋's avatar
潘栩锋 committed
194 195 196 197
            plcos.Init();

        }

潘栩锋's avatar
潘栩锋 committed
198

潘栩锋's avatar
潘栩锋 committed
199 200 201 202
        void AddConfigFile()
        {
            plcos.AddConfigFile("plcgroup.json", (plcgroup) =>
            {
潘栩锋's avatar
潘栩锋 committed
203 204 205 206 207 208 209 210
                this.plcgroup = plcgroup;

                //从plcgroup 分析出 是否有IBC
                HasIbc = plcgroup.Variables.Any(var => var.OwnerName == nameof(Item));


                //从plcgroup 分析出 有多少层温控
                Regex r = new Regex($@"{nameof(TAreas)}\[([0-9])\]");
潘栩锋's avatar
潘栩锋 committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
                int tAreasCnt = 0;
                foreach (PLCGroup.PLCVariable var in plcgroup.Variables)
                {
                    Match m = r.Match(var.OwnerName);
                    if (m.Success)
                    {
                        int tArea_idx = int.Parse(m.Groups[1].Value);
                        if (tAreasCnt <= tArea_idx)
                            tAreasCnt = tArea_idx + 1;
                    }
                }
                TAreasCnt = tAreasCnt;

                for (int i = 0; i < TAreasCnt; i++)
                {
潘栩锋's avatar
潘栩锋 committed
226 227 228 229 230 231 232 233 234 235 236 237 238
                    string number = Index2Number_tArea(i);
                    TempArea ta = new TempArea();
                    ta.Init(number);
                    TAreas.Add(ta);
                }

                //从plcgroup 分析出 有多少层吸料
                r = new Regex($@"{nameof(FeederDatas)}\[([0-9])\]");
                int feederCnt = 0;
                foreach (PLCGroup.PLCVariable var in plcgroup.Variables)
                {
                    Match m = r.Match(var.OwnerName);
                    if (m.Success)
潘栩锋's avatar
潘栩锋 committed
239
                    {
潘栩锋's avatar
潘栩锋 committed
240 241 242
                        int feeder_idx = int.Parse(m.Groups[1].Value);
                        if (feederCnt <= feeder_idx)
                            feederCnt = feeder_idx + 1;
潘栩锋's avatar
潘栩锋 committed
243
                    }
潘栩锋's avatar
潘栩锋 committed
244 245
                }
                FeederCnt = feederCnt;
潘栩锋's avatar
潘栩锋 committed
246

潘栩锋's avatar
潘栩锋 committed
247 248 249 250 251 252
                for (int i = 0; i < FeederCnt; i++)
                {
                    string number = Index2Number_feeder(i);
                    FeederData fd = new FeederData();
                    fd.Init(number);
                    FeederDatas.Add(fd);
潘栩锋's avatar
潘栩锋 committed
253 254 255
                }


潘栩锋's avatar
潘栩锋 committed
256 257 258 259 260
                if (HasIbc == false && TAreasCnt == 0 && FeederCnt == 0) {
                    //异常,什么都没有,加载文件有问题!!!!!!
                    throw new Exception("加载配置文件有问题, 没有IBC,没有温控,没有吸料");
                }

潘栩锋's avatar
潘栩锋 committed
261 262 263 264
                //objname 转 obj
                Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();

                objnames.Add(nameof(Item), Item);
潘栩锋's avatar
潘栩锋 committed
265

潘栩锋's avatar
潘栩锋 committed
266 267
                for (int i = 0; i < TAreasCnt; i++)
                    objnames.Add($"{nameof(TAreas)}[{i}]", TAreas[i]);
潘栩锋's avatar
潘栩锋 committed
268 269 270 271 272 273

                for (int i = 0; i < FeederCnt; i++)
                    objnames.Add($"{nameof(FeederDatas)}[{i}]", FeederDatas[i]);

                objnames.Add(nameof(FeederWarningData), FeederWarningData);
                
潘栩锋's avatar
潘栩锋 committed
274 275 276 277 278
                return objnames;
            });

        }

潘栩锋's avatar
潘栩锋 committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        string Index2Number_tArea(int index)
        {
            string number;
            if (index == 0)
            {
                number = "M";
            }
            else
            {
                number = ((char)('A' + (index - 1))).ToString();
            }

            return number;
        }

        string Index2Number_feeder(int index)
        {
            string number;
            number = ((char)('A' + (index))).ToString();
            
            return number;
        }

潘栩锋's avatar
潘栩锋 committed
302
        public event PropertyChangedEventHandler PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
303 304

        string filePath = "ibc.json";
潘栩锋's avatar
潘栩锋 committed
305
        void Save()
潘栩锋's avatar
潘栩锋 committed
306
        {
潘栩锋's avatar
潘栩锋 committed
307
            string json = JsonConvert.SerializeObject(this, Formatting.Indented);
潘栩锋's avatar
潘栩锋 committed
308
            File.WriteAllText(filePath, json);
潘栩锋's avatar
潘栩锋 committed
309
        }
潘栩锋's avatar
潘栩锋 committed
310
        void Load()
潘栩锋's avatar
潘栩锋 committed
311
        {
潘栩锋's avatar
潘栩锋 committed
312
            if (!File.Exists(filePath))
潘栩锋's avatar
潘栩锋 committed
313
                return;
潘栩锋's avatar
潘栩锋 committed
314
            string json = File.ReadAllText(filePath);
潘栩锋's avatar
潘栩锋 committed
315 316 317 318 319 320 321 322
            try
            {
                JsonConvert.PopulateObject(json, this);
            }
            catch 
            {
            
            }
潘栩锋's avatar
潘栩锋 committed
323
        }
潘栩锋's avatar
潘栩锋 committed
324 325 326 327

        public string[] GetSyncPropNames()
        {
            return new string[]{
潘栩锋's avatar
潘栩锋 committed
328 329 330
                nameof(HasIbc),
                nameof(TAreasCnt),
                nameof(FeederCnt)
潘栩锋's avatar
潘栩锋 committed
331 332 333 334 335 336 337
                };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }
潘栩锋's avatar
潘栩锋 committed
338
    }
潘栩锋's avatar
潘栩锋 committed
339

潘栩锋's avatar
潘栩锋 committed
340
}