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.Text;
namespace FLY.OBJComponents.Server.OBJProxy
{
///
/// 同步属性 OBJ协议代理
///
public class SyncProp_OBJProxy : FObj
{
Dictionary ObjNames;
public SyncProp_OBJProxy(int objsys_idx, UInt32 serverID, Dictionary objnames) : base(objsys_idx)
{
ID = serverID;
ObjNames = objnames;
Init();
}
void Init()
{
foreach (var obj in ObjNames.Values)
{
obj.PropertyChanged += Data_PropertyChanged;
}
}
///
/// 属性变化时通知
///
///
///
void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
string objname = (from kv in ObjNames where kv.Value == sender select kv.Key).First();
Dictionary> DsDso = new Dictionary>
{
{
objname, new Dictionary
{
{e.PropertyName, Misc.PropertiesManager.GetValue(sender, e.PropertyName)}
}
}
};
string json = JsonConvert.SerializeObject(DsDso);
FObjBase.FObjSys.Current.PushObjInfoEx(this, SYNCPROP_OBJ_INTERFACE.PUSH_DATA,
Misc.Converter.StringToBytes(json));
}
Dictionary GetDso(object obj)
{
List dnames = new List();
if (obj is IPropertyOpt)
{
IPropertyOpt opt = obj as IPropertyOpt;
IEnumerable sync = opt.GetSyncPropNames();
if (sync == null)
{
dnames.AddRange(Misc.PropertiesManager.GetAllPropertyNames(obj));
string[] nosync = opt.GetNoSyncPropNames();
if (nosync != null)
{
dnames.RemoveAll(n => nosync.Contains(n));
}
}
else
dnames.AddRange(sync);
}
else
{
dnames.AddRange(Misc.PropertiesManager.GetAllPropertyNames(obj));
}
Dictionary Dso = new Dictionary();
foreach (string dn in dnames)
{
object val = Misc.PropertiesManager.GetValue(obj, dn);
Dso.Add(dn, val);
}
return Dso;
}
public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
{
switch (funcid)
{
case SYNCPROP_OBJ_INTERFACE.CALL_GET_ALL_DATA:
{
Dictionary> DsDso = new Dictionary>();
foreach (var kv in ObjNames)
{
DsDso.Add(kv.Key, GetDso(kv.Value));
}
string json = JsonConvert.SerializeObject(DsDso);
FObjBase.FObjSys.Current.PushCallFunctionEx(from, srcid, this.ID, magic, funcid, Misc.Converter.StringToBytes(json));
}
break;
case SYNCPROP_OBJ_INTERFACE.CALL_GET_DATA:
{
string json = Misc.Converter.BytesToString(infodata);
string objname = JsonConvert.DeserializeObject(json);
Dictionary> DsDso = new Dictionary>();
INotifyPropertyChanged obj = ObjNames[objname];
DsDso.Add(objname, GetDso(obj));
json = JsonConvert.SerializeObject(DsDso);
FObjBase.FObjSys.Current.PushCallFunctionEx(from, srcid, this.ID, magic, funcid, Misc.Converter.StringToBytes(json));
}
break;
case SYNCPROP_OBJ_INTERFACE.CALL_SET_DATA:
{
string json = Misc.Converter.BytesToString(infodata);
Dictionary> DsDso = JsonConvert.DeserializeObject> > (json);
foreach (var Dso in DsDso)
{
object obj = ObjNames[Dso.Key];
foreach (var dv in Dso.Value)
{
PropertiesManager_JSON.SetValue(obj, dv.Key, dv.Value);
}
}
}
break;
}
}
}
}