1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
namespace FLY.OBJComponents.Client
{
public class SyncPropServiceClient : FObj
{
Dictionary<string, INotifyPropertyChanged> ObjNames;
IFConn mConn;
UInt32 mServerID;
//public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 当前 INotifyPropertyChanged 对象 引发了 PropertyChanged, 是由于 接收到数据导致的
/// </summary>
public bool IsInPushValue { get;protected set; }
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)
{
PropertyInfo property = sender.GetType().GetProperty(e.PropertyName);
if (!property.CanWrite) {
return;
}
if (sender is IPropertyOpt)
{
var opt = (IPropertyOpt)sender;
string[] sync = opt.GetSyncPropNames();
if (sync != null && sync.Count() != 0)
{
if (!sync.Contains(e.PropertyName))
{
//没有包含,不需要同步
return;
}
}
else
{
string[] nosync = opt.GetNoSyncPropNames();
if (nosync != null && nosync.Count() != 0)
{
if (nosync.Contains(e.PropertyName))
{
//这个不需要同步
return;
}
}
}
}
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);
IsInPushValue = true;
foreach (var Dso in DsDso)
{
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;
}
IsInPushValue = false;
}
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);
IsInPushValue = true;
foreach (var Dso in DsDso)
{
if (ObjNames.ContainsKey(Dso.Key))
{
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;
}
}
IsInPushValue = false;
}
break;
}
}
}
}