286 lines
7.3 KiB
Go
286 lines
7.3 KiB
Go
package config
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"be.ems/lib/email"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// PsapConfig PSAP配置结构体
|
|
type PsapConfig struct {
|
|
Ticket struct {
|
|
TicketNotification email.EmailConfig `yaml:"ticketNotification" json:"ticketNotification"` // 注意:配置文件中是 "ticketNotification",保持一致
|
|
TicketTimeout TicketTimeout `yaml:"ticketTimeout" json:"ticketTimeout"` // 注意:配置文件中是 "ticketTimeout",保持一致{
|
|
} `yaml:"ticket" json:"ticket"` // PSAP工单相关配置
|
|
}
|
|
|
|
type TicketTimeout struct {
|
|
New int `yaml:"new" json:"new"` // NEW状态超时时间(分钟)
|
|
InProgress int `yaml:"inProgress" json:"inProgress"` // IN_PROGRESS状态超时时间(分钟)
|
|
NoAnswer1 int `yaml:"noAnswer1" json:"noAnswer1"` // NO_ANSWER_1状态超时时间(分钟)
|
|
NoAnswer2 int `yaml:"noAnswer2" json:"noAnswer2"` // NO_ANSWER_2状态超时时间(分钟)
|
|
NearlyTimeout int `yaml:"nearlyTimeout" json:"nearlyTimeout"` // 提前提醒时间(分钟)
|
|
}
|
|
|
|
var psapConfig *PsapConfig
|
|
|
|
var PsapYamlConfigInfo YamlConfigFile = YamlConfigFile{}
|
|
|
|
// ReadPsapConfig 读取PSAP配置文件
|
|
func ReadPsapConfig(configFile string) (*PsapConfig, error) {
|
|
PsapYamlConfigInfo.FilePath = configFile
|
|
|
|
yamlFile, err := os.ReadFile(configFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read psap config file error: %w", err)
|
|
}
|
|
|
|
var config PsapConfig
|
|
err = yaml.Unmarshal(yamlFile, &config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unmarshal psap config error: %w", err)
|
|
}
|
|
|
|
psapConfig = &config
|
|
// PsapYamlConfigInfo.ConfigLines = config
|
|
|
|
// 读取原始文件行
|
|
err = ReadPsapOriginalConfig(configFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read psap original config error: %w", err)
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// ReadPsapOriginalConfig 读取PSAP原始配置文件行
|
|
func ReadPsapOriginalConfig(configFile string) error {
|
|
inputFile, err := os.Open(configFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open psap config file: %w", err)
|
|
}
|
|
defer inputFile.Close()
|
|
|
|
// 清空之前的内容
|
|
PsapYamlConfigInfo.OrignalLines = nil
|
|
|
|
scanner := bufio.NewScanner(inputFile)
|
|
for scanner.Scan() {
|
|
PsapYamlConfigInfo.OrignalLines = append(PsapYamlConfigInfo.OrignalLines, scanner.Text())
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return fmt.Errorf("failed to scan psap config file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// WritePsapOriginalConfig 写回PSAP原始配置文件
|
|
func WritePsapOriginalConfig(configFile string, paramName string, paramData map[string]any) error {
|
|
lines := PsapYamlConfigInfo.OrignalLines
|
|
for i, line := range lines {
|
|
if strings.Contains(line, paramName) {
|
|
for k, v := range paramData {
|
|
// 在paramName附近查找对应的字段
|
|
for j := i + 1; j < len(lines); j++ {
|
|
// 跳过注释行
|
|
trimmedLine := strings.TrimSpace(lines[j])
|
|
if strings.HasPrefix(trimmedLine, "#") {
|
|
continue
|
|
}
|
|
|
|
if strings.Contains(lines[j], k+":") {
|
|
index := strings.Index(lines[j], k)
|
|
// 根据v的类型确定格式
|
|
switch v := v.(type) {
|
|
case string:
|
|
lines[j] = lines[j][:index] + fmt.Sprintf("%s: \"%s\"", k, v)
|
|
case bool:
|
|
lines[j] = lines[j][:index] + fmt.Sprintf("%s: %t", k, v)
|
|
default:
|
|
lines[j] = lines[j][:index] + fmt.Sprintf("%s: %v", k, v)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
// 写回yaml文件
|
|
outputFile, err := os.Create(configFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create psap config file: %w", err)
|
|
}
|
|
defer outputFile.Close()
|
|
|
|
writer := bufio.NewWriter(outputFile)
|
|
for _, line := range PsapYamlConfigInfo.OrignalLines {
|
|
writer.WriteString(line + "\n")
|
|
}
|
|
return writer.Flush()
|
|
}
|
|
|
|
// UpdatePsapStructFromMap 更新PSAP结构体字段
|
|
func UpdatePsapStructFromMap(s any, updates map[string]any) {
|
|
v := reflect.ValueOf(s).Elem()
|
|
t := v.Type()
|
|
|
|
for key, value := range updates {
|
|
for i := 0; i < t.NumField(); i++ {
|
|
field := t.Field(i)
|
|
if field.Tag.Get("json") == key {
|
|
structField := v.FieldByName(field.Name)
|
|
if structField.IsValid() && structField.CanSet() {
|
|
// 转换值为适当的类型
|
|
convertedValue := reflect.ValueOf(value).Convert(structField.Type())
|
|
if structField.Type() == convertedValue.Type() {
|
|
structField.Set(convertedValue)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetPsapConfig 获取PSAP配置
|
|
func GetPsapConfig() *PsapConfig {
|
|
if psapConfig == nil {
|
|
// 如果配置未加载,尝试从默认位置加载
|
|
config, err := ReadPsapConfig("./etc/psap.yaml")
|
|
if err != nil {
|
|
fmt.Printf("Failed to load PSAP config: %v\n", err)
|
|
return nil
|
|
}
|
|
psapConfig = config
|
|
}
|
|
return psapConfig
|
|
}
|
|
|
|
// GetSMTPConfig 获取SMTP配置
|
|
func GetSMTPConfig() *email.EmailConfig {
|
|
config := GetPsapConfig()
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
return &config.Ticket.TicketNotification
|
|
}
|
|
|
|
// GetTimeoutConfig 获取超时配置
|
|
func GetTimeoutConfig() *TicketTimeout {
|
|
config := GetPsapConfig()
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
return &config.Ticket.TicketTimeout
|
|
}
|
|
|
|
// 以下是具体的配置获取方法
|
|
|
|
// GetNewTicketTimeoutMicros 获取NEW状态超时时间(微秒)
|
|
func GetNewTicketTimeoutMicros() int64 {
|
|
config := GetTimeoutConfig()
|
|
if config == nil {
|
|
return 60 * 60 * 1000000 // 默认60分钟
|
|
}
|
|
return int64(config.New) * 60 * 1000000 // 分钟转微秒
|
|
}
|
|
|
|
// GetInProgressTicketTimeoutMicros 获取IN_PROGRESS状态超时时间(微秒)
|
|
func GetInProgressTicketTimeoutMicros() int64 {
|
|
config := GetTimeoutConfig()
|
|
if config == nil {
|
|
return 60 * 60 * 1000000 // 默认60分钟
|
|
}
|
|
return int64(config.InProgress) * 60 * 1000000
|
|
}
|
|
|
|
// GetNoAnswer1TicketTimeoutMicros 获取NO_ANSWER_1状态超时时间(微秒)
|
|
func GetNoAnswer1TicketTimeoutMicros() int64 {
|
|
config := GetTimeoutConfig()
|
|
if config == nil {
|
|
return 4 * 60 * 60 * 1000000 // 默认4小时
|
|
}
|
|
return int64(config.NoAnswer1) * 60 * 1000000
|
|
}
|
|
|
|
// GetNoAnswer2TicketTimeoutMicros 获取NO_ANSWER_2状态超时时间(微秒)
|
|
func GetNoAnswer2TicketTimeoutMicros() int64 {
|
|
config := GetTimeoutConfig()
|
|
if config == nil {
|
|
return 8 * 60 * 60 * 1000000 // 默认8小时
|
|
}
|
|
return int64(config.NoAnswer2) * 60 * 1000000
|
|
}
|
|
|
|
// GetNearlyTimeoutMicros 获取提前提醒时间(微秒)
|
|
func GetNearlyTimeoutMicros() int64 {
|
|
config := GetTimeoutConfig()
|
|
if config == nil {
|
|
return 20 * 60 * 1000000 // 默认20分钟
|
|
}
|
|
return int64(config.NearlyTimeout) * 60 * 1000000
|
|
}
|
|
|
|
// IsSMTPEnabled 检查SMTP是否启用
|
|
func IsSMTPEnabled() bool {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return false
|
|
}
|
|
return config.Enabled
|
|
}
|
|
|
|
// GetSMTPHost 获取SMTP主机
|
|
func GetSMTPHost() string {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return ""
|
|
}
|
|
return config.Host
|
|
}
|
|
|
|
// GetSMTPPort 获取SMTP端口
|
|
func GetSMTPPort() int {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return 25
|
|
}
|
|
return config.Port
|
|
}
|
|
|
|
// GetSMTPUser 获取SMTP用户名
|
|
func GetSMTPUsername() string {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return ""
|
|
}
|
|
return config.Username
|
|
}
|
|
|
|
// GetSMTPPassword 获取SMTP密码
|
|
func GetSMTPPassword() string {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return ""
|
|
}
|
|
return config.Password
|
|
}
|
|
|
|
// GetSMTPFrom 获取SMTP发件人
|
|
func GetSMTPFrom() string {
|
|
config := GetSMTPConfig()
|
|
if config == nil {
|
|
return ""
|
|
}
|
|
return config.From
|
|
}
|