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
|
}
|