mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-27 05:49:37 +08:00
【feat】添加tcp指令下发
This commit is contained in:
@@ -11,8 +11,9 @@ import (
|
||||
"github.com/PandaXGO/PandaKit/biz"
|
||||
"github.com/PandaXGO/PandaKit/model"
|
||||
"github.com/PandaXGO/PandaKit/restfulx"
|
||||
"pandax/iothub/client/mqttclient"
|
||||
"pandax/iothub/client/tcpclient"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/mqtt"
|
||||
"pandax/pkg/shadow"
|
||||
"pandax/pkg/tool"
|
||||
"strings"
|
||||
@@ -143,18 +144,25 @@ func (p *DeviceApi) GetDeviceTelemetryHistory(rc *restfulx.ReqCtx) {
|
||||
|
||||
// 下发设备属性
|
||||
func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
|
||||
//id := restfulx.PathParam(rc, "id")
|
||||
id := restfulx.PathParam(rc, "id")
|
||||
key := restfulx.QueryParam(rc, "key")
|
||||
value := restfulx.QueryParam(rc, "value")
|
||||
// 下发指令
|
||||
contentMap := map[string]interface{}{
|
||||
key: value,
|
||||
one := p.DeviceApp.FindOne(id)
|
||||
if one.Product.ProtocolName == global.TCPProtocol {
|
||||
err := tcpclient.Send(id, value)
|
||||
biz.ErrIsNil(err, "属性下发失败")
|
||||
}
|
||||
if one.Product.ProtocolName == global.MQTTProtocol {
|
||||
// 下发指令
|
||||
contentMap := map[string]interface{}{
|
||||
key: value,
|
||||
}
|
||||
content, _ := json.Marshal(contentMap)
|
||||
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
err := rpc.RequestAttributes(mqttclient.RpcPayload{Params: string(content)})
|
||||
biz.ErrIsNil(err, "属性下发失败")
|
||||
}
|
||||
content, _ := json.Marshal(contentMap)
|
||||
var rpc = &mqtt.RpcRequest{Client: global.MqttClient, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
err := rpc.RequestAttributes(mqtt.RpcPayload{Params: string(content)})
|
||||
biz.ErrIsNil(err, "属性下发失败")
|
||||
}
|
||||
|
||||
// InsertDevice 添加Device
|
||||
|
||||
@@ -5,8 +5,9 @@ import (
|
||||
"github.com/PandaXGO/PandaKit/biz"
|
||||
"github.com/PandaXGO/PandaKit/model"
|
||||
"github.com/PandaXGO/PandaKit/restfulx"
|
||||
"pandax/iothub/client/mqttclient"
|
||||
"pandax/iothub/client/tcpclient"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/mqtt"
|
||||
"pandax/pkg/tool"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
|
||||
type DeviceCmdLogApi struct {
|
||||
DeviceCmdLogApp services.DeviceCmdLogModel
|
||||
DeviceApp services.DeviceModel
|
||||
}
|
||||
|
||||
// GetDeviceCmdLogList 告警列表数据
|
||||
@@ -45,15 +47,22 @@ func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) {
|
||||
data.Id = tool.GenerateID()
|
||||
data.State = "2"
|
||||
data.RequestTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
one := p.DeviceApp.FindOne(data.DeviceId)
|
||||
if one.Product.ProtocolName == global.TCPProtocol {
|
||||
err := tcpclient.Send(data.DeviceId, data.CmdContent)
|
||||
biz.ErrIsNil(err, "指令下发失败")
|
||||
data.State = "0"
|
||||
}
|
||||
if one.Product.ProtocolName == global.MQTTProtocol {
|
||||
// 下发指令
|
||||
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
_, err := rpc.RequestCmd(mqttclient.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
|
||||
biz.ErrIsNil(err, "指令下发失败")
|
||||
data.State = "0"
|
||||
}
|
||||
err := p.DeviceCmdLogApp.Insert(data)
|
||||
biz.ErrIsNil(err, "添加指令记录失败")
|
||||
// 下发指令
|
||||
var rpc = &mqtt.RpcRequest{Client: global.MqttClient, Mode: "single"}
|
||||
rpc.GetRequestId()
|
||||
_, err = rpc.RequestCmd(mqtt.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
|
||||
if err != nil {
|
||||
global.Log.Error("指令下发失败")
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteDeviceCmdLog 删除告警
|
||||
|
||||
@@ -57,36 +57,30 @@ func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) {
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = tool.GenerateID()
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
// 向超级表及子表中添加字段
|
||||
stable := ""
|
||||
len := 0
|
||||
if data.Classify == entity.ATTRIBUTES_TSL {
|
||||
stable = data.Pid + "_" + entity.ATTRIBUTES_TSL
|
||||
err := global.TdDb.AddSTableField(stable, data.Key, data.Type, len)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
} else if data.Classify == entity.TELEMETRY_TSL {
|
||||
stable = data.Pid + "_" + entity.TELEMETRY_TSL
|
||||
if data.Type == "string" {
|
||||
len = int(data.Define["length"].(int64))
|
||||
if data.Classify == entity.ATTRIBUTES_TSL || data.Classify == entity.TELEMETRY_TSL {
|
||||
// 向超级表及子表中添加字段
|
||||
len := 0
|
||||
stable := data.Pid + "_" + data.Classify
|
||||
if data.Classify == entity.TELEMETRY_TSL {
|
||||
if data.Type == "string" {
|
||||
len = int(data.Define["length"].(int64))
|
||||
}
|
||||
}
|
||||
err := global.TdDb.AddSTableField(stable, data.Key, data.Type, len)
|
||||
biz.ErrIsNil(err, "添加字段失败")
|
||||
} else {
|
||||
return
|
||||
// 向子表中添加字段
|
||||
go func() {
|
||||
tables, err := global.TdDb.GetListTableByStableName(stable)
|
||||
if err != nil {
|
||||
global.Log.Error(err)
|
||||
return
|
||||
}
|
||||
for _, table := range tables {
|
||||
err = global.TdDb.AddTableField(table.TableName, data.Key, data.Type, 0)
|
||||
global.Log.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
// 向子表中添加字段
|
||||
go func() {
|
||||
tables, err := global.TdDb.GetListTableByStableName(stable)
|
||||
if err != nil {
|
||||
global.Log.Error(err)
|
||||
return
|
||||
}
|
||||
for _, table := range tables {
|
||||
err = global.TdDb.AddTableField(table.TableName, data.Key, data.Type, 0)
|
||||
global.Log.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
p.ProductTemplateApp.Insert(data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user