gatewayEmp.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package gateway
  2. import (
  3. "confrontation-training/constant"
  4. "confrontation-training/global"
  5. "fmt"
  6. mqtt "github.com/eclipse/paho.mqtt.golang"
  7. "time"
  8. )
  9. func Publish(client mqtt.Client, topic string, message string) {
  10. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  11. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  12. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  13. } else {
  14. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  15. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  16. }
  17. //time.Sleep(time.Second * 1)
  18. time.Sleep(time.Millisecond * 100)
  19. }
  20. func SendUUID(client mqtt.Client, topic string) {
  21. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, constant.CmdSetUUID); token.Wait() && token.Error() != nil {
  22. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
  23. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", constant.CmdSetUUID)
  24. } else {
  25. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
  26. global.Log4J.Info("publish success, topic: "+topic+", payload: ", constant.CmdSetUUID)
  27. }
  28. //time.Sleep(time.Second * 1)
  29. time.Sleep(time.Millisecond * 100)
  30. }
  31. func ReadUUID(client mqtt.Client, topic string) {
  32. message := "[{\"cmd\":\"AT+SUUID=\",\"param\":\"?\",\"i\":\"0\"}]"
  33. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  34. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  35. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  36. } else {
  37. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  38. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  39. }
  40. //time.Sleep(time.Second * 1)
  41. time.Sleep(time.Millisecond * 100)
  42. }
  43. func ConnectDeviceEmq(client mqtt.Client, topic string, mac string, index string, ai string, at string) {
  44. message := "[{\"cmd\":\"AT+CNN=\",\"m\":\"" + mac + "\",\"i\":\"" + index + "\",\"ai\":\"" + ai + "\",\"at\":\"" + at + "\",\"l\":\"1\",\"x\":\"251\",\"relink\":\"0\",\"timeout\":\"12000\"}]"
  45. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  46. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  47. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  48. } else {
  49. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  50. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  51. }
  52. //time.Sleep(time.Second * 1)
  53. time.Sleep(time.Millisecond * 100)
  54. }
  55. func DisConnectDeviceEmq(client mqtt.Client, topic string, mac string) {
  56. message := "[{\"cmd\":\"AT+DISCNN=\",\"m\":\"" + mac + "\"}]"
  57. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  58. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  59. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  60. } else {
  61. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  62. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  63. }
  64. //time.Sleep(time.Second * 1)
  65. time.Sleep(time.Millisecond * 100)
  66. }
  67. func ConnectedNumber(client mqtt.Client, topic string) {
  68. message := "{\"cmd\":\"AT+CNB=\",\"param\":\"?\"}"
  69. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  70. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  71. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  72. } else {
  73. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  74. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  75. }
  76. //time.Sleep(time.Second * 1)
  77. time.Sleep(time.Millisecond * 100)
  78. }
  79. func ConnectedListEmq(client mqtt.Client, topic string, index string) {
  80. message := "{\"cmd\":\"AT+QUE=\",\"param\":\"?\",\"h\":\"" + index + "\"}"
  81. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  82. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  83. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  84. } else {
  85. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  86. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  87. }
  88. //time.Sleep(time.Second * 1)
  89. time.Sleep(time.Millisecond * 100)
  90. }
  91. // WNP 写入指令 write no response
  92. func WNP(client mqtt.Client, topic string, mac string, uuid string, data string) {
  93. message := "[{\"cmd\":\"AT+WNP=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  94. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  95. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  96. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  97. } else {
  98. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  99. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  100. }
  101. //time.Sleep(time.Second * 1)
  102. time.Sleep(time.Millisecond * 100)
  103. }
  104. // W 写入数据
  105. func W(client mqtt.Client, topic string, mac string, uuid string, data string) {
  106. message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF1\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  107. //messagee = "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF1\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  108. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  109. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  110. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  111. } else {
  112. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  113. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  114. }
  115. //time.Sleep(time.Second * 1)
  116. time.Sleep(time.Millisecond * 100)
  117. }
  118. func Read(client mqtt.Client, topic string, mac string, uuid string) {
  119. message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\"]"
  120. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  121. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  122. global.Log4J.Info("publish failed, topic: "+topic+", payload: ", message)
  123. } else {
  124. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  125. global.Log4J.Info("publish success, topic: "+topic+", payload: ", message)
  126. }
  127. //time.Sleep(time.Second * 1)
  128. time.Sleep(time.Millisecond * 100)
  129. }