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"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"pandax/pkg/global"
|
"pandax/pkg/global"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// key 设备id,value MQTT的clientID
|
// key 设备id,value MQTT的clientID
|
||||||
var MqttClient = make(map[string]string)
|
var MqttClient sync.Map
|
||||||
|
|
||||||
const ClientsInfo string = "client"
|
const ClientsInfo string = "client"
|
||||||
const SubscribeTopicsInfo string = "subscribe"
|
const SubscribeTopicsInfo string = "subscribe"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mqttclient
|
package mqttclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@@ -25,12 +26,20 @@ type RpcRequest struct {
|
|||||||
// RequestCmd 下发指令
|
// RequestCmd 下发指令
|
||||||
func (rpc RpcRequest) RequestCmd(deviceId, rpcPayload string) error {
|
func (rpc RpcRequest) RequestCmd(deviceId, rpcPayload string) error {
|
||||||
topic := fmt.Sprintf(RpcReqTopic, rpc.RequestId)
|
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 {
|
func (rpc RpcRequest) Pub(deviceId, reqPayload string) error {
|
||||||
topic := fmt.Sprintf(RpcRespTopic, rpc.RequestId)
|
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() {
|
func (rpc *RpcRequest) GetRequestId() {
|
||||||
|
|||||||
@@ -2,14 +2,17 @@ package tcpclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"pandax/pkg/global"
|
"pandax/pkg/global"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TcpClient = make(map[string]*net.TCPConn)
|
var TcpClient sync.Map
|
||||||
|
|
||||||
func Send(deviceId, msg string) error {
|
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)
|
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||||
_, err := conn.Write([]byte(msg))
|
_, err := conn.Write([]byte(msg))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -17,12 +20,15 @@ func Send(deviceId, msg string) error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
global.Log.Infof("设备%s TCP连接不存在, 发送指令失败", deviceId)
|
global.Log.Infof("设备%s TCP连接不存在, 发送指令失败", deviceId)
|
||||||
|
return errors.New("为获取到设备的MQTT连接")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendHex(deviceId, msg string) error {
|
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)
|
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||||
b, err := hex.DecodeString(msg)
|
b, err := hex.DecodeString(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"net"
|
"net"
|
||||||
"pandax/pkg/global"
|
"pandax/pkg/global"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UdpClientT struct {
|
type UdpClientT struct {
|
||||||
@@ -11,10 +12,12 @@ type UdpClientT struct {
|
|||||||
Addr *net.UDPAddr
|
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 {
|
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)
|
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||||
_, err := conn.Conn.WriteToUDP([]byte(msg), conn.Addr)
|
_, err := conn.Conn.WriteToUDP([]byte(msg), conn.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -27,7 +30,8 @@ func Send(deviceId, msg string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SendHex(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)
|
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
|
||||||
b, err := hex.DecodeString(msg)
|
b, err := hex.DecodeString(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package netbase
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"pandax/apps/device/entity"
|
"pandax/apps/device/entity"
|
||||||
"pandax/apps/device/services"
|
"pandax/apps/device/services"
|
||||||
"pandax/iothub/server/emqxserver/protobuf"
|
"pandax/iothub/server/emqxserver/protobuf"
|
||||||
@@ -38,7 +37,6 @@ func Auth(authToken string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
etoken = services.GetDeviceToken(&device.Device)
|
etoken = services.GetDeviceToken(&device.Device)
|
||||||
log.Println("设置设备协议", device.Product.ProtocolName)
|
|
||||||
etoken.DeviceProtocol = device.Product.ProtocolName
|
etoken.DeviceProtocol = device.Product.ProtocolName
|
||||||
err = cache.SetDeviceEtoken(authToken, etoken.GetMarshal(), time.Hour*24*365)
|
err = cache.SetDeviceEtoken(authToken, etoken.GetMarshal(), time.Hour*24*365)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func (s *HookGrpcService) OnClientConnected(ctx context.Context, in *exhook2.Cli
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//添加连接ID
|
//添加连接ID
|
||||||
mqttclient.MqttClient[etoken.DeviceId] = in.Clientinfo.Clientid
|
mqttclient.MqttClient.Store(etoken.DeviceId, in.Clientinfo.Clientid)
|
||||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
data := netbase.CreateConnectionInfo(message.ConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
||||||
s.HookService.MessageCh <- data
|
s.HookService.MessageCh <- data
|
||||||
return &exhook2.EmptySuccess{}, nil
|
return &exhook2.EmptySuccess{}, nil
|
||||||
@@ -102,7 +102,7 @@ func (s *HookGrpcService) OnClientDisconnected(ctx context.Context, in *exhook2.
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//删除连接ID
|
//删除连接ID
|
||||||
delete(mqttclient.MqttClient, etoken.DeviceId)
|
mqttclient.MqttClient.Delete(etoken.DeviceId)
|
||||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
data := netbase.CreateConnectionInfo(message.DisConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
||||||
s.HookService.MessageCh <- data
|
s.HookService.MessageCh <- data
|
||||||
return &exhook2.EmptySuccess{}, nil
|
return &exhook2.EmptySuccess{}, nil
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func (hhs *HookTcpService) hook() {
|
|||||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||||
hhs.HookService.MessageCh <- data
|
hhs.HookService.MessageCh <- data
|
||||||
}
|
}
|
||||||
delete(tcpclient.TcpClient, etoken.DeviceId)
|
tcpclient.TcpClient.Delete(etoken.DeviceId)
|
||||||
isAuth = false
|
isAuth = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ func (hhs *HookTcpService) hook() {
|
|||||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||||
hhs.HookService.MessageCh <- data
|
hhs.HookService.MessageCh <- data
|
||||||
isAuth = true
|
isAuth = true
|
||||||
tcpclient.TcpClient[etoken.DeviceId] = hhs.conn
|
tcpclient.TcpClient.Store(etoken.DeviceId, hhs.conn)
|
||||||
hhs.Send("success")
|
hhs.Send("success")
|
||||||
} else {
|
} else {
|
||||||
hhs.Send("fail")
|
hhs.Send("fail")
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
|
|||||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
|
data := netbase.CreateConnectionInfo(message.DisConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
|
||||||
hhs.HookService.MessageCh <- data
|
hhs.HookService.MessageCh <- data
|
||||||
}
|
}
|
||||||
delete(udpclient.UdpClient, etoken.DeviceId)
|
udpclient.UdpClient.Delete(etoken.DeviceId)
|
||||||
delete(authMap, client.AddrPort().String())
|
delete(authMap, client.AddrPort().String())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -73,10 +73,10 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
|
|||||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
|
data := netbase.CreateConnectionInfo(message.ConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
|
||||||
hhs.HookService.MessageCh <- data
|
hhs.HookService.MessageCh <- data
|
||||||
authMap[client.AddrPort().String()] = true
|
authMap[client.AddrPort().String()] = true
|
||||||
udpclient.UdpClient[etoken.DeviceId] = &udpclient.UdpClientT{
|
udpclient.UdpClient.Store(etoken.DeviceId, &udpclient.UdpClientT{
|
||||||
Conn: server.listener,
|
Conn: server.listener,
|
||||||
Addr: client,
|
Addr: client,
|
||||||
}
|
})
|
||||||
hhs.Send(client, "success")
|
hhs.Send(client, "success")
|
||||||
} else {
|
} else {
|
||||||
hhs.Send(client, "fail")
|
hhs.Send(client, "fail")
|
||||||
|
|||||||
Reference in New Issue
Block a user