zhangzengfei
2024-10-19 1ca4879df9cc3c22181b2a200aa4b793116d3d35
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
package service
 
import (
    "basic.com/pubsub/esutil.git"
    "basic.com/valib/logger.git"
    "bytes"
    "data_msg_push_server/config"
    "data_msg_push_server/model"
    "data_msg_push_server/util"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "regexp"
)
 
func ConnectControl() bool {
    url := config.Options.ServerUrl
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println("连接失败", err)
        return false
    }
    defer resp.Body.Close()
 
    fmt.Println("连接成功!")
    return true
}
 
func GetTotal() (total int, err error) {
    url := "http://" + config.Options.EsUrl + "/ai_face_ocean/_search"
    queryDSL := `{
    "size": "1",
    "query": {
        "bool": {
            "filter": [
                {
                "term": {
                    "isDelete": false
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "updateTime": {
                "order": "asc"
            }
        }
    ]
}`
    buf, err := esutil.EsReq("POST", url, []byte(queryDSL))
    if err != nil {
        return 0, err
    }
    totals, err := util.SourceTotal(buf)
    if err != nil {
        return 0, err
    }
    if totals == 0 {
        return 0, nil
    }
    return totals, nil
}
 
func GetData() (*model.PushDataInfoV2, error) {
    //fmt.Println(config.Options.EsUrl)
    //fmt.Println(config.Options.ServerUrl)
    url := "http://" + config.Options.EsUrl + "/ai_face_ocean/_search"
    queryDSL := `{
    "size": "1",
    "query": {
        "bool": {
            "filter": [
                {
                "term": {
                    "isDelete": false
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "updateTime": {
                "order": "asc"
            }
        }
    ]
}`
 
    buf, err := esutil.EsReq("POST", url, []byte(queryDSL))
    if err != nil {
        return nil, err
    }
    source, err := util.Sourcelist(buf)
    if err != nil {
        return nil, err
    }
    if len(source) == 0 {
        return nil, nil
    }
 
    b, _ := json.Marshal(source[0])
 
    var esInfoV2 model.ESInfoV2
    err = json.Unmarshal(b, &esInfoV2)
    if err != nil {
        return nil, err
    }
 
    picMaxImages := make([]byte, 0) // 存储图片数据的数组
    if esInfoV2.PicMaxUrl != "" {
        picMaxImages, err = GetImageData("http://" + esInfoV2.PicMaxUrl)
        if err != nil {
            logger.Warn("获取大图数据失败:", err.Error())
        }
    }
 
    if len(picMaxImages) == 0 {
        return nil, errors.New("获取大图失败, id:" + esInfoV2.TargetInfo.PicSmUrl)
    }
 
    //fmt.Println("图片数据数组大小:", len(picMaxImages))
 
    picSmImages := make([]byte, 0) // 存储图片数据的数组
    if esInfoV2.TargetInfo.PicSmUrl != "" {
        picSmImages, err = GetImageData("http://" + esInfoV2.TargetInfo.PicSmUrl)
        if err != nil {
            fmt.Println("获取小图数据失败:", err)
        }
    }
 
    if len(picSmImages) == 0 {
        return nil, errors.New("获取小图数据失败, id:" + esInfoV2.Id)
    }
 
    var pushData = new(model.PushDataInfoV2)
    pushData.CameraId = esInfoV2.CameraId
    pushData.PicDate = esInfoV2.PicDate
    pushData.PicId = esInfoV2.Id
    pushData.CameraFloor = esInfoV2.CameraLocation.Floor
    pushData.Direction = esInfoV2.CameraLocation.Direction
    pushData.PicMaxImages = append(pushData.PicMaxImages, picMaxImages)
    pushData.PicSmImages = append(pushData.PicSmImages, picSmImages)
    //fmt.Println(model.PushDataInfo.SourceData)
 
    return pushData, nil
}
 
func extractFloor(s string) string {
    // 使用正则表达式匹配楼层信息
    re := regexp.MustCompile(`[-\d]+F`)
    matches := re.FindStringSubmatch(s)
    if len(matches) >= 1 {
        return matches[0] // 返回匹配到的楼层信息
    }
    return "" // 如果没有匹配到,返回空字符串
}
 
func SendData(pushDataInfo interface{}, url string) (id string, err error) {
    id = ""
    payload, err := json.Marshal(pushDataInfo)
    if err != nil {
        return id, err
    }
 
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
    if err != nil {
        return id, err
    }
 
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return id, err
    }
    defer resp.Body.Close()
 
    var responseData map[string]interface{}
    err = json.NewDecoder(resp.Body).Decode(&responseData)
    if err != nil {
        return id, err
    }
    id = responseData["data"].(string)
    // 处理响应
    // 这里可以根据实际需求进行处理,例如读取响应内容或检查状态码等
 
    return id, nil
 
}
 
func DeleteData(id string) bool {
    url := "http://" + config.Options.EsUrl + "/ai_face_ocean/_delete_by_query?refresh=true"
    deleteDSL := `{
                    "query":{
                        "bool":{
                            "filter":[{
                                "term":{
                                    "id":"` + id + `"
                                }
                            }]
                        }
                    }
                }`
    buf, err := esutil.EsReq("POST", url, []byte(deleteDSL))
    if err != nil {
        fmt.Println(err)
    }
    deleted, err := util.SourceDeleted(buf)
    if err != nil {
        fmt.Println(err)
    }
    if deleted >= 1 {
        return true
    }
    return false
}
 
func MarkData(id string) bool {
    url := "http://" + config.Options.EsUrl + "/ai_face_ocean/_update_by_query?refresh=true"
    markDSL := `{
    "script": {
        "source": "ctx._source.isDelete=true"
    },
    "query": {
        "term": {
            "id": "` + id + `"
        }
    }
}`
    buf, err := esutil.EsReq("POST", url, []byte(markDSL))
    if err != nil {
        fmt.Println(err)
        return false
    }
    total, err := util.SourceUpdated(buf)
    if err != nil {
        fmt.Println(err)
        return false
    }
    fmt.Println(total)
    return true
}
 
func GetImageData(url string) ([]byte, error) {
    // 发起HTTP GET请求
    response, err := http.Get(url)
    if err != nil {
        return nil, err
    }
    defer response.Body.Close()
 
    // 读取图片数据
    imageData, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return nil, err
    }
 
    return imageData, nil
}