NatsSbM.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Sb_Message(Company_Charging_r Account.Company, Warning_r *Warning.Warning, PhoneNumbers string, TemplateCode string, TemplateParam string) {
  13. logs.Println("Nats =>", lib.FuncName(), PhoneNumbers, TemplateCode, TemplateParam)
  14. type Ali_Message struct {
  15. PhoneNumbers string
  16. TemplateCode string
  17. TemplateParam string
  18. }
  19. b, err := msgpack.Marshal(&Ali_Message{
  20. PhoneNumbers: PhoneNumbers,
  21. TemplateCode: TemplateCode,
  22. TemplateParam: TemplateParam,
  23. })
  24. if err != nil {
  25. logs.Println("Submail_Message", err)
  26. return
  27. }
  28. cun_x := 0
  29. Data := ""
  30. breakHere:
  31. cun_x++
  32. // 控制发送
  33. if cun_x > 3 {
  34. // 添加
  35. Warning_r_e := Warning.Warning{}
  36. Warning_r_e.T_pid = 0
  37. Warning_r_e.T_tp = 1014
  38. Warning_r_e.T_D_name = "短信服务通讯"
  39. Warning_r_e.T_Remark = "短信服务通讯 异常!"
  40. Warning_r_e.T_Log = Data
  41. Warning_r_e.T_State = 3
  42. Warning_r_e.T_Ut = time.Now()
  43. //WarningNotice.WarningToAdminId(&Warning_r_e, []int{1}) // 发送给 绑定公司管理员
  44. // 添加报警
  45. Warning.Add_Warning(Warning_r_e)
  46. logs.PrintlnError("短信通知", "短信服务通讯 异常!")
  47. // 达到数量上线跳出
  48. return
  49. }
  50. // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  51. msg, err := lib.Nats.Request("Submail_Message", b, 30*time.Second)
  52. if err != nil {
  53. logs.PrintlnError("Submail_Message %s", err.Error())
  54. Warning.Add_Warning_Log(Warning_r, "---->短信服务发送超时 异常!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  55. Data = "短信服务通讯 异常!Nats Err!"
  56. time.Sleep(time.Second * time.Duration(cun_x))
  57. goto breakHere // 跳转到标签
  58. }
  59. Data = string(msg.Data)
  60. logs.Println("Submail_Message => : %s\n", Data)
  61. if !strings.Contains(Data, "成功") {
  62. Warning.Add_Warning_Log(Warning_r, "---->短信服务发送失败!["+Data+"]!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  63. time.Sleep(time.Second * time.Duration(cun_x))
  64. goto breakHere // 跳转到标签
  65. }
  66. Warning.Add_Warning_Log(Warning_r, "->"+Data+" "+time.Now().Format("15:04:05")+"\n")
  67. // ============== 计费 ==============
  68. Warning.Add_WarningSand(Warning.WarningSand{
  69. T_pid: Company_Charging_r.Id,
  70. T_Spid: Warning_r.T_pid,
  71. T_tp: Warning_r.T_tp,
  72. T_Notice: PhoneNumbers,
  73. T_Ntype: 1,
  74. T_Remark: "WID:" + strconv.FormatInt(Warning_r.Id, 10) + "\n" +
  75. "类型:" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\n" +
  76. "SN:" + Warning_r.T_sn + "\n" +
  77. "主机:" + Warning_r.T_D_name + "\n" +
  78. "设备:" + Warning_r.T_DS_name + "\n" +
  79. "备注:" + Warning_r.T_Remark + "\n" +
  80. "通知方式:短信通知\n" +
  81. "通知对象:" + PhoneNumbers,
  82. })
  83. }