From cc9d7e2dfad7c634e8ce69d02ce5fc4065e809e4 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期日, 25 四月 2021 11:34:47 +0800 Subject: [PATCH] 使用bhsgo by lichao --- sbusClient.go | 120 +++++++++++++++++++---------------------------------------- 1 files changed, 39 insertions(+), 81 deletions(-) diff --git a/sbusClient.go b/sbusClient.go index 39839f2..6d4471c 100644 --- a/sbusClient.go +++ b/sbusClient.go @@ -1,27 +1,21 @@ package bhomedbapi import ( - "basic.com/valib/bhomebus.git" + "basic.com/valib/bhshmq.git/api/bhsgo" + "basic.com/valib/bhshmq.git/proto/source/bhome_msg" "encoding/json" "errors" "strconv" ) type SBusClient struct { - nodes []bhomebus.NetNode + nodes []bhome_msg.BHAddress } type ProcInfo struct { Name string `json:"name"` // 杩涚▼鍚嶇О ID string `json:"id"` // 杩涚▼鍞竴鏍囪瘑 Info string `json:"info"` // 杩涚▼鐨勬弿杩颁俊鎭紝鐢ㄤ簬鍖哄垎鍚屼竴杩涚▼鍚嶇О涓嬪涓繘绋� -} - -type MsgInfo struct { - SrcProc ProcInfo `json:"srcProc"` // 婧愯繘绋嬪熀鏈俊鎭� - MsgType string `json:"msgType"` // 鏁版嵁绫诲瀷锛屽彲涓鸿姹傘�佸彂甯冦�佽闃呫�佸簲绛旂瓑 - Topic string `json:"topic"` //璇锋眰涓婚 - Body []byte `json:"body"` //璇锋眰鍐呭鎴栬�呭弽棣堢粨鏋� } type Request struct { @@ -42,9 +36,6 @@ } func (sc SBusClient) DoGetRequest(url string, params map[string]string, headers map[string]string) ([]byte, error) { - if sc.nodes == nil || len(sc.nodes) == 0 { - return nil, errors.New("invalid netNodes") - } req := Request{ Path: url, @@ -57,23 +48,17 @@ if err !=nil { return nil,err } - rMsg := MsgInfo{ - Topic: req.Path, - Body: rb, - } - rData, err := json.Marshal(rMsg) - if err != nil { - return nil, err + rMsg := bhome_msg.MsgRequestTopic{ + Topic: []byte(req.Path), + Data: rb, } - return busReq(rData, sc.nodes) + return busReq(&rMsg, sc.nodes) //return doReq(req, sc.nodes) } func (sc SBusClient) DoPostRequest(url string, contentType string, body map[string]interface{}, params map[string]string, headers map[string]string) ([]byte, error) { - if sc.nodes == nil || len(sc.nodes) == 0 { - return nil, errors.New("invalid port") - } + req := Request{ Path: url, Method: "POST", @@ -113,22 +98,16 @@ if err !=nil { return nil,err } - rMsg := MsgInfo{ - Topic: req.Path, - Body: rb, - } - rData, err := json.Marshal(rMsg) - if err != nil { - return nil, err + rMsg := bhome_msg.MsgRequestTopic{ + Topic: []byte(req.Path), + Data: rb, } - return busReq(rData, sc.nodes) + return busReq(&rMsg, sc.nodes) } func (sc SBusClient) DoPutRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) { - if sc.nodes == nil || len(sc.nodes) == 0 { - return nil, errors.New("invalid port") - } + req := Request{ Path: url, Method: "PUT", @@ -140,22 +119,16 @@ if err !=nil { return nil,err } - rMsg := MsgInfo{ - Topic: req.Path, - Body: rb, - } - rData, err := json.Marshal(rMsg) - if err != nil { - return nil, err + rMsg := bhome_msg.MsgRequestTopic{ + Topic: []byte(req.Path), + Data: rb, } - return busReq(rData, sc.nodes) + return busReq(&rMsg, sc.nodes) } func (sc SBusClient) DoDeleteRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) { - if sc.nodes == nil || len(sc.nodes) == 0 { - return nil, errors.New("invalid port") - } + req := Request{ Path: url, Method: "DELETE", @@ -167,16 +140,12 @@ if err !=nil { return nil,err } - rMsg := MsgInfo{ - Topic: req.Path, - Body: rb, - } - rData, err := json.Marshal(rMsg) - if err != nil { - return nil, err + rMsg := bhome_msg.MsgRequestTopic{ + Topic: []byte(req.Path), + Data: rb, } - return busReq(rData, sc.nodes) + return busReq(&rMsg, sc.nodes) } func fillParam(req *Request,headers map[string]string, params map[string]string, body map[string]interface{}) { @@ -200,33 +169,22 @@ } else { req.Body = b } - } -//鍦ㄦ澶勪娇鐢∣penSocket浼氬湪Ctrl-C鐨勬椂鍊欙紝瀵艰嚧socket骞舵湭鎴愬姛Close锛屽叡浜唴瀛樺潡涓嶄細閲婃斁. -//鎵�浠ユ帶鍒跺叡浜唴瀛樺潡鐨勬垚鍔熷洖鏀堕渶瑕佸湪涓婂眰鍋�,鐒跺悗璋僆nitDoReq灏嗗嚱鏁版寚閽堜紶閫掕繘鏉� -//func doReq(req Request, nodes []bhomebus.NetNode) ([]byte,error) { -// rb, err := json.Marshal(req) -// if err !=nil { -// return nil,err -// } -// rMsg := MsgInfo{ -// Topic: req.Path, -// Body: rb, -// } -// data, err := json.Marshal(rMsg) -// if err != nil { -// return nil, err -// } -// s := bhomebus.OpenSocket() -// defer s.Close() -// var ret []bhomebus.Mesg -// if n := s.SendandrecvTimeout(nodes, data, &ret, 5000);n == 0 { //n==0琛ㄧず娌℃湁璇锋眰鎴愬姛 -// return nil, fmt.Errorf("doReq s.SendandrecvTimeout result n:%d", n) -// } else { -// if len(ret) > 0 { -// return ret[0].Data, nil -// } -// return nil, fmt.Errorf("no any response") -// } -//} \ No newline at end of file +/** +* 鏂扮増bhs鐩存帴鍙戣捣璇锋眰锛屼笉浼氫骇鐢熷叡浜唴瀛樻硠婕忕殑闂锛� +*/ +func doReq(req *bhome_msg.MsgRequestTopic, destArr []bhome_msg.BHAddress) ([]byte, error) { + dest := bhome_msg.BHAddress{} + if destArr != nil && len(destArr) > 0 { + dest = destArr[0] + } + pid := "" + r := bhome_msg.MsgRequestTopicReply{} + if bhsgo.Request(&dest, req, &pid, &r, 5000) { + return r.Data, nil + } else { + logPrint("bhsgo.Request request err:", r.Errmsg) + return nil, errors.New("bhsgo.Request return false") + } +} \ No newline at end of file -- Gitblit v1.8.0