config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package util
  2. import (
  3. "net"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/go-ini/ini"
  8. )
  9. type MySQLConfig struct {
  10. Mysql_Host string
  11. Mysql_Port int
  12. Mysql_Name string
  13. Mysql_Timezone string
  14. Mysql_User string
  15. Mysql_Password string
  16. }
  17. type RedisConfig struct {
  18. Redis_Address string
  19. Redis_Password string
  20. }
  21. type MQTTConfig struct {
  22. Mqtt_Id string
  23. Mqtt_Server string
  24. Mqtt_User string
  25. Mqtt_Password string
  26. Mqtt_Topics []string
  27. }
  28. func (o *MySQLConfig) ReadConfig(path, sec string) bool {
  29. var ret bool = false
  30. fini, err := ini.Load(path + "mysql.ini")
  31. if err == nil {
  32. fsec := fini.Section(sec)
  33. if fsec != nil {
  34. o.Mysql_Host = fsec.Key("host").String()
  35. o.Mysql_Port, _ = fsec.Key("port").Int()
  36. o.Mysql_User = fsec.Key("user").String()
  37. o.Mysql_Password = fsec.Key("password").String()
  38. o.Mysql_Name = fsec.Key("name").String()
  39. o.Mysql_Timezone = fsec.Key("timezone").String()
  40. ret = true
  41. }
  42. }
  43. return ret
  44. }
  45. func (o *RedisConfig) ReadConfig(path, sec string) bool {
  46. var ret bool = false
  47. fini, err := ini.Load(path + "redis.ini")
  48. if err == nil {
  49. fsec := fini.Section(sec)
  50. if fsec != nil {
  51. o.Redis_Address = fsec.Key("address").String()
  52. o.Redis_Password = fsec.Key("password").String()
  53. ret = true
  54. }
  55. }
  56. return ret
  57. }
  58. func (o *MQTTConfig) ReadConfig(path, sec string) bool {
  59. var ret bool = false
  60. fini, err := ini.Load(path + "broker.ini")
  61. if err == nil {
  62. fsec := fini.Section(sec)
  63. if fsec != nil {
  64. o.Mqtt_Id = fsec.Key("id").String() + "_" + GetOneMac() + "_" +
  65. strings.ToUpper(strings.Split(filepath.Base(os.Args[0]), ".")[0])
  66. o.Mqtt_Server = fsec.Key("server").String()
  67. o.Mqtt_User = fsec.Key("user").String()
  68. o.Mqtt_Password = fsec.Key("password").String()
  69. o.Mqtt_Topics = fsec.Key("topics").Strings(",")
  70. ret = true
  71. }
  72. }
  73. return ret
  74. }
  75. func GetOneMac() string {
  76. interfaces, err := net.Interfaces()
  77. if err == nil {
  78. for _, inter := range interfaces {
  79. if inter.Flags&net.FlagBroadcast == net.FlagBroadcast {
  80. return strings.ToUpper(strings.Replace(inter.HardwareAddr.String(), ":", "", -1))
  81. }
  82. }
  83. }
  84. return ""
  85. }
  86. func GetPath(flag int) string {
  87. //basedir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  88. basedir, _ := os.Getwd()
  89. switch flag {
  90. case 0:
  91. return basedir + string(filepath.Separator) + "conf" + string(filepath.Separator)
  92. case 1:
  93. return basedir + string(filepath.Separator) + "dev" + string(filepath.Separator)
  94. case 2:
  95. return basedir + string(filepath.Separator) + "model" + string(filepath.Separator)
  96. case 3:
  97. return basedir + string(filepath.Separator) + "log" + string(filepath.Separator)
  98. case 4:
  99. return basedir + string(filepath.Separator) + "tmp" + string(filepath.Separator)
  100. case 5:
  101. return basedir + string(filepath.Separator) + "pole" + string(filepath.Separator)
  102. case 6:
  103. return basedir + string(filepath.Separator) + "rule" + string(filepath.Separator)
  104. }
  105. return ""
  106. }