修复一些小问题

Signed-off-by: lixxxww <941403820@qq.com>
This commit is contained in:
lixxxww
2024-01-22 09:59:15 +00:00
committed by Gitee
parent ae7cf745e4
commit 032b14d8ba
3 changed files with 31 additions and 15 deletions

View File

@@ -1,11 +1,16 @@
package api package api
// ==========================================================================
// 生成日期2023-06-30 09:19:43 +0800 CST
// 生成路径: apps/device/api/devices.go
// 生成人panda
// ==========================================================================
import ( import (
"fmt" "fmt"
"pandax/apps/device/util" "pandax/apps/device/util"
"github.com/PandaXGO/PandaKit/biz" "pandax/kit/biz"
"github.com/PandaXGO/PandaKit/model" "pandax/kit/model"
"github.com/PandaXGO/PandaKit/restfulx" "pandax/kit/restfulx"
"pandax/pkg/global" "pandax/pkg/global"
model2 "pandax/pkg/global/model" model2 "pandax/pkg/global/model"
"pandax/pkg/shadow" "pandax/pkg/shadow"
@@ -21,7 +26,6 @@ type DeviceApi struct {
DeviceAlarmApp services.DeviceAlarmModel DeviceAlarmApp services.DeviceAlarmModel
ProductApp services.ProductModel ProductApp services.ProductModel
ProductTemplateApp services.ProductTemplateModel ProductTemplateApp services.ProductTemplateModel
DeviceShadow shadow.DeviceShadow // 添加设备影子
} }
func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) { func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) {
@@ -92,12 +96,13 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
biz.ErrIsNil(err, "查询设备模板失败") biz.ErrIsNil(err, "查询设备模板失败")
// 从设备影子中读取 // 从设备影子中读取
res := make([]entity.DeviceStatusVo, 0) res := make([]entity.DeviceStatusVo, 0)
getDevice := shadow.InitDeviceShadow(device.Name, device.Pid)
rs := make(map[string]shadow.DevicePoint) rs := make(map[string]shadow.DevicePoint)
if classify == global.TslAttributesType { if classify == global.TslAttributesType {
rs, _ = p.DeviceShadow.GetDevicePoints(device.Name, global.TslAttributesType) // 使用GetDevicePoints()函数 rs = getDevice.AttributesPoints
} }
if classify == global.TslTelemetryType { if classify == global.TslTelemetryType {
rs, _ = p.DeviceShadow.GetDevicePoints(device.Name, global.TslTelemetryType) // 使用GetDevicePoints()函数 rs = getDevice.TelemetryPoints
} }
for _, tel := range *template { for _, tel := range *template {
sdv := entity.DeviceStatusVo{ sdv := entity.DeviceStatusVo{

View File

@@ -57,6 +57,18 @@ func init() {
DeviceShadowInstance = shadow DeviceShadowInstance = shadow
} }
func InitDeviceShadow(deviceName, ProductId string) Device {
device, err := DeviceShadowInstance.GetDevice(deviceName)
if err == UnknownDeviceErr {
attributes := make(map[string]DevicePoint)
telemetry := make(map[string]DevicePoint)
device = NewDevice(deviceName, ProductId, attributes, telemetry)
DeviceShadowInstance.AddDevice(device)
//shadow.DeviceShadowInstance.SetDeviceTTL()
}
return device
}
func (d *deviceShadow) AddDevice(device Device) (err error) { func (d *deviceShadow) AddDevice(device Device) (err error) {
if _, ok := d.m.Load(device.Name); ok { if _, ok := d.m.Load(device.Name); ok {
return DeviceRepeatErr return DeviceRepeatErr
@@ -78,7 +90,6 @@ func (d *deviceShadow) GetDevice(deviceName string) (device Device, err error) {
return Device{}, UnknownDeviceErr return Device{}, UnknownDeviceErr
} }
} }
func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, value interface{}) (err error) { func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, value interface{}) (err error) {
deviceAny, ok := d.m.Load(deviceName) deviceAny, ok := d.m.Load(deviceName)
if !ok { if !ok {

View File

@@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/kakuilan/kgo"
) )
// RunSql 运行 // RunSql 运行
@@ -20,19 +22,17 @@ func (s *TdEngine) InsertDevice(deviceKey string, data map[string]interface{}) e
} }
var ( var (
field []string field = []string{}
value []interface{} value = []string{}
placeholders []string
) )
for k, v := range data { for k, v := range data {
field = append(field, k) field = append(field, k)
value = append(value, v) value = append(value, "'"+kgo.KConv.ToStr(v)+"'")
placeholders = append(placeholders, "?")
} }
// 存在sql注入隐患在之后的提交修复
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", strings.ToLower(deviceKey), strings.Join(field, ","), strings.Join(placeholders, ",")) sql := "INSERT INTO ? (?) VALUES (?)"
_, err := s.db.Exec(sql, value...) _, err := s.db.Exec(sql, strings.ToLower(deviceKey), strings.Join(field, ","), strings.Join(value, ","))
return err return err
} }