package bhomedbapi import ( "basic.com/valib/c_bhomebus.git/api/bhsgo" "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" json "github.com/json-iterator/go" "errors" "strconv" ) type SBusClient struct { nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress } 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 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) { req := Request{ Path: url, Method: "GET", ContentType: CONTENT_TYPE_JSON, } fillParam(&req, headers, params, nil) 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) { req := Request{ Path: url, Method: "POST", ContentType: contentType, } fillParam(&req, headers, params, body) if contentType == CONTENT_TYPE_FORM || contentType == CONTENT_TYPE_MULFORM { if body != nil { req.PostFormMap = make(map[string][]string) req.FormMap = map[string][]string{} for k,v := range body { switch v.(type) { case string: req.FormMap[k] = []string{v.(string)} req.PostFormMap[k] = []string{v.(string)} case int: req.FormMap[k] = []string{strconv.Itoa(v.(int))} req.PostFormMap[k] = []string{strconv.Itoa(v.(int))} case bool: req.FormMap[k] = []string{strconv.FormatBool(v.(bool))} req.PostFormMap[k] = []string{strconv.FormatBool(v.(bool))} default: logPrint("fill FORM or MultiForm,unknown value type,type is ", v) } } } } else if contentType == CONTENT_TYPE_JSON { bs, err := json.Marshal(body) if err != nil { logPrint("fill json body err:", err) } else { req.Body = bs } } 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) { req := Request{ Path: url, Method: "PUT", ContentType: contentType, } fillParam(&req, headers, nil, body) 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) { req := Request{ Path: url, Method: "DELETE", ContentType: contentType, } fillParam(&req, headers, nil, body) 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{}) { headerMap := make(map[string][]string) queryMap := make(map[string][]string) if headers != nil { for k,v := range headers { headerMap[k] = []string{v} } } if params != nil { for k,v := range params { queryMap[k] = []string{v} } } req.HeaderMap = headerMap req.QueryMap = queryMap b, err := json.Marshal(body) if err != nil { logPrint("marshal body err:", err) } else { req.Body = b } } /** * 新版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") } }