snmp-mqtt/config/config.go

66 lines
1.3 KiB
Go
Raw Normal View History

2019-10-15 11:40:10 -04:00
package config
import (
"encoding/json"
"os"
"strconv"
)
2019-10-15 11:42:24 -04:00
// OIDTopicObject maps OIDs to MQTT topics
2019-10-15 11:40:10 -04:00
type OIDTopicObject struct {
OID string `json:"oid"`
Topic string `json:"topic"`
}
2019-10-15 11:42:24 -04:00
// SNMPEndpointObject is the SNMP Endpoint definition
2019-10-15 11:40:10 -04:00
type SNMPEndpointObject struct {
Endpoint string `json:"endpoint"`
Community string `json:"community"`
OIDTopics []OIDTopicObject `json:"oidTopics"`
}
2019-10-15 11:42:24 -04:00
// SNMPMapObject basic map of endpoints
2019-10-15 11:40:10 -04:00
type SNMPMapObject struct {
SNMPEndpoints []SNMPEndpointObject `json:"snmpEndpoints"`
}
var (
// SNMPMap is the loaded JSON configuration
SNMPMap *SNMPMapObject
// Server is the MQTT server address
Server string
// Port is the MQTT server listen port
Port int
// ClientID is how the name of the client
ClientID string
// Interval is the poll interval in seconds
Interval int
)
// ConnectionString returns the MQTT connection string
func ConnectionString() string {
return "tcp://" + Server + ":" + strconv.Itoa(Port)
}
// LoadMap loads the file in to the struct
func LoadMap(file string) error {
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
return err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&SNMPMap)
if err != nil {
return err
}
return nil
}