NatsAppWarn.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package NatsServer
  2. import (
  3. "Cold_mqtt/lib"
  4. "Cold_mqtt/logs"
  5. "Cold_mqtt/models/Account"
  6. "Cold_mqtt/models/Warning"
  7. "github.com/vmihailenco/msgpack/v5"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func App_Warn(Company_Charging_r Account.Company, Warning_r *Warning.Warning, Uuid string, Dataj string) {
  13. logs.Println("Nats =>", lib.FuncName(), Uuid, Dataj)
  14. type Ali_Message struct {
  15. Uuid string
  16. Data string
  17. }
  18. b, err := msgpack.Marshal(&Ali_Message{
  19. Uuid: Uuid,
  20. Data: Dataj,
  21. })
  22. if err != nil {
  23. logs.Println("App_Warn", err)
  24. return
  25. }
  26. cun_x := 0
  27. Data := ""
  28. breakHere:
  29. cun_x++
  30. // 控制发送
  31. if cun_x > 3 {
  32. // 添加
  33. Warning_r_e := Warning.Warning{}
  34. Warning_r_e.T_pid = 0
  35. Warning_r_e.T_tp = 1014
  36. Warning_r_e.T_D_name = "App_Warn通讯"
  37. Warning_r_e.T_Remark = "App_Warn通讯 异常!"
  38. Warning_r_e.T_Log = Data
  39. Warning_r_e.T_State = 3
  40. Warning_r_e.T_Ut = time.Now()
  41. //WarningNotice.WarningToAdminId(&Warning_r_e, []int{1}) // 发送给 绑定公司管理员
  42. // 添加报警
  43. Warning.Add_Warning(Warning_r_e)
  44. logs.PrintlnError("App通知", "App_Warn通讯 异常!")
  45. // 达到数量上线跳出
  46. return
  47. }
  48. // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  49. msg, err := lib.Nats.Request("App_Warn", b, 60*time.Second)
  50. if err != nil {
  51. logs.PrintlnError("App_Warn %s", err.Error())
  52. Warning.Add_Warning_Log(Warning_r, "---->App_Warn服务发送超时 异常!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  53. Data = "App_Warn服务通讯 异常!Nats Err!"
  54. time.Sleep(time.Second * time.Duration(cun_x))
  55. goto breakHere // 跳转到标签
  56. }
  57. Data = string(msg.Data)
  58. logs.Println("App_Warn => : %s\n", Data)
  59. if !strings.Contains(Data, "成功") {
  60. Warning.Add_Warning_Log(Warning_r, "---->App_Warn服务发送失败!["+Data+"]!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  61. time.Sleep(time.Second * time.Duration(cun_x))
  62. goto breakHere // 跳转到标签
  63. }
  64. Warning.Add_Warning_Log(Warning_r, "->"+Data+"\n")
  65. }