Skip to content

Commit

Permalink
fix:设备功能的执行结果无返回bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chanvi committed Jun 19, 2024
1 parent 9874cd3 commit 4039d0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
24 changes: 12 additions & 12 deletions network/core/logic/baseLogic/asyncMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type AsyncMap struct {
sync.RWMutex
info map[string]*FInfo
Info map[string]*FInfo
}

type FInfo struct {
Expand All @@ -19,26 +19,26 @@ type FInfo struct {
Response chan interface{}
}

var asyncMapInfo = &AsyncMap{info: make(map[string]*FInfo)}
var AsyncMapInfo = &AsyncMap{Info: make(map[string]*FInfo)}

func SyncRequest(ctx context.Context, id, funcKey string, params interface{}, timeout int) (interface{}, error) {
if timeout == 0 {
timeout = 45
}

responseChan := make(chan interface{})
asyncMapInfo.Lock()
asyncMapInfo.info[id] = &FInfo{
responseChan := make(chan interface{}, 1)
AsyncMapInfo.Lock()
AsyncMapInfo.Info[id] = &FInfo{
FuncKey: funcKey,
Request: params,
Response: responseChan,
}
asyncMapInfo.Unlock()
AsyncMapInfo.Unlock()

defer func() {
asyncMapInfo.Lock()
delete(asyncMapInfo.info, id)
asyncMapInfo.Unlock()
AsyncMapInfo.Lock()
delete(AsyncMapInfo.Info, id)
AsyncMapInfo.Unlock()
close(responseChan)
}()

Expand All @@ -53,9 +53,9 @@ func SyncRequest(ctx context.Context, id, funcKey string, params interface{}, ti
}

func GetCallInfoById(ctx context.Context, id string) (funcKey string, params interface{}, response chan interface{}, err error) {
asyncMapInfo.RLock()
defer asyncMapInfo.RUnlock()
if info, ok := asyncMapInfo.info[id]; !ok {
AsyncMapInfo.RLock()
defer AsyncMapInfo.RUnlock()
if info, ok := AsyncMapInfo.Info[id]; !ok {
return "", nil, nil, errors.New("cannot get call info by id " + id)
} else {
return info.FuncKey, info.Request, info.Response, nil
Expand Down
12 changes: 12 additions & 0 deletions network/core/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"fmt"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"sagooiot/internal/consts"
"sagooiot/internal/model"
"sagooiot/internal/mqtt"
"sagooiot/network/core/logic/baseLogic"
"sagooiot/pkg/dcache"
"sagooiot/pkg/gpool"
"sagooiot/pkg/iotModel/sagooProtocol"
"sagooiot/pkg/iotModel/topicModel"
"sagooiot/pkg/jsinterpreter"
"sagooiot/pkg/plugins"
Expand Down Expand Up @@ -80,6 +82,16 @@ func (s *SubMap) HandleMessage(ctx context.Context, handleF handleFunc) func(con
return nil
}

// 处理设备应答
if strings.HasSuffix(topicInfo[6], "reply") {
var msg sagooProtocol.ServiceCallOutputRes
json.Unmarshal([]byte(res), &msg)

if info, ok := baseLogic.AsyncMapInfo.Info[msg.Id]; ok {
info.Response <- gconv.Map(msg)
}
}

// 获取设备详情,拿出来消息协议,然后按照产品定义的消息协议解析消息
deviceInfo, err := dcache.GetDeviceDetailInfo(deviceKey)
if err != nil {
Expand Down

0 comments on commit 4039d0a

Please sign in to comment.