github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/extend/emqx/emqx.go (about)

     1  package emqx
     2  
     3  import (
     4  	"errors"
     5  	mqtt "github.com/eclipse/paho.mqtt.golang"
     6  	"github.com/isyscore/isc-gobase/config"
     7  	"github.com/isyscore/isc-gobase/logger"
     8  	t0 "time"
     9  )
    10  
    11  func init() {
    12  	config.LoadConfig()
    13  
    14  	if config.ExistConfigFile() && config.GetValueBoolDefault("base.emqx.enable", false) {
    15  		err := config.GetValueObject("base.emqx", &config.EmqxCfg)
    16  		if err != nil {
    17  			logger.Warn("读取emqx配置异常, %v", err.Error())
    18  			return
    19  		}
    20  
    21  		mqtt.DEBUG = emqxLogger{"DEBUG"}
    22  		mqtt.WARN = emqxLogger{"WARN"}
    23  		mqtt.CRITICAL = emqxLogger{"CRITICAL"}
    24  		mqtt.ERROR = emqxLogger{"ERROR"}
    25  	}
    26  }
    27  
    28  type emqxLogger struct {
    29  	Level string
    30  }
    31  
    32  func (log emqxLogger) Println(v ...interface{}) {
    33  	switch log.Level {
    34  	case "DEBUG":
    35  		logger.DebugDirect(v...)
    36  	case "WARN":
    37  		logger.WarnDirect(v...)
    38  	case "CRITICAL":
    39  		logger.ErrorDirect(v...)
    40  	case "ERROR":
    41  		logger.ErrorDirect(v...)
    42  	}
    43  }
    44  func (log emqxLogger) Printf(format string, v ...interface{}) {
    45  	switch log.Level {
    46  	case "DEBUG":
    47  		logger.Debug(format, v...)
    48  	case "WARN":
    49  		logger.Warn(format, v...)
    50  	case "CRITICAL":
    51  		logger.Error(format, v...)
    52  	case "ERROR":
    53  		logger.Error(format, v...)
    54  	}
    55  }
    56  
    57  func NewEmqxClient() (mqtt.Client, error) {
    58  	if !config.GetValueBoolDefault("base.emqx.enable", false) {
    59  		logger.Error("emqx没有配置,请先配置")
    60  		return nil, errors.New("emqx没有配置,请先配置")
    61  	}
    62  
    63  	_emqxClient := mqtt.NewClient(localEmqxOptions())
    64  	if token := _emqxClient.Connect(); token.Wait() && token.Error() != nil {
    65  		logger.Error("链接emqx client失败, %v", token.Error().Error())
    66  		return nil, token.Error()
    67  	}
    68  	return _emqxClient, nil
    69  }
    70  
    71  func localEmqxOptions() *mqtt.ClientOptions {
    72  	opts := mqtt.NewClientOptions()
    73  	for _, server := range config.EmqxCfg.Servers {
    74  		opts.AddBroker(server)
    75  	}
    76  
    77  	if config.EmqxCfg.ClientId != "" {
    78  		opts.SetClientID(config.EmqxCfg.ClientId)
    79  	}
    80  
    81  	if config.EmqxCfg.Username != "" {
    82  		opts.SetUsername(config.EmqxCfg.Username)
    83  	}
    84  
    85  	if config.EmqxCfg.Password != "" {
    86  		opts.SetPassword(config.EmqxCfg.Password)
    87  	}
    88  
    89  	if config.EmqxCfg.CleanSession != true && config.GetValueString("base.emqx.clean-session") != "" {
    90  		opts.SetCleanSession(config.EmqxCfg.CleanSession)
    91  	}
    92  
    93  	if config.EmqxCfg.Order != true && config.GetValueString("base.emqx.order") != "" {
    94  		opts.SetOrderMatters(config.EmqxCfg.Order)
    95  	}
    96  
    97  	if config.EmqxCfg.WillEnabled != false && config.GetValueString("base.emqx.will-enabled") != "" {
    98  		opts.WillEnabled = config.EmqxCfg.WillEnabled
    99  	}
   100  
   101  	if config.EmqxCfg.WillTopic != "" {
   102  		opts.WillTopic = config.EmqxCfg.WillTopic
   103  	}
   104  
   105  	if config.EmqxCfg.WillQos != 0 {
   106  		opts.WillQos = config.EmqxCfg.WillQos
   107  	}
   108  
   109  	if config.EmqxCfg.WillRetained != false && config.GetValueString("base.emqx.will-retained") != "" {
   110  		opts.WillRetained = config.EmqxCfg.WillRetained
   111  	}
   112  
   113  	if config.EmqxCfg.ProtocolVersion != 0 {
   114  		opts.ProtocolVersion = config.EmqxCfg.ProtocolVersion
   115  	}
   116  
   117  	if config.EmqxCfg.KeepAlive != 30 && config.GetValueString("base.emqx.keep-alive") != "" {
   118  		opts.KeepAlive = config.EmqxCfg.KeepAlive
   119  	}
   120  
   121  	if config.EmqxCfg.PingTimeout != "10s" && config.GetValueString("base.emqx.ping-timeout") != "" {
   122  		t, err := t0.ParseDuration(config.EmqxCfg.PingTimeout)
   123  		if err != nil {
   124  			logger.Warn("读取配置【base.emqx.ping-timeout】异常:%v", err.Error())
   125  		} else {
   126  			opts.PingTimeout = t
   127  		}
   128  	}
   129  
   130  	if config.EmqxCfg.ConnectTimeout != "30s" && config.GetValueString("base.emqx.connect-timeout") != "" {
   131  		t, err := t0.ParseDuration(config.EmqxCfg.ConnectTimeout)
   132  		if err != nil {
   133  			logger.Warn("读取配置【base.emqx.connect-timeout】异常:%v", err.Error())
   134  		} else {
   135  			opts.PingTimeout = t
   136  		}
   137  	}
   138  
   139  	if config.EmqxCfg.MaxReconnectInterval != "10m" && config.GetValueString("base.emqx.max-reconnect-interval") != "" {
   140  		t, err := t0.ParseDuration(config.EmqxCfg.MaxReconnectInterval)
   141  		if err != nil {
   142  			logger.Warn("读取配置【base.emqx.max-reconnect-interval】异常:%v", err.Error())
   143  		} else {
   144  			opts.MaxReconnectInterval = t
   145  		}
   146  	}
   147  
   148  	if config.EmqxCfg.AutoReconnect != true && config.GetValueString("base.emqx.auto-reconnect") != "" {
   149  		opts.AutoReconnect = config.EmqxCfg.AutoReconnect
   150  	}
   151  
   152  	if config.EmqxCfg.ConnectRetryInterval != "30s" && config.GetValueString("base.emqx.connect-retry-interval") != "" {
   153  		t, err := t0.ParseDuration(config.EmqxCfg.ConnectRetryInterval)
   154  		if err != nil {
   155  			logger.Warn("读取配置【base.emqx.connect-retry-interval】异常:%v", err.Error())
   156  		} else {
   157  			opts.ConnectRetryInterval = t
   158  		}
   159  	}
   160  
   161  	if config.EmqxCfg.ConnectRetry != false && config.GetValueString("base.emqx.connect-retry") != "" {
   162  		opts.ConnectRetry = config.EmqxCfg.ConnectRetry
   163  	}
   164  
   165  	if config.EmqxCfg.WriteTimeout != "0" && config.GetValueString("base.emqx.write-timeout") != "" {
   166  		t, err := t0.ParseDuration(config.EmqxCfg.WriteTimeout)
   167  		if err != nil {
   168  			logger.Warn("读取配置【base.emqx.write-timeout】异常:%v", err.Error())
   169  		} else {
   170  			opts.WriteTimeout = t
   171  		}
   172  	}
   173  
   174  	if config.EmqxCfg.ResumeSubs != false && config.GetValueString("base.emqx.resume-subs") != "" {
   175  		opts.ResumeSubs = config.EmqxCfg.ResumeSubs
   176  	}
   177  
   178  	if config.EmqxCfg.MaxResumePubInFlight != 0 {
   179  		opts.MaxResumePubInFlight = config.EmqxCfg.MaxResumePubInFlight
   180  	}
   181  
   182  	if config.EmqxCfg.AutoAckDisabled != false && config.GetValueString("base.emqx.auto-ack-disabled") != "" {
   183  		opts.AutoAckDisabled = config.EmqxCfg.AutoAckDisabled
   184  	}
   185  
   186  	return opts
   187  }