---
panlei
2019-07-18 b5771bb208fc5f00f0b19b10a104f3716ee4de79
insertdata/insertDataToEs.go
@@ -6,7 +6,6 @@
   "fmt"
   "io/ioutil"
   "net"
   "os"
   "ruleprocess/cache"
   "ruleprocess/logger"
   "strings"
@@ -20,10 +19,15 @@
   "ruleprocess/util"
)
var weedfsUrl string
var weedfsUrl, videoPersonUrl, personAction string
type conf struct {
   PhotoUrl string `yaml:"photoUrl"`
   PhotoUrl       string `yaml:"photoUrl"`
   VideoPersons   string `yaml:"videoPersons"`
   PersonAction   string `yaml:"personAction"`
   ServerIp       string `yaml:"serverIp"`
   ServerPort     string `yaml:"serverPort"`
   DbTablePersons string `yaml:"dbTablePersons"`
}
func init() {
@@ -36,39 +40,41 @@
   //把yaml形式的字符串解析成struct类型
   yaml.Unmarshal(data, &c)
   weedfsUrl = c.PhotoUrl
   videoPersonUrl = c.VideoPersons
   personAction = c.PersonAction
}
// 人脸的数据结构
type PerVideoPicture struct {
   Id              string               `json:"id"`
   CameraId        string               `json:"cameraId"`
   CameraAddr      string               `json:"cameraAddr"`
   PicDate         string               `json:"picDate"`
   PicMaxUrl       string               `json:"picMaxUrl"`
   TaskId          string               `json:"taskId"`
   TaskName        string               `json:"taskName"`
   SdkName         string               `json:"sdkName"`
   Content         string               `json:"content"`
   LikeDate        string               `json:"likeDate"`
   Sex             string               `json:"sex"`
   Age             int32                `json:"age"`
   AgeDescription  string               `json:"ageDescription"`
   Race            string               `json:"race"`
   SmileLevel      int32                `json:"smileLevel"`
   BeautyLevel     int32                `json:"beautyLevel"`
   FaceFeature     string               `json:"faceFeature"`
   PicSmUrl        []string             `json:"picSmUrl"`
   VideoUrl        string               `json:"videoUrl"`
   AnalyServerId   string               `json:"analyServerId"`
   AnalyServerName string               `json:"analyServerName"`
   AnalyServerIp   string               `json:"analyServerIp"`
   ClusterId       string               `json:"clusterId"`
   DetectScore     float64              `json:"detectScore"`
   IsAlarm         int                  `json:"isAlarm"`
   IsAckAlarm      int                  `json:"isAckAlarm"`
   IsCollect       int                  `json:"isCollect"`
   IsDelete        int                  `json:"isDelete"`
   BaseInfo        []*protomsg.Baseinfo `json:"baseInfo"`
   Id              string                 `json:"id"`
   CameraId        string                 `json:"cameraId"`
   CameraAddr      string                 `json:"cameraAddr"`
   PicDate         string                 `json:"picDate"`
   PicMaxUrl       string                 `json:"picMaxUrl"`
   TaskId          string                 `json:"taskId"`
   TaskName        string                 `json:"taskName"`
   SdkName         string                 `json:"sdkName"`
   Content         string                 `json:"content"`
   LikeDate        string                 `json:"likeDate"`
   Sex             string                 `json:"sex"`
   Age             int32                  `json:"age"`
   AgeDescription  string                 `json:"ageDescription"`
   Race            string                 `json:"race"`
   SmileLevel      int32                  `json:"smileLevel"`
   BeautyLevel     int32                  `json:"beautyLevel"`
   FaceFeature     string                 `json:"faceFeature"`
   PicSmUrl        []string               `json:"picSmUrl"`
   VideoUrl        string                 `json:"videoUrl"`
   AnalyServerId   string                 `json:"analyServerId"`
   AnalyServerName string                 `json:"analyServerName"`
   AnalyServerIp   string                 `json:"analyServerIp"`
   ClusterId       string                 `json:"clusterId"`
   DetectScore     float64                `json:"detectScore"`
   IsAlarm         int                    `json:"isAlarm"`
   IsAckAlarm      int                    `json:"isAckAlarm"`
   IsCollect       int                    `json:"isCollect"`
   IsDelete        int                    `json:"isDelete"`
   BaseInfo        []*ruleserver.BaseInfo `json:"baseInfo"`
}
//  yolo行为的数据结构
@@ -119,7 +125,7 @@
   }
   if timeLabel == "12" { // 并非报警数据,只是状态改变的数据
      ChangeStatusFace(msg)
      //ChangeStatusFace(msg)
      ChangeStatusYolo(msg)
   }
}
@@ -170,7 +176,7 @@
            uuid.NewV4().String(),
            msg.Cid,
            camera.Addr,
            time.Now().Format("2006-01-02 15:04:05"),
            i.Timestamp,
            strings.Split(bigPhotoUrl["fileUrl"].(string), "/")[1],
            msg.Tasklab.Taskid,
            msg.Tasklab.Taskname,
@@ -203,7 +209,7 @@
            logger.Info("json parse error ", err)
            return
         }
         err1 := EsReq("POST", "http://192.168.1.182:9200/videopersons/perVideoPicture", requstbody)
         err1 := EsReq("POST", videoPersonUrl, requstbody)
         if err1 != nil {
            logger.Error("上传ES出错!---", err1)
         }
@@ -211,72 +217,74 @@
   }
}
func ChangeStatusFace(msg ruleserver.ResultMsg) {
   logger.Info("往ES插非报警但是状态转换数据")
      // 上传大图
      // 解压缩并上传图片
      bdata, err := util.UnCompress(msg.Data)
      if err != nil {
         panic("解压缩图片时出现错误")
      }
      i := protomsg.Image{}
      err = proto.Unmarshal(bdata, &i)
      bigPhotoUrl := make(map[string]interface{})
      bigPhotoUrl, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String())
      logger.Debug("========大图路径:", bigPhotoUrl)
      // 人脸检测,没有相似的底库人员
      localConfig, err := cache.GetServerInfo()
      if err != nil {
         logger.Error("查询本机信息失败!")
      }
      serverIp, err := GetLocalIP()
      // 查询cameraName
      camera, err := cache.GetCameraById(msg.Cid)
      if err != nil {
         logger.Error("查询摄像机信息失败")
      }
   logger.Info("往ES插入人脸非报警但是状态转换数据")
   // 上传大图
   // 解压缩并上传图片
   bdata, err := util.UnCompress(msg.Data)
   if err != nil {
      panic("解压缩图片时出现错误")
   }
   i := protomsg.Image{}
   err = proto.Unmarshal(bdata, &i)
   bigPhotoUrl := make(map[string]interface{})
   bigPhotoUrl, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String())
   logger.Debug("========大图路径:", bigPhotoUrl)
   // 人脸检测,没有相似的底库人员
   localConfig, err := cache.GetServerInfo()
   if err != nil {
      logger.Error("查询本机信息失败!")
   }
   serverIp, err := GetLocalIP()
   // 查询cameraName
   camera, err := cache.GetCameraById(msg.Cid)
   if err != nil {
      logger.Error("查询摄像机信息失败")
   }
      pervideo := PerVideoPicture{
         uuid.NewV4().String(),
         msg.Cid,
         camera.Addr,
         time.Now().Format("2006-01-02 15:04:05"),
         strings.Split(bigPhotoUrl["fileUrl"].(string), "/")[1],
         msg.Tasklab.Taskid,
         msg.Tasklab.Taskname,
         "人脸",
         "状态转换数据,非报警数据",
         time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间
         "",
         0,
         "",
         "",
         0,
         0,
         "",
         []string{""},
         "暂无集群",
         localConfig.ServerId,
         localConfig.ServerName,
         serverIp,
         "",
         0,
         1,
         0,
         0,
         0,
         []*protomsg.Baseinfo{},
      }
      requstbody, err := json.Marshal(pervideo)
   pervideo := PerVideoPicture{
      uuid.NewV4().String(),
      msg.Cid,
      camera.Addr,
      i.Timestamp,
      strings.Split(bigPhotoUrl["fileUrl"].(string), "/")[1],
      msg.Tasklab.Taskid,
      msg.Tasklab.Taskname,
      "人脸",
      "状态转换数据,非报警数据",
      time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间
      "",
      0,
      "",
      "",
      0,
      0,
      "",
      []string{""},
      "暂无集群",
      localConfig.ServerId,
      localConfig.ServerName,
      serverIp,
      "",
      0,
      1,
      0,
      0,
      0,
      []*ruleserver.BaseInfo{},
   }
   requstbody, err := json.Marshal(pervideo)
      if err != nil {
         logger.Info("json parse error ", err)
         return
      }
      err1 := EsReq("POST", "http://192.168.1.182:9200/videopersons/perVideoPicture", requstbody)
      if err1 != nil {
         logger.Error("上传ES出错!---", err1)
      }
      InsertFace(msg.RuleResult["cacheData"].(ruleserver.ResultMsg))
   if err != nil {
      logger.Info("json parse error ", err)
      return
   }
   err1 := EsReq("POST", videoPersonUrl, requstbody)
   if err1 != nil {
      logger.Error("上传ES出错!---", err1)
   }
   //if msg.RuleResult["cacheData"] != nil {
   //   InsertFace(msg.RuleResult["cacheData"].(ruleserver.ResultMsg))
   //}
}
// 往es中插入yolo数据
@@ -292,17 +300,17 @@
      }
      isAlarm := 0
      resp := make(map[string]interface{})
      // 解压缩并上传图片
      bdata, err := util.UnCompress(msg.Data)
      if err != nil {
         panic("解压缩图片时出现错误")
      }
      i := protomsg.Image{}
      err = proto.Unmarshal(bdata, &i)
      if len(alarmRules) > 0 {
         isAlarm = 1
         // 解压缩并上传图片
         bdata, err := util.UnCompress(msg.Data)
         if err != nil {
            panic("解压缩图片时出现错误")
         }
         i := protomsg.Image{}
         err = proto.Unmarshal(bdata, &i)
         //resp, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String())
         resp, err = util.DrawPolygonOnImage(msg.Cid, i, msg.RuleResult["yolo"].([]ruleserver.Result))
         resp, err = util.DrawPolygonOnImage(msg.Cid, i, msg.RuleResult["yolo"].([]ruleserver.Result), weedfsUrl)
         if err != nil {
            logger.Error("画框或上传图片服务器出错", err)
         } else {
@@ -340,7 +348,7 @@
         serverIp,
         "",
         []string{strings.Split(resp["fileUrl"].(string), "/")[1]},
         time.Now().Format("2006-01-02 15:04:05"),
         i.Timestamp,
         "",
         isAlarm,
         0,
@@ -354,16 +362,16 @@
         return
      }
      err = EsReq("POST", "http://192.168.1.182:9200/personaction/perVideoAction", requstbody)
      err = EsReq("POST", personAction, requstbody)
      if err != nil {
         logger.Error("往ES插入数据失败", err)
      } else {
         logger.Warn("__________________________________________往ES插入yolo数据成功")
         //os.Exit(1)
      }
      logger.Debug("============瞅瞅缓存数据:",msg.RuleResult["cacheData"].(ruleserver.ResultMsg).RuleResult)
      InsertYolo(msg.RuleResult["cacheData"].(ruleserver.ResultMsg))
      os.Exit(1)
      if msg.RuleResult["cacheData"] != nil {
         InsertYolo(msg.RuleResult["cacheData"].(ruleserver.ResultMsg))
      }
   }
}
func ChangeStatusYolo(msg ruleserver.ResultMsg) {
@@ -377,7 +385,7 @@
   i := protomsg.Image{}
   err = proto.Unmarshal(bdata, &i)
   //resp, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String())
   resp, err := util.DrawPolygonOnImage(msg.Cid, i, msg.RuleResult["yolo"].([]ruleserver.Result))
   resp, err := util.DrawPolygonOnImage(msg.Cid, i, msg.RuleResult["yolo"].([]ruleserver.Result), weedfsUrl)
   if err != nil {
      logger.Error("画框或上传图片服务器出错", err)
   } else {
@@ -410,7 +418,7 @@
      serverIp,
      "",
      []string{strings.Split(resp["fileUrl"].(string), "/")[1]},
      time.Now().Format("2006-01-02 15:04:05"),
      i.Timestamp,
      "",
      0,
      0,
@@ -424,7 +432,7 @@
      return
   }
   err = EsReq("POST", "http://192.168.1.182:9200/personaction/perVideoAction", requstbody)
   err = EsReq("POST", personAction, requstbody)
   if err != nil {
      logger.Error("往ES插入数据失败", err)
   } else {
@@ -432,6 +440,7 @@
      //os.Exit(1)
   }
}
// 获取本机ip
func GetLocalIP() (ipv4 string, err error) {
   var (