-
Notifications
You must be signed in to change notification settings - Fork 41
/
basestrategy.go
473 lines (404 loc) · 9.74 KB
/
basestrategy.go
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package goalgo
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"github.com/frankrap/goalgo/log"
"runtime/debug"
"github.com/Workiva/go-datastructures/queue"
"github.com/hashicorp/go-plugin"
)
const (
// OptionTag 选项Tag
OptionTag = "option"
)
// OptionInfo 参数信息
type OptionInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Value interface{} `json:"value"`
DefaultValue interface{} `json:"default_value"`
}
// AfterOptionsChanged 策略参数改变接口,实现此接口的策略,在动态改变参数后触发此方法
type AfterOptionsChanged interface {
AfterOptionsChanged()
}
// BaseStrategy 策略基础类
type BaseStrategy struct {
self interface{}
mutex sync.RWMutex
commandQueue queue.Queue
status RobotStatus
proxy string
}
// SetSelf 设置 self 对象
func (s *BaseStrategy) SetSelf(self Strategy) {
s.self = self.(interface{})
}
// SetProxy 设置访问网络的代理(主要方便本地测试,如:http://127.0.0.1:1080)
func (s *BaseStrategy) SetProxy(proxy string) {
s.proxy = proxy
}
// GetProxy 获取当前代理
func (s *BaseStrategy) GetProxy() string {
return s.proxy
}
// GetState 获取策略状态
func (s *BaseStrategy) GetState() RobotStatus {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.status
}
// IsRunning 是否运行中
func (s *BaseStrategy) IsRunning() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.status == RobotStatusRunning
}
// GetOptions 获取参数
func (s *BaseStrategy) GetOptions() (optionMap map[string]*OptionInfo) {
//log.Info("GetOptions")
optionMap = map[string]*OptionInfo{}
if s.self == nil {
return
}
val := reflect.ValueOf(s.self)
// If it's an interface or a pointer, unwrap it.
if val.Kind() == reflect.Ptr && val.Elem().Kind() == reflect.Struct {
val = val.Elem()
} else {
return
}
valNumFields := val.NumField()
for i := 0; i < valNumFields; i++ {
field := val.Field(i)
fieldKind := field.Kind()
if fieldKind == reflect.Ptr && field.Elem().Kind() == reflect.Struct {
continue
}
typeField := val.Type().Field(i)
fieldName := typeField.Name
tag := typeField.Tag
if !field.CanInterface() {
continue
}
option := tag.Get(OptionTag)
if option == "" {
continue
}
var description string
var defaultValueString string
index := strings.Index(option, ",")
//fmt.Printf("tag: %v i: %v\n", option, index)
if index != -1 {
description = option[0:index]
defaultValueString = option[index+1:]
} else {
description = option
}
value := field.Interface()
defaultValue := s.getDefaultValue(fieldKind, defaultValueString)
optionMap[fieldName] = &OptionInfo{
Name: fieldName,
Description: description,
Type: typeField.Type.String(),
Value: value,
DefaultValue: defaultValue,
}
//log.Infof("F: %v V: %v", fieldName, value)
}
return
}
func (s *BaseStrategy) getDefaultValue(kind reflect.Kind, value string) interface{} {
switch kind {
case reflect.Bool:
return ToBool(value)
case reflect.String:
return value
case reflect.Int:
return ToInt(value)
case reflect.Int8:
return int8(ToInt(value))
case reflect.Int16:
return int16(ToInt(value))
case reflect.Int32:
return int32(ToInt(value))
case reflect.Int64:
return ToInt64(value)
case reflect.Uint:
return uint(ToInt(value))
case reflect.Uint8:
return uint8(ToInt(value))
case reflect.Uint16:
return uint16(ToInt(value))
case reflect.Uint32:
return uint32(ToInt(value))
case reflect.Uint64:
return uint64(ToInt64(value))
case reflect.Float32:
return ToFloat32(value)
case reflect.Float64:
return ToFloat(value)
}
return 0
}
// SetOptions 设置参数
func (s *BaseStrategy) SetOptions(options map[string]interface{}) plugin.BasicError {
log.Info("SetOptions")
if len(options) == 0 {
return plugin.BasicError{}
}
rawOptions := s.GetOptions()
// 反射成员变量
val := reflect.ValueOf(s.self)
// If it's an interface or a pointer, unwrap it.
if val.Kind() == reflect.Ptr && val.Elem().Kind() == reflect.Struct {
val = val.Elem()
} else {
return plugin.BasicError{}
}
for name, value := range options {
var fieldName string
if ipi, ok := rawOptions[name]; !ok {
continue
} else {
fieldName = ipi.Name
}
//fmt.Println(fieldName)
v := val.FieldByName(fieldName)
if !v.IsValid() {
continue
}
switch v.Kind() {
default:
fmt.Printf("Error Kind: %v\n", v.Kind())
case reflect.Bool:
v.SetBool(ToBool(value))
case reflect.String:
v.SetString(value.(string))
case reflect.Int:
v.SetInt(ToInt64(value))
case reflect.Int8:
v.SetInt(ToInt64(value))
case reflect.Int16:
v.SetInt(ToInt64(value))
case reflect.Int32:
v.SetInt(ToInt64(value))
case reflect.Int64:
v.SetInt(ToInt64(value))
case reflect.Uint:
v.SetUint(ToUint64(value))
case reflect.Uint8:
v.SetUint(ToUint64(value))
case reflect.Uint16:
v.SetUint(ToUint64(value))
case reflect.Uint32:
v.SetUint(ToUint64(value))
case reflect.Uint64:
v.SetUint(ToUint64(value))
case reflect.Float32:
v.SetFloat(ToFloat(value))
case reflect.Float64:
v.SetFloat(ToFloat(value))
// case reflect.Struct:
// v.Set(reflect.ValueOf(value))
}
}
// 触发事件
if v, ok := s.self.(AfterOptionsChanged); ok {
v.AfterOptionsChanged()
}
return plugin.BasicError{}
}
// QueueCommand 命令入队列
func (s *BaseStrategy) QueueCommand(command string) plugin.BasicError {
cmd := Command{}
err := json.Unmarshal([]byte(command), &cmd)
if err != nil {
return plugin.BasicError{}
}
_ = s.commandQueue.Put(&cmd)
return plugin.BasicError{}
}
// QueueCommandRaw 命令入队列
func (s *BaseStrategy) QueueCommandRaw(command Command) {
cmd := command
_ = s.commandQueue.Put(&cmd)
}
// GetCommand 获取一个命令结构
func (s *BaseStrategy) GetCommand() *Command {
_, err := s.commandQueue.Peek()
if err != nil {
return nil
}
result, err := s.commandQueue.Get(1)
if err != nil {
return nil
}
if len(result) != 1 {
return nil
}
return result[0].(*Command)
}
// Start 启动
func (s *BaseStrategy) Start() plugin.BasicError {
go s.run()
return plugin.BasicError{}
}
func (s *BaseStrategy) run() {
defer func() {
if err := recover(); err != nil {
log.Errorf("Run error: %v", err)
s.status = RobotStatusStopped
}
}()
//log.Info("Start")
if s.self == nil {
log.Error("The strategy this is nil")
s.status = RobotStatusStopped
s.updateStatus(s.status)
return
}
strategy, ok := s.self.(Strategy)
if !ok {
log.Error("The strategy does not implement Strategy")
s.status = RobotStatusStopped
s.updateStatus(s.status)
return
}
var rError error
var pErr plugin.BasicError
// Set options
func() {
defer func() {
if r := recover(); r != nil {
rError = fmt.Errorf("%v", r)
}
}()
client := GetClient()
options, err := client.GetRobotOptions("", id)
if err != nil {
log.Errorf("GetRobotOptions error: %v", err)
}
mOptions := map[string]interface{}{}
for _, v := range options {
mOptions[v.Key] = v.Value
}
pErr = strategy.SetOptions(mOptions)
}()
if rError != nil {
log.Errorf("Setup error: %v", rError)
s.status = RobotStatusError
s.updateStatus(s.status)
return
}
if pErr.Error() != "" {
log.Errorf("SetOptions error: %v", pErr.Error())
s.status = RobotStatusError
s.updateStatus(s.status)
return
}
// Setup
func() {
defer func() {
if r := recover(); r != nil {
rError = fmt.Errorf("%v", r)
}
}()
client := GetClient()
exchanges, err := client.GetRobotExchangeInfo("", id)
if err != nil {
log.Errorf("GetRobotExchangeInfo error: %v", err)
} else {
var params []ExchangeParams
for _, ex := range exchanges {
params = append(params, ExchangeParams{
Label: ex.Label,
Name: ex.Name,
AccessKey: ex.AccessKey,
SecretKey: ex.SecretKey,
})
}
//log.Info("Setup...")
rError = strategy.Setup(params)
//log.Info("Setup.")
}
}()
if rError != nil {
log.Errorf("Setup error: %v", rError)
s.status = RobotStatusError
s.updateStatus(s.status)
return
}
func() {
defer func() {
if r := recover(); r != nil {
rError = fmt.Errorf("%v", r)
}
}()
rError = strategy.Init()
}()
if rError != nil {
log.Errorf("Init error: %v", rError)
s.status = RobotStatusError
s.updateStatus(s.status)
return
}
s.status = RobotStatusRunning
func() {
defer func() {
if r := recover(); r != nil {
rError = fmt.Errorf("%v", r)
log.Errorf("Run error: stack=%v", string(debug.Stack()))
}
}()
rError = strategy.Run()
}()
//log.Info("Run done")
if rError != nil {
s.status = RobotStatusError
log.Errorf("Run error: %v", rError)
} else {
s.status = RobotStatusStopped
log.Info("Stopped")
}
// 同步状态
s.updateStatus(s.status)
}
func (s *BaseStrategy) updateStatus(status RobotStatus) {
client := GetClient()
client.UpdateStatus(id, status)
}
// Stop 停止
func (s *BaseStrategy) Stop() plugin.BasicError {
log.Info("OnStop")
s.mutex.Lock()
defer s.mutex.Unlock()
if s.status == RobotStatusStopped {
return plugin.BasicError{}
}
if s.status != RobotStatusRunning {
return plugin.BasicError{Message: "State error"}
}
s.status = RobotStatusRequested
return plugin.BasicError{}
}
// Pause 暂停
func (s *BaseStrategy) Pause() plugin.BasicError {
return plugin.BasicError{}
}
// GetValue 获取一个全局变量
func (s *BaseStrategy) GetValue(key string) (Value, error) {
return GetValue(key)
}
// SetValue 设置一个全局变量
func (s *BaseStrategy) SetValue(key string, value Value) error {
return SetValue(key, value)
}
// UpdateStat 更新统计数据
func (s *BaseStrategy) UpdateStat(name string, value []byte) error {
return GetClient().UpdateStat(name, value)
}