package service
|
|
import (
|
"encoding/json"
|
"errors"
|
|
"basic.com/valib/bhomedbapi.git"
|
"basic.com/valib/go-aiot.git/aiotProto/aiot"
|
"basic.com/valib/logger.git"
|
)
|
|
// 返回结果
|
type Reply struct {
|
Success bool `json:"success"`
|
Msg string `json:"msg"`
|
Data interface{} `json:"data"`
|
}
|
|
// 获取content-type字符串
|
func GetContentTypeString(contentType aiot.RequestContentType) string {
|
switch contentType {
|
case aiot.RequestContentType_ApplicationJson:
|
// application/json 格式
|
return bhomedbapi.CONTENT_TYPE_JSON
|
case aiot.RequestContentType_ApplicationXWwwFormUrlencoded:
|
// application/x-www-form-urlencoded 格式
|
return bhomedbapi.CONTENT_TYPE_FORM
|
case aiot.RequestContentType_MultipartFormData:
|
// multipart/form-data 格式
|
return bhomedbapi.CONTENT_TYPE_MULFORM
|
case aiot.RequestContentType_ApplicationXml:
|
// application/xml
|
return "application/xml"
|
default:
|
// application/json 格式
|
return bhomedbapi.CONTENT_TYPE_JSON
|
}
|
}
|
|
// 单节点请求
|
func NodeReq(msg *aiot.Protocol, req *aiot.NodeReq, reply *aiot.BusinessReply) error {
|
// 根据参数调用服务
|
var data []byte
|
param := make(map[string]interface{})
|
err := json.Unmarshal(req.Req, ¶m)
|
if err != nil {
|
logger.Error("Fail to Unmarshal req.Req", req.Req, err)
|
return err
|
}
|
|
var nodeId string
|
if len(msg.DeviceProto.DeviceIds) == 1 {
|
nodeId = msg.DeviceProto.DeviceIds[0]
|
} else if msg.DeviceProto.MasterDeviceId != "" {
|
// debug_test
|
nodeId = msg.DeviceProto.MasterDeviceId
|
} else {
|
err = errors.New("the node can not be null for NodeReq")
|
logger.Error("The node is null", err, msg)
|
return err
|
}
|
|
logger.Debugf("bus return result nodeId=%v, topic=%v, Method=%v, DeviceProto=%v", nodeId, req.Topic, req.Method, msg.DeviceProto)
|
data, err = DoBusReq(req.Topic, nodeId, req.Method, req.ContentType, param)
|
dataReply := Reply{}
|
_ = json.Unmarshal(data, &dataReply)
|
|
reply.Code = 200
|
if !dataReply.Success {
|
reply.Code = 500
|
}
|
reply.Success = dataReply.Success
|
reply.Msg = dataReply.Msg
|
reply.Data, _ = json.Marshal(dataReply.Data)
|
|
if err != nil {
|
return err
|
}
|
return nil
|
}
|