Skip to content

Commit

Permalink
修改多通道设备时,同步修改各通道设备的相关信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chao committed Aug 8, 2024
1 parent e231217 commit 47249ed
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/andychao217/magistrala"
Expand Down Expand Up @@ -506,21 +505,40 @@ func (svc service) UpdateClient(ctx context.Context, token string, cli mgclients

cRepo := postgres.NewRepository(database)
for i := 2; i <= outChannel; i++ {
thing, _ := cRepo.RetrieveByIdentity(ctx, client.Credentials.Identity+"_"+strconv.Itoa(i))
oriName := thing.Name
thing.Name = client.Name
if client.Metadata["info"] != nil {
thing.Metadata["info"] = client.Metadata["info"]
thing, err := cRepo.RetrieveByIdentity(ctx, client.Credentials.Identity+"_"+strconv.Itoa(i))
if err != nil {
fmt.Println("RetrieveByIdentity Error: ", err)
continue // 如果检索失败,继续下一个循环
}
alias, ok := thing.Metadata["aliase"].(string)
thing.Name = client.Name
// 类型断言
info, ok := client.Metadata["info"].(map[string]interface{})
if !ok {
fmt.Println("Invalid type for out_channel")
} else {
thing.Metadata["aliase"] = strings.Replace(alias, oriName, client.Name, -1)
fmt.Println("Invalid type for Metadata['info']")
continue
}

thing.Metadata["info"] = deepcopy.Copy(info).(map[string]interface{})
// 处理 out_channel
if outChannelData, ok := info["out_channel"].(map[string]interface{}); ok {
if channels, ok := outChannelData["channel"].([]interface{}); ok {
if len(channels) > 0 {
if i-1 < len(channels) {
channelInfo, ok := channels[i-1].(map[string]interface{})
if ok {
if aliase, exists := channelInfo["aliase"].(string); exists {
thing.Metadata["aliase"] = client.Name + "_" + aliase
}
}
}
thing.Metadata["out_channel_array"] = deepcopy.Copy(channels).([]interface{})
}
thing.Metadata["out_channel"] = strconv.Itoa(len(channels))
}
}
_, err = svc.UpdateClient(ctx, token, thing)
if err != nil {
fmt.Println("DeleteClient Error: ", err)
fmt.Println("UpdateClient Error: ", err)
}
}
}
Expand Down

0 comments on commit 47249ed

Please sign in to comment.