You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.3 KiB
84 lines
2.3 KiB
2 weeks ago
|
package config
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func LoadConfigJson() map[string]string {
|
||
|
//加载目录下所有文件
|
||
|
allConfig := make(map[string]string)
|
||
|
|
||
|
files, err := os.ReadDir("configFiles")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
for _, file := range files {
|
||
|
log.Printf("加载配置文件:%s", file.Name())
|
||
|
if file.IsDir() {
|
||
|
log.Printf("非文件[%s]跳过", file.Name())
|
||
|
continue
|
||
|
}
|
||
|
// 读取mqtt配置
|
||
|
configMqttBytes, _ := os.ReadFile(fmt.Sprintf("configFiles/%s", file.Name()))
|
||
|
consumer := new(Consumer)
|
||
|
// 将 JSON 格式的数据解析到结构体中
|
||
|
err = json.Unmarshal(configMqttBytes, &consumer)
|
||
|
if err != nil {
|
||
|
log.Printf("读取配置文件[%s]异常 err=%v", consumer.Consumer, err.Error())
|
||
|
continue
|
||
|
}
|
||
|
allConfig[consumer.Consumer] = string(configMqttBytes)
|
||
|
//// 读取sensor配置
|
||
|
//configSensorBytes, _ := os.ReadFile("configSensor.json")
|
||
|
//// 将 JSON 格式的数据解析到结构体中
|
||
|
//err2 := json.Unmarshal(configSensorBytes, &userConfig.SensorConfig)
|
||
|
//if err2 != nil {
|
||
|
// log.Printf("读取mqtt配置异常 err=%v", err2.Error())
|
||
|
//}
|
||
|
//allConfig[]
|
||
|
}
|
||
|
return allConfig
|
||
|
}
|
||
|
func LoadConfigJsonByPath(path string) map[string]string {
|
||
|
//加载目录下所有文件
|
||
|
allConfig := make(map[string]string)
|
||
|
|
||
|
files, err := os.ReadDir(path)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
for _, file := range files {
|
||
|
log.Printf("加载配置文件:%s", file.Name())
|
||
|
if file.IsDir() {
|
||
|
log.Printf("非文件[%s]跳过", file.Name())
|
||
|
continue
|
||
|
}
|
||
|
// 读取mqtt配置
|
||
|
filePath := fmt.Sprintf("%s/%s", path, file.Name())
|
||
|
|
||
|
configMqttBytes, _ := os.ReadFile(filePath)
|
||
|
consumer := new(Consumer)
|
||
|
// 将 JSON 格式的数据解析到结构体中
|
||
|
err = json.Unmarshal(configMqttBytes, &consumer)
|
||
|
if err != nil {
|
||
|
log.Printf("读取配置文件[%s]异常 err=%v", consumer.Consumer, err.Error())
|
||
|
continue
|
||
|
}
|
||
|
allConfig[consumer.Consumer] = string(configMqttBytes)
|
||
|
//// 读取sensor配置
|
||
|
//configSensorBytes, _ := os.ReadFile("configSensor.json")
|
||
|
//// 将 JSON 格式的数据解析到结构体中
|
||
|
//err2 := json.Unmarshal(configSensorBytes, &userConfig.SensorConfig)
|
||
|
//if err2 != nil {
|
||
|
// log.Printf("读取mqtt配置异常 err=%v", err2.Error())
|
||
|
//}
|
||
|
//allConfig[]
|
||
|
}
|
||
|
return allConfig
|
||
|
}
|