WeightSystemServiceClient.cs 7.37 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Weight.OBJ_INTERFACE;
using System.Reflection;
using Misc;
using System.ComponentModel;
using System.Net;
using System.Xml;
using FLY.Modbus;
using FLY.Weight.IService;
using FLY.Weight.Common;
using FLY.OBJComponents.Client;
using FLY.OBJComponents.IService;
using System.Collections.ObjectModel;
using FLY.Weight.Server.Model;
using System.IO;

namespace FLY.Weight.Client
{
    public class WeightSystemServiceClient: FObjServiceClient, IWeightSystemService, IPropertyOpt
    {

        #region IWeightSystemService 接口

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

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

        /// <summary>
        /// PLC代理系统
        /// </summary>
        public IPLCProxySystemService PLCos { get; set; }

        /// <summary>
        /// 流量记录周期,单位s
        /// </summary>
        public int FlowInterval { get; set; } = 10;



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

        /// <summary>
        /// 每层仓数
        /// </summary>
        public int[] BinCnts { get; set; } = new int[] { 4, 4, 4 };

        #endregion

        SyncPropServiceClient syncPropServiceClient;


        public event Action ResetItemsEvent;
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="serviceId"></param>
        public WeightSystemServiceClient(UInt32 serviceId) : base(serviceId) {
            Init();
        }
        public WeightSystemServiceClient(UInt32 serviceId, string connName) : base(serviceId) {
            Init();
            ConnName = connName;
            filePath = connName + "." +filePath;

            FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
        }


        /// <summary>
        /// 以给定的各层仓数创建,当接收到 BinCnts变化,
        /// 重新修改各层, 删除,或添加
        /// </summary>
        /// <param name="bincnts"></param>
        void Init()
        {
            Load();
            //BinCnts = bincnts;
            for (int i = 0; i < BinCnts.Count(); i++)
            {
                WeighterC w = new WeighterC(((char)('A' + i)).ToString(), BinCnts[i]);
                Items.Add(w);
            }
            ItemsCnt = BinCnts.Count();

            //--------------------------------------------------------------
            //属性同步
            Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();
            objnames.Add(".", this);
            objnames.Add("Accessory", Accessory);
            for (int i = 0; i < Items.Count(); i++)
                objnames.Add($"Items[{i}]", Items[i]);
            
            syncPropServiceClient = new SyncPropServiceClient(mServerID + 1, objnames);

            //--------------------------------------------------------------
            //PLC代理,用于添加更新任务
            Dictionary<string, INotifyPropertyChanged> objnames2 = new Dictionary<string, INotifyPropertyChanged>();
            objnames2.Add("Accessory", Accessory);
            for (int i = 0; i < Items.Count(); i++)
                objnames2.Add($"Items[{i}]", Items[i]);

            PLCos = new PLCProxySystemServiceClient(mServerID + 2, objnames2);




            this.PropertyChanged += WeightSystemClient_PropertyChanged;
        }

        string filePath = "weightSystemClient.json";
        void Save() 
        {
            WeightSystemClientJsonDb jsonDb = new WeightSystemClientJsonDb()
            {
                BinCnts = BinCnts
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
            File.WriteAllText(filePath, json);
        }
        void Load() {
            if (!File.Exists(filePath))
                return;
            string json =File.ReadAllText(filePath);
            var p = Newtonsoft.Json.JsonConvert.DeserializeObject< WeightSystemClientJsonDb>(json);
            BinCnts = p.BinCnts;
        }
        private void WeightSystemClient_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "BinCnts")
            {
                ResetItems();
                Save();
            }
        }

        /// <summary>
        /// 重新创建 Items
        /// </summary>
        void ResetItems()
        {

            int last_itemcnt = Items.Count();

            for (int i = 0; i < Items.Count(); i++)
            {
                if (i < BinCnts.Count())
                {
                    if (Items[i].BinCnt != BinCnts[i])
                    {
                        Items[i].BinsReBinding(BinCnts[i]);
                    }
                }
                else
                {
                    //多出来的Item 都要删除
                    int remove_cnt = Items.Count() - BinCnts.Count();
                    for (i = 0; i < remove_cnt; i++)
                        Items.RemoveAt(Items.Count() - 1);

                    goto _step1;
                }
            }

            for (int i = Items.Count(); i < BinCnts.Count(); i++)
            {
                //少了,需要添加
                WeighterC w = new WeighterC(((char)('A' + i)).ToString(), BinCnts[i]);
                Items.Add(w);
            }

        _step1:
            //处理属性同步
            if (last_itemcnt == Items.Count())//数量相同,不需要处理
                goto _step2;

            if (last_itemcnt < Items.Count())
            {
                //少了需要添加
                for (int i = last_itemcnt; i < Items.Count(); i++)
                {
                    syncPropServiceClient.Add($"Items[{i}]", Items[i]);
                    PLCos.ObjNames.Add($"Items[{i}]", Items[i]);
                }
            }
            else
            {
                //多了需要删除
                for (int i = Items.Count(); i < last_itemcnt; i++)
                {
                    syncPropServiceClient.Remove($"Items[{i}]");
                    PLCos.ObjNames.Remove($"Items[{i}]");
                }
            }

        _step2:
            ResetItemsEvent?.Invoke();
            return;
        }
        public override UInt32[] GetIDs()
        {
            List<UInt32> IDs = new List<uint>
            {
                ID,
                syncPropServiceClient.ID,
            };
            IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());
            return IDs.ToArray();
        }
        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[]{
                "FlowInterval",
                "ItemsCnt",
                "BinCnts"
                };
        }

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

    }

    public class WeightSystemClientJsonDb 
    {
        /// <summary>
        /// 每层仓数
        /// </summary>
        public int[] BinCnts { get; set; } = new int[] { 4, 4, 4 };
    }
}