mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【feat】完成tcp,http的设备上报,及模拟设备测试
This commit is contained in:
@@ -10,9 +10,7 @@ import (
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/mqtt"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/tdengine"
|
||||
"pandax/pkg/tool"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -84,64 +82,30 @@ func (s *HookGrpcService) OnClientConnack(ctx context.Context, in *exhook2.Clien
|
||||
|
||||
func (s *HookGrpcService) OnClientConnected(ctx context.Context, in *exhook2.ClientConnectedRequest) (*exhook2.EmptySuccess, error) {
|
||||
global.Log.Info(fmt.Sprintf("Client %s Connected ", in.Clientinfo.GetNode()))
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
|
||||
if in.Clientinfo.Clientid == mqtt.DefaultDownStreamClientId {
|
||||
return &exhook2.EmptySuccess{}, nil
|
||||
}
|
||||
token := netbase.GetUserName(in.Clientinfo)
|
||||
etoken := &tool.DeviceAuth{}
|
||||
etoken.GetDeviceToken(token)
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: in.Clientinfo.Clientid,
|
||||
DeviceId: etoken.DeviceId,
|
||||
PeerHost: in.Clientinfo.Peerhost,
|
||||
Protocol: in.Clientinfo.Protocol,
|
||||
SocketPort: strconv.Itoa(int(in.Clientinfo.Sockport)),
|
||||
Type: message.ConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, err := netbase.EncodeData(*ci)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 添加设备上线记录
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
Datas: string(v),
|
||||
Type: message.ConnectMes,
|
||||
}
|
||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
||||
s.HookService.MessageCh <- data
|
||||
|
||||
return &exhook2.EmptySuccess{}, nil
|
||||
}
|
||||
|
||||
func (s *HookGrpcService) OnClientDisconnected(ctx context.Context, in *exhook2.ClientDisconnectedRequest) (*exhook2.EmptySuccess, error) {
|
||||
global.Log.Info(fmt.Sprintf("%s断开连接", in.Clientinfo.Username))
|
||||
token := netbase.GetUserName(in.Clientinfo)
|
||||
|
||||
if in.Clientinfo.Clientid == mqtt.DefaultDownStreamClientId {
|
||||
return &exhook2.EmptySuccess{}, nil
|
||||
}
|
||||
etoken := &tool.DeviceAuth{}
|
||||
err := etoken.GetDeviceToken(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: in.Clientinfo.Clientid,
|
||||
DeviceId: etoken.DeviceId,
|
||||
PeerHost: in.Clientinfo.Peerhost,
|
||||
Protocol: in.Clientinfo.Protocol,
|
||||
SocketPort: strconv.Itoa(int(in.Clientinfo.Sockport)),
|
||||
Type: message.DisConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, err := netbase.EncodeData(*ci)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 添加设备下线记录
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
Datas: string(v),
|
||||
Type: message.DisConnectMes,
|
||||
}
|
||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "mqtt", in.Clientinfo.Clientid, in.Clientinfo.Peerhost, etoken)
|
||||
s.HookService.MessageCh <- data
|
||||
return &exhook2.EmptySuccess{}, nil
|
||||
}
|
||||
@@ -225,9 +189,10 @@ func (s *HookGrpcService) OnMessagePublish(ctx context.Context, in *exhook2.Mess
|
||||
eventType := IotHubTopic.GetMessageType(in.Message.Topic)
|
||||
datas := string(in.GetMessage().GetPayload())
|
||||
data := &netbase.DeviceEventInfo{
|
||||
Type: eventType,
|
||||
Datas: datas,
|
||||
DeviceId: etoken.DeviceId,
|
||||
Type: eventType,
|
||||
Datas: datas,
|
||||
DeviceId: etoken.DeviceId,
|
||||
DeviceAuth: etoken,
|
||||
}
|
||||
// 如果是网关子设备单独处理
|
||||
if eventType == message.GATEWAY {
|
||||
@@ -283,32 +248,12 @@ func (s *HookGrpcService) OnMessagePublish(ctx context.Context, in *exhook2.Mess
|
||||
}
|
||||
}
|
||||
if in.Message.Topic == ConnectGatewayTopic {
|
||||
data.Type = message.ConnectMes
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: in.Message.From,
|
||||
Protocol: in.Message.Headers["protocol"],
|
||||
PeerHost: in.Message.Headers["peerhost"],
|
||||
DeviceId: key,
|
||||
Type: message.ConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, _ := netbase.EncodeData(*ci)
|
||||
data.Datas = string(v)
|
||||
data = netbase.CreateConnectionInfo(message.ConnectMes, "mqtt", in.Message.From, in.Message.Headers["peerhost"], etoken)
|
||||
// 子设备发送到队列里
|
||||
s.HookService.MessageCh <- data
|
||||
}
|
||||
if in.Message.Topic == DisconnectGatewayTopic {
|
||||
data.Type = message.DisConnectMes
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: in.Message.From,
|
||||
DeviceId: key,
|
||||
Protocol: in.Message.Headers["protocol"],
|
||||
PeerHost: in.Message.Headers["peerhost"],
|
||||
Type: message.DisConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, _ := netbase.EncodeData(*ci)
|
||||
data.Datas = string(v)
|
||||
data = netbase.CreateConnectionInfo(message.DisConnectMes, "mqtt", in.Message.From, in.Message.Headers["peerhost"], etoken)
|
||||
// 子设备发送到队列里
|
||||
s.HookService.MessageCh <- data
|
||||
}
|
||||
@@ -319,6 +264,7 @@ func (s *HookGrpcService) OnMessagePublish(ctx context.Context, in *exhook2.Mess
|
||||
|
||||
switch eventType {
|
||||
case message.RowMes:
|
||||
data.Type = message.RowMes
|
||||
data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, data.Datas)
|
||||
case message.AttributesMes:
|
||||
attributesData := netbase.UpdateDeviceAttributesData(data.Datas)
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"pandax/iothub/netbase"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/tdengine"
|
||||
"pandax/pkg/tool"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -41,26 +40,8 @@ func InitHttpHook(addr string, hs *hook_message_work.HookService) {
|
||||
// 断开连接
|
||||
switch state {
|
||||
case http.StateHijacked, http.StateClosed:
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
deviceId, _ := activeConnections.Load(conn.RemoteAddr())
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: conn.RemoteAddr().String(),
|
||||
DeviceId: deviceId.(string),
|
||||
PeerHost: conn.RemoteAddr().String(),
|
||||
Protocol: "http",
|
||||
Type: message.ConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, err := netbase.EncodeData(*ci)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 添加设备上线记录
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: deviceId.(string),
|
||||
Datas: string(v),
|
||||
Type: message.ConnectMes,
|
||||
}
|
||||
etoken, _ := activeConnections.Load(conn.RemoteAddr())
|
||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "http", conn.RemoteAddr().String(), conn.RemoteAddr().String(), etoken.(*tool.DeviceAuth))
|
||||
service.HookService.MessageCh <- data
|
||||
activeConnections.Delete(conn.RemoteAddr())
|
||||
}
|
||||
@@ -99,38 +80,22 @@ func (hhs *HookHttpService) hook(req *restful.Request, resp *restful.Response) {
|
||||
}
|
||||
etoken := &tool.DeviceAuth{}
|
||||
etoken.GetDeviceToken(token)
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
_, ok := activeConnections.Load(req.Request.RemoteAddr)
|
||||
// 是否需要添加设备上线通知
|
||||
if !ok {
|
||||
activeConnections.Store(req.Request.RemoteAddr, etoken.DeviceId)
|
||||
ci := &tdengine.ConnectInfo{
|
||||
ClientID: req.Request.RemoteAddr,
|
||||
DeviceId: etoken.DeviceId,
|
||||
PeerHost: req.Request.RemoteAddr,
|
||||
Protocol: "http",
|
||||
Type: message.ConnectMes,
|
||||
Ts: ts,
|
||||
}
|
||||
v, err := netbase.EncodeData(*ci)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 添加设备上线记录
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
Datas: string(v),
|
||||
Type: message.ConnectMes,
|
||||
}
|
||||
activeConnections.Store(req.Request.RemoteAddr, etoken)
|
||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "http", req.Request.RemoteAddr, req.Request.RemoteAddr, etoken)
|
||||
hhs.HookService.MessageCh <- data
|
||||
}
|
||||
marshal, _ := json.Marshal(upData)
|
||||
data := &netbase.DeviceEventInfo{
|
||||
Datas: string(marshal),
|
||||
DeviceId: etoken.DeviceId,
|
||||
Datas: string(marshal),
|
||||
DeviceAuth: etoken,
|
||||
DeviceId: etoken.DeviceId,
|
||||
}
|
||||
switch pathType {
|
||||
case Row:
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
data.Type = message.RowMes
|
||||
data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, data.Datas)
|
||||
case Telemetry:
|
||||
|
||||
@@ -3,28 +3,22 @@ package tcpserver
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
"log"
|
||||
"fmt"
|
||||
"net"
|
||||
"pandax/iothub/hook_message_work"
|
||||
"pandax/iothub/netbase"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"strings"
|
||||
"pandax/pkg/tool"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HookTcpService struct {
|
||||
HookService *hook_message_work.HookService
|
||||
keepAlive int64
|
||||
conn *net.TCPConn
|
||||
}
|
||||
|
||||
func InitTcpHook(addr string, hs *hook_message_work.HookService) {
|
||||
hhs := &HookTcpService{
|
||||
HookService: hs,
|
||||
keepAlive: 20,
|
||||
}
|
||||
server := NewTcpServer(addr)
|
||||
err := server.Start(context.TODO())
|
||||
if err != nil {
|
||||
@@ -40,43 +34,58 @@ func InitTcpHook(addr string, hs *hook_message_work.HookService) {
|
||||
global.Log.Error("Error accepting connection:", err)
|
||||
continue
|
||||
}
|
||||
conn.SetReadDeadline(time.Now().Add(20 * time.Second))
|
||||
hhs.conn = conn
|
||||
conn.SetReadDeadline(time.Now().Add(2 * time.Minute))
|
||||
hhs := &HookTcpService{
|
||||
HookService: hs,
|
||||
conn: conn,
|
||||
}
|
||||
go hhs.hook()
|
||||
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 获取token进行认证
|
||||
func basicAuthenticate(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
|
||||
path := req.Request.URL.Path
|
||||
log.Println(path)
|
||||
split := strings.Split(path, "/")
|
||||
log.Println(split)
|
||||
chain.ProcessFilter(req, resp)
|
||||
}
|
||||
|
||||
func (hhs *HookTcpService) hook() {
|
||||
isAuth := false
|
||||
etoken := &tool.DeviceAuth{}
|
||||
for {
|
||||
buf := make([]byte, 128)
|
||||
n := 0
|
||||
n, err := hhs.conn.Read(buf)
|
||||
if err != nil {
|
||||
// 断开连接 掉线
|
||||
log.Println("断开连接")
|
||||
_ = hhs.conn.Close()
|
||||
//设置断开连接
|
||||
if isAuth {
|
||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||
hhs.HookService.MessageCh <- data
|
||||
}
|
||||
isAuth = false
|
||||
return
|
||||
}
|
||||
if !isAuth {
|
||||
token := string(buf[:n])
|
||||
log.Println(token)
|
||||
isAuth = true
|
||||
etoken.GetDeviceToken(token)
|
||||
auth := netbase.Auth(token)
|
||||
// 认证成功,创建连接记录
|
||||
if auth {
|
||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||
hhs.HookService.MessageCh <- data
|
||||
isAuth = true
|
||||
hhs.Send("success")
|
||||
} else {
|
||||
hhs.Send("fail")
|
||||
}
|
||||
} else {
|
||||
hexData := hex.EncodeToString(buf[:n])
|
||||
log.Println(hexData)
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
DeviceAuth: etoken,
|
||||
Type: message.RowMes,
|
||||
}
|
||||
data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData)
|
||||
// etoken中添加设备标识
|
||||
hhs.HookService.MessageCh <- data
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user