pic_yufan_httpv1_whiter.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package gate_pic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/database"
  6. "github.com/tidwall/gjson"
  7. dbmodel "property-device/model"
  8. "property-device/utils/gate_utils"
  9. "time"
  10. )
  11. type PicYufanHttpv1Whiter struct {
  12. Up *dbmodel.TUserPic
  13. Device *dbmodel.TGate
  14. Gp *dbmodel.TGatePic
  15. PersonAddSuc bool
  16. ByIp bool
  17. command bool
  18. }
  19. func whitePicSetFail(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, msg string) {
  20. gp := dbmodel.TGatePic{}
  21. where := [][2]interface{}{}
  22. where = dbmodel.WhereAdd(where, "device_id", device.ID)
  23. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  24. values := map[string]interface{}{"status": status, "msg": msg, "updated_at": time.Now()}
  25. gp.Update(database.DB(), where, values)
  26. upi := dbmodel.TUserPic{}
  27. where = [][2]interface{}{}
  28. where = dbmodel.WhereAdd(where, "id", up.ID)
  29. values = map[string]interface{}{"down_status": 3}
  30. upi.Update(database.DB(), where, nil)
  31. }
  32. func whitePicPersonSuccess(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, faceId string) {
  33. gp := dbmodel.TGatePic{}
  34. where := [][2]interface{}{}
  35. where = dbmodel.WhereAdd(where, "device_id", device.ID)
  36. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  37. values := map[string]interface{}{"status": status, "updated_at": time.Now()}
  38. if faceId != "" {
  39. values["face_id"] = faceId
  40. }
  41. gp.Update(database.DB(), where, values)
  42. }
  43. func (p *PicYufanHttpv1Whiter) PersonAdd() {
  44. if p.ByIp {
  45. p.PersonAddByIp()
  46. return
  47. }
  48. task := gate_utils.PersonRegesterTask{
  49. TaskNo: "",
  50. InterfaceName: gate_utils.PersonRegersterTaskName,
  51. Result: true,
  52. Person: gate_utils.PersonRegesterData{
  53. Id: p.Up.Uid,
  54. Name: p.Up.Name,
  55. IdCardNum: "",
  56. //todo
  57. IdNumber: "",
  58. FacePermission: 2,
  59. IdCardPermission: 2,
  60. FaceAndCardPermission: 2,
  61. IDPermission: 1,
  62. Tag: "",
  63. Phone: "",
  64. },
  65. }
  66. bytes, _ := json.Marshal(task)
  67. now := time.Now()
  68. gcmd := &dbmodel.TGateCommand{
  69. CreatedAt: now,
  70. UpdatedAt: now,
  71. DeviceId: p.Device.ID,
  72. Param: string(bytes),
  73. Desc: gate_utils.CommandCodeMap[gate_utils.DownCommand],
  74. Status: gate_utils.CommandStatusWait,
  75. ResultStatus: 0,
  76. ResultStatusDesc: "",
  77. Code: gate_utils.DownCommand,
  78. }
  79. if err := gcmd.Insert(database.DB()); err != nil {
  80. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
  81. return
  82. }
  83. p.command = true
  84. return
  85. }
  86. func (p *PicYufanHttpv1Whiter) PicAdd() {
  87. if p.ByIp {
  88. p.PicAddByIp()
  89. return
  90. }
  91. // 非ip时,收到人员添加任务执行结果后再执行添加照片任务(command模块里)
  92. return
  93. }
  94. func (p *PicYufanHttpv1Whiter) PicUpdate() {
  95. if p.ByIp {
  96. p.PicUpdateByIp()
  97. return
  98. }
  99. if p.Gp == nil {
  100. return
  101. }
  102. switch p.Gp.Status {
  103. case gate_utils.WhiteAddStatusWait:
  104. p.PersonAdd()
  105. p.PicAdd()
  106. return
  107. case gate_utils.WhiteAddStatusPersonSuc:
  108. break
  109. case gate_utils.WhiteAddStatusAllSuc:
  110. break
  111. case gate_utils.WhiteAddStatusPersonFail:
  112. p.PersonAdd()
  113. p.PicAdd()
  114. return
  115. case gate_utils.WhiteAddStatusPicFail:
  116. break
  117. }
  118. task := gate_utils.PhotoUpdateTask{
  119. Result: true,
  120. TaskNo: "",
  121. InterfaceName: gate_utils.PhotoUpdateByUrlTaskName,
  122. }
  123. task.PersonId = p.Up.Uid
  124. task.Url = p.Up.PicUrl
  125. task.FaceId = p.Gp.FaceId
  126. bytes, _ := json.Marshal(task)
  127. now := time.Now()
  128. cmd := dbmodel.TGateCommand{
  129. CreatedAt: now,
  130. UpdatedAt: now,
  131. DeviceId: p.Gp.DeviceId,
  132. Param: string(bytes),
  133. Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
  134. Status: gate_utils.CommandStatusWait,
  135. ResultStatus: 0,
  136. ResultStatusDesc: "",
  137. Code: gate_utils.UpdatePicCommand,
  138. }
  139. err := cmd.Insert(database.DB())
  140. if err != nil {
  141. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error())
  142. return
  143. }
  144. p.command = true
  145. return
  146. }
  147. func (p *PicYufanHttpv1Whiter) PersonDel() {
  148. if p.ByIp {
  149. p.PersonAddByIp()
  150. return
  151. }
  152. task := gate_utils.PersonDelTask{
  153. Result: true,
  154. TaskNo: "",
  155. InterfaceName: gate_utils.PersonDelTaskName,
  156. }
  157. task.Id = p.Up.Uid
  158. bytes, _ := json.Marshal(task)
  159. now := time.Now()
  160. cmd := dbmodel.TGateCommand{
  161. CreatedAt: now,
  162. UpdatedAt: now,
  163. DeviceId: p.Gp.DeviceId,
  164. Param: string(bytes),
  165. Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
  166. Status: gate_utils.CommandStatusWait,
  167. ResultStatus: 0,
  168. ResultStatusDesc: "",
  169. Code: gate_utils.DelCommand,
  170. }
  171. err := cmd.Insert(database.DB())
  172. if err != nil {
  173. return
  174. }
  175. p.command = true
  176. return
  177. }
  178. func (p *PicYufanHttpv1Whiter) PersonAddByIp() {
  179. personReq := gate_utils.PersonRegesterData{
  180. Id: p.Up.Uid,
  181. Name: p.Up.Name,
  182. IdCardNum: "",
  183. //todo
  184. IdNumber: "",
  185. FacePermission: 2,
  186. IdCardPermission: 2,
  187. FaceAndCardPermission: 2,
  188. IDPermission: 1,
  189. Tag: "",
  190. Phone: "",
  191. }
  192. body := map[string]interface{}{
  193. "pass": p.Device.Password,
  194. "person": personReq,
  195. }
  196. bytes, _ := json.Marshal(body)
  197. h := gate_utils.HttpRequestWithHead{
  198. Method: "POST",
  199. Body: bytes,
  200. TimeOut: 20 * time.Second,
  201. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  202. Url: fmt.Sprintf("http://%s:%d/person/create", p.Device.Ip, p.Device.Port),
  203. }
  204. bytes, err := h.Request()
  205. if err != nil {
  206. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
  207. return
  208. }
  209. code := gjson.GetBytes(bytes, "code").String()
  210. msg := gjson.GetBytes(bytes, "msg").String()
  211. if code == "LAN_SUS-0" || code == "LAN_EXP-3005" {
  212. whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusPersonSuc, "")
  213. } else {
  214. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, fmt.Sprintf("%s-%s", code, msg))
  215. return
  216. }
  217. p.PersonAddSuc = true
  218. return
  219. }
  220. func (p *PicYufanHttpv1Whiter) PicAddByIp() {
  221. if !p.PersonAddSuc {
  222. return
  223. }
  224. body := map[string]interface{}{
  225. "pass": p.Device.Password,
  226. "imgUrl": p.Up.PicUrl,
  227. "personId": p.Up.Uid,
  228. "faceId": p.Up.Uid,
  229. }
  230. bytes, _ := json.Marshal(body)
  231. h := gate_utils.HttpRequestWithHead{
  232. Method: "POST",
  233. Body: bytes,
  234. TimeOut: 20 * time.Second,
  235. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  236. Url: fmt.Sprintf("http://%s:%d/face/createByUrl", p.Device.Ip, p.Device.Port),
  237. }
  238. bytes, err := h.Request()
  239. if err != nil {
  240. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error())
  241. return
  242. }
  243. code := gjson.GetBytes(bytes, "code").String()
  244. msg := gjson.GetBytes(bytes, "msg").String()
  245. if code == "LAN_SUS-0" || code == "LAN_EXP-4007" {
  246. whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Up.Uid)
  247. } else {
  248. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg))
  249. return
  250. }
  251. return
  252. }
  253. func (p *PicYufanHttpv1Whiter) PicUpdateByIp() {
  254. if p.Gp == nil {
  255. return
  256. }
  257. switch p.Gp.Status {
  258. case gate_utils.WhiteAddStatusWait:
  259. p.PersonAddByIp()
  260. p.PicAddByIp()
  261. return
  262. case gate_utils.WhiteAddStatusPersonSuc:
  263. p.PersonAddSuc = true
  264. p.PicAddByIp()
  265. return
  266. case gate_utils.WhiteAddStatusAllSuc:
  267. p.PersonAddSuc = true
  268. break
  269. case gate_utils.WhiteAddStatusPersonFail:
  270. p.PersonAddByIp()
  271. p.PicAddByIp()
  272. return
  273. case gate_utils.WhiteAddStatusPicFail:
  274. p.PersonAddSuc = true
  275. p.PicAddByIp()
  276. return
  277. }
  278. body := map[string]interface{}{
  279. "pass": p.Device.Password,
  280. "personId": p.Up.Uid,
  281. "faceId": p.Gp.FaceId,
  282. "url": p.Up.PicUrl,
  283. }
  284. bytes, _ := json.Marshal(body)
  285. h := gate_utils.HttpRequestWithHead{
  286. Method: "PUT",
  287. Body: bytes,
  288. TimeOut: 20 * time.Second,
  289. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  290. Url: fmt.Sprintf("http://%s:%d/face/", p.Device.Ip, p.Device.Port),
  291. }
  292. bytes, err := h.Request()
  293. if err != nil {
  294. return
  295. }
  296. code := gjson.GetBytes(bytes, "code").String()
  297. msg := gjson.GetBytes(bytes, "msg").String()
  298. if code == "LAN_SUS-0" {
  299. whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Gp.FaceId)
  300. } else {
  301. whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg))
  302. }
  303. }
  304. func (p *PicYufanHttpv1Whiter) PersonDelByIp() {
  305. body := map[string]interface{}{
  306. "pass": p.Device.Password,
  307. "id": p.Up.Uid,
  308. }
  309. bytes, _ := json.Marshal(body)
  310. h := gate_utils.HttpRequestWithHead{
  311. Method: "POST",
  312. Body: bytes,
  313. TimeOut: 20 * time.Second,
  314. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  315. Url: fmt.Sprintf("http://%s:%d/person/delete", p.Device.Ip, p.Device.Port),
  316. }
  317. bytes, err := h.Request()
  318. if err != nil {
  319. return
  320. }
  321. code := gjson.GetBytes(bytes, "code").String()
  322. if code == "LAN_SUS-0" || code == "LAN_EXP-3009" {
  323. gp := dbmodel.TGatePic{}
  324. where := [][2]interface{}{}
  325. where = dbmodel.WhereAdd(where, "device_id", p.Device.ID)
  326. where = dbmodel.WhereAdd(where, "record_id", p.Up.ID)
  327. err = gp.Delete(database.DB(), where)
  328. if err != nil {
  329. return
  330. }
  331. CheckUserPicDel(p.Up.ID, database.DB())
  332. }
  333. return
  334. }
  335. func (p *PicYufanHttpv1Whiter) Command() bool {
  336. return p.command
  337. }