IntegratedSystem.cs 7.11 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
using FLY.Integrated.Common;
using FLY.Integrated.IService;
using FLY.Integrated.Server.Model;
using FLY.Modbus;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FObjBase;
using Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Integrated.Server
{
    [JsonObject(MemberSerialization.OptIn)]
    public class IntegratedSystem : IIntegratedSystemService
    {
        #region IIntegratedSystemService
        /// <summary>
        /// 数据
        /// </summary>
28
        public IbcData Ibc { get; } = new IbcData();
潘栩锋's avatar
潘栩锋 committed
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 68 69 70 71 72 73 74 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

        /// <summary>
        ///  收卷
        /// </summary>
        public WinderAccessory Accessory { get; } = new WinderAccessory();
        private int ctrlInterval = 5;
        /// <summary>
        /// 控制记录周期,单位s, 最小值为2
        /// </summary>
        [JsonProperty]
        public int CtrlInterval
        {
            get { return ctrlInterval; }
            set
            {
                if (value < 2)
                    value = 2;
                if (ctrlInterval != value)
                {
                    ctrlInterval = value;
                }
            }
        }


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

        #endregion

        /// <summary>
        /// 报警系统
        /// </summary>
        WarningSystem warning;
        /// <summary>
        /// 记录到数据库
        /// </summary>
        HistoryDb historyDb;
        /// <summary>
        /// 报警配置
        /// </summary>
        ErrorConf errorConf;

        /// <summary>
        /// 周期保存Ibc数据
        /// </summary>
        PeriodicallySaveData<Db_Width> psdWidth;

        /// <summary>
        /// 周期保存收卷数据
        /// </summary>
        PeriodicallySaveData<Db_WinderInfo> psdWinderInfo;

        public event PropertyChangedEventHandler PropertyChanged;

        public IntegratedSystem()
        {
            Load();

            //加载PLC寄存器 文件
            AddConfigFile("WS.json");
        }
        public void Init(HistoryDb historyDb, WarningSystem warning) 
        {
            this.historyDb = historyDb;
            this.warning = warning;


            //--------------------------------------------------------------------------------
            //报警配置
102
            errorConf = new ErrorConf(PLCos, this.warning, "综合IBC");
潘栩锋's avatar
潘栩锋 committed
103 104 105 106 107 108

            byte code = 0;
            errorConf.AddErrorAction(
                Accessory, ref code);

            errorConf.AddErrorAction(
109
                Ibc, ref code);
潘栩锋's avatar
潘栩锋 committed
110 111 112 113 114 115 116 117

            errorConf.InitError();


            //--------------------------------------------------------------------------------
            //历史数据记录

            //添加任务
118
            PLCos.SetPlan("Ibc", new string[] {
潘栩锋's avatar
潘栩锋 committed
119 120 121 122 123 124 125 126 127 128 129
                        "FilmWidth","InletAirFreq","OutletAirFreq","ExCoolFreq"
                    }, 0);

            psdWidth = new PeriodicallySaveData<Db_Width>();
            psdWidth.Init(
                CtrlInterval,
                this.historyDb.AddWidthRange, 
                () =>{
                    if (!PLCos.IsConnectedWithPLC)
                        return null;

130
                    if (Ibc.InletAirFreq < 2 && Ibc.OutletAirFreq < 2)//没有工作!!!!
潘栩锋's avatar
潘栩锋 committed
131 132 133 134 135 136
                        return null;

                    //记录数据
                    return new Db_Width()
                    {
                        Time = DateTime.Now,
137 138 139 140
                        FilmWidth = Math.Round(Ibc.FilmWidth, 1),
                        InletAirFreq = Math.Round(Ibc.InletAirFreq, 1),
                        OutletAirFreq = Math.Round(Ibc.OutletAirFreq, 1),
                        ExCoolFreq = Math.Round(Ibc.ExCoolFreq, 1)
潘栩锋's avatar
潘栩锋 committed
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                    };
                });

            PLCos.SetPlan("Accessory", new string[] {
                        "TractionVelocity","RotaryFreq","IsRotaryForw","IsRotaryBackw"
                    }, 0);

            psdWinderInfo = new PeriodicallySaveData<Db_WinderInfo>();
            psdWinderInfo.Init(
                10,
                this.historyDb.AddWinderInfos,
                () => {
                    if (!PLCos.IsConnectedWithPLC)
                        return null;

                    if (Accessory.TractionVelocity < 1 )//没有工作!!!!
                        return null;

                    //记录数据
                    return new Db_WinderInfo()
                    {
                        Time = DateTime.Now,
                        TractionVelocity = Math.Round(Accessory.TractionVelocity, 1),
                        RotaryFreq = Math.Round(Accessory.RotaryFreq, 1),
                        IsRotaryBackw = Accessory.IsRotaryBackw,
                        IsRotaryForw = Accessory.IsRotaryForw
                    };
                });

            //--------------------------------------------------------------------------------
            //注册通知事件
            plcos.Init();
        }

        void AddConfigFile(string configfile)
        {
            PLCGroup plcgroup = new PLCGroup();
            string json = File.ReadAllText(configfile);
            JsonConvert.PopulateObject(json, plcgroup);

            foreach (PLCGroup.PLCDevice device in plcgroup.Devices)
            {
                var plc = new Modbus.WithThread.ModbusMapper_Client(device.EP);
                plcos.PLCs.Add(plc);
            }

            //objname 转 obj
188
            PLCos.ObjNames.Add("Ibc", Ibc);
潘栩锋's avatar
潘栩锋 committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            PLCos.ObjNames.Add("Accessory", Accessory);

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

                List<Modbus.DataToRegs> drs = plcos.DRMap;
                var plc = plcos.PLCs[var.DeviceIndex];

                Modbus.DataToRegs dr = plc.MapDataToRegs(
                    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);
            }
            foreach (var plc in plcos.PLCs)
                plc.Build();
        }

        void Save()
        {
            string json = JsonConvert.SerializeObject(this, Formatting.Indented);
            File.WriteAllText("integrated.json", json);
        }
        void Load()
        {

            if (File.Exists("integrated.json"))
            {
                try
                {
                    string json = File.ReadAllText("integrated.json");
                    JsonConvert.PopulateObject(json, this);
                }
                catch
                {

                }
            }

        }
    }



}