fix: 修复一堆bug

This commit is contained in:
godLei6
2025-05-10 13:06:05 +08:00
parent d593f8f5d2
commit 3a929557c9
9 changed files with 130 additions and 28 deletions

4
go.mod
View File

@@ -8,8 +8,8 @@ toolchain go1.23.8
//gitee.com/unitedrhino/share => ../share
require (
gitee.com/unitedrhino/core v1.4.2
gitee.com/unitedrhino/share v1.4.8
gitee.com/unitedrhino/core v1.4.3
gitee.com/unitedrhino/share v1.4.9
gitee.com/unitedrhino/squirrel v1.20.6
github.com/dgraph-io/ristretto v0.1.0
github.com/dop251/goja v0.0.0-20231024180952-594410467bc6

8
go.sum
View File

@@ -1,11 +1,11 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
gitee.com/unitedrhino/core v1.4.2 h1:Ez2xEWegIFwmVHK7ZsDw37JiME1Kdezh0CLoFzaMJ2w=
gitee.com/unitedrhino/core v1.4.2/go.mod h1:CLrrQyuDifrUDo0tprz9XlhlRTgL2CxhCWouLkT5CmI=
gitee.com/unitedrhino/core v1.4.3 h1:fAvbtKRdrjWTAvxXlYfW5lHzOp4bPj8aVR6PeoU4YbM=
gitee.com/unitedrhino/core v1.4.3/go.mod h1:vEO68lZNFTtJYygV6+wF44ogYpVm3sjTtGQPKSERJ7o=
gitee.com/unitedrhino/driver-go/v3 v3.30.2 h1:DxiojcyCCdA7CnoWI7igGH6xM2PvAWAhqgl2si4u8vU=
gitee.com/unitedrhino/driver-go/v3 v3.30.2/go.mod h1:ZVLHSMMq8n7ShN1pxRXL43GBjkFdnx9TqEd6M69oe1A=
gitee.com/unitedrhino/share v1.4.8 h1:8NDo8aYGopJyV5DPuKbkc66cbAreH2lh3ookV0TTeIs=
gitee.com/unitedrhino/share v1.4.8/go.mod h1:LPGOFhkKduMTiJ6RXZ0+KmuunzICkRJ6TNQdN8SyQ1o=
gitee.com/unitedrhino/share v1.4.9 h1:aB99RJmoWlnJObAkrGYAdUdOcC2jSh1P7Ja+4tUHndM=
gitee.com/unitedrhino/share v1.4.9/go.mod h1:LPGOFhkKduMTiJ6RXZ0+KmuunzICkRJ6TNQdN8SyQ1o=
gitee.com/unitedrhino/squirrel v1.20.6 h1:VtPbNOGOZtTyLsxdriWS+lq8KmnoMTpiIrD/AU5OchM=
gitee.com/unitedrhino/squirrel v1.20.6/go.mod h1:RebzP2xW9MiaYlrwrwl8rmkwnjLV7JYYE46rNXylLTo=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=

View File

@@ -11,7 +11,6 @@ import (
"gitee.com/unitedrhino/core/share/middlewares"
"gitee.com/unitedrhino/share/caches"
"gitee.com/unitedrhino/share/conf"
"gitee.com/unitedrhino/share/ctxs"
"gitee.com/unitedrhino/share/eventBus"
"gitee.com/unitedrhino/share/oss"
"gitee.com/unitedrhino/share/utils"
@@ -162,7 +161,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
CheckTokenWare: middlewares.NewCheckTokenWareMiddleware(ur, ro, tm, lo).Handle,
InitCtxsWare: ctxs.InitMiddleware,
InitCtxsWare: middlewares.InitMiddleware,
OssClient: ossClient,
OtaM: otaM,
Ws: ws.MustNewServer(c.RestConf),

View File

@@ -179,7 +179,13 @@ func (s *ScriptTrans) GetFunc(ctx context.Context, script string, funcName strin
}
return handle.Interface(), &logs, nil
}
func (s *ScriptTrans) PublishMsgRun(ctx context.Context, msg *deviceMsg.PublishMsg, script string) (*deviceMsg.PublishMsg, []string, error) {
func (s *ScriptTrans) PublishMsgRun(ctx context.Context, msg *deviceMsg.PublishMsg, script string) (msgs *deviceMsg.PublishMsg, retLogs []string, err error) {
defer func() {
if p := recover(); p != nil {
err = errors.Parameter.AddMsgf("执行panic:%v", p)
return
}
}()
var out = *msg
handle, logs, err := s.GetFunc(ctx, script, "Handle")
if err != nil {
@@ -196,7 +202,13 @@ func (s *ScriptTrans) PublishMsgRun(ctx context.Context, msg *deviceMsg.PublishM
return newMsg, *logs, nil
}
func (s *ScriptTrans) RespMsgRun(ctx context.Context, req *deviceMsg.PublishMsg, resp *deviceMsg.PublishMsg, script string) ([]string, error) {
func (s *ScriptTrans) RespMsgRun(ctx context.Context, req *deviceMsg.PublishMsg, resp *deviceMsg.PublishMsg, script string) (retLogs []string, err error) {
defer func() {
if p := recover(); p != nil {
err = errors.Parameter.AddMsgf("执行panic:%v", p)
return
}
}()
handle, logs, err := s.GetFunc(ctx, script, "Handle")
if err != nil {
return nil, errors.Parameter.AddMsg("结构体中需要定义: func Handle(context.Context, *dm.PublishMsg) *dm.PublishMsg")

View File

@@ -182,7 +182,7 @@ func (l *ThingLogic) InsertPackReport(msg *deviceMsg.PublishMsg, t *schema.Model
}
for _, tp := range pTp {
timeStamp := time.UnixMilli(tp.Timestamp)
if timeStamp.IsZero() {
if tp.Timestamp == 0 {
timeStamp = l.dreq.GetTimeStamp(msg.Timestamp)
}
@@ -279,7 +279,7 @@ func (l *ThingLogic) InsertPackReport(msg *deviceMsg.PublishMsg, t *schema.Model
return err
}
dbData.TimeStamp = time.UnixMilli(tp.Timestamp)
if dbData.TimeStamp.IsZero() {
if tp.Timestamp == 0 {
dbData.TimeStamp = l.dreq.GetTimeStamp(msg.Timestamp)
}
paramValues, err := msgThing.ToParamValues(tp.Params)
@@ -1017,16 +1017,16 @@ func (l *ThingLogic) OnlineFix(msg *deviceMsg.PublishMsg, di *dm.DeviceInfo, gw
if di.IsOnline == def.True {
return
}
err = devicemanagelogic.HandleOnlineFix(l.ctx, l.svcCtx, &deviceStatus.ConnectMsg{
ClientID: deviceAuth.GenClientID(di.ProductID, di.DeviceName),
Timestamp: l.dreq.GetTimeStamp(msg.Timestamp),
Action: devices.ActionConnected,
Reason: "report_fix",
})
if err != nil {
l.Error(err)
}
if di.DeviceType != def.DeviceTypeSubset { //如果不是子设备类型则直接修复即可
err := devicemanagelogic.HandleOnlineFix(l.ctx, l.svcCtx, &deviceStatus.ConnectMsg{
ClientID: deviceAuth.GenClientID(di.ProductID, di.DeviceName),
Timestamp: l.dreq.GetTimeStamp(msg.Timestamp),
Action: devices.ActionConnected,
Reason: "report_fix",
})
if err != nil {
l.Error(err)
}
return
}
var inDev = devices.WithGateway{

View File

@@ -3,6 +3,8 @@ package devicegrouplogic
import (
"context"
"fmt"
"gitee.com/unitedrhino/core/share/dataType"
"gitee.com/unitedrhino/share/ctxs"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/share/oss"
"gitee.com/unitedrhino/things/service/dmsvr/internal/repo/relationDB"
@@ -41,6 +43,11 @@ func (l *GroupInfoUpdateLogic) GroupInfoUpdate(in *dm.GroupInfo) (*dm.Empty, err
if po.Tags == nil {
po.Tags = map[string]string{}
}
if ctxs.IsAdmin(l.ctx) == nil {
if in.AreaID != 0 {
po.AreaID = dataType.AreaID(in.AreaID)
}
}
if in.Files != nil {
if po.Files == nil {
po.Files = map[string]string{}

View File

@@ -0,0 +1,12 @@
package devicemanagelogic
import (
"context"
"gitee.com/unitedrhino/things/service/dmsvr/internal/svc"
"gitee.com/unitedrhino/things/share/devices"
)
func UpdateDevice(ctx context.Context, svcCtx *svc.ServiceContext, devs []*devices.Core, affiliation devices.Affiliation) error {
svcCtx.AbnormalRepo.UpdateDevice(ctx, devs, affiliation)
return nil
}

View File

@@ -29,15 +29,22 @@ func AffiliationToMap(in devices.Affiliation) map[string]any {
func AlterTag(ctx context.Context, t *clients.Td, tables []string, tags map[string]any) error {
for _, table := range tables {
var vals []string
for k, v := range tags {
_, err := t.ExecContext(ctx, fmt.Sprintf(" ALTER TABLE %s SET TAG `%s`='%v'; ",
table, k, v))
vals = append(vals, fmt.Sprintf(" `%s`='%v' ", k, v))
}
for i := 3; i > 0; i-- { //重试三次
val := strings.Join(vals, ",")
_, err := t.ExecContext(ctx, fmt.Sprintf(" ALTER TABLE %s SET TAG %s; ",
table, val))
if err != nil {
if strings.Contains(err.Error(), "Table does not exist") {
break
}
logx.Error(err)
logx.WithContext(ctx).Error(err)
continue
}
break
}
}
return nil
@@ -50,17 +57,23 @@ type Tag struct {
func AlterTags(ctx context.Context, t *clients.Td, tags []Tag) error {
for _, tag := range tags {
var vals []string
for k, v := range tag.Tags {
_, err := t.ExecContext(ctx, fmt.Sprintf(" ALTER TABLE %s SET TAG `%s`='%v'; ",
tag.Table, k, v))
vals = append(vals, fmt.Sprintf(" `%s`='%v' ", k, v))
}
for i := 3; i > 0; i-- { //重试三次
val := strings.Join(vals, ",")
_, err := t.ExecContext(ctx, fmt.Sprintf(" ALTER TABLE %s SET TAG %s; ",
tag.Table, val))
if err != nil {
if strings.Contains(err.Error(), "Table does not exist") {
break
}
logx.Error(err)
logx.WithContext(ctx).Error(err)
continue
}
break
}
}
return nil
}

59
tests/script/youren.go Normal file
View File

@@ -0,0 +1,59 @@
package script
import (
"gitee.com/unitedrhino/things/share/domain/deviceMsg"
"gitee.com/unitedrhino/things/share/domain/deviceMsg/msgThing"
"github.com/tidwall/gjson"
"strings"
)
type Req struct {
Params struct {
Dir string `json:"dir"`
Id string `json:"id"`
RData []struct {
Name string `json:"name"`
Value string `json:"value"`
Err string `json:"err"`
} `json:"r_data"`
} `json:"params"`
}
var j = `{
"params": {
"dir": "up",
"id": "02102925031500024611",
"r_data": [
{
"name": "365.lux",
"value": "792",
"err": "0"
}
]
}
}`
func Run() {
var req msgThing.Req
rData := gjson.Get(j, "params.r_data")
arr := rData.Array()
var property = map[string]map[string]any{} //k为设备ID k2 为属性id v为值
for _, v := range arr {
name := v.Get("name").String()
Value := v.Get("value").String()
dev, p, _ := strings.Cut(name, ".")
if property[name] == nil {
property[name] = make(map[string]any)
}
property[dev][p] = Value
}
for k, v := range property {
req.SubDevices = append(req.SubDevices, &msgThing.SubDevice{
ProductID: "01S",
DeviceName: k,
Properties: []*deviceMsg.TimeParams{{
Params: v,
}},
})
}
}