---
panlei
2019-12-17 cd60b7e3a0bbc09f4501f66260b5d961d58aaf49
insertdata/insertDataToEs.go
@@ -1,285 +1,41 @@
package insertdata
import (
   "errors"
   "encoding/json"
   "log"
   "net"
   "time"
   "basic.com/pubsub/protomsg.git"
   "basic.com/dbapi.git"
   "ruleprocess/ruleserver"
   "ruleprocess/util"
   "github.com/golang/protobuf/proto"
   "github.com/satori/go.uuid"
   "plugin"
   "ruleprocess/cache"
   "basic.com/valib/logger.git"
   "ruleprocess/structure"
   "strconv"
)
var weedfsUrl = "http://192.168.1.182:6333/submit"
// 人脸的数据结构
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             int32  `json:"sex"`
   Age             int32  `json:"age"`
   AgeDescription  string `json:"ageDescription"`
   Race            int32  `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"`
   IsAlarm         string `json:"isAlarm"`
   IsAckAlarm      string `json:"isAckAlarm"`
   IsCollect       string `json:"isCollect"`
   IsDelete        int    `json:"isDelete"`
   BaseInfo        Base   `json:"baseInfo"`
}
type Base struct {
   TableId      string  `json:"tableId"`
   TableName    string  `json:"tableName"`
   CompareScore float64 `json:"compareScore"`
   PersonId     string  `json:"personId"`
   PersonName   string  `json:"personName"`
   PersonPicUrl string  `json:"personPicUrl"`
   PhoneNum     string  `json:"phoneNum"`
   Sex          string  `json:"sex"`
   IdCard       string  `json:"idCard"`
   MonitorLevel string  `json:"monitorLevel"`
   Content      string  `json:"content"`
}
//  yolo行为的数据结构
type Personaction struct {
   Id              string `json:"id"`
   CameraId        string `json:"cameraId"`
   CameraName      string `json:"cameraName"`
   CameraAddr      string `json:"cameraAddr"`
   TaskId          string `json:"taskId"`
   TaskName        string `json:"taskName"`
   SdkName         string `json:"sdkName"`
   Content         string `json:"content"`
   AlarmRules      []AlarmRule
   AnalyServerId   string `json:"analyServerId"`
   AnalyServerName string `json:"analyServerName"`
   AnalyServerIp   string `json:"analyServerIp"`
   ClusterId       string `json:"clusterId"`
   PicUrl          string `json:"picUrl"`
   PicDate         string `json:"picDate"`
   VideoUrl        string `json:"videoUrl"`
   IsAlarm         string `json:"isAlarm"`
   IsAckAlarm      string `json:"isAckAlarm"`
   IsCollect       string `json:"isCollect"`
   IsDelete        int    `json:"isDelete"`
}
type AlarmRule struct {
   GroupId    string `json:"groupId"`
   AlarmLevel int32  `json:"alarmLevel"`
   RuleText   string `json:"ruleText"`
}
// 往ES插数据
func InsertToEs(msg ruleserver.ResultMsg) {
   log.Println("往ES插数据")
   // 直接从规则的标签数据里拿符合规则的人脸结果
   for _, result := range msg.RuleResult {
      if !result.IsYolo {
         for _,face1 := range result.Faces{
            println(face1)
         }
//var weedfsUrl, videoPersonUrl, personAction string
//var serverIp string
//var serverPort string
func InsertToEs(msg structure.ResultMsg) {
   defer func() {
      if err := recover(); err != nil {
         logger.Error("es模块儿的异常捕获:", err)
      }
   }()
   localConfig1, err := cache.GetServerInfo()
   if err != nil {
      panic("配置文件不合法")
   }
   for _, sdkinfo := range msg.Tasklab.Sdkinfos {
      //if sdkinfo.Sdktype == "FaceDetect" {
      //   if len(sdkinfo.Sdkdata) > 1 {
      //
      //      faceParam := protomsg.ParamFacePos{}
      //      err1 := proto.Unmarshal(sdkinfo.Sdkdata, &faceParam)
      //      if err1 != nil {
      //         log.Println("解析FACE sdk有误", err1)
      //         continue
      //      }
      //      for _, face := range faceParam.Faces {
      //         pervideo := PerVideoPicture{
      //            uuid.NewV4().String(),
      //            msg.Cid,
      //            msg.Caddr,
      //            time.Now().Format("2006-01-02 15:04:05"),
      //            "",
      //            msg.Tasklab.Taskid,
      //            msg.Tasklab.Taskname,
      //            sdkinfo.SdkName,
      //            "",
      //            "",
      //            face.Result.Gender,
      //            face.Result.Age,
      //            "",
      //            face.Result.Race,
      //            face.Result.Smile,
      //            face.Result.Beauty,
      //            "不是每个人脸算法都有",
      //            "---",
      //            "",
      //            "",
      //            "",
      //            "",
      //            "",
      //            "",
      //            "",
      //            "",
      //            0,
      //            Base{ // 只有人脸比对有这个信息,人脸检测并没有
      //               "是每个人脸算法都有吗",
      //               "",
      //               0,
      //               "",
      //               "",
      //               "",
      //               "",
      //               "",
      //               "",
      //               "",
      //               "",
      //            },
      //         }
      //         requstbody, err := json.Marshal(pervideo)
      //
      //         if err != nil {
      //            log.Println("json parse error ", err)
      //            return
      //
      //         }
      //         err = EsReq("POST", "http://192.168.1.182:9200/videopersons/perVideoPicture", requstbody)
      //         if err != nil {
      //            log.Println("es can not execute right.")
      //         }
      //      }
      //
      //   } else {
      //      continue
      //   }
      //}
      if sdkinfo.Sdktype == "Yolo" {
         if len(sdkinfo.Sdkdata) > 1 {
            yoloObj := protomsg.ParamYoloObj{}
            err1 := proto.Unmarshal(sdkinfo.Sdkdata, &yoloObj)
            if err1 != nil {
               log.Println("解析YOLO sdk有误", err1)
               //continue
            }
            alarmRules := []AlarmRule{}
            for _,result := range msg.RuleResult {
               alarmRules = append(alarmRules,AlarmRule{result.RuleGroupId,result.AlarmLevel,result.RuleText})
            }
            isAlarm := ""
            resp := make(map[string]interface{})
            resp["fileUrl"] = ""
            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())
               log.Println("已报警并上传改帧图片到服务器")
            }else {
               isAlarm = "0"
               // 不是报警数据不存
               continue
            }
            log.Println("图片上传返回值:",resp)
            // 查询本机信息
            flag,localConfig := dbapi.SysSetApi{}.GetServerInfo()
            if !flag {
               log.Println("查询本机信息失败!")
            }
            // 查询cameraName
            camera,err := dbapi.CameraApi{}.GetCameraById(msg.Cid)
            if err != nil {
               log.Println("查询摄像机信息失败")
            }
            serverIp,err := GetLocalIP()
            peraction := Personaction{
               uuid.NewV4().String(),
               msg.Cid,
               camera.Name,
               msg.Caddr,
               msg.Tasklab.Taskid,
               msg.Tasklab.Taskname,
               sdkinfo.SdkName,
               "",
               alarmRules,
               localConfig.ServerId,
               localConfig.ServerName,
               serverIp,
               "",
               resp["fileUrl"].(string),
               time.Now().Format("2006-01-02 15:04:05"),
               "",
               isAlarm,
               "",
               "",
               0,
            }
            requstbody, err := json.Marshal(peraction)
            if err != nil {
               log.Println("json parse error ", err)
               return
            }
            err = EsReq("POST", "http://192.168.1.182:9200/personaction/perVideoAction", requstbody)
            if err != nil {
               log.Println("es can not execute right.")
            }
         } else {
            continue
         }
      }
   }
   weedfsUrl := "http://" + localConfig1.WebPicIp + ":" + strconv.Itoa(int(localConfig1.WebPicPort)) + "/submit"
   videoPersonUrl := "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + structure.EsInfo.EsIndex.AIOcean.IndexName + "/" + structure.EsInfo.EsIndex.AIOcean.IndexType+"?refresh=true"
   serverIp := localConfig1.AlarmIp
   serverPort := strconv.Itoa(int(localConfig1.AlarmPort))
   handle(msg,weedfsUrl,videoPersonUrl,serverIp,serverPort)
}
// 获取本机ip
func GetLocalIP() (ipv4 string, err error) {
   var (
      addrs   []net.Addr
      addr    net.Addr
      ipNet   *net.IPNet // IP地址
      isIpNet bool
   )
   // 获取所有网卡
   if addrs, err = net.InterfaceAddrs(); err != nil {
      return
func handle (msg structure.ResultMsg,weedfsUrl,videoPersonUrl,serverIp,serverPort string) {
   p,err :=  plugin.Open("./algorithm/middleware.so")
   if err != nil {
      panic(err)
   }
   // 取第一个非lo的网卡IP
   for _, addr = range addrs {
      // 这个网络地址是IP地址: ipv4, ipv6
      if ipNet, isIpNet = addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() {
         // 跳过IPV6
         if ipNet.IP.To4() != nil {
            ipv4 = ipNet.IP.String() // 192.168.1.1
            return
         }
      }
   f,err1 := p.Lookup("InsertToEs")
   if err1 != nil {
      panic("没有找到中间件的格式化数据函数")
   }
   err = errors.New("ipv4 not found")
   return
   f.(func(msg structure.ResultMsg,weedfsUrl,videoPersonUrl,serverIp,serverPort string))(msg,weedfsUrl,videoPersonUrl,serverIp,serverPort)
}