123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package gateway
- import (
- "confrontation-training/constant"
- "confrontation-training/global"
- "fmt"
- mqtt "github.com/eclipse/paho.mqtt.golang"
- "time"
- )
- func Publish(client mqtt.Client, topic string, message string) {
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func SendUUID(client mqtt.Client, topic string) {
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, constant.CmdSetUUID); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func ReadUUID(client mqtt.Client, topic string) {
- message := "[{\"cmd\":\"AT+SUUID=\",\"param\":\"?\",\"i\":\"0\"}]"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func ConnectDeviceEmq(client mqtt.Client, topic string, mac string, index string, ai string, at string) {
- message := "[{\"cmd\":\"AT+CNN=\",\"m\":\"" + mac + "\",\"i\":\"" + index + "\",\"ai\":\"" + ai + "\",\"at\":\"" + at + "\",\"l\":\"1\",\"x\":\"251\",\"relink\":\"0\",\"timeout\":\"12000\"}]"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func DisConnectDeviceEmq(client mqtt.Client, topic string, mac string) {
- message := "[{\"cmd\":\"AT+DISCNN=\",\"m\":\"" + mac + "\"}]"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func ConnectedNumber(client mqtt.Client, topic string) {
- message := "{\"cmd\":\"AT+CNB=\",\"param\":\"?\"}"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func ConnectedListEmq(client mqtt.Client, topic string, index string) {
- message := "{\"cmd\":\"AT+QUE=\",\"param\":\"?\",\"h\":\"" + index + "\"}"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func WNP(client mqtt.Client, topic string, mac string, uuid string, data string) {
- message := "[{\"cmd\":\"AT+WNP=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func W(client mqtt.Client, topic string, mac string, uuid string, data string) {
- message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF1\",\"u\":\"" + uuid + "\",\"d\":\"" + data + "\"}]"
-
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
- func Read(client mqtt.Client, topic string, mac string, uuid string) {
- message := "[{\"cmd\":\"AT+W=\",\"m\":\"" + mac + "\",\"s\":\"FFF0\",\"u\":\"" + uuid + "\"]"
- if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
- fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
- } else {
- fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
- }
-
- time.Sleep(time.Millisecond * 100)
- }
|