Clients.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package MqttServer
  2. import (
  3. "Yunlot/Handle"
  4. "Yunlot/logs"
  5. "Yunlot/models/Device"
  6. "encoding/json"
  7. "time"
  8. )
  9. // / - 连接 实体
  10. type Ms_Project_Connected struct {
  11. Clientid string `json:"clientid"`
  12. Username string `json:"username"`
  13. Ipaddress string `json:"ipaddress"`
  14. Reason string `json:"reason"` // sha ccv
  15. }
  16. // / - 离线断开 实体
  17. type Ms_Project_Disconnected struct {
  18. Clientid string `json:"clientid"`
  19. Username string `json:"username"`
  20. Ipaddress string `json:"ipaddress"`
  21. Reason string `json:"reason"`
  22. //终端连接断开原因:
  23. //normal:客户端主动断开
  24. //kicked:服务端踢出,通过 REST API
  25. //keepalive_timeout: keepalive 超时
  26. //not_authorized: 认证失败,或者 acl_nomatch = disconnect 时没有权限的 Pub/Sub 会主动断开客户端
  27. //tcp_closed: 对端关闭了网络连接
  28. //internal_error: 畸形报文或其他未知错误
  29. }
  30. // 设备连接
  31. func MessageConnected(topicName string, message []byte) {
  32. logs.Println("============= MessageConnected Mqtt JSON =============")
  33. logs.Println("topic:", topicName, " message:", string(message))
  34. //logs.Println("=>", a)
  35. var Ms_project Ms_Project_Connected
  36. err := json.Unmarshal(message, &Ms_project)
  37. if err != nil {
  38. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Project]", string(message))
  39. return
  40. }
  41. var r_Device Device.Device
  42. r_Device.T_sn = Ms_project.Clientid
  43. r_Device.Read()
  44. if err != nil {
  45. logs.Println("MessageDisconnected 没有该设备:", Ms_project.Clientid)
  46. return
  47. }
  48. logs.Println("Clientid:", Ms_project.Clientid)
  49. logs.Println("Username:", Ms_project.Username)
  50. r_Device.T_online = 1
  51. // 同步参数
  52. r_Device.Update("T_online")
  53. var Device_online_r map[string]interface{}
  54. Device_online_r = make(map[string]interface{})
  55. Device_online_r["online"] = r_Device.T_online
  56. Device_online_r["msg"] = "设备主动上线[" + Ms_project.Ipaddress + "]"
  57. Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
  58. Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
  59. logs.Println("============= MessageConnected Mqtt JSON AND =============", topicName)
  60. }
  61. func ToReason(str string) (Reason string) {
  62. switch str {
  63. case "normal":
  64. Reason = "客户端主动断开"
  65. break
  66. case "kicked":
  67. Reason = "设备异常掉线,服务端踢出,通过 REST API"
  68. break
  69. case "keepalive_timeout":
  70. Reason = "设备异常掉线,keepalive 超时"
  71. break
  72. case "not_authorized":
  73. Reason = "设备异常掉线,认证失败"
  74. break
  75. case "tcp_closed":
  76. Reason = "设备异常掉线,对端关闭了网络连接"
  77. break
  78. case "internal_error":
  79. Reason = "设备异常掉线,畸形报文或其他未知错误"
  80. break
  81. }
  82. return Reason
  83. }
  84. // 设备断开
  85. func MessageDisconnected(topicName string, message []byte) {
  86. logs.Println("============= MessageDisconnected Mqtt JSON =============")
  87. logs.Println("topic:", topicName, " message:", string(message))
  88. //logs.Println("=>", a)
  89. var Ms_project Ms_Project_Disconnected
  90. err := json.Unmarshal(message, &Ms_project)
  91. if err != nil {
  92. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Project]", string(message))
  93. return
  94. }
  95. logs.Println("Clientid:", Ms_project.Clientid)
  96. logs.Println("Username:", Ms_project.Username)
  97. var r_Device Device.Device
  98. r_Device.T_sn = Ms_project.Clientid
  99. r_Device.Read()
  100. if err != nil {
  101. logs.Println("MessageDisconnected 没有该设备:", Ms_project.Clientid)
  102. return
  103. }
  104. r_Device.T_online = 2
  105. // 同步参数
  106. r_Device.Update("T_online")
  107. var Device_online_r map[string]interface{}
  108. Device_online_r = make(map[string]interface{})
  109. Device_online_r["online"] = r_Device.T_online
  110. Device_online_r["msg"] = "设备离线[" + ToReason(Ms_project.Reason) + "]"
  111. Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
  112. Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
  113. logs.Println("============= MessageDisconnected Mqtt JSON AND =============", topicName)
  114. }