Skip to content

Commit

Permalink
[优化]大改动,指令下发采用规则链rpc请求
Browse files Browse the repository at this point in the history
  • Loading branch information
PandaX-Go committed Oct 14, 2023
1 parent 42be3b2 commit 7c8001a
Show file tree
Hide file tree
Showing 54 changed files with 1,256 additions and 294 deletions.
6 changes: 3 additions & 3 deletions apps/develop/services/gen_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/PandaXGO/PandaKit/utils"
"pandax/apps/develop/entity"
"pandax/pkg/global"
"pandax/pkg/tool"
"pandax/pkg/global_model"
)

/**
Expand Down Expand Up @@ -135,7 +135,7 @@ func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.DevGe
db = db.Where("table_comment = ?", data.TableComment)
}
// 组织数据访问权限
tool.OrgAuthSet(db, data.RoleId, data.Owner)
global_model.OrgAuthSet(db, data.RoleId, data.Owner)
err := db.Find(&resData).Error
biz.ErrIsNil(err, "获取TableTree失败")
for i := 0; i < len(resData); i++ {
Expand All @@ -162,7 +162,7 @@ func (m *devGenTableModelImpl) FindListPage(page, pageSize int, data entity.DevG
db = db.Where("table_comment = ?", data.TableComment)
}
// 组织数据访问权限
tool.OrgAuthSet(db, data.RoleId, data.Owner)
global_model.OrgAuthSet(db, data.RoleId, data.Owner)
db.Where("delete_time IS NULL")
err := db.Count(&total).Error
err = db.Limit(pageSize).Offset(offset).Find(&list).Error
Expand Down
6 changes: 3 additions & 3 deletions apps/device/api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"pandax/iothub/client/mqttclient"
"pandax/iothub/client/tcpclient"
"pandax/pkg/global"
"pandax/pkg/global_model"
"pandax/pkg/shadow"
"pandax/pkg/tool"
"strings"
"time"

Expand Down Expand Up @@ -161,7 +161,7 @@ func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
content, _ := json.Marshal(contentMap)
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: "single"}
rpc.GetRequestId()
err := rpc.RequestAttributes(mqttclient.RpcPayload{Params: string(content)})
err := rpc.RequestAttributes(global_model.RpcPayload{Params: string(content)})
biz.ErrIsNil(err, "属性下发失败")
}
}
Expand All @@ -174,7 +174,7 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) {
data.OrgId = rc.LoginAccount.OrganizationId
list := p.DeviceApp.FindList(entity.Device{Name: data.Name})
biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("名称%s已存在,设置其他命名", data.Name))
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.LinkStatus = global.INACTIVE
data.LastAt = time.Now()
p.DeviceApp.Insert(data)
Expand Down
71 changes: 52 additions & 19 deletions apps/device/api/device_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package api

// ==========================================================================
import (
"context"
"encoding/json"
"github.com/PandaXGO/PandaKit/biz"
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/iothub/client/mqttclient"
"pandax/iothub/client/tcpclient"
ruleEntity "pandax/apps/rule/entity"
ruleService "pandax/apps/rule/services"
"pandax/pkg/global"
"pandax/pkg/global_model"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"pandax/pkg/tool"
"strings"
"time"
Expand Down Expand Up @@ -44,29 +49,57 @@ func (p *DeviceCmdLogApi) GetDeviceCmdLogList(rc *restfulx.ReqCtx) {
func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) {
var data entity.DeviceCmdLog
restfulx.BindJsonAndValid(rc, &data)
data.Id = tool.GenerateID()
//验证指令格式
ms := make(map[string]interface{})
err := json.Unmarshal([]byte(data.CmdContent), &ms)
biz.ErrIsNil(err, "指令格式不正确")

data.Id = global_model.GenerateID()
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")
// 查询规则链
findOne := ruleService.RuleChainModelDao.FindOne(one.Product.RuleChainId)
ruleData := ruleEntity.RuleDataJson{}
err = tool.StringToStruct(findOne.RuleDataJson, &ruleData)
biz.ErrIsNil(err, "规则链数据转化失败")
dataCode := ruleData.LfData.DataCode
code, err := json.Marshal(dataCode)
//新建规则链实体
instance, errs := rule_engine.NewRuleChainInstance(code)
if len(errs) > 0 {
global.Log.Error("规则链初始化失败", errs[0])
return
}
if one.Product.ProtocolName == global.MQTTProtocol {
// 下发指令
var rpc = &mqttclient.RpcRequest{Client: mqttclient.MqttClient, Mode: data.Mode}
rpc.GetRequestId()
res, err := rpc.RequestCmd(mqttclient.RpcPayload{Method: data.CmdName, Params: data.CmdContent})
biz.ErrIsNil(err, "指令下发失败")
data.State = "0"
go func() {
// 构建规则链消息
metadataVals := map[string]interface{}{
"deviceId": data.DeviceId,
"mode": data.Mode,
"deviceName": one.Name,
"deviceType": one.DeviceType,
"deviceProtocol": one.Product.ProtocolName,
"productId": one.Pid,
"orgId": one.OrgId,
"owner": one.Owner,
}
msg := message.NewMessage(one.Owner, message.RpcRequestToDevice, map[string]interface{}{
"method": data.CmdName,
"params": ms,
}, metadataVals)
err = instance.StartRuleChain(context.Background(), msg)
if err != nil {
global.Log.Error("规则链执行失败", errs)
data.State = "1"
} else {
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, "添加指令记录失败")
err = p.DeviceCmdLogApp.Insert(data)
biz.ErrIsNil(err, "添加指令记录失败")
}()

}

// DeleteDeviceCmdLog 删除告警
Expand Down
4 changes: 2 additions & 2 deletions apps/device/api/device_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/apps/device/entity"
"pandax/apps/device/services"
"pandax/pkg/tool"
"pandax/pkg/global_model"
"strings"
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func (p *DeviceGroupApi) GetDeviceGroup(rc *restfulx.ReqCtx) {
func (p *DeviceGroupApi) InsertDeviceGroup(rc *restfulx.ReqCtx) {
var data entity.DeviceGroup
restfulx.BindJsonAndValid(rc, &data)
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.Owner = rc.LoginAccount.UserName
data.OrgId = rc.LoginAccount.OrganizationId
p.DeviceGroupApp.Insert(data)
Expand Down
4 changes: 2 additions & 2 deletions apps/device/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/pkg/global"
"pandax/pkg/tool"
"pandax/pkg/global_model"
"strings"

"pandax/apps/device/entity"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (p *ProductApi) GetProduct(rc *restfulx.ReqCtx) {
func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) {
var data entity.Product
restfulx.BindJsonAndValid(rc, &data)
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.Owner = rc.LoginAccount.UserName
data.OrgId = rc.LoginAccount.OrganizationId
// 如果未设置规则链,默认为主链
Expand Down
4 changes: 2 additions & 2 deletions apps/device/api/product_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/apps/device/entity"
"pandax/apps/device/services"
"pandax/pkg/tool"
"pandax/pkg/global_model"
"strings"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func (p *ProductCategoryApi) GetProductCategory(rc *restfulx.ReqCtx) {
func (p *ProductCategoryApi) InsertProductCategory(rc *restfulx.ReqCtx) {
var data entity.ProductCategory
restfulx.BindJsonAndValid(rc, &data)
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.Owner = rc.LoginAccount.UserName
data.OrgId = rc.LoginAccount.OrganizationId
p.ProductCategoryApp.Insert(data)
Expand Down
3 changes: 2 additions & 1 deletion apps/device/api/product_ota.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/PandaXGO/PandaKit/biz"
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/pkg/global_model"
"pandax/pkg/tool"
"path"
"strings"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (p *ProductOtaApi) InsertProductOta(rc *restfulx.ReqCtx) {
// 生成文件MD5值
md5, err := tool.GetFileMd5(path.Join(filePath, data.Url))
biz.ErrIsNil(err, "读取文件md5校验值错误")
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.Check = md5
p.ProductOtaApp.Insert(data)
}
Expand Down
4 changes: 2 additions & 2 deletions apps/device/api/product_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
"pandax/pkg/global"
"pandax/pkg/tool"
"pandax/pkg/global_model"
"strings"

"pandax/apps/device/entity"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (p *ProductTemplateApi) GetProductTemplate(rc *restfulx.ReqCtx) {
func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) {
var data entity.ProductTemplate
restfulx.BindJsonAndValid(rc, &data)
data.Id = tool.GenerateID()
data.Id = global_model.GenerateID()
data.OrgId = rc.LoginAccount.OrganizationId
if data.Classify == entity.ATTRIBUTES_TSL || data.Classify == entity.TELEMETRY_TSL {
// 向超级表及子表中添加字段
Expand Down
6 changes: 3 additions & 3 deletions apps/device/entity/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"database/sql/driver"
"encoding/json"
"errors"
"pandax/pkg/global"
"pandax/pkg/global_model"
"time"
)

// DeviceGroup 设备分组
type DeviceGroup struct {
global.BaseAuthModel
global_model.BaseAuthModel
Name string `json:"name" gorm:"type:varchar(128);comment:设备分组名称" validate:"required"`
Pid string `json:"pid" gorm:"type:varchar(64);comment:设备分组类型"`
Path string `json:"path" gorm:"type:varchar(255);comment:设备分组路径"`
Expand All @@ -30,7 +30,7 @@ type DeviceGroupLabel struct {
}

type Device struct {
global.BaseAuthModel
global_model.BaseAuthModel
Name string `json:"name" gorm:"type:varchar(128);comment:设备名称" validate:"required,alphanum"` // mqtt 用户名英文
ParentId string `json:"parentId" gorm:"type:varchar(64);comment:父设备"`
DeviceType string `json:"deviceType" gorm:"type:varchar(64);comment:设备类型"`
Expand Down
10 changes: 5 additions & 5 deletions apps/device/entity/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql/driver"
"encoding/json"
"errors"
"pandax/pkg/global"
"pandax/pkg/global_model"
)

const (
Expand All @@ -22,7 +22,7 @@ const (
)

type ProductCategory struct {
global.BaseAuthModel
global_model.BaseAuthModel
Name string `json:"name" gorm:"type:varchar(128);comment:产品类型名称" validate:"required"`
Pid string `json:"pid" gorm:"type:varchar(64);comment:父产品类型"`
Path string `json:"path" gorm:"type:varchar(255);comment:产品类型路径"`
Expand All @@ -39,7 +39,7 @@ type ProductCategoryLabel struct {
}

type Product struct {
global.BaseAuthModel
global_model.BaseAuthModel
Name string `json:"name" gorm:"type:varchar(128);comment:产品名称" validate:"required"`
PhotoUrl string `json:"photoUrl" gorm:"type:varchar(255);comment:图片地址"`
Description string `json:"description" gorm:"type:varchar(255);comment:产品说明"`
Expand All @@ -58,7 +58,7 @@ type ProductRes struct {
}

type ProductTemplate struct {
global.BaseModel
global_model.BaseModel
Pid string `json:"pid" gorm:"type:varchar(64);comment:产品Id" validate:"required"`
Classify string `json:"classify" gorm:"type:varchar(64);comment:模型归类" validate:"required"` // 属性 遥测 命令 事件
Name string `json:"name" gorm:"type:varchar(64);comment:名称" validate:"required"`
Expand All @@ -69,7 +69,7 @@ type ProductTemplate struct {
}

type ProductOta struct {
global.BaseModel
global_model.BaseModel
Pid string `json:"pid" gorm:"comment:产品Id" validate:"required"`
Name string `json:"name" gorm:"type:varchar(64);comment:固件名称" validate:"required"`
Version string `json:"version" gorm:"type:varchar(64);comment:固件版本" validate:"required"`
Expand Down
Loading

0 comments on commit 7c8001a

Please sign in to comment.