package util import ( "net" "os" "path/filepath" "strings" "github.com/go-ini/ini" ) type MySQLConfig struct { Mysql_Host string Mysql_Port int Mysql_Name string Mysql_Timezone string Mysql_User string Mysql_Password string } type RedisConfig struct { Redis_Address string Redis_Password string } type MQTTConfig struct { Mqtt_Id string Mqtt_Server string Mqtt_User string Mqtt_Password string Mqtt_Topics []string } func (o *MySQLConfig) ReadConfig(path, sec string) bool { var ret bool = false fini, err := ini.Load(path + "mysql.ini") if err == nil { fsec := fini.Section(sec) if fsec != nil { o.Mysql_Host = fsec.Key("host").String() o.Mysql_Port, _ = fsec.Key("port").Int() o.Mysql_User = fsec.Key("user").String() o.Mysql_Password = fsec.Key("password").String() o.Mysql_Name = fsec.Key("name").String() o.Mysql_Timezone = fsec.Key("timezone").String() ret = true } } return ret } func (o *RedisConfig) ReadConfig(path, sec string) bool { var ret bool = false fini, err := ini.Load(path + "redis.ini") if err == nil { fsec := fini.Section(sec) if fsec != nil { o.Redis_Address = fsec.Key("address").String() o.Redis_Password = fsec.Key("password").String() ret = true } } return ret } func (o *MQTTConfig) ReadConfig(path, sec string) bool { var ret bool = false fini, err := ini.Load(path + "broker.ini") if err == nil { fsec := fini.Section(sec) if fsec != nil { o.Mqtt_Id = fsec.Key("id").String() + "_" + GetOneMac() + "_" + strings.ToUpper(strings.Split(filepath.Base(os.Args[0]), ".")[0]) o.Mqtt_Server = fsec.Key("server").String() o.Mqtt_User = fsec.Key("user").String() o.Mqtt_Password = fsec.Key("password").String() o.Mqtt_Topics = fsec.Key("topics").Strings(",") ret = true } } return ret } func GetOneMac() string { interfaces, err := net.Interfaces() if err == nil { for _, inter := range interfaces { if inter.Flags&net.FlagBroadcast == net.FlagBroadcast { return strings.ToUpper(strings.Replace(inter.HardwareAddr.String(), ":", "", -1)) } } } return "" } func GetPath(flag int) string { //basedir, _ := filepath.Abs(filepath.Dir(os.Args[0])) basedir, _ := os.Getwd() switch flag { case 0: return basedir + string(filepath.Separator) + "conf" + string(filepath.Separator) case 1: return basedir + string(filepath.Separator) + "dev" + string(filepath.Separator) case 2: return basedir + string(filepath.Separator) + "model" + string(filepath.Separator) case 3: return basedir + string(filepath.Separator) + "log" + string(filepath.Separator) case 4: return basedir + string(filepath.Separator) + "tmp" + string(filepath.Separator) case 5: return basedir + string(filepath.Separator) + "pole" + string(filepath.Separator) case 6: return basedir + string(filepath.Separator) + "rule" + string(filepath.Separator) } return "" }