【fix】修复设置属性,指令下发,单双向

This commit is contained in:
PandaX
2023-10-10 15:19:35 +08:00
parent eb83923849
commit 4323383bb8
9 changed files with 251 additions and 63 deletions

View File

@@ -148,6 +148,7 @@ func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
key := restfulx.QueryParam(rc, "key")
value := restfulx.QueryParam(rc, "value")
one := p.DeviceApp.FindOne(id)
biz.IsTrue(one.LinkStatus == global.ONLINE, "设备不在线无法设置属性")
if one.Product.ProtocolName == global.TCPProtocol {
err := tcpclient.Send(id, value)
biz.ErrIsNil(err, "属性下发失败")

View File

@@ -48,18 +48,22 @@ func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) {
data.State = "2"
data.RequestTime = time.Now().Format("2006-01-02 15:04:05")
one := p.DeviceApp.FindOne(data.DeviceId)
biz.IsTrue(one.LinkStatus == global.ONLINE, "设备不在线无法下发指令")
if one.Product.ProtocolName == global.TCPProtocol {
err := tcpclient.Send(data.DeviceId, data.CmdContent)
biz.ErrIsNil(err, "指令下发失败")
data.State = "0"
data.ResponseTime = time.Now().Format("2006-01-02 15:04:05.000")
}
if one.Product.ProtocolName == global.MQTTProtocol {
// 下发指令
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: "single"}
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: data.Mode}
rpc.GetRequestId()
_, err := rpc.RequestCmd(mqttclient.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
res, err := rpc.RequestCmd(mqttclient.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
biz.ErrIsNil(err, "指令下发失败")
data.State = "0"
data.ResponseTime = time.Now().Format("2006-01-02 15:04:05.000")
data.CmdContent = res
}
err := p.DeviceCmdLogApp.Insert(data)
biz.ErrIsNil(err, "添加指令记录失败")

View File

@@ -35,6 +35,7 @@ type DeviceCmdLog struct {
CmdName string `gorm:"type:varchar(64);comment:命令名称" json:"cmdName"`
CmdContent string `gorm:"type:longtext;comment:命令内容" json:"cmdContent"`
State string `gorm:"type:varchar(1);comment:命令状态" json:"state"`
Mode string `gorm:"type:varchar(64);comment:下发模式" json:"mode"`
Type string `gorm:"type:varchar(1);comment:命令类型" json:"type"` // 0 自定义 1 命令
ResponseContent string `gorm:"type:longtext;comment:响应内容" json:"responseContent"`
RequestTime string `gorm:"comment:命令下发时间" json:"requestTime"`