From 01dfd9dc8de7b19f9dfa4284722e01bbd5837801 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 19 一月 2024 09:10:30 +0800
Subject: [PATCH] replace json to json-iterator
---
sbusClient.go | 159 ++++++++++++++++++++++++++++++++---------------------
1 files changed, 96 insertions(+), 63 deletions(-)
diff --git a/sbusClient.go b/sbusClient.go
index ef4442a..bf9c140 100644
--- a/sbusClient.go
+++ b/sbusClient.go
@@ -1,53 +1,66 @@
package bhomedbapi
import (
- "basic.com/valib/bhomebus.git"
- "encoding/json"
+ "basic.com/valib/c_bhomebus.git/api/bhsgo"
+ "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
"errors"
- "fmt"
+ jsoniter "github.com/json-iterator/go"
"strconv"
)
type SBusClient struct {
- nodes []bhomebus.NetNode
+ nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress
}
-type MsgInfo struct {
- Topic string `json:"topic"` //璇锋眰涓婚
- Body []byte `json:"body"` //璇锋眰鍐呭
+type ProcInfo struct {
+ Name string `json:"name"` // 杩涚▼鍚嶇О
+ ID string `json:"id"` // 杩涚▼鍞竴鏍囪瘑
+ Info string `json:"info"` // 杩涚▼鐨勬弿杩颁俊鎭紝鐢ㄤ簬鍖哄垎鍚屼竴杩涚▼鍚嶇О涓嬪涓繘绋�
}
-type request struct {
- Path string `json:"path"`
- Method string `json:"method"`
- ContentType string `json:"contentType"`
- HeaderMap map[string][]string `json:"headerMap"`
- QueryMap map[string][]string `json:"queryMap"`
- FormMap map[string][]string `json:"formMap"`
- PostFormMap map[string][]string `json:"postFormMap"`
- Body []byte `json:"body"`
+type Request struct {
+ Path string `json:"path"`
+ Method string `json:"method"`
+ ContentType string `json:"contentType"`
+ HeaderMap map[string][]string `json:"headerMap"`
+ QueryMap map[string][]string `json:"queryMap"`
+ FormMap map[string][]string `json:"formMap"`
+ PostFormMap map[string][]string `json:"postFormMap"`
+ Body []byte `json:"body"`
+}
+
+type reply struct {
+ Success bool `json:"success"`
+ Msg string `json:"msg"`
+ Data interface{} `json:"data"`
}
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 {
+ req := Request{
Path: url,
Method: "GET",
ContentType: CONTENT_TYPE_JSON,
}
fillParam(&req, headers, params, nil)
- return doReq(req, sc.nodes)
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
+ rb, err := json.Marshal(req)
+ if err != nil {
+ return nil, err
+ }
+ rMsg := bhome_msg.MsgRequestTopic{
+ Topic: []byte(req.Path),
+ Data: rb,
+ }
+
+ 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 {
+
+ req := Request{
Path: url,
Method: "POST",
ContentType: contentType,
@@ -56,7 +69,8 @@
if contentType == CONTENT_TYPE_FORM || contentType == CONTENT_TYPE_MULFORM {
if body != nil {
req.PostFormMap = make(map[string][]string)
- for k,v := range body {
+ req.FormMap = map[string][]string{}
+ for k, v := range body {
switch v.(type) {
case string:
req.FormMap[k] = []string{v.(string)}
@@ -74,6 +88,7 @@
}
}
} else if contentType == CONTENT_TYPE_JSON {
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
bs, err := json.Marshal(body)
if err != nil {
logPrint("fill json body err:", err)
@@ -82,83 +97,101 @@
}
}
- return doReq(req, sc.nodes)
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
+ rb, err := json.Marshal(req)
+ if err != nil {
+ return nil, err
+ }
+ rMsg := bhome_msg.MsgRequestTopic{
+ Topic: []byte(req.Path),
+ Data: rb,
+ }
+
+ 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 {
+
+ req := Request{
Path: url,
Method: "PUT",
ContentType: contentType,
}
fillParam(&req, headers, nil, body)
- return doReq(req, sc.nodes)
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
+ rb, err := json.Marshal(req)
+ if err != nil {
+ return nil, err
+ }
+ rMsg := bhome_msg.MsgRequestTopic{
+ Topic: []byte(req.Path),
+ Data: rb,
+ }
+
+ 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 {
+
+ req := Request{
Path: url,
Method: "DELETE",
ContentType: contentType,
}
fillParam(&req, headers, nil, body)
- return doReq(req, sc.nodes)
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
+ rb, err := json.Marshal(req)
+ if err != nil {
+ return nil, err
+ }
+ rMsg := bhome_msg.MsgRequestTopic{
+ Topic: []byte(req.Path),
+ Data: rb,
+ }
+
+ return busReq(&rMsg, sc.nodes)
}
-func fillParam(req *request,headers map[string]string, params map[string]string, body map[string]interface{}) {
+func fillParam(req *Request, headers map[string]string, params map[string]string, body map[string]interface{}) {
headerMap := make(map[string][]string)
queryMap := make(map[string][]string)
if headers != nil {
- for k,v := range headers {
+ for k, v := range headers {
headerMap[k] = []string{v}
}
}
if params != nil {
- for k,v := range params {
+ for k, v := range params {
queryMap[k] = []string{v}
}
}
req.HeaderMap = headerMap
req.QueryMap = queryMap
+ var json = jsoniter.ConfigCompatibleWithStandardLibrary
b, err := json.Marshal(body)
if err != nil {
logPrint("marshal body err:", err)
} else {
req.Body = b
}
-
}
-func doReq(req request, nodes []bhomebus.NetNode) ([]byte,error) {
- rb, err := json.Marshal(req)
- if err !=nil {
- return nil,err
+/**
+* 鏂扮増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]
}
- 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 {
- if len(ret) > 0 {
- return ret[0].Data, nil
- }
- return nil, fmt.Errorf("no any response")
+ pid := ""
+ r := bhome_msg.MsgRequestTopicReply{}
+ if bhsgo.Request(&dest, req, &pid, &r, 5000) {
+ return r.Data, nil
} else {
- return nil, fmt.Errorf("s.SendandrecvTimeout result n:%d", n)
+ 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