gatewayEmp.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. } else {
  13. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  14. }
  15. //time.Sleep(time.Second * 1)
  16. time.Sleep(time.Millisecond * 100)
  17. }
  18. func SendUUID(client mqtt.Client, topic string) {
  19. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, constant.CmdSetUUID); token.Wait() && token.Error() != nil {
  20. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
  21. } else {
  22. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
  23. }
  24. //time.Sleep(time.Second * 1)
  25. time.Sleep(time.Millisecond * 100)
  26. }
  27. func ReadUUID(client mqtt.Client, topic string) {
  28. message := "[{\"cmd\":\"AT+SUUID=\",\"param\":\"?\",\"i\":\"0\"}]"
  29. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  30. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  31. } else {
  32. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  33. }
  34. //time.Sleep(time.Second * 1)
  35. time.Sleep(time.Millisecond * 100)
  36. }
  37. func ConnectDeviceEmq(client mqtt.Client, topic string, mac string, index string, ai string, at string) {
  38. message := "[{\"cmd\":\"AT+CNN=\",\"m\":\"" + mac + "\",\"i\":\"" + index + "\",\"ai\":\"" + ai + "\",\"at\":\"" + at + "\",\"l\":\"1\",\"x\":\"251\",\"relink\":\"0\",\"timeout\":\"12000\"}]"
  39. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  40. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  41. } else {
  42. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  43. }
  44. //time.Sleep(time.Second * 1)
  45. time.Sleep(time.Millisecond * 100)
  46. }
  47. func DisConnectDeviceEmq(client mqtt.Client, topic string, mac string) {
  48. message := "[{\"cmd\":\"AT+DISCNN=\",\"m\":\"" + mac + "\"}]"
  49. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  50. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  51. } else {
  52. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  53. }
  54. //time.Sleep(time.Second * 1)
  55. time.Sleep(time.Millisecond * 100)
  56. }
  57. func ConnectedNumber(client mqtt.Client, topic string) {
  58. message := "{\"cmd\":\"AT+CNB=\",\"param\":\"?\"}"
  59. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  60. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  61. } else {
  62. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  63. }
  64. //time.Sleep(time.Second * 1)
  65. time.Sleep(time.Millisecond * 100)
  66. }
  67. func ConnectedListEmq(client mqtt.Client, topic string, index string) {
  68. message := "{\"cmd\":\"AT+QUE=\",\"param\":\"?\",\"h\":\"" + index + "\"}"
  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. } else {
  72. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  73. }
  74. //time.Sleep(time.Second * 1)
  75. time.Sleep(time.Millisecond * 100)
  76. }
  77. // WNP 写入指令 write no response
  78. func WNP(client mqtt.Client, topic string, mac string, uuid string, data string) {
  79. message := "[{\"cmd\":\"AT+WNP=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  80. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  81. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  82. } else {
  83. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  84. }
  85. //time.Sleep(time.Second * 1)
  86. time.Sleep(time.Millisecond * 100)
  87. }
  88. // W 写入数据
  89. func W(client mqtt.Client, topic string, mac string, uuid string, data string) {
  90. message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF1\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  91. //messagee = "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF1\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
  92. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  93. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  94. } else {
  95. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  96. }
  97. //time.Sleep(time.Second * 1)
  98. time.Sleep(time.Millisecond * 100)
  99. }
  100. func Read(client mqtt.Client, topic string, mac string, uuid string) {
  101. message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\"]"
  102. if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
  103. fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
  104. } else {
  105. fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
  106. }
  107. //time.Sleep(time.Second * 1)
  108. time.Sleep(time.Millisecond * 100)
  109. }