-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathfutuapi.go
More file actions
362 lines (322 loc) · 9.23 KB
/
futuapi.go
File metadata and controls
362 lines (322 loc) · 9.23 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package futuapi
import (
"context"
"errors"
"time"
"google.golang.org/protobuf/proto"
"github.com/hurisheng/go-futu-api/pb/common"
"github.com/hurisheng/go-futu-api/pb/getdelaystatistics"
"github.com/hurisheng/go-futu-api/pb/getglobalstate"
"github.com/hurisheng/go-futu-api/pb/getuserinfo"
"github.com/hurisheng/go-futu-api/pb/initconnect"
"github.com/hurisheng/go-futu-api/pb/keepalive"
"github.com/hurisheng/go-futu-api/pb/notify"
"github.com/hurisheng/go-futu-api/pb/verification"
"github.com/hurisheng/go-futu-api/protocol"
)
const (
ProtoIDInitConnect = 1001 //InitConnect 初始化连接
ProtoIDGetGlobalState = 1002 //GetGlobalState 获取全局状态
ProtoIDNotify = 1003 //Notify 系统通知推送
ProtoIDKeepAlive = 1004 //KeepAlive 保活心跳
ProtoIDGetUserInfo = 1005 //GetUserInfo 获取用户信息
ProtoIDVerification = 1006 // 请求或输入验证码
ProtoIDGetDelayStatistics = 1007 //GetDelayStatistics 获取延迟统计
)
var workers map[uint32]protocol.Worker = make(map[uint32]protocol.Worker)
func init() {
workers[ProtoIDInitConnect] = protocol.NewGetter()
workers[ProtoIDGetGlobalState] = protocol.NewGetter()
workers[ProtoIDNotify] = protocol.NewUpdater()
workers[ProtoIDKeepAlive] = protocol.NewGetter()
workers[ProtoIDGetUserInfo] = protocol.NewGetter()
workers[ProtoIDVerification] = protocol.NewGetter()
workers[ProtoIDGetDelayStatistics] = protocol.NewGetter()
}
var (
ErrParameters = errors.New("parameters missing or invalid")
ErrInterrupted = errors.New("process is interrupted")
ErrChannelClosed = errors.New("channel is closed")
)
type OptionalInt32 struct {
Value int32
}
type OptionalUInt64 struct {
Value uint64
}
type OptionalDouble struct {
Value float64
}
type OptionalBool struct {
Value bool
}
// FutuAPI 是富途开放API的主要操作对象。
type FutuAPI struct {
// 连接配置,通过方法设置,不设置默认为零值
clientVer int32
clientID string
recvNotify bool
encAlgo common.PacketEncAlgo
protoFmt common.ProtoFmt
// TCP连接,连接后设置
connID uint64
userID uint64
// protocol
proto *protocol.FutuProtocol
// 发送心跳的定时器,连接后设置
ticker *time.Ticker
// 心跳定时器关闭信号通道
done chan struct{}
}
// NewFutuAPI 创建API对象,并启动goroutine进行发送保活心跳.
func NewFutuAPI() *FutuAPI {
return &FutuAPI{
done: make(chan struct{}),
}
}
// 设置调用接口信息, 非必调接口
func (api *FutuAPI) SetClientInfo(id string, ver int32) {
api.clientID = id
api.clientVer = ver
}
// 设置通讯协议 body 格式, 目前支持 Protobuf|Json 两种格式,默认 ProtoBuf, 非必调接口
func (api *FutuAPI) SetProtoFmt(fmt common.ProtoFmt) {
api.protoFmt = fmt
}
// 获取连接 ID,连接初始化成功后才会有值
func (api *FutuAPI) ConnID() uint64 {
return api.connID
}
func (api *FutuAPI) UserID() uint64 {
return api.userID
}
func (api *FutuAPI) SetRecvNotify(recv bool) {
api.recvNotify = recv
}
func (api *FutuAPI) SetEncAlgo(algo common.PacketEncAlgo) {
api.encAlgo = algo
}
func (api *FutuAPI) packetID() *common.PacketID {
return &common.PacketID{
ConnID: proto.Uint64(api.connID),
SerialNo: proto.Uint32(api.proto.SerialNo()),
}
}
// 连接FutuOpenD
func (api *FutuAPI) Connect(ctx context.Context, address string) error {
proto, err := protocol.Connect(address, workers)
if err != nil {
return err
}
api.proto = proto
resp, err := api.initConnect(ctx, api.clientVer, api.clientID, api.recvNotify, api.encAlgo, api.protoFmt, "go")
if err != nil {
return err
}
api.connID = resp.GetConnID()
api.userID = resp.GetLoginUserID()
if d := resp.GetKeepAliveInterval(); d > 0 {
api.ticker = time.NewTicker(time.Second * time.Duration(d))
go api.heartBeat(ctx)
}
return nil
}
// 关闭连接
func (api *FutuAPI) Close(ctx context.Context) error {
if err := api.proto.Close(); err != nil {
return err
}
close(api.done)
return nil
}
func (api *FutuAPI) heartBeat(ctx context.Context) {
for {
select {
case <-api.done:
api.ticker.Stop()
return
case <-api.ticker.C:
if _, err := api.keepAlive(ctx, time.Now().Unix()); err != nil {
return
}
}
}
}
// 初始化连接
func (api *FutuAPI) initConnect(ctx context.Context, clientVer int32, clientID string,
recvNotify bool, encAlgo common.PacketEncAlgo, protoFmt common.ProtoFmt, lang string) (*initconnect.S2C, error) {
if clientID == "" {
return nil, ErrParameters
}
// 请求参数
req := &initconnect.Request{
C2S: &initconnect.C2S{
ClientVer: proto.Int32(clientVer),
ClientID: proto.String(clientID),
RecvNotify: proto.Bool(recvNotify),
PacketEncAlgo: proto.Int32(int32(encAlgo)),
PushProtoFmt: proto.Int32(int32(protoFmt)),
},
}
if lang != "" {
req.C2S.ProgrammingLanguage = proto.String(lang)
}
// 发送请求,同步返回结果
ch := make(chan *initconnect.Response)
if err := api.proto.RegisterGet(ProtoIDInitConnect, req, protocol.NewProtobufChan(ch)); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return resp.GetS2C(), protocol.Error(resp)
}
}
// KeepAlive 保活心跳
func (api *FutuAPI) keepAlive(ctx context.Context, t int64) (int64, error) {
// 请求参数
req := &keepalive.Request{
C2S: &keepalive.C2S{
Time: proto.Int64(t),
},
}
// 发送请求,同步返回结果
ch := make(chan *keepalive.Response)
if err := api.proto.RegisterGet(ProtoIDKeepAlive, req, protocol.NewProtobufChan(ch)); err != nil {
return 0, err
}
select {
case <-ctx.Done():
return 0, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return 0, ErrChannelClosed
}
return resp.GetS2C().GetTime(), protocol.Error(resp)
}
}
// 获取全局状态
func (api *FutuAPI) GetGlobalState(ctx context.Context) (*getglobalstate.S2C, error) {
// 请求参数
req := &getglobalstate.Request{
C2S: &getglobalstate.C2S{
UserID: proto.Uint64(api.UserID()),
},
}
// 发送请求,同步返回结果
ch := make(chan *getglobalstate.Response)
if err := api.proto.RegisterGet(ProtoIDGetGlobalState, req, protocol.NewProtobufChan(ch)); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return resp.GetS2C(), protocol.Error(resp)
}
}
// 系统推送通知
func (api *FutuAPI) SysNotify(ctx context.Context) (<-chan *notify.Response, error) {
ch := make(chan *notify.Response)
if err := api.proto.RegisterUpdate(ProtoIDNotify, protocol.NewProtobufChan(ch)); err != nil {
return nil, err
}
return ch, nil
}
// 获取延迟统计
func (api *FutuAPI) GetDelayStatistics(ctx context.Context, typeList []getdelaystatistics.DelayStatisticsType,
pushStage getdelaystatistics.QotPushStage, segmentList []int32) (*getdelaystatistics.S2C, error) {
if len(typeList) == 0 {
return nil, ErrParameters
}
// 请求参数
req := &getdelaystatistics.Request{
C2S: &getdelaystatistics.C2S{
TypeList: make([]int32, len(typeList)),
},
}
for i, v := range typeList {
req.C2S.TypeList[i] = int32(v)
}
if pushStage != getdelaystatistics.QotPushStage_QotPushStage_Unkonw {
req.C2S.QotPushStage = proto.Int32(int32(pushStage))
}
if len(segmentList) != 0 {
req.C2S.SegmentList = segmentList
}
// 发送请求,同步返回结果
ch := make(chan *getdelaystatistics.Response)
if err := api.proto.RegisterGet(ProtoIDGetDelayStatistics, req, protocol.NewProtobufChan(ch)); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return resp.GetS2C(), protocol.Error(resp)
}
}
func (api *FutuAPI) Verify(ctx context.Context, vType verification.VerificationType, op verification.VerificationOp,
code string) error {
if vType == verification.VerificationType_VerificationType_Unknow ||
op == verification.VerificationOp_VerificationOp_Unknow ||
(op == verification.VerificationOp_VerificationOp_InputAndLogin && code == "") {
return ErrParameters
}
req := &verification.Request{
C2S: &verification.C2S{
Type: proto.Int32(int32(vType)),
Op: proto.Int32(int32(op)),
},
}
if code != "" {
req.C2S.Code = proto.String(code)
}
ch := make(chan *verification.Response)
if err := api.proto.RegisterGet(ProtoIDVerification, req, protocol.NewProtobufChan(ch)); err != nil {
return err
}
select {
case <-ctx.Done():
return ErrInterrupted
case resp, ok := <-ch:
if !ok {
return ErrChannelClosed
}
return protocol.Error(resp)
}
}
// 获取用户信息, flag = 0返回全部信息
func (api *FutuAPI) GetUserInfo(ctx context.Context, flag getuserinfo.UserInfoField) (*getuserinfo.S2C, error) {
// 请求参数
req := &getuserinfo.Request{
C2S: &getuserinfo.C2S{},
}
if flag != 0 {
req.C2S.Flag = proto.Int32(int32(flag))
}
// 发送请求,同步返回结果
ch := make(chan *getuserinfo.Response)
if err := api.proto.RegisterGet(ProtoIDGetUserInfo, req, protocol.NewProtobufChan(ch)); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return resp.GetS2C(), protocol.Error(resp)
}
}