zhangmeng
2020-01-13 94e9f50569bd20a697edb36711d017de1c19d1a5
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
package sdk
 
import (
    "analysis/logo"
    "analysis/work"
    "analysis/work/cache"
    "context"
    "encoding/binary"
    "math"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/ruleForSdk.git"
 
    "github.com/gogo/protobuf/proto"
    sq "github.com/yireyun/go-queue"
)
 
// Engine interface sdk
type Engine interface {
    Init() bool
    Run(context.Context, <-chan work.MsgRS, chan<- work.MsgRS, string)
}
 
func createQueue(length int) *sq.EsQueue {
    q := sq.NewQueue(uint32(length))
    for i := 0; i < length; i++ {
        q.Put(i)
    }
    return q
}
 
func ejectResult(res []byte, msg work.MsgRS, out chan<- work.MsgRS) {
    //压缩数据
    // rData, err := work.Compress(res)
    // if err != nil {
    //     logo.Errorln("compress result error: ", err)
    //     return
    // }
 
    if res == nil {
        out <- msg
        return
    }
    index := int(msg.Msg.Tasklab.Index)
 
    if index >= len(msg.Msg.Tasklab.Sdkinfos) {
        return
    }
 
    // var tmpD []byte
    // var tmpE error
    // tmpD, tmpE = proto.Marshal(&msg.Msg)
    // if tmpE == nil {
    //     logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " orig SDK DATA: ", len(tmpD), " index :", index)
    // }
 
    msg.Msg.Tasklab.Sdkinfos[index].Sdkdata = res
 
    // tmpD, tmpE = proto.Marshal(&msg.Msg)
    // if tmpE == nil {
    //     logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " after SDK DATA: ", len(tmpD), " index :", index)
    // }
 
    // if index == 1{
    // logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " orig SDK DATA: ", len(res), " index :", index)
    // }
 
    out <- msg
}
 
func byteToFloat32(bytes []byte) float32 {
    bits := binary.LittleEndian.Uint32(bytes)
    return math.Float32frombits(bits)
}
 
func float32ToByte(float float32) []byte {
    bits := math.Float32bits(float)
    bytes := make([]byte, 4)
    binary.LittleEndian.PutUint32(bytes, bits)
    return bytes
}
 
/////////////////////////////////////////////////////////////
func validRemoteMessage(msg work.MsgRS, fnName string) bool {
    if msg.Msg.Tasklab == nil {
        logo.Errorf("%s recieve msg nil\n", fnName)
        return false
    }
 
    sdkLen := len(msg.Msg.Tasklab.Sdkinfos)
    if sdkLen == 0 {
        logo.Errorf("%s has no sdk info\n", fnName)
        return false
    }
 
    curIndex := int(msg.Msg.Tasklab.Index)
    if curIndex < 0 || curIndex >= sdkLen {
        logo.Errorf("%s tasklab index %d error\n", fnName, curIndex)
        return false
    }
    if msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype != fnName {
        logo.Errorf("%s is different from %s\n", fnName, msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype)
        return false
    }
    return true
}
 
func unpackImage(msg work.MsgRS, fnName string) *protomsg.Image {
    // 解压获取传入的数据
    bData, err := work.UnCompress(msg.Msg.Data)
    if err != nil {
        logo.Errorf("%s uncompress image failed\n", fnName)
        return nil
    }
    // 反序列化数据得到sdk入参
    i := &protomsg.Image{}
    err = proto.Unmarshal(bData, i)
    if err != nil {
        logo.Errorf("%s protobuf decode CameraImage error : %s\n", fnName, err.Error())
        return nil
    }
    if i.Data == nil {
        logo.Errorf("%s protomsg.Image data null\n", fnName)
        return nil
    }
    return i
}
 
////////////////////////////////////////////////////////////
func filter(tid, sid string, score, angle float32, size int) bool {
     return true
    data := ruleForSdk.TargetData{
        TaskId: tid,
        SdkId:  sid,
        Score:  score * 100.0,
        Size:   size,
        Angle:  angle,
    }
    rules := cache.GetTaskSdkRules(data.TaskId)
    if rules == nil {
        return true
    }
    logo.Infoln("TASKSDKRULE: ", rules)
    return ruleForSdk.Judge(data, rules)
}