SyncPropServiceClient.cs 10.7 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
        //public event PropertyChangedEventHandler PropertyChanged;

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

29 30 31 32 33 34 35 36 37 38 39 40 41
        /// <summary>
        /// 同步完成
        /// </summary>
        public bool IsSynced { get; private set; }

        /// <summary>
        /// 缓存 发出 GetAllData指令,到收到回复 之间时段的 全部推送
        /// </summary>
        List<Dictionary<string, Dictionary<string, object>>> pushDataList = new List<Dictionary<string, Dictionary<string, object>>>();
        /// <summary>
        /// Add 函数, 添加的 objname, 必须等全部 都返回。  只要 addObjNameList.Count() 不为0, 也是要缓存 推送的
        /// </summary>
        List<string> addObjNameList = new List<string>();
潘栩锋's avatar
潘栩锋 committed
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

        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);

            //向服务器,只获取这个对象的数据
78 79 80 81 82

            //暂停接收推送。。。。
            IsSynced = false;
            addObjNameList.Add(objname);

潘栩锋's avatar
潘栩锋 committed
83 84 85 86 87 88 89 90 91 92 93
            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)
        {
94
            PropertyInfo property = sender.GetType().GetProperty(e.PropertyName);
95 96
            if (!property.CanWrite)
            {
97 98 99 100 101 102
                return;
            }

            if (sender is IPropertyOpt)
            {
                var opt = (IPropertyOpt)sender;
103 104 105 106 107 108
                string[] sync = opt.GetSyncPropNames();
                if (sync != null && sync.Count() != 0)
                {
                    if (!sync.Contains(e.PropertyName))
                    {
                        //没有包含,不需要同步
109 110 111
                        return;
                    }
                }
112 113 114 115 116 117 118 119 120 121 122 123
                else
                {
                    string[] nosync = opt.GetNoSyncPropNames();
                    if (nosync != null && nosync.Count() != 0)
                    {
                        if (nosync.Contains(e.PropertyName))
                        {
                            //这个不需要同步
                            return;
                        }
                    }
                }
124 125
            }

潘栩锋's avatar
潘栩锋 committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            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;
146 147 148 149

            IsSynced = false;
            pushDataList.Clear();

潘栩锋's avatar
潘栩锋 committed
150 151 152 153 154 155 156 157 158
            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);
            }
159

潘栩锋's avatar
潘栩锋 committed
160 161
        }

162 163


潘栩锋's avatar
潘栩锋 committed
164 165 166 167 168 169 170 171
        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);
172 173 174 175 176 177 178 179

                        if (!IsSynced)
                        {
                            //还没同步完成,先缓存
                            pushDataList.Add(DsDso);
                            return;
                        }

180
                        IsInPushValue = true;
181

潘栩锋's avatar
潘栩锋 committed
182 183
                        foreach (var Dso in DsDso)
                        {
184
                            INotifyPropertyChanged obj = ObjNames[Dso.Key];
潘栩锋's avatar
潘栩锋 committed
185
                            obj.PropertyChanged -= Data_PropertyChanged;
186

潘栩锋's avatar
潘栩锋 committed
187 188 189 190
                            foreach (var dv in Dso.Value)
                            {
                                PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
                            }
191

潘栩锋's avatar
潘栩锋 committed
192 193
                            obj.PropertyChanged += Data_PropertyChanged;
                        }
194
                        IsInPushValue = false;
潘栩锋's avatar
潘栩锋 committed
195 196 197 198
                    }
                    break;
            }
        }
199 200 201 202 203 204 205 206 207
        /// <summary>
        /// 处理全部 缓存的推送数据
        /// </summary>
        void DealPushDataList()
        {
            IsInPushValue = true;
            for (int i = 0; i < pushDataList.Count(); i++)
            {
                var DsDso = pushDataList[i];
潘栩锋's avatar
潘栩锋 committed
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
                foreach (var Dso in DsDso)
                {
                    INotifyPropertyChanged obj = ObjNames[Dso.Key];
                    obj.PropertyChanged -= Data_PropertyChanged;

                    foreach (var dv in Dso.Value)
                    {
                        PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
                    }

                    obj.PropertyChanged += Data_PropertyChanged;
                }
            }
            IsInPushValue = false;

            pushDataList.Clear();
        }
226
        public override void PushCallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] retdata, AsyncCBHandler asyncDelegate, object asyncContext)
潘栩锋's avatar
潘栩锋 committed
227 228 229 230
        {
            switch (funcid)
            {
                case SYNCPROP_OBJ_INTERFACE.CALL_GET_ALL_DATA:
231 232 233 234 235 236 237 238 239 240
                    {
                        //这个可能是大包来的,很慢,需要记录中途全部 push
                        string json = Misc.Converter.BytesToString(retdata);
                        //log.Debug("GetAllData:" +json);
                        Dictionary<string, Dictionary<string, object>> DsDso = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(json);
                        IsInPushValue = true;
                        foreach (var Dso in DsDso)
                        {
                            if (ObjNames.ContainsKey(Dso.Key))
                            {
241
                                INotifyPropertyChanged obj = ObjNames[Dso.Key];
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                                obj.PropertyChanged -= Data_PropertyChanged;
                                foreach (var dv in Dso.Value)
                                {
                                    PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
                                }
                                obj.PropertyChanged += Data_PropertyChanged;
                            }
                        }
                        IsInPushValue = false;
                        //完成同步
                        IsSynced = true;
                        //处理之前的推送事件
                        DealPushDataList();
                    }
                    break;
                case SYNCPROP_OBJ_INTERFACE.CALL_GET_DATA:
潘栩锋's avatar
潘栩锋 committed
258 259 260
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        Dictionary<string, Dictionary<string, object>> DsDso = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(json);
261
                        IsInPushValue = true;
潘栩锋's avatar
潘栩锋 committed
262 263
                        foreach (var Dso in DsDso)
                        {
264
                            if (ObjNames.ContainsKey(Dso.Key))
潘栩锋's avatar
潘栩锋 committed
265
                            {
266 267 268 269 270 271 272
                                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
273
                            }
274 275

                            addObjNameList.Remove(Dso.Key);
潘栩锋's avatar
潘栩锋 committed
276
                        }
277
                        IsInPushValue = false;
278 279 280 281 282 283 284 285 286

                        //全部接收完!!!
                        if (addObjNameList.Count() == 0)
                        {
                            //完成同步
                            IsSynced = true;
                            //处理之前的推送事件
                            DealPushDataList();
                        }
潘栩锋's avatar
潘栩锋 committed
287 288 289 290 291
                    }
                    break;
            }
        }
    }
292

潘栩锋's avatar
潘栩锋 committed
293
}