WeightSystem.cs 8.47 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

using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FLY.Weight2.Common;
using FLY.Weight2.IService;
using FLY.Weight2.Server.Model;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using FLY.Modbus;


namespace FLY.Weight2.Server
{
    public class WeightSystem : IWeightSystemService, IPropertyOpt
    {
24
        const int FlowIntervalSec = 10;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        #region IWeightSystemService 接口

        public ObservableCollection<WeighterC> Items { get; } = new ObservableCollection<WeighterC>();


        public WeighterAccessory Accessory { get; } = new WeighterAccessory();

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

        /// <summary>
        /// 层数
        /// </summary>
        public int ItemsCnt { get; private set; } = 3;

        #endregion

45
        string[] NumberNames;
46 47 48 49 50 51 52 53 54 55 56 57
        /// <summary>
        /// 报警系统
        /// </summary>
        WarningSystem warning;
        /// <summary>
        /// 记录到数据库
        /// </summary>
        HistoryDb historyDb;
        /// <summary>
        /// 报警配置
        /// </summary>
        ErrorConf errorConf;
58 59 60 61
        /// <summary>
        /// 周期保存Ibc数据
        /// </summary>
        PeriodicallySaveData<Lc_Flow> psdFlow;
62 63
        public WeightSystem()
        {
64 65
            if (!LoadNumberNames())
                SaveNumberNames();
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

            //--------------------------------------------------------------------------------
            //step 1 加载PLC寄存器 文件
            AddConfigFile();



        }

        public void Init(HistoryDb historyDb, WarningSystem warning)
        {
            this.historyDb = historyDb;
            this.warning = warning;

            //--------------------------------------------------------------------------------
            //step 2 报警配置
83
            errorConf = new ErrorConf(plcos, this.warning, "称重");
84 85 86 87 88 89 90 91 92 93 94 95 96 97
            errorConf.AddErrorAction(Accessory);
            for (int i = 0; i < Items.Count(); i++)
            {
                errorConf.AddErrorAction(
                    Items[i],
                        (ref string description, object state) => {
                            int idx = (int)state;
                            description = Items[idx].Number + "层 " + description;
                        },
                        i);
            }
            errorConf.InitError();

            //报警使能
98
            Misc.BindingOperations.SetBinding(Accessory, nameof(Accessory.AlarmIsOn), () =>
99 100 101 102 103
            {
                this.warning.Enable = !Accessory.AlarmIsOn;
            });


104
            Misc.BindingOperations.SetBinding(this.warning, nameof(this.warning.Enable), () =>
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            {
                Accessory.AlarmIsOn = !this.warning.Enable;
            });

            //报警复位呢?

            //Misc.BindingOperations.SetBinding(this.warning, "IsRinging", () =>
            //{
            //    if (!this.warning.IsRinging)
            //    {
            //        for (int i = 0; i < ItemsCnt; i++)
            //        {
            //            Items[i].IsAlarmReseted = false;
            //        }
            //    }
            //});



124 125 126
            //step 3 历史数据记录


127 128 129 130 131
            //添加任务
            for (int i = 0; i < Items.Count(); i++)
            {
                List<string> props = new List<string>
                {
132 133 134
                    nameof(WeighterC.CurrentFlow),
                    nameof(WeighterC.ScrewPDisp),
                    nameof(WeighterC.ScrewMotorFreq)
135
                };
136
                PLCos.SetPlan($"{nameof(Items)}[{i}]", props, 0);
137
            }
138
            PLCos.SetPlan(nameof(Accessory), new string[] { nameof(Accessory.TotalFlow) }, 0);
139

140 141
            psdFlow = new PeriodicallySaveData<Lc_Flow>();
            psdFlow.Init(
142
                FlowIntervalSec,
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
                this.historyDb.AddFlowRange,
                () => {
                    if (!PLCos.IsConnectedWithPLC)
                        return null;

                    if (Accessory.TotalFlow < 5)//没有生产
                        return null;

                    //记录数据
                    Lc_Flow lc_Flow = new Lc_Flow
                    {
                        Time = DateTime.Now,
                        Total = Math.Round(Accessory.TotalFlow, 4),
                        Items = new Lc_FlowItem[Items.Count]
                    };
                    for (int i = 0; i < Items.Count; i++)
                    {
                        lc_Flow.Items[i] = new Lc_FlowItem
                        {
                            Flow = Math.Round(Items[i].CurrentFlow, 4),
                            ScrewPDisp = Math.Round(Items[i].ScrewPDisp, 2),
                            ScrewMotorFreq = Math.Round(Items[i].ScrewMotorFreq, 1)
                        };
                    }
                    return lc_Flow;
                });

170 171 172 173 174
            //--------------------------------------------------------------------------------
            //last step  PLC 更新计划服务初始化
            plcos.Init();
        }

175 176 177
        string GetNumber(int index)
        {
            string number;
178

179 180 181 182 183 184 185 186 187 188 189 190
            if (NumberNames != null
                && index < NumberNames.Count()
                && !string.IsNullOrEmpty(NumberNames[index]))
            {
                number = NumberNames[index];
            }
            else
            {
                number = ((char)('A' + index)).ToString();
            }
            return number;
        }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        void AddConfigFile()
        {
            plcos.AddConfigFile("plcgroup.json", (plcgroup) =>
            {
                //从plcgroup 分析出 有多少层,每层的仓数
                int itemsCnt = 0;
                Regex r = new Regex(@"Items\[([0-9])\]");

                foreach (PLCGroup.PLCVariable var in plcgroup.Variables)
                {
                    Match m = r.Match(var.OwnerName);
                    if (m.Success)
                    {
                        int weighter_idx = int.Parse(m.Groups[1].Value);
                        if (itemsCnt <= weighter_idx)//记录最大值
                            itemsCnt = weighter_idx + 1;
                    }
                }
                ItemsCnt = itemsCnt;

                for (int i = 0; i < ItemsCnt; i++)
                {
213 214
                    string number = GetNumber(i);
                    WeighterC w = new WeighterC(number, i);
215 216 217 218 219 220 221
                    Items.Add(w);
                }


                //objname 转 obj
                Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();

222
                objnames.Add(nameof(Accessory), Accessory);
223
                for (int i = 0; i < ItemsCnt; i++)
224
                    objnames.Add($"{nameof(Items)}[{i}]", Items[i]);
225 226 227 228
                return objnames;
            });

            //这个是从测厚仪获取的,每几秒就写一次,不调试!!!!!
229
            plcos.IgnoreLogProperties.Add(new SenderProperty() { sender = Accessory, propertyName = nameof(Accessory.CurrentVelocitySet) });
230 231 232 233 234

        }

        public event PropertyChangedEventHandler PropertyChanged;

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        string filePath_number = "weight2NumberNames.json";
        bool LoadNumberNames()
        {
            if (!File.Exists(filePath_number))
                return false;
            string json = File.ReadAllText(filePath_number);
            var p = Newtonsoft.Json.JsonConvert.DeserializeObject<NumberNamesJsonDb>(json);
            NumberNames = p.NumberNames;
            return true;
        }
        void SaveNumberNames()
        {
            NumberNamesJsonDb jsonDb = new NumberNamesJsonDb
            {
                NumberNames = NumberNames
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
            File.WriteAllText(filePath_number, json);
        }
254 255 256 257

        public string[] GetSyncPropNames()
        {
            return new string[]{
258
                nameof(ItemsCnt)
259 260 261 262 263 264 265 266 267
                };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }
    }

268 269 270 271
    public class NumberNamesJsonDb
    {
        public string[] NumberNames;
    }
272
}