|
@@ -6,15 +6,19 @@ import (
|
|
"Yunlot/logs"
|
|
"Yunlot/logs"
|
|
"Yunlot/models/Device"
|
|
"Yunlot/models/Device"
|
|
"Yunlot/models/Product"
|
|
"Yunlot/models/Product"
|
|
|
|
+ "encoding/hex"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"plugin"
|
|
"plugin"
|
|
"reflect"
|
|
"reflect"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
// 设备->平台
|
|
// 设备->平台
|
|
func PullHandle(Device_r *Device.Device, topicName string, message []byte) lib.JSONR {
|
|
func PullHandle(Device_r *Device.Device, topicName string, message []byte) lib.JSONR {
|
|
|
|
+ DeviceRealLogR_ := []string{}
|
|
|
|
+
|
|
var Rt_r = lib.JSONR{Code: 200, Msg: "ok"}
|
|
var Rt_r = lib.JSONR{Code: 200, Msg: "ok"}
|
|
|
|
|
|
// 设备协议
|
|
// 设备协议
|
|
@@ -26,6 +30,13 @@ func PullHandle(Device_r *Device.Device, topicName string, message []byte) lib.J
|
|
return Rt_r
|
|
return Rt_r
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 加入设备日志 类型
|
|
|
|
+ if message[0] == '{' {
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "<-接收["+topicName+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+string(message)) // 加入设备日志
|
|
|
|
+ } else {
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "<-接收["+topicName+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+hex.EncodeToString(message)) // 加入设备日志
|
|
|
|
+ }
|
|
|
|
+
|
|
messagejson := string(message)
|
|
messagejson := string(message)
|
|
// 是否加载转换协议
|
|
// 是否加载转换协议
|
|
if ProductProt_r.T_lang != 0 && len(ProductProt_r.T_analysis) != 0 {
|
|
if ProductProt_r.T_lang != 0 && len(ProductProt_r.T_analysis) != 0 {
|
|
@@ -50,6 +61,7 @@ func PullHandle(Device_r *Device.Device, topicName string, message []byte) lib.J
|
|
// 开始处理
|
|
// 开始处理
|
|
logs.Println("协议后:", messagejson)
|
|
logs.Println("协议后:", messagejson)
|
|
|
|
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "<-转换["+topicName+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+messagejson) // 加入设备日志
|
|
}
|
|
}
|
|
|
|
|
|
//logs.Println("首字符:", string(f[0]))
|
|
//logs.Println("首字符:", string(f[0]))
|
|
@@ -112,13 +124,20 @@ func PullHandle(Device_r *Device.Device, topicName string, message []byte) lib.J
|
|
// Device_r.UpdateTime.NowDbTime()
|
|
// Device_r.UpdateTime.NowDbTime()
|
|
// Device_r.Update("T_data", "UpdateTime")
|
|
// Device_r.Update("T_data", "UpdateTime")
|
|
//}
|
|
//}
|
|
|
|
+ //logs.Println("DeviceRealLogR_:",DeviceRealLogR_)
|
|
|
|
+ // 更新设备记录日志
|
|
|
|
+ v, is := logs.DeviceRealLogMap[Device_r.T_sn]
|
|
|
|
+ if is {
|
|
|
|
+ v.Data = append(v.Data, DeviceRealLogR_...)
|
|
|
|
+ logs.DeviceRealLogMap[Device_r.T_sn] = v
|
|
|
|
+ }
|
|
|
|
|
|
return Rt_r
|
|
return Rt_r
|
|
}
|
|
}
|
|
|
|
|
|
// 平台->设备
|
|
// 平台->设备
|
|
func PushHandle(Device_r *Device.Device, topicName string, message string) (string, []byte) {
|
|
func PushHandle(Device_r *Device.Device, topicName string, message string) (string, []byte) {
|
|
-
|
|
|
|
|
|
+ DeviceRealLogR_ := []string{}
|
|
// 设备协议
|
|
// 设备协议
|
|
ProductProt_r := Product.ProductProt{Id: Device_r.T_ProductJson.T_prot}
|
|
ProductProt_r := Product.ProductProt{Id: Device_r.T_ProductJson.T_prot}
|
|
if !ProductProt_r.Read() {
|
|
if !ProductProt_r.Read() {
|
|
@@ -143,17 +162,22 @@ func PushHandle(Device_r *Device.Device, topicName string, message string) (stri
|
|
logs.PrintlnError("PushHandle:", err)
|
|
logs.PrintlnError("PushHandle:", err)
|
|
return "", []byte{}
|
|
return "", []byte{}
|
|
}
|
|
}
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "->转换["+topicName+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+message) // 加入设备日志
|
|
// 类型转换
|
|
// 类型转换
|
|
topicName_r, byte_r = s.(func(sn string, b string) (string, []byte))(topicName, message)
|
|
topicName_r, byte_r = s.(func(sn string, b string) (string, []byte))(topicName, message)
|
|
|
|
+ // 开始处理
|
|
|
|
+ logs.Println("协议后:", byte_r)
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// 无效消息,不用推送
|
|
// 无效消息,不用推送
|
|
if len(topicName_r) == 0 || len(byte_r) == 0 {
|
|
if len(topicName_r) == 0 || len(byte_r) == 0 {
|
|
|
|
+ logs.Println("无效消息,不用推送!", len(topicName_r), len(byte_r))
|
|
return "", []byte{}
|
|
return "", []byte{}
|
|
}
|
|
}
|
|
|
|
|
|
// 长连接 网关
|
|
// 长连接 网关
|
|
|
|
+ logs.Println("ProductProt_r.T_mode:", ProductProt_r.T_mode)
|
|
switch ProductProt_r.T_mode {
|
|
switch ProductProt_r.T_mode {
|
|
case 1: //mqtt
|
|
case 1: //mqtt
|
|
// 如果 订阅地址与发布相同,在后面强行加 _reply,避免发布后无法收到消息
|
|
// 如果 订阅地址与发布相同,在后面强行加 _reply,避免发布后无法收到消息
|
|
@@ -162,18 +186,24 @@ func PushHandle(Device_r *Device.Device, topicName string, message string) (stri
|
|
}
|
|
}
|
|
lib.Mqtt_publish(topicName_r, byte_r) // 返回数据
|
|
lib.Mqtt_publish(topicName_r, byte_r) // 返回数据
|
|
break
|
|
break
|
|
- //case 2: //tcp
|
|
|
|
- //
|
|
|
|
- // break
|
|
|
|
- //case 3: //CoAP
|
|
|
|
- //
|
|
|
|
- // break
|
|
|
|
- //case 4: //websocket
|
|
|
|
- //
|
|
|
|
- // break
|
|
|
|
|
|
+ case 2: //tcp
|
|
|
|
+ lib.TCP_publish(Device_r.T_sn, byte_r)
|
|
|
|
+ break
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if byte_r[0] == '{' {
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "->推送["+topicName_r+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+string(byte_r)) // 加入设备日志
|
|
|
|
+ } else {
|
|
|
|
+ DeviceRealLogR_ = append(DeviceRealLogR_, "->推送["+topicName_r+"]\\r\\n"+time.Now().Format("15:04:05")+"\\r\\n"+hex.EncodeToString(byte_r)) // 加入设备日志
|
|
|
|
+ }
|
|
|
|
+ // 更新设备记录日志
|
|
|
|
+ v, is := logs.DeviceRealLogMap[Device_r.T_sn]
|
|
|
|
+ if is {
|
|
|
|
+ v.Data = append(v.Data, DeviceRealLogR_...)
|
|
|
|
+ logs.DeviceRealLogMap[Device_r.T_sn] = v
|
|
|
|
+ }
|
|
|
|
+
|
|
return topicName_r, byte_r
|
|
return topicName_r, byte_r
|
|
|
|
|
|
}
|
|
}
|