123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- package gate_pic
- import (
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "github.com/tidwall/gjson"
- dbmodel "property-device/model"
- "property-device/utils/gate_utils"
- "time"
- )
- type PicYufanHttpv1Whiter struct {
- Up *dbmodel.TUserPic
- Device *dbmodel.TGate
- Gp *dbmodel.TGatePic
- PersonAddSuc bool
- ByIp bool
- command bool
- }
- func whitePicSetFail(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, msg string) {
- gp := dbmodel.TGatePic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", device.ID)
- where = dbmodel.WhereAdd(where, "record_id", up.ID)
- values := map[string]interface{}{"status": status, "msg": msg, "updated_at": time.Now()}
- gp.Update(database.DB(), where, values)
- upi := dbmodel.TUserPic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", up.ID)
- values = map[string]interface{}{"down_status": 3}
- upi.Update(database.DB(), where, nil)
- }
- func whitePicPersonSuccess(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, faceId string) {
- gp := dbmodel.TGatePic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", device.ID)
- where = dbmodel.WhereAdd(where, "record_id", up.ID)
- values := map[string]interface{}{"status": status, "updated_at": time.Now()}
- if faceId != "" {
- values["face_id"] = faceId
- }
- gp.Update(database.DB(), where, values)
- }
- func (p *PicYufanHttpv1Whiter) PersonAdd() {
- if p.ByIp {
- p.PersonAddByIp()
- return
- }
- task := gate_utils.PersonRegesterTask{
- TaskNo: "",
- InterfaceName: gate_utils.PersonRegersterTaskName,
- Result: true,
- Person: gate_utils.PersonRegesterData{
- Id: p.Up.Uid,
- Name: p.Up.Name,
- IdCardNum: "",
- //todo
- IdNumber: "",
- FacePermission: 2,
- IdCardPermission: 2,
- FaceAndCardPermission: 2,
- IDPermission: 1,
- Tag: "",
- Phone: "",
- },
- }
- bytes, _ := json.Marshal(task)
- now := time.Now()
- gcmd := &dbmodel.TGateCommand{
- CreatedAt: now,
- UpdatedAt: now,
- DeviceId: p.Device.ID,
- Param: string(bytes),
- Desc: gate_utils.CommandCodeMap[gate_utils.DownCommand],
- Status: gate_utils.CommandStatusWait,
- ResultStatus: 0,
- ResultStatusDesc: "",
- Code: gate_utils.DownCommand,
- }
- if err := gcmd.Insert(database.DB()); err != nil {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
- return
- }
- p.command = true
- return
- }
- func (p *PicYufanHttpv1Whiter) PicAdd() {
- if p.ByIp {
- p.PicAddByIp()
- return
- }
- // 非ip时,收到人员添加任务执行结果后再执行添加照片任务(command模块里)
- return
- }
- func (p *PicYufanHttpv1Whiter) PicUpdate() {
- if p.ByIp {
- p.PicUpdateByIp()
- return
- }
- if p.Gp == nil {
- return
- }
- switch p.Gp.Status {
- case gate_utils.WhiteAddStatusWait:
- p.PersonAdd()
- p.PicAdd()
- return
- case gate_utils.WhiteAddStatusPersonSuc:
- break
- case gate_utils.WhiteAddStatusAllSuc:
- break
- case gate_utils.WhiteAddStatusPersonFail:
- p.PersonAdd()
- p.PicAdd()
- return
- case gate_utils.WhiteAddStatusPicFail:
- break
- }
- task := gate_utils.PhotoUpdateTask{
- Result: true,
- TaskNo: "",
- InterfaceName: gate_utils.PhotoUpdateByUrlTaskName,
- }
- task.PersonId = p.Up.Uid
- task.Url = p.Up.PicUrl
- task.FaceId = p.Gp.FaceId
- bytes, _ := json.Marshal(task)
- now := time.Now()
- cmd := dbmodel.TGateCommand{
- CreatedAt: now,
- UpdatedAt: now,
- DeviceId: p.Gp.DeviceId,
- Param: string(bytes),
- Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
- Status: gate_utils.CommandStatusWait,
- ResultStatus: 0,
- ResultStatusDesc: "",
- Code: gate_utils.UpdatePicCommand,
- }
- err := cmd.Insert(database.DB())
- if err != nil {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error())
- return
- }
- p.command = true
- return
- }
- func (p *PicYufanHttpv1Whiter) PersonDel() {
- if p.ByIp {
- p.PersonAddByIp()
- return
- }
- task := gate_utils.PersonDelTask{
- Result: true,
- TaskNo: "",
- InterfaceName: gate_utils.PersonDelTaskName,
- }
- task.Id = p.Up.Uid
- bytes, _ := json.Marshal(task)
- now := time.Now()
- cmd := dbmodel.TGateCommand{
- CreatedAt: now,
- UpdatedAt: now,
- DeviceId: p.Gp.DeviceId,
- Param: string(bytes),
- Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
- Status: gate_utils.CommandStatusWait,
- ResultStatus: 0,
- ResultStatusDesc: "",
- Code: gate_utils.DelCommand,
- }
- err := cmd.Insert(database.DB())
- if err != nil {
- return
- }
- p.command = true
- return
- }
- func (p *PicYufanHttpv1Whiter) PersonAddByIp() {
- personReq := gate_utils.PersonRegesterData{
- Id: p.Up.Uid,
- Name: p.Up.Name,
- IdCardNum: "",
- //todo
- IdNumber: "",
- FacePermission: 2,
- IdCardPermission: 2,
- FaceAndCardPermission: 2,
- IDPermission: 1,
- Tag: "",
- Phone: "",
- }
- body := map[string]interface{}{
- "pass": p.Device.Password,
- "person": personReq,
- }
- bytes, _ := json.Marshal(body)
- h := gate_utils.HttpRequestWithHead{
- Method: "POST",
- Body: bytes,
- TimeOut: 20 * time.Second,
- Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
- Url: fmt.Sprintf("http://%s:%d/person/create", p.Device.Ip, p.Device.Port),
- }
- bytes, err := h.Request()
- if err != nil {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
- return
- }
- code := gjson.GetBytes(bytes, "code").String()
- msg := gjson.GetBytes(bytes, "msg").String()
- if code == "LAN_SUS-0" || code == "LAN_EXP-3005" {
- whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusPersonSuc, "")
- } else {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, fmt.Sprintf("%s-%s", code, msg))
- return
- }
- p.PersonAddSuc = true
- return
- }
- func (p *PicYufanHttpv1Whiter) PicAddByIp() {
- if !p.PersonAddSuc {
- return
- }
- body := map[string]interface{}{
- "pass": p.Device.Password,
- "imgUrl": p.Up.PicUrl,
- "personId": p.Up.Uid,
- "faceId": p.Up.Uid,
- }
- bytes, _ := json.Marshal(body)
- h := gate_utils.HttpRequestWithHead{
- Method: "POST",
- Body: bytes,
- TimeOut: 20 * time.Second,
- Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
- Url: fmt.Sprintf("http://%s:%d/face/createByUrl", p.Device.Ip, p.Device.Port),
- }
- bytes, err := h.Request()
- if err != nil {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error())
- return
- }
- code := gjson.GetBytes(bytes, "code").String()
- msg := gjson.GetBytes(bytes, "msg").String()
- if code == "LAN_SUS-0" || code == "LAN_EXP-4007" {
- whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Up.Uid)
- } else {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg))
- return
- }
- return
- }
- func (p *PicYufanHttpv1Whiter) PicUpdateByIp() {
- if p.Gp == nil {
- return
- }
- switch p.Gp.Status {
- case gate_utils.WhiteAddStatusWait:
- p.PersonAddByIp()
- p.PicAddByIp()
- return
- case gate_utils.WhiteAddStatusPersonSuc:
- p.PersonAddSuc = true
- p.PicAddByIp()
- return
- case gate_utils.WhiteAddStatusAllSuc:
- p.PersonAddSuc = true
- break
- case gate_utils.WhiteAddStatusPersonFail:
- p.PersonAddByIp()
- p.PicAddByIp()
- return
- case gate_utils.WhiteAddStatusPicFail:
- p.PersonAddSuc = true
- p.PicAddByIp()
- return
- }
- body := map[string]interface{}{
- "pass": p.Device.Password,
- "personId": p.Up.Uid,
- "faceId": p.Gp.FaceId,
- "url": p.Up.PicUrl,
- }
- bytes, _ := json.Marshal(body)
- h := gate_utils.HttpRequestWithHead{
- Method: "PUT",
- Body: bytes,
- TimeOut: 20 * time.Second,
- Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
- Url: fmt.Sprintf("http://%s:%d/face/", p.Device.Ip, p.Device.Port),
- }
- bytes, err := h.Request()
- if err != nil {
- return
- }
- code := gjson.GetBytes(bytes, "code").String()
- msg := gjson.GetBytes(bytes, "msg").String()
- if code == "LAN_SUS-0" {
- whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Gp.FaceId)
- } else {
- whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg))
- }
- }
- func (p *PicYufanHttpv1Whiter) PersonDelByIp() {
- body := map[string]interface{}{
- "pass": p.Device.Password,
- "id": p.Up.Uid,
- }
- bytes, _ := json.Marshal(body)
- h := gate_utils.HttpRequestWithHead{
- Method: "POST",
- Body: bytes,
- TimeOut: 20 * time.Second,
- Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
- Url: fmt.Sprintf("http://%s:%d/person/delete", p.Device.Ip, p.Device.Port),
- }
- bytes, err := h.Request()
- if err != nil {
- return
- }
- code := gjson.GetBytes(bytes, "code").String()
- if code == "LAN_SUS-0" || code == "LAN_EXP-3009" {
- gp := dbmodel.TGatePic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", p.Device.ID)
- where = dbmodel.WhereAdd(where, "record_id", p.Up.ID)
- err = gp.Delete(database.DB(), where)
- if err != nil {
- return
- }
- CheckUserPicDel(p.Up.ID, database.DB())
- }
- return
- }
- func (p *PicYufanHttpv1Whiter) Command() bool {
- return p.command
- }
|