|
|
@@ -2,18 +2,23 @@ package mqttreporter
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
"os"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
)
|
|
|
|
|
|
type Config struct {
|
|
|
- mu sync.RWMutex
|
|
|
- mqttServers []MQTTServer
|
|
|
- ftpServers []FTPServer
|
|
|
+ mu sync.RWMutex
|
|
|
+ mqttServers []MQTTServer
|
|
|
+ imageServers []ImageServer
|
|
|
+ upgradeServers []UpgradeServer
|
|
|
}
|
|
|
|
|
|
type MQTTServer struct {
|
|
|
Enable bool `json:"enable"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Protocol string `json:"protocol"`
|
|
|
BrokerAddress string `json:"brokerAddress"`
|
|
|
Username string `json:"username"`
|
|
|
Password string `json:"password"`
|
|
|
@@ -26,45 +31,69 @@ type Topics struct {
|
|
|
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 ImageServer struct {
|
|
|
+ Enable bool `json:"enable"`
|
|
|
+ URL string `json:"url"`
|
|
|
+}
|
|
|
+
|
|
|
+type UpgradeServer struct {
|
|
|
+ Enable bool `json:"enable"`
|
|
|
+ Target string `json:"target"`
|
|
|
+ URL string `json:"url"`
|
|
|
}
|
|
|
|
|
|
type configFile struct {
|
|
|
- MQTTServers []MQTTServer `json:"mqttServers"`
|
|
|
- FTPServers []FTPServer `json:"ftpServers"`
|
|
|
+ MQTTServers []MQTTServer `json:"mqttServers"`
|
|
|
+ ImageServers []ImageServer `json:"imageServers"`
|
|
|
+ UpgradeServers []UpgradeServer `json:"upgradeServers"`
|
|
|
}
|
|
|
|
|
|
const defaultConfig = `{
|
|
|
"mqttServers": [
|
|
|
{
|
|
|
"enable": true,
|
|
|
+ "name": "yfkj's mqtt server",
|
|
|
+ "protocol": "yfkj-private",
|
|
|
"brokerAddress": "tcp://8.136.98.49:61883",
|
|
|
"username": "user",
|
|
|
"password": "f335bf402c655ee5fd2b5300905124e",
|
|
|
"topics": {
|
|
|
- "pub": "/yfkj/cbd/pub/",
|
|
|
- "sub": "/yfkj/cbd/sub/",
|
|
|
- "offline": "/yfkj/cbd/offline/"
|
|
|
+ "pub": "/yfkj/cbd/pub/${id}/",
|
|
|
+ "sub": "/yfkj/cbd/sub/${id}/",
|
|
|
+ "offline": "/yfkj/cbd/offline/${id}/"
|
|
|
}
|
|
|
}
|
|
|
],
|
|
|
- "ftpServers": [
|
|
|
+ "imageServers": [
|
|
|
{
|
|
|
"enable": true,
|
|
|
- "purpose": "imageUpload",
|
|
|
- "serverAddress": "8.136.98.49:21",
|
|
|
- "username": "cbd_user",
|
|
|
- "password": "yf1234567"
|
|
|
+ "url": "ftp://cbd_user:YFkj%406O2O@8.136.98.49:21"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "upgradeServers": [
|
|
|
+ {
|
|
|
+ "enable": false,
|
|
|
+ "target": "mcu",
|
|
|
+ "url": ""
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "enable": false,
|
|
|
+ "target": "hmi",
|
|
|
+ "url": ""
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "enable": false,
|
|
|
+ "target": "rtu",
|
|
|
+ "url": ""
|
|
|
}
|
|
|
]
|
|
|
}`
|
|
|
|
|
|
-func LoadConfig(path string) (*Config, error) {
|
|
|
+func LoadConfig(path, id string) (*Config, error) {
|
|
|
+ if strings.TrimSpace(id) == "" {
|
|
|
+ return nil, errors.New("device id is empty")
|
|
|
+ }
|
|
|
+
|
|
|
data, err := os.ReadFile(path)
|
|
|
isDefault := os.IsNotExist(err)
|
|
|
if err != nil {
|
|
|
@@ -79,25 +108,33 @@ func LoadConfig(path string) (*Config, error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- c := &Config{
|
|
|
- mqttServers: cfg.MQTTServers,
|
|
|
- ftpServers: cfg.FTPServers,
|
|
|
- }
|
|
|
-
|
|
|
if isDefault {
|
|
|
- if err := c.Save(path); err != nil {
|
|
|
+ if err := os.WriteFile(path, append(data, '\n'), 0644); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return c, nil
|
|
|
+ r := strings.NewReplacer("${id}", id)
|
|
|
+ for i := range cfg.MQTTServers {
|
|
|
+ t := &cfg.MQTTServers[i].Topics
|
|
|
+ t.Pub = r.Replace(t.Pub)
|
|
|
+ t.Sub = r.Replace(t.Sub)
|
|
|
+ t.Offline = r.Replace(t.Offline)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &Config{
|
|
|
+ mqttServers: cfg.MQTTServers,
|
|
|
+ imageServers: cfg.ImageServers,
|
|
|
+ upgradeServers: cfg.UpgradeServers,
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
func (c *Config) Save(path string) error {
|
|
|
c.mu.RLock()
|
|
|
data, err := json.MarshalIndent(configFile{
|
|
|
- MQTTServers: c.mqttServers,
|
|
|
- FTPServers: c.ftpServers,
|
|
|
+ MQTTServers: c.mqttServers,
|
|
|
+ ImageServers: c.imageServers,
|
|
|
+ UpgradeServers: c.upgradeServers,
|
|
|
}, "", " ")
|
|
|
c.mu.RUnlock()
|
|
|
if err != nil {
|
|
|
@@ -122,25 +159,23 @@ func (c *Config) DelMQTTServer(index int) bool {
|
|
|
}
|
|
|
|
|
|
c.mqttServers = append(c.mqttServers[:index], c.mqttServers[index+1:]...)
|
|
|
-
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
-func (c *Config) AddFTPServer(server FTPServer) {
|
|
|
+func (c *Config) AddImageServer(server ImageServer) {
|
|
|
c.mu.Lock()
|
|
|
defer c.mu.Unlock()
|
|
|
- c.ftpServers = append(c.ftpServers, server)
|
|
|
+ c.imageServers = append(c.imageServers, server)
|
|
|
}
|
|
|
|
|
|
-func (c *Config) DelFTPServer(index int) bool {
|
|
|
+func (c *Config) DelImageServer(index int) bool {
|
|
|
c.mu.Lock()
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
- if index < 0 || index >= len(c.ftpServers) {
|
|
|
+ if index < 0 || index >= len(c.imageServers) {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
- c.ftpServers = append(c.ftpServers[:index], c.ftpServers[index+1:]...)
|
|
|
-
|
|
|
+ c.imageServers = append(c.imageServers[:index], c.imageServers[index+1:]...)
|
|
|
return true
|
|
|
}
|