mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[fix] 客户端map错误
This commit is contained in:
@@ -8,10 +8,11 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"pandax/pkg/global"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// key 设备id,value MQTT的clientID
|
||||
var MqttClient = make(map[string]string)
|
||||
var MqttClient sync.Map
|
||||
|
||||
const ClientsInfo string = "client"
|
||||
const SubscribeTopicsInfo string = "subscribe"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mqttclient
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
@@ -25,12 +26,20 @@ type RpcRequest struct {
|
||||
// RequestCmd 下发指令
|
||||
func (rpc RpcRequest) RequestCmd(deviceId, rpcPayload string) error {
|
||||
topic := fmt.Sprintf(RpcReqTopic, rpc.RequestId)
|
||||
return Publish(topic, MqttClient[deviceId], rpcPayload)
|
||||
value, ok := MqttClient.Load(deviceId)
|
||||
if !ok {
|
||||
return errors.New("为获取到设备的MQTT连接")
|
||||
}
|
||||
return Publish(topic, value.(string), rpcPayload)
|
||||
}
|
||||
|
||||
func (rpc RpcRequest) Pub(deviceId, reqPayload string) error {
|
||||
topic := fmt.Sprintf(RpcRespTopic, rpc.RequestId)
|
||||
return Publish(topic, MqttClient[deviceId], reqPayload)
|
||||
value, ok := MqttClient.Load(deviceId)
|
||||
if !ok {
|
||||
return errors.New("为获取到设备的MQTT连接")
|
||||
}
|
||||
return Publish(topic, value.(string), reqPayload)
|
||||
}
|
||||
|
||||
func (rpc *RpcRequest) GetRequestId() {
|
||||
|
||||
@@ -2,14 +2,17 @@ package tcpclient
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net"
|
||||
"pandax/pkg/global"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var TcpClient = make(map[string]*net.TCPConn)
|
||||
var TcpClient sync.Map
|
||||
|
||||
func Send(deviceId, msg string) error {
|
||||
if conn, ok := TcpClient[deviceId]; ok {
|
||||
if conn, ok := TcpClient.Load(deviceId); ok {
|
||||
conn := conn.(*net.TCPConn)
|
||||
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||
_, err := conn.Write([]byte(msg))
|
||||
if err != nil {
|
||||
@@ -17,12 +20,15 @@ func Send(deviceId, msg string) error {
|
||||
}
|
||||
} else {
|
||||
global.Log.Infof("设备%s TCP连接不存在, 发送指令失败", deviceId)
|
||||
return errors.New("为获取到设备的MQTT连接")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendHex(deviceId, msg string) error {
|
||||
if conn, ok := TcpClient[deviceId]; ok {
|
||||
|
||||
if conn, ok := TcpClient.Load(deviceId); ok {
|
||||
conn := conn.(*net.TCPConn)
|
||||
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||
b, err := hex.DecodeString(msg)
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"pandax/pkg/global"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type UdpClientT struct {
|
||||
@@ -11,10 +12,12 @@ type UdpClientT struct {
|
||||
Addr *net.UDPAddr
|
||||
}
|
||||
|
||||
var UdpClient = make(map[string]*UdpClientT)
|
||||
// var UdpClient = make(map[string]*UdpClientT)
|
||||
var UdpClient sync.Map
|
||||
|
||||
func Send(deviceId, msg string) error {
|
||||
if conn, ok := UdpClient[deviceId]; ok {
|
||||
if conn, ok := UdpClient.Load(deviceId); ok {
|
||||
conn := conn.(*UdpClientT)
|
||||
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||
_, err := conn.Conn.WriteToUDP([]byte(msg), conn.Addr)
|
||||
if err != nil {
|
||||
@@ -27,7 +30,8 @@ func Send(deviceId, msg string) error {
|
||||
}
|
||||
|
||||
func SendHex(deviceId, msg string) error {
|
||||
if conn, ok := UdpClient[deviceId]; ok {
|
||||
if conn, ok := UdpClient.Load(deviceId); ok {
|
||||
conn := conn.(*UdpClientT)
|
||||
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||
b, err := hex.DecodeString(msg)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user