Browse Source

支持多 MQTT 和 FTP 服务器配置管理

niujiuru 1 month ago
parent
commit
36831cf39c
2 changed files with 146 additions and 0 deletions
  1. 145 0
      mqtt_reporter/config.go
  2. 1 0
      mqtt_reporter/reporter.go

+ 145 - 0
mqtt_reporter/config.go

@@ -1 +1,146 @@
 package mqttreporter
 package mqttreporter
+
+import (
+	"encoding/json"
+	"os"
+	"sync"
+)
+
+type Config struct {
+	mu          sync.RWMutex
+	mqttServers []MQTTServer
+	ftpServers  []FTPServer
+}
+
+type MQTTServer struct {
+	Enable        bool   `json:"enable"`
+	BrokerAddress string `json:"brokerAddress"`
+	Username      string `json:"username"`
+	Password      string `json:"password"`
+	Topics        Topics `json:"topics"`
+}
+
+type Topics struct {
+	Pub     string `json:"pub"`
+	Sub     string `json:"sub"`
+	Offline string `json:"offline"`
+}
+
+type FTPServer struct {
+	Enable        bool   `json:"enable"`
+	Purpose       string `json:"purpose"`
+	ServerAddress string `json:"serverAddress"`
+	Username      string `json:"username"`
+	Password      string `json:"password"`
+}
+
+type configFile struct {
+	MQTTServers []MQTTServer `json:"mqttServers"`
+	FTPServers  []FTPServer  `json:"ftpServers"`
+}
+
+const defaultConfig = `{
+  "mqttServers": [
+    {
+      "enable": true,
+      "brokerAddress": "tcp://8.136.98.49:61883",
+      "username": "user",
+      "password": "f335bf402c655ee5fd2b5300905124e",
+      "topics": {
+        "pub": "/yfkj/cbd/pub/",
+        "sub": "/yfkj/cbd/sub/",
+        "offline": "/yfkj/cbd/offline/"
+      }
+    }
+  ],
+  "ftpServers": [
+    {
+      "enable": true,
+      "purpose": "imageUpload",
+      "serverAddress": "8.136.98.49:21",
+      "username": "cbd_user",
+      "password": "yf1234567"
+    }
+  ]
+}`
+
+func LoadConfig(path string) (*Config, error) {
+	data, err := os.ReadFile(path)
+	isDefault := os.IsNotExist(err)
+	if err != nil {
+		if !isDefault {
+			return nil, err
+		}
+		data = []byte(defaultConfig)
+	}
+
+	var cfg configFile
+	if err := json.Unmarshal(data, &cfg); err != nil {
+		return nil, err
+	}
+
+	c := &Config{
+		mqttServers: cfg.MQTTServers,
+		ftpServers:  cfg.FTPServers,
+	}
+
+	if isDefault {
+		if err := c.Save(path); err != nil {
+			return nil, err
+		}
+	}
+
+	return c, nil
+}
+
+func (c *Config) Save(path string) error {
+	c.mu.RLock()
+	data, err := json.MarshalIndent(configFile{
+		MQTTServers: c.mqttServers,
+		FTPServers:  c.ftpServers,
+	}, "", "  ")
+	c.mu.RUnlock()
+	if err != nil {
+		return err
+	}
+
+	return os.WriteFile(path, append(data, '\n'), 0644)
+}
+
+func (c *Config) AddMQTTServer(server MQTTServer) {
+	c.mu.Lock()
+	defer c.mu.Unlock()
+	c.mqttServers = append(c.mqttServers, server)
+}
+
+func (c *Config) DelMQTTServer(index int) bool {
+	c.mu.Lock()
+	defer c.mu.Unlock()
+
+	if index < 0 || index >= len(c.mqttServers) {
+		return false
+	}
+
+	c.mqttServers = append(c.mqttServers[:index], c.mqttServers[index+1:]...)
+
+	return true
+}
+
+func (c *Config) AddFTPServer(server FTPServer) {
+	c.mu.Lock()
+	defer c.mu.Unlock()
+	c.ftpServers = append(c.ftpServers, server)
+}
+
+func (c *Config) DelFTPServer(index int) bool {
+	c.mu.Lock()
+	defer c.mu.Unlock()
+
+	if index < 0 || index >= len(c.ftpServers) {
+		return false
+	}
+
+	c.ftpServers = append(c.ftpServers[:index], c.ftpServers[index+1:]...)
+
+	return true
+}

+ 1 - 0
mqtt_reporter/reporter.go

@@ -0,0 +1 @@
+package mqttreporter