237 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			237 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Package config 提供配置文件读取和解析功能
 | 
						|
// 支持YAML格式的配置文件解析
 | 
						|
// 包含服务器和数据库相关配置
 | 
						|
package config
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"os"
 | 
						|
 | 
						|
	"gopkg.in/yaml.v2"
 | 
						|
)
 | 
						|
 | 
						|
// Config 代表应用的完整配置结构
 | 
						|
type Config struct {
 | 
						|
	// App 应用基础配置
 | 
						|
	App AppConfig `yaml:"app"`
 | 
						|
 | 
						|
	// Server 服务器配置
 | 
						|
	Server ServerConfig `yaml:"server"`
 | 
						|
 | 
						|
	// Log 日志配置
 | 
						|
	Log LogConfig `yaml:"log"`
 | 
						|
 | 
						|
	// Database 数据库配置
 | 
						|
	Database DatabaseConfig `yaml:"database"`
 | 
						|
 | 
						|
	// WebSocket WebSocket配置
 | 
						|
	WebSocket WebSocketConfig `yaml:"websocket"`
 | 
						|
 | 
						|
	// Heartbeat 心跳配置
 | 
						|
	Heartbeat HeartbeatConfig `yaml:"heartbeat"`
 | 
						|
 | 
						|
	// ChirpStack ChirpStack API 配置
 | 
						|
	ChirpStack ChirpStackConfig `yaml:"chirp_stack"`
 | 
						|
 | 
						|
	// TaskConfig 任务调度配置
 | 
						|
	Task TaskConfig `yaml:"task"`
 | 
						|
 | 
						|
	// Lora Lora配置
 | 
						|
	Lora LoraConfig `yaml:"lora"`
 | 
						|
 | 
						|
	// LoraMesh LoraMesh配置
 | 
						|
	LoraMesh LoraMeshConfig `yaml:"lora_mesh"`
 | 
						|
 | 
						|
	// Notify 通知服务配置
 | 
						|
	Notify NotifyConfig `yaml:"notify"`
 | 
						|
 | 
						|
	// Collection 定时采集配置
 | 
						|
	Collection CollectionConfig `yaml:"collection"`
 | 
						|
}
 | 
						|
 | 
						|
// AppConfig 代表应用基础配置
 | 
						|
type AppConfig struct {
 | 
						|
	Name      string `yaml:"name"`
 | 
						|
	Version   string `yaml:"version"`
 | 
						|
	JWTSecret string `yaml:"jwt_secret"` // JWT 密钥
 | 
						|
}
 | 
						|
 | 
						|
// ServerConfig 代表服务器配置
 | 
						|
type ServerConfig struct {
 | 
						|
	// Port 服务器监听端口
 | 
						|
	Port int `yaml:"port"`
 | 
						|
	// Mode 服务器运行模式
 | 
						|
	Mode string `yaml:"mode"`
 | 
						|
}
 | 
						|
 | 
						|
// LogConfig 代表日志配置
 | 
						|
type LogConfig struct {
 | 
						|
	Level      string `yaml:"level"`
 | 
						|
	Format     string `yaml:"format"`
 | 
						|
	EnableFile bool   `yaml:"enable_file"`
 | 
						|
	FilePath   string `yaml:"file_path"`
 | 
						|
	MaxSize    int    `yaml:"max_size"`
 | 
						|
	MaxBackups int    `yaml:"max_backups"`
 | 
						|
	MaxAge     int    `yaml:"max_age"`
 | 
						|
	Compress   bool   `yaml:"compress"`
 | 
						|
}
 | 
						|
 | 
						|
// DatabaseConfig 代表数据库配置
 | 
						|
type DatabaseConfig struct {
 | 
						|
	// Host 数据库主机地址
 | 
						|
	Host string `yaml:"host"`
 | 
						|
 | 
						|
	// Port 数据库端口
 | 
						|
	Port int `yaml:"port"`
 | 
						|
 | 
						|
	// Username 数据库用户名
 | 
						|
	Username string `yaml:"username"`
 | 
						|
 | 
						|
	// Password 数据库密码
 | 
						|
	Password string `yaml:"password"`
 | 
						|
 | 
						|
	// DBName 数据库名称
 | 
						|
	DBName string `yaml:"dbname"`
 | 
						|
 | 
						|
	// SSLMode SSL模式
 | 
						|
	SSLMode string `yaml:"sslmode"`
 | 
						|
 | 
						|
	// IsTimescaleDB is timescaledb
 | 
						|
	IsTimescaleDB bool `yaml:"is_timescaledb"`
 | 
						|
 | 
						|
	// MaxOpenConns 最大开放连接数
 | 
						|
	MaxOpenConns int `yaml:"max_open_conns"`
 | 
						|
 | 
						|
	// MaxIdleConns 最大空闲连接数
 | 
						|
	MaxIdleConns int `yaml:"max_idle_conns"`
 | 
						|
 | 
						|
	// ConnMaxLifetime 连接最大生命周期(秒)
 | 
						|
	ConnMaxLifetime int `yaml:"conn_max_lifetime"`
 | 
						|
}
 | 
						|
 | 
						|
// WebSocketConfig 代表WebSocket配置
 | 
						|
type WebSocketConfig struct {
 | 
						|
	// Timeout WebSocket请求超时时间(秒)
 | 
						|
	Timeout int `yaml:"timeout"`
 | 
						|
 | 
						|
	// HeartbeatInterval 心跳检测间隔(秒), 如果超过这个时间没有消息往来系统会自动发送一个心跳包维持长链接
 | 
						|
	HeartbeatInterval int `yaml:"heartbeat_interval"`
 | 
						|
}
 | 
						|
 | 
						|
// HeartbeatConfig 代表心跳配置
 | 
						|
type HeartbeatConfig struct {
 | 
						|
	// Interval 心跳间隔(秒)
 | 
						|
	Interval int `yaml:"interval"`
 | 
						|
 | 
						|
	// Concurrency 请求并发数
 | 
						|
	Concurrency int `yaml:"concurrency"`
 | 
						|
}
 | 
						|
 | 
						|
// ChirpStackConfig 代表 ChirpStack API 配置
 | 
						|
type ChirpStackConfig struct {
 | 
						|
	APIHost                  string `yaml:"api_host"`
 | 
						|
	APIToken                 string `yaml:"api_token"`
 | 
						|
	FPort                    int    `yaml:"fport"`
 | 
						|
	APITimeout               int    `yaml:"api_timeout"`
 | 
						|
	CollectionRequestTimeout int    `yaml:"collection_request_timeout"`
 | 
						|
}
 | 
						|
 | 
						|
// TaskConfig 代表任务调度配置
 | 
						|
type TaskConfig struct {
 | 
						|
	Interval   int `yaml:"interval"`
 | 
						|
	NumWorkers int `yaml:"num_workers"`
 | 
						|
}
 | 
						|
 | 
						|
type LoraMode string
 | 
						|
 | 
						|
const (
 | 
						|
	LoraMode_LoRaWAN  LoraMode = "lora_wan"
 | 
						|
	LoraMode_LoRaMesh LoraMode = "lora_mesh"
 | 
						|
)
 | 
						|
 | 
						|
// LoraConfig 代表Lora配置
 | 
						|
type LoraConfig struct {
 | 
						|
	Mode LoraMode `yaml:"mode"`
 | 
						|
}
 | 
						|
 | 
						|
// LoraMeshConfig 代表Lora Mesh配置
 | 
						|
type LoraMeshConfig struct {
 | 
						|
	UARTPort          string `yaml:"uart_port"`
 | 
						|
	BaudRate          int    `yaml:"baud_rate"`
 | 
						|
	Timeout           int    `yaml:"timeout"`
 | 
						|
	LoraMeshMode      string `yaml:"lora_mesh_mode"`
 | 
						|
	MaxChunkSize      int    `yaml:"max_chunk_size"`
 | 
						|
	ReassemblyTimeout int    `yaml:"reassembly_timeout"`
 | 
						|
}
 | 
						|
 | 
						|
// NotifyConfig 包含了所有与通知服务相关的配置
 | 
						|
type NotifyConfig struct {
 | 
						|
	Primary          string       `yaml:"primary"`          // 首选通知渠道 (e.g., "邮件", "企业微信", "飞书", "日志")
 | 
						|
	FailureThreshold int          `yaml:"failureThreshold"` // 连续失败多少次后触发广播模式
 | 
						|
	SMTP             SMTPConfig   `yaml:"smtp"`
 | 
						|
	WeChat           WeChatConfig `yaml:"wechat"`
 | 
						|
	Lark             LarkConfig   `yaml:"lark"`
 | 
						|
}
 | 
						|
 | 
						|
// SMTPConfig SMTP邮件配置
 | 
						|
type SMTPConfig struct {
 | 
						|
	Enabled  bool   `yaml:"enabled"`
 | 
						|
	Host     string `yaml:"host"`
 | 
						|
	Port     int    `yaml:"port"`
 | 
						|
	Username string `yaml:"username"`
 | 
						|
	Password string `yaml:"password"`
 | 
						|
	Sender   string `yaml:"sender"`
 | 
						|
}
 | 
						|
 | 
						|
// WeChatConfig 企业微信应用配置
 | 
						|
type WeChatConfig struct {
 | 
						|
	Enabled bool   `yaml:"enabled"`
 | 
						|
	CorpID  string `yaml:"corpID"`
 | 
						|
	AgentID string `yaml:"agentID"`
 | 
						|
	Secret  string `yaml:"secret"`
 | 
						|
}
 | 
						|
 | 
						|
// LarkConfig 飞书应用配置
 | 
						|
type LarkConfig struct {
 | 
						|
	Enabled   bool   `yaml:"enabled"`
 | 
						|
	AppID     string `yaml:"appID"`
 | 
						|
	AppSecret string `yaml:"appSecret"`
 | 
						|
}
 | 
						|
 | 
						|
// CollectionConfig 代表定时采集配置
 | 
						|
type CollectionConfig struct {
 | 
						|
	// Interval 采集间隔(分钟), 默认 1
 | 
						|
	Interval int `yaml:"interval"`
 | 
						|
}
 | 
						|
 | 
						|
// NewConfig 创建并返回一个新的配置实例
 | 
						|
func NewConfig() *Config {
 | 
						|
	// 默认值可以在这里设置,但我们优先使用配置文件中的值
 | 
						|
	return &Config{
 | 
						|
		Collection: CollectionConfig{
 | 
						|
			Interval: 1, // 默认为1分钟
 | 
						|
		},
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// Load 从指定路径加载配置文件
 | 
						|
func (c *Config) Load(path string) error {
 | 
						|
	// 读取配置文件
 | 
						|
	data, err := os.ReadFile(path)
 | 
						|
	if err != nil {
 | 
						|
		return fmt.Errorf("配置文件读取失败: %v", err)
 | 
						|
	}
 | 
						|
 | 
						|
	// 解析YAML配置
 | 
						|
	if err := yaml.Unmarshal(data, c); err != nil {
 | 
						|
		return fmt.Errorf("配置文件解析失败: %v", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
// GenerateAPIKey 用于补齐API Key作为请求头时缺失的部分
 | 
						|
func (c ChirpStackConfig) GenerateAPIKey() string {
 | 
						|
	return "Bearer " + c.APIToken
 | 
						|
}
 |