Nats.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package Nats
  2. import (
  3. "ColdVerify_server/Nats/NatsServer"
  4. "ColdVerify_server/conf"
  5. "ColdVerify_server/lib"
  6. "ColdVerify_server/logs"
  7. "ColdVerify_server/models/Device"
  8. "ColdVerify_server/models/System"
  9. "ColdVerify_server/models/Task"
  10. "fmt"
  11. "github.com/nats-io/nats.go"
  12. "github.com/vmihailenco/msgpack/v5"
  13. "strings"
  14. "sync"
  15. )
  16. func init() {
  17. logs.Println("============Nats init============")
  18. var err error
  19. // 连接Nats服务器
  20. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  21. if err != nil {
  22. fmt.Println("nats 连接失败!")
  23. panic(err)
  24. }
  25. logs.Println("nats OK!")
  26. // 本地测试,屏蔽本地nats
  27. if !conf.NatsForbidden {
  28. go NatsInit()
  29. }
  30. }
  31. type Extract_TaskData_Back struct {
  32. T_uuid string `xml:"T_uuid"` // 任务主键id
  33. Time_start string `xml:"Time_start"`
  34. Time_end string `xml:"Time_end"`
  35. DeviceClassList []Device.DeviceClassList `xml:"DeviceClassList"` // 泛型
  36. Task Task.Task `xml:"Task"` // 泛型
  37. }
  38. func NatsInit() {
  39. // 发布-订阅 模式,打包数据
  40. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Extract_TaskData_Back", "Extract_TaskData", func(m *nats.Msg) {
  41. logs.Debug("Extract_TaskData_Back message: \n", string(m.Data))
  42. var resp Extract_TaskData_Back
  43. err := msgpack.Unmarshal(m.Data, &resp)
  44. if err != nil {
  45. System.Add_Logs("Nats", "msgpack Unmarshal err", string(m.Data))
  46. return
  47. }
  48. Task_r := resp.Task
  49. // 清空表
  50. Task.Truncate_TaskData(Task_r.T_task_id)
  51. //失败重试5次
  52. DeviceClassList := new(sync.Map)
  53. var count int
  54. for _, v := range resp.DeviceClassList {
  55. DeviceClassList.Store(fmt.Sprintf("%s|%s", v.T_sn, v.T_id), 5)
  56. //err = Task.Import_TaskData_Back(v.T_sn, v.T_id, resp.Task.T_task_id, resp.Time_start, resp.Time_end)
  57. count++
  58. //time.Sleep(5 * time.Second)
  59. }
  60. for count > 0 {
  61. DeviceClassList.Range(func(k, v any) bool {
  62. T_snid := strings.Split(k.(string), "|")
  63. T_sn := T_snid[0]
  64. T_id := T_snid[1]
  65. temp := v.(int)
  66. temp--
  67. DeviceClassList.Store(k, temp)
  68. err = Task.Import_TaskData_Back(T_sn, T_id, resp.Task.T_task_id, resp.Time_start, resp.Time_end)
  69. if err == nil || strings.Contains(err.Error(), "doesn't exist") {
  70. DeviceClassList.Delete(k)
  71. count--
  72. }
  73. return true
  74. })
  75. }
  76. TaskData_Num := Task.Read_TaskData_Count(Task_r.T_task_id)
  77. //if TaskData_Num == 0 {
  78. // Task_r.T_collection_num = 0
  79. // if !Task.Update_Task(Task_r, "T_collection_state") {
  80. // logs.Error(lib.FuncName(), "后台执行修改任务数据失败")
  81. // }
  82. // return
  83. //}
  84. // 导入到本地数据
  85. NatsServer.Import_TaskData(resp.T_uuid, Task_r.T_task_id, TaskData_Num)
  86. System.Add_UserLogs_T(resp.T_uuid, "任务", "修改", Task_r)
  87. System.Add_UserLogs(resp.T_uuid, "提取数据", "提取数据"+Task_r.T_name, Task_r.T_task_id+"|"+resp.Time_start+"|"+resp.Time_end)
  88. })
  89. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Update_Task", "Update_Task", func(m *nats.Msg) {
  90. var t_Req Task.Task
  91. var t_R lib.JSONS
  92. err := msgpack.Unmarshal(m.Data, &t_Req)
  93. if err != nil {
  94. logs.Error("Mats", lib.FuncName(), err)
  95. t_R.Code = 202
  96. t_R.Msg = err.Error()
  97. b, _ := msgpack.Marshal(&t_R)
  98. _ = lib.Nats.Publish(m.Reply, b)
  99. return
  100. }
  101. fmt.Printf("ColdVerify_Server_Update_Task message: %+v\n", t_Req)
  102. col := []string{}
  103. if t_Req.T_delivery_state > 0 {
  104. col = append(col, "T_delivery_state")
  105. }
  106. if !Task.Update_Task(t_Req, col...) {
  107. logs.Error("Mats", lib.FuncName(), err)
  108. t_R.Code = 202
  109. t_R.Msg = err.Error()
  110. b, _ := msgpack.Marshal(&t_R)
  111. _ = lib.Nats.Publish(m.Reply, b)
  112. return
  113. }
  114. Task.Add_TaskLogs_T("nats", t_Req.T_task_id, "任务管理", "修改", t_R)
  115. System.Add_UserLogs_T("nats", "任务管理", "修改", t_R)
  116. t_R.Code = 200
  117. t_R.Msg = "ok"
  118. b, _ := msgpack.Marshal(&t_R)
  119. _ = lib.Nats.Publish(m.Reply, b)
  120. })
  121. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Read_Task", "Read_Task", func(m *nats.Msg) {
  122. logs.Println("ColdVerify_Server_Read_Task message: %+v\n", string(m.Data))
  123. var t_R lib.JSONS
  124. task, is := Task.Read_Task(string(m.Data))
  125. if !is {
  126. logs.Error("Mats", lib.FuncName())
  127. t_R.Code = 202
  128. t_R.Msg = "查询失败"
  129. b, _ := msgpack.Marshal(&t_R)
  130. _ = lib.Nats.Publish(m.Reply, b)
  131. return
  132. }
  133. t_R.Code = 200
  134. t_R.Msg = "ok"
  135. t_R.Data = task
  136. b, _ := msgpack.Marshal(&t_R)
  137. _ = lib.Nats.Publish(m.Reply, b)
  138. })
  139. }