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
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.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Client
{
public class BufferServiceClient<T> : FObjServiceClient, IBuffer<T>
{
#region property
public int NewestID { get; private set; } = -1;
public int Count { get; private set; } = 0;
/// <summary>
/// 总容量,当总数量 大于等于 总容量的 100%, 前面的10%数据会被删除
/// </summary>
public int Capacity { get; set; } = 1000;
#endregion
public event NotifyBufferChangedEventHandler<T> BufferChanged;
public event PropertyChangedEventHandler PropertyChanged;
public BufferServiceClient(UInt32 serviceId) : base(serviceId) { }
/// <summary>
///
/// </summary>
/// <param name="serviceId"></param>
/// <param name="connName"></param>
public BufferServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
public override void ConnectNotify(IFConn from)
{
base.ConnectNotify(from);
if (from.IsConnected)
{
//获取所有数据,设置推送
CurrObjSys.GetValueEx(
mConn, mServerID, ID,
BUFFER_OBJ_INTERFACE.GET_PARAMS);
CurrObjSys.GetValueEx(
mConn, mServerID, ID,
BUFFER_OBJ_INTERFACE.GET_STATUS);
CurrObjSys.SenseConfigEx(
mConn, mServerID, ID,
0xffffffff,
SENSE_CONFIG.ADD);
BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<T>()
{
Action = NotifyBufferChangedAction.IsConnected
});
}
}
public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
{
switch (memid)
{
case BUFFER_OBJ_INTERFACE.GET_PARAMS:
{
string json = Misc.Converter.BytesToString(infodata);
var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_Params>(json);
Capacity = p.capcity;
}
break;
case BUFFER_OBJ_INTERFACE.GET_STATUS:
{
string json = Misc.Converter.BytesToString(infodata);
var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_Status>(json);
Count = p.count;
NewestID = p.newestId;
}
break;
}
}
public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
{
switch (infoid)
{
case BUFFER_OBJ_INTERFACE.PUSH_STATUS:
case BUFFER_OBJ_INTERFACE.PUSH_PARAMS:
{
PushGetValue(from, srcid, infoid, infodata);
}
break;
case BUFFER_OBJ_INTERFACE.PUSH_BUFFERCHANGED:
{
string json = Misc.Converter.BytesToString(infodata);
NotifyBufferChangedEventArgs<T> p = JsonConvert.DeserializeObject<NotifyBufferChangedEventArgs<T>>(json);
BufferChanged?.Invoke(this, p);
}
break;
}
}
public override void PushCallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] retdata, object AsyncDelegate, object AsyncState)
{
switch (funcid)
{
case BUFFER_OBJ_INTERFACE.CALL_GETRECORD:
case BUFFER_OBJ_INTERFACE.CALL_GETRECORD_NEWEST:
{
string json = Misc.Converter.BytesToString(retdata);
GetRecordReponse<T> p = JsonConvert.DeserializeObject<GetRecordReponse<T>>(json);
((AsyncCBHandler)AsyncDelegate)(AsyncState, p);
}
break;
}
}
public void Reset()
{
CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_RESET, null);
}
public void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
{
BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest request = new BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest()
{
last_id = last_id,
count = count
};
string json = JsonConvert.SerializeObject(request);
CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_GETRECORD,
Misc.Converter.StringToBytes(json),
asyncCB, asyncContext
);
}
public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
{
BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest request = new BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest()
{
count = count
};
string json = JsonConvert.SerializeObject(request);
CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_GETRECORD_NEWEST,
Misc.Converter.StringToBytes(json),
asyncCB, asyncContext
);
}
public void Apply()
{
var p = new BUFFER_OBJ_INTERFACE.Pack_Params()
{
capcity = Capacity
};
string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
CurrObjSys.SetValueEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.SET_PARAMS,
Misc.Converter.StringToBytes(json));
}
}
}