mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-06 03:51:25 +08:00
优化
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"pandax/kit/utils"
|
||||
"pandax/pkg/cache"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/shadow"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -97,14 +96,7 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
|
||||
biz.ErrIsNil(err, "查询设备模板失败")
|
||||
// 从设备影子中读取
|
||||
res := make([]entity.DeviceStatusVo, 0)
|
||||
getDevice := shadow.InitDeviceShadow(device.Name, device.Pid)
|
||||
rs := make(map[string]shadow.DevicePoint)
|
||||
if classify == global.TslAttributesType {
|
||||
rs = getDevice.AttributesPoints
|
||||
}
|
||||
if classify == global.TslTelemetryType {
|
||||
rs = getDevice.TelemetryPoints
|
||||
}
|
||||
|
||||
for _, tel := range *template {
|
||||
sdv := entity.DeviceStatusVo{
|
||||
Name: tel.Name,
|
||||
@@ -112,32 +104,25 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
|
||||
Type: tel.Type,
|
||||
Define: tel.Define,
|
||||
}
|
||||
// 有直接从设备影子中查询,没有查询时序数据库最后一条记录
|
||||
if point, ok := rs[tel.Key]; ok {
|
||||
value := point.Value
|
||||
sdv.Time = point.UpdatedAt
|
||||
sdv.Value = value
|
||||
var table string
|
||||
if classify == global.TslTelemetryType {
|
||||
table = fmt.Sprintf("%s_telemetry", strings.ToLower(device.Name))
|
||||
}
|
||||
if classify == global.TslAttributesType {
|
||||
table = fmt.Sprintf("%s_attributes", strings.ToLower(device.Name))
|
||||
}
|
||||
sql := `select ts,? from ? order by ts desc`
|
||||
one, err := global.TdDb.GetOne(sql, strings.ToLower(tel.Key), table)
|
||||
if err == nil {
|
||||
sdv.Value = one[strings.ToLower(tel.Key)]
|
||||
sdv.Time = time.Now()
|
||||
} else {
|
||||
var table string
|
||||
if classify == global.TslTelemetryType {
|
||||
table = fmt.Sprintf("%s_telemetry", strings.ToLower(device.Name))
|
||||
}
|
||||
if classify == global.TslAttributesType {
|
||||
table = fmt.Sprintf("%s_attributes", strings.ToLower(device.Name))
|
||||
}
|
||||
sql := `select ts,? from ? order by ts desc`
|
||||
one, err := global.TdDb.GetOne(sql, strings.ToLower(tel.Key), table)
|
||||
if err == nil {
|
||||
sdv.Value = one[strings.ToLower(tel.Key)]
|
||||
if value, ok := tel.Define["default_value"]; ok {
|
||||
sdv.Value = value
|
||||
sdv.Time = time.Now()
|
||||
} else {
|
||||
if value, ok := tel.Define["default_value"]; ok {
|
||||
sdv.Value = value
|
||||
sdv.Time = time.Now()
|
||||
} else {
|
||||
sdv.Value = "未知"
|
||||
sdv.Time = time.Now()
|
||||
}
|
||||
sdv.Value = "未知"
|
||||
sdv.Time = time.Now()
|
||||
}
|
||||
}
|
||||
res = append(res, sdv)
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"pandax/kit/model"
|
||||
"pandax/kit/restfulx"
|
||||
"pandax/kit/utils"
|
||||
"pandax/pkg/cache"
|
||||
"pandax/pkg/global"
|
||||
"strings"
|
||||
|
||||
@@ -70,6 +69,7 @@ func (p *ProductApi) GetProductTsl(rc *restfulx.ReqCtx) {
|
||||
attributes := make([]map[string]interface{}, 0)
|
||||
telemetry := make([]map[string]interface{}, 0)
|
||||
commands := make([]map[string]interface{}, 0)
|
||||
events := make([]map[string]interface{}, 0)
|
||||
for _, template := range *templates {
|
||||
tslData := map[string]interface{}{
|
||||
"name": template.Name,
|
||||
@@ -86,12 +86,17 @@ func (p *ProductApi) GetProductTsl(rc *restfulx.ReqCtx) {
|
||||
if template.Classify == global.TslCommandsType {
|
||||
commands = append(commands, tslData)
|
||||
}
|
||||
if template.Classify == global.TslEventType {
|
||||
events = append(events, tslData)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rc.ResData = map[string]interface{}{
|
||||
"attributes": attributes,
|
||||
"telemetry": telemetry,
|
||||
"commands": commands,
|
||||
"events": events,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,26 +154,4 @@ func (p *ProductApi) DeleteProduct(rc *restfulx.ReqCtx) {
|
||||
// 删除产品
|
||||
err := p.ProductApp.Delete(ids)
|
||||
biz.ErrIsNil(err, "产品删除失败")
|
||||
// 删除所有模型,固件
|
||||
for _, id := range ids {
|
||||
// 删除超级表
|
||||
deleteDeviceStable(id)
|
||||
// 删除所有缓存
|
||||
cache.DelProductRule(id)
|
||||
// 删除绑定的属性及OTA记录
|
||||
p.TemplateApp.Delete([]string{id})
|
||||
p.OtaAPP.Delete([]string{id})
|
||||
}
|
||||
}
|
||||
|
||||
func deleteDeviceStable(productId string) error {
|
||||
err := global.TdDb.DropStable(productId + "_" + entity.ATTRIBUTES_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = global.TdDb.DropStable(productId + "_" + entity.TELEMETRY_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ const (
|
||||
ATTRIBUTES_TSL = "attributes"
|
||||
TELEMETRY_TSL = "telemetry"
|
||||
COMMANDS_TSL = "commands"
|
||||
EVENT_TSL = "events"
|
||||
LOG_TSL = "logs"
|
||||
TAGS_TSL = "tags"
|
||||
)
|
||||
|
||||
@@ -77,7 +79,7 @@ type ProductOta struct {
|
||||
Description string `json:"description" gorm:"type:varchar(255);comment:说明"`
|
||||
}
|
||||
|
||||
type Define map[string]interface{}
|
||||
type Define map[string]any
|
||||
|
||||
func (a Define) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/cache"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
@@ -117,14 +118,26 @@ func (m *productModelImpl) Delete(ids []string) error {
|
||||
if err := global.Db.Table(m.table).Delete(&entity.Product{}, "id in (?)", ids).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除所有模型,固件
|
||||
for _, id := range ids {
|
||||
// 删除超级表
|
||||
deleteDeviceStable(id)
|
||||
// 删除所有缓存
|
||||
cache.DelProductRule(id)
|
||||
// 删除绑定的属性及OTA记录
|
||||
ProductTemplateModelDao.Delete([]string{id})
|
||||
ProductOtaModelDao.Delete([]string{id})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createDeviceStable(productId string) error {
|
||||
// 属性表
|
||||
err := global.TdDb.CreateStable(productId + "_" + entity.ATTRIBUTES_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 遥测表
|
||||
err = global.TdDb.CreateStable(productId + "_" + entity.TELEMETRY_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -132,6 +145,18 @@ func createDeviceStable(productId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteDeviceStable(productId string) error {
|
||||
err := global.TdDb.DropStable(productId + "_" + entity.ATTRIBUTES_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = global.TdDb.DropStable(productId + "_" + entity.TELEMETRY_TSL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取产品数量统计
|
||||
func (m *productModelImpl) FindProductCount() (count entity.DeviceCount, err error) {
|
||||
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = CURDATE()) AS today FROM products`
|
||||
|
||||
Reference in New Issue
Block a user