定义 ChirpStackTransport
This commit is contained in:
52
internal/infra/transport/lora/chirp_stack.go
Normal file
52
internal/infra/transport/lora/chirp_stack.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package lora
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"github.com/go-openapi/runtime"
|
||||
httptransport "github.com/go-openapi/runtime/client"
|
||||
"github.com/go-openapi/strfmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/lora/chirp_stack/client"
|
||||
)
|
||||
|
||||
// ChirpStackConfig 保存连接到 ChirpStack API 所需的配置。
|
||||
type ChirpStackConfig struct {
|
||||
// ServerAddress 是 ChirpStack API 服务器的地址,例如 "localhost:8080"。
|
||||
ServerAddress string
|
||||
// APIKey 是用于认证的 API 密钥。
|
||||
APIKey string
|
||||
}
|
||||
|
||||
// GenerateAPIKey 用于补齐API Key作为请求头时缺失的部分
|
||||
func (c ChirpStackConfig) GenerateAPIKey() string {
|
||||
return "Bearer " + c.APIKey
|
||||
}
|
||||
|
||||
// ChirpStackTransport 是一个客户端,用于封装与 ChirpStack REST API 的交互。
|
||||
type ChirpStackTransport struct {
|
||||
client *client.ChirpStackRESTAPI
|
||||
authInfo runtime.ClientAuthInfoWriter
|
||||
config ChirpStackConfig
|
||||
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewChirpStackTransport 创建一个新的通信实例,用于与 ChirpStack 通信。
|
||||
func NewChirpStackTransport(config ChirpStackConfig, logger *logs.Logger) *ChirpStackTransport {
|
||||
// 使用配置中的服务器地址创建一个 HTTP transport。
|
||||
// 它会使用生成的客户端中定义的默认 base path 和 schemes。
|
||||
transport := httptransport.New(config.ServerAddress, client.DefaultBasePath, client.DefaultSchemes)
|
||||
|
||||
// 使用 transport 和默认的字符串格式化器,创建一个 API 主客户端。
|
||||
apiClient := client.New(transport, strfmt.Default)
|
||||
|
||||
// 使用 API Key 创建认证信息写入器。
|
||||
authInfo := httptransport.APIKeyAuth("grpc-metadata-authorization", "header", config.GenerateAPIKey())
|
||||
|
||||
return &ChirpStackTransport{
|
||||
client: apiClient,
|
||||
authInfo: authInfo,
|
||||
config: config,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user