Files
PandaX/iothub/client/updclient/udp.go
2023-10-07 15:03:03 +08:00

40 lines
831 B
Go

package tcpclient
import (
"encoding/hex"
"net"
"pandax/pkg/global"
)
var UdpClient = make(map[string]*net.UDPConn)
func Send(deviceId, msg string) error {
if conn, ok := UdpClient[deviceId]; ok {
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
_, err := conn.Write([]byte(msg))
if err != nil {
return err
}
} else {
global.Log.Infof("设备%s TCP连接不存在, 发送指令失败", deviceId)
}
return nil
}
func SendHex(deviceId, msg string) error {
if conn, ok := UdpClient[deviceId]; ok {
global.Log.Infof("设备%s, 发送指令%s", deviceId, msg)
b, err := hex.DecodeString(msg)
if err != nil {
return err
}
_, err = conn.Write(b)
if err != nil {
return err
}
} else {
global.Log.Infof("设备%s TCP连接不存在, 发送指令失败", deviceId)
}
return nil
}