DownBlowingSystemServiceClient.cs 9.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using FLY.DownBlowing.Common;
using FLY.DownBlowing.IService;
using FLY.OBJComponents.Client;
using FLY.OBJComponents.IService;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.DownBlowing.Client
{
18
    public class DownBlowingSystemServiceClient : FObjServiceClient, IDownBlowingSystemService, IPropertyOpt
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    {
        #region IDownBlowingSystemService
        /// <summary>
        /// 挤出机层数
        /// </summary>
        public int TAreasCnt { get; set; } = 8;

        /// <summary>
        /// 收卷
        /// </summary>
        public WinderData WinderData { get; } = new WinderData();

        /// <summary>
        /// 外冷 
        /// </summary>
        public IbcData IbcData { get; } = new IbcData();

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

41 42 43
        /// <summary>
        /// 挤出温控
        /// </summary>
44
        public ObservableCollection<TempArea> TAreas { get; } = new ObservableCollection<TempArea>();
45 46 47 48

        PLCProxySystemServiceClient plcos;
        public IPLCProxySystemService PLCos => plcos;

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        #endregion

        SyncPropServiceClient syncPropServiceClient;

        public event Action ResetItemsEvent;

        /// <summary>
        /// 当前 INotifyPropertyChanged 对象 引发了 PropertyChanged,  是由于 接收到数据导致的
        /// </summary>
        public bool IsInPushValue
        {
            get
            {
                return syncPropServiceClient.IsInPushValue;
            }
        }

66 67 68 69
        public DownBlowingSystemServiceClient(UInt32 serviceId) : base(serviceId)
        {
            Init();
        }
70
        public DownBlowingSystemServiceClient(UInt32 serviceId, string connName) : base(serviceId)
71 72 73 74 75
        {
            Init();
            ConnName = connName;
            FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
        }
76 77 78 79 80 81 82 83
        void TAreasAdd(int i)
        {
            string number = Index2Number(i);

            //少了,需要添加
            var tarea = new TempArea();

            tarea.Init(number);
84

85 86
            TAreas.Add(tarea);
        }
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        string Index2Number(int index)
        {
            string number;
            if (index == 0)
            {
                number = "M";
            }
            else
            {
                number = ((char)('A' + (index - 1))).ToString();
            }
            return number;
        }

101 102 103 104 105 106 107 108
        void FeederDatasAdd(int i) {
            string number = Index2Number(i+1);
            var feederData = new FeederData();

            feederData.Init(number);

            FeederDatas.Add(feederData);
        }
109 110 111 112 113 114
        void Init()
        {
            Load();
            if (TAreasCnt <= 1)
                TAreasCnt = 0;

115
            for (int i = 0; i < TAreasCnt; i++)
116
            {
117 118 119 120 121
                TAreasAdd(i);
            }
            for (int i = 0; i < TAreasCnt-1; i++)
            {
                FeederDatasAdd(i);
122 123
            }

124 125 126 127
            //--------------------------------------------------------------
            //属性同步
            Dictionary<string, INotifyPropertyChanged> objnames = GetObjNames();
            objnames.Add(".", this);
128

129
            syncPropServiceClient = new SyncPropServiceClient(mServerID + 1, objnames);
130 131 132
            //--------------------------------------------------------------
            //PLC代理,用于添加更新任务

133
            objnames = GetObjNames();
潘栩锋's avatar
潘栩锋 committed
134
            plcos = new PLCProxySystemServiceClient(mServerID + 2);
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


            this.PropertyChanged += DownBlowingSystemServiceClient_PropertyChanged;
        }
        Dictionary<string, INotifyPropertyChanged> GetObjNames() 
        {
            Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();

            objnames.Add(nameof(WinderData), WinderData);
            objnames.Add(nameof(IbcData), IbcData);

            if (TAreas != null)
            {
                for (int i = 0; i < TAreas.Count(); i++)
                    objnames.Add($"{nameof(TAreas)}[{i}]", TAreas[i]);
            }
            if(FeederDatas!=null)
                for (int i = 0; i < FeederDatas.Count(); i++)
                objnames.Add($"{nameof(FeederDatas)}[{i}]", FeederDatas[i]);

            return objnames;
        }
        string filePath = "downBlowingSystemClient.json";
        void Save()
        {
            DownBlowingSystemServiceClientJsonDb.Save(this, filePath);
        }
        void Load()
        {
            DownBlowingSystemServiceClientJsonDb.Load(this, filePath);

        }

        private void DownBlowingSystemServiceClient_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(TAreasCnt))
            {
172 173 174 175 176
                if (IsTAreasCntChanged())
                {
                    ResetTAreas();
                    Save();
                }
177
            }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        }

        bool IsTAreasCntChanged()
        {
            if (TAreas.Count() != TAreasCnt)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 重新创建 Items
        /// </summary>
        void ResetTAreas()
        {
            if (TAreas.Count() == TAreasCnt)
                return;

            int last_tAreasCnt = TAreas.Count();
            int remove_cnt = TAreas.Count() - TAreasCnt;
            
            if (remove_cnt > 0)
            {
                //多出来的Item 都要删除
                for (int i = 0; i < remove_cnt; i++)
202
                {
203 204 205 206 207 208 209 210 211 212 213 214
                    int index = TAreas.Count() - 1;
                    TAreas.RemoveAt(index);
                    string objname = $"{nameof(TAreas)}[{index}]";
                    syncPropServiceClient.Remove(objname);
                }

                for (int i = 0; i < remove_cnt; i++)
                {
                    int index = FeederDatas.Count() - 1;
                    FeederDatas.RemoveAt(index);
                    string objname = $"{nameof(FeederDatas)}[{index}]";
                    syncPropServiceClient.Remove(objname);
215 216
                }
            }
217
            else if (remove_cnt < 0)
218
            {
219 220 221 222 223 224 225 226 227 228 229 230
                //少了,需要添加
                for (int i = 0; i < -remove_cnt; i++)
                {
                    int index = TAreas.Count();
                    TAreasAdd(index);

                    string objname = $"{nameof(TAreas)}[{index}]";
                    syncPropServiceClient.Add(objname, TAreas[index]);
                }


                for (int i = 0; i < -remove_cnt; i++)
231
                {
232 233 234 235 236
                    int index = FeederDatas.Count();
                    FeederDatasAdd(index);

                    string objname = $"{nameof(FeederDatas)}[{index}]";
                    syncPropServiceClient.Add(objname, FeederDatas[index]);
237 238 239
                }
            }

240 241 242
            ResetItemsEvent?.Invoke();
            return;
        }
243 244 245 246 247 248 249 250 251

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override UInt32[] GetIDs()
        {
            List<UInt32> IDs = new List<uint>();
            IDs.Add(ID);
252
            IDs.Add(syncPropServiceClient.ID);
253 254 255
            IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());
            return IDs.ToArray();
        }
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        public string[] GetSyncPropNames()
        {
            return new string[]{
                nameof(TAreasCnt)
                };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    }

    public class DownBlowingSystemServiceClientJsonDb
    {
        public int TAreasCnt;

        public static bool Save(DownBlowingSystemServiceClient src, string filePath)
        {
            try
            {
                DownBlowingSystemServiceClientJsonDb jsonDb = new DownBlowingSystemServiceClientJsonDb()
                {
                    TAreasCnt = src.TAreasCnt
                };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
                File.WriteAllText(filePath, json);
                return true;
            }
            catch {
                return false;
            }

        }
        public static bool Load(DownBlowingSystemServiceClient src, string filePath)
        {
            if (!File.Exists(filePath))
                return false;
            try
            {
                string json = File.ReadAllText(filePath);
                var p = Newtonsoft.Json.JsonConvert.DeserializeObject<DownBlowingSystemServiceClientJsonDb>(json);
                src.TAreasCnt = p.TAreasCnt;
                return true;
            }
            catch
            {
                //失败就算了。。。
                return false;
            }

        }
    }
}