DownBlowingSystemServiceClient.cs 8.81 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
    {
        #region IDownBlowingSystemService
21 22


23
        /// <summary>
24
        /// 收卷
25
        /// </summary>
26
        public WinderAccessory WinderAccessory { get; } = new WinderAccessory();
27 28

        /// <summary>
29
        /// 内收卷 外收卷
30
        /// </summary>
31
        public List<WinderInsideOutside> WIOs { get; private set; } = new List<WinderInsideOutside>() { new WinderInsideOutside(), new WinderInsideOutside() };
32 33 34 35 36 37

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

38 39 40 41 42 43
        #region 挤出温控

        /// <summary>
        /// 挤出机层数
        /// </summary>
        public int TAreasCnt { get; set; } = 10;
44 45 46
        /// <summary>
        /// 挤出温控
        /// </summary>
47
        public ObservableCollection<TempArea> TAreas { get; } = new ObservableCollection<TempArea>();
48

49 50
        #endregion

51 52 53
        PLCProxySystemServiceClient plcos;
        public IPLCProxySystemService PLCos => plcos;

54 55 56 57
        /// <summary>
        /// 版本
        /// </summary>
        public string Version { get; set; } = "3";
58 59 60 61
        #endregion

        SyncPropServiceClient syncPropServiceClient;

62 63
        public event Action ResetTAreasEvent;
        public event Action ResetFeederDatasEvent;
64 65 66 67 68 69 70 71 72 73 74
        /// <summary>
        /// 当前 INotifyPropertyChanged 对象 引发了 PropertyChanged,  是由于 接收到数据导致的
        /// </summary>
        public bool IsInPushValue
        {
            get
            {
                return syncPropServiceClient.IsInPushValue;
            }
        }

75 76 77 78
        public DownBlowingSystemServiceClient(UInt32 serviceId) : base(serviceId)
        {
            Init();
        }
79
        public DownBlowingSystemServiceClient(UInt32 serviceId, string connName) : base(serviceId)
80 81 82 83 84
        {
            Init();
            ConnName = connName;
            FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
        }
85 86 87 88 89 90 91 92
        void TAreasAdd(int i)
        {
            string number = Index2Number(i);

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

            tarea.Init(number);
93

94 95
            TAreas.Add(tarea);
        }
96 97 98 99 100 101 102 103 104 105 106 107 108
        string Index2Number(int index)
        {
            string number;
            if (index == 0)
            {
                number = "M";
            }
            else
            {
                number = ((char)('A' + (index - 1))).ToString();
            }
            return number;
        }
109

110 111 112
        void Init()
        {
            Load();
113 114 115 116

            WIOs[0].Init("外");
            WIOs[1].Init("内");

117 118 119
            if (TAreasCnt <= 1)
                TAreasCnt = 0;

120
            for (int i = 0; i < TAreasCnt; i++)
121
            {
122 123
                TAreasAdd(i);
            }
124

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

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

134
            objnames = GetObjNames();
潘栩锋's avatar
潘栩锋 committed
135
            plcos = new PLCProxySystemServiceClient(mServerID + 2);
136 137 138 139 140 141 142 143


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

144
            objnames.Add(nameof(WinderAccessory), WinderAccessory);
145 146
            objnames.Add(nameof(IbcData), IbcData);

147 148 149
            for (int i = 0; i < WIOs.Count(); i++)
                objnames.Add($"{nameof(WIOs)}[{i}]", WIOs[i]);

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
            if (TAreas != null)
            {
                for (int i = 0; i < TAreas.Count(); i++)
                    objnames.Add($"{nameof(TAreas)}[{i}]", TAreas[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))
            {
173 174 175 176 177
                if (IsTAreasCntChanged())
                {
                    ResetTAreas();
                    Save();
                }
178
            }
179 180 181 182
            else if (e.PropertyName == nameof(Version))
            {
                Save();
            }
183 184 185 186 187 188 189 190 191 192
        }

        bool IsTAreasCntChanged()
        {
            if (TAreas.Count() != TAreasCnt)
                return true;
            else
                return false;
        }
        /// <summary>
193
        /// 重新创建 TAreas
194 195 196 197 198 199 200 201 202 203
        /// </summary>
        void ResetTAreas()
        {
            int last_tAreasCnt = TAreas.Count();
            int remove_cnt = TAreas.Count() - TAreasCnt;
            
            if (remove_cnt > 0)
            {
                //多出来的Item 都要删除
                for (int i = 0; i < remove_cnt; i++)
204
                {
205 206 207 208 209
                    int index = TAreas.Count() - 1;
                    TAreas.RemoveAt(index);
                    string objname = $"{nameof(TAreas)}[{index}]";
                    syncPropServiceClient.Remove(objname);
                }
210
            }
211
            else if (remove_cnt < 0)
212
            {
213 214 215 216 217 218 219 220 221
                //少了,需要添加
                for (int i = 0; i < -remove_cnt; i++)
                {
                    int index = TAreas.Count();
                    TAreasAdd(index);

                    string objname = $"{nameof(TAreas)}[{index}]";
                    syncPropServiceClient.Add(objname, TAreas[index]);
                }
222 223 224 225 226
            }

            ResetTAreasEvent?.Invoke();
            return;
        }
227

228 229 230 231 232 233 234 235
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override UInt32[] GetIDs()
        {
            List<UInt32> IDs = new List<uint>();
            IDs.Add(ID);
236
            IDs.Add(syncPropServiceClient.ID);
237 238 239
            IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());
            return IDs.ToArray();
        }
240 241 242 243 244 245 246 247 248 249 250 251 252 253

        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[]{
254 255
                nameof(TAreasCnt),
                nameof(Version)
256 257 258 259 260 261 262
                };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }
263 264 265 266
    }

    public class DownBlowingSystemServiceClientJsonDb
    {
267 268 269 270
        public int TAreasCnt=10;


        public string Version = "3";
271 272 273 274 275 276 277

        public static bool Save(DownBlowingSystemServiceClient src, string filePath)
        {
            try
            {
                DownBlowingSystemServiceClientJsonDb jsonDb = new DownBlowingSystemServiceClientJsonDb()
                {
278 279
                    TAreasCnt = src.TAreasCnt,
                    Version = src.Version
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                };
                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;
299
                src.Version = p.Version;
300 301 302 303 304 305 306 307 308 309 310
                return true;
            }
            catch
            {
                //失败就算了。。。
                return false;
            }

        }
    }
}