SyncPropServiceClient.cs 7.12 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FLY.OBJComponents.Common;
2
using FLY.OBJComponents.IService;
潘栩锋's avatar
潘栩锋 committed
3 4 5 6 7 8 9
using FLY.OBJComponents.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
10
using System.Reflection;
潘栩锋's avatar
潘栩锋 committed
11 12 13 14 15 16 17 18 19 20 21
using System.Text;

namespace FLY.OBJComponents.Client
{
    public class SyncPropServiceClient : FObj
    {
        Dictionary<string, INotifyPropertyChanged> ObjNames;

        IFConn mConn;
        UInt32 mServerID;

22 23 24 25 26 27 28
        //public event PropertyChangedEventHandler PropertyChanged;

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

潘栩锋'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

        public SyncPropServiceClient(UInt32 serverID, Dictionary<string, INotifyPropertyChanged> objnames)
        {
            mServerID = serverID;
            Init(objnames);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="objnames"></param>
        void Init(Dictionary<string, INotifyPropertyChanged> objnames)
        {
            ObjNames = objnames;

            foreach (INotifyPropertyChanged obj in ObjNames.Values)
            {
                obj.PropertyChanged += Data_PropertyChanged;
            }
        }
        /// <summary>
        /// 在Init后,删除某个obj
        /// </summary>
        /// <param name="objname"></param>
        public void Remove(string objname)
        {
            ObjNames[objname].PropertyChanged -= Data_PropertyChanged;
            ObjNames.Remove(objname);
        }
        /// <summary>
        /// 在Init后,添加某个obj
        /// </summary>
        public void Add(string objname, INotifyPropertyChanged obj)
        {
            ObjNames.Add(objname, obj);

            //向服务器,只获取这个对象的数据
            string json = JsonConvert.SerializeObject(objname);
            FObjBase.FObjSys.Current.CallFunctionEx(mConn, mServerID, ID, SYNCPROP_OBJ_INTERFACE.CALL_GET_DATA,
                Misc.Converter.StringToBytes(json));
        }
        /// <summary>
        /// 属性变化时通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
76 77 78 79 80 81 82 83
            PropertyInfo property = sender.GetType().GetProperty(e.PropertyName);
            if (!property.CanWrite) {
                return;
            }

            if (sender is IPropertyOpt)
            {
                var opt = (IPropertyOpt)sender;
84 85 86 87 88 89
                string[] sync = opt.GetSyncPropNames();
                if (sync != null && sync.Count() != 0)
                {
                    if (!sync.Contains(e.PropertyName))
                    {
                        //没有包含,不需要同步
90 91 92
                        return;
                    }
                }
93 94 95 96 97 98 99 100 101 102 103 104
                else
                {
                    string[] nosync = opt.GetNoSyncPropNames();
                    if (nosync != null && nosync.Count() != 0)
                    {
                        if (nosync.Contains(e.PropertyName))
                        {
                            //这个不需要同步
                            return;
                        }
                    }
                }
105 106
            }

潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            string objname = (from kv in ObjNames where kv.Value == sender select kv.Key).First();

            Dictionary<string, Dictionary<string, object>> DsDso = new Dictionary<string, Dictionary<string, object>>
            {
                {
                    objname, new Dictionary<string, object>
                    {
                        {e.PropertyName, Misc.PropertiesManager.GetValue(sender, e.PropertyName)}
                    }
                }
            };

            string json = JsonConvert.SerializeObject(DsDso);
            FObjBase.FObjSys.Current.CallFunctionEx(mConn, mServerID, ID, SYNCPROP_OBJ_INTERFACE.CALL_SET_DATA,
                Misc.Converter.StringToBytes(json));
        }

        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            
            if (from.IsConnected)
            {
                CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                    SYNCPROP_OBJ_INTERFACE.CALL_GET_ALL_DATA, null);


                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case SYNCPROP_OBJ_INTERFACE.PUSH_DATA:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        Dictionary<string, Dictionary<string, object>> DsDso = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(json);
147
                        IsInPushValue = true;
潘栩锋's avatar
潘栩锋 committed
148 149 150 151
                        foreach (var Dso in DsDso)
                        {
                            INotifyPropertyChanged obj = ObjNames[Dso.Key] as INotifyPropertyChanged;
                            obj.PropertyChanged -= Data_PropertyChanged;
152
                            
潘栩锋's avatar
潘栩锋 committed
153 154 155 156
                            foreach (var dv in Dso.Value)
                            {
                                PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
                            }
157
                            
潘栩锋's avatar
潘栩锋 committed
158 159
                            obj.PropertyChanged += Data_PropertyChanged;
                        }
160
                        IsInPushValue = false;
潘栩锋's avatar
潘栩锋 committed
161 162 163 164 165 166 167 168 169 170 171 172 173
                    }
                    break;
            }
        }

        public override void PushCallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] retdata, object AsyncDelegate, object AsyncState)
        {
            switch (funcid)
            {
                case SYNCPROP_OBJ_INTERFACE.CALL_GET_ALL_DATA:
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        Dictionary<string, Dictionary<string, object>> DsDso = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(json);
174
                        IsInPushValue = true;
潘栩锋's avatar
潘栩锋 committed
175 176
                        foreach (var Dso in DsDso)
                        {
177
                            if (ObjNames.ContainsKey(Dso.Key))
潘栩锋's avatar
潘栩锋 committed
178
                            {
179 180 181 182 183 184 185
                                INotifyPropertyChanged obj = ObjNames[Dso.Key] as INotifyPropertyChanged;
                                obj.PropertyChanged -= Data_PropertyChanged;
                                foreach (var dv in Dso.Value)
                                {
                                    PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
                                }
                                obj.PropertyChanged += Data_PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
186 187
                            }
                        }
188
                        IsInPushValue = false;
潘栩锋's avatar
潘栩锋 committed
189 190 191 192 193 194 195
                    }
                    break;
            }
        }
    }
    
}