修复一些小问题

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

@@ -57,6 +57,18 @@ func init() {
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) {
if _, ok := d.m.Load(device.Name); ok {
return DeviceRepeatErr
@@ -78,7 +90,6 @@ func (d *deviceShadow) GetDevice(deviceName string) (device Device, err error) {
return Device{}, UnknownDeviceErr
}
}
func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, value interface{}) (err error) {
deviceAny, ok := d.m.Load(deviceName)
if !ok {

View File

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