示例#1
0
func (c *MqttClient) ForceDisconnect() {
	if c.internalClient == nil || !c.internalClient.IsConnected() {
		log.Warn("Already closed mqtt-server")
		return
	}
	c.internalClient.ForceDisconnect()
	log.Infof("ForceDisconnect mqtt-server: %v", c.setting.URL)
}
示例#2
0
func (db *sqlxDB) CloseDB() error {
	if db == nil || db.Dbm == nil {
		return fmt.Errorf("Already closed db")
	}
	if err := db.Dbm.Close(); err != nil {
		log.Warn("Failed to close Database !")
	} else {
		log.Info("Close Database !")
	}
	return nil
}
示例#3
0
func (txn *sqlxTxn) Rollback() error {
	if txn.Tx == nil {
		return nil
	}
	log.Warn("sqlx#Rollback")
	if err := txn.Tx.Rollback(); err != nil {
		log.Error("Failed to rollback-transaction %v", err)
		return err
	}
	txn.Dbm = nil
	return nil
}
示例#4
0
func (txn *gormTxn) Rollback() error {
	if txn.Dbm == nil {
		return nil
	}
	log.Warn("gorm#Rollback")
	if err := txn.Dbm.Rollback().Error; err != nil {
		log.Error("Failed to rollback-transaction %v", err)
		return err
	}
	txn.Dbm = nil
	return nil
}
示例#5
0
func (c *MqttClient) Connect() error {
	if c.internalClient == nil {
		return fmt.Errorf("Already closed mqtt-server")
	}
	if c.internalClient.IsConnected() {
		log.Warn("Already connected mqtt-server")
		return nil
	}
	token := c.internalClient.Connect()
	if token.Wait() && token.Error() != nil {
		return fmt.Errorf("Failed to connect mqtt-server: %v", token.Error())
	}
	time.Sleep(2 * time.Second)
	log.Infof("Connect mqtt-server: %v", c.setting.URL)
	return nil
}
示例#6
0
func (c *MqttClient) Subscribe(topic string, qos QoS, callback SubscribeMessageHandler) error {
	token := c.internalClient.Subscribe(
		topic,
		byte(qos),
		func(c *mqtt.Client, msg mqtt.Message) {
			if msg == nil {
				log.Warn("Subscribe message is nil")
				return
			}
			if err := callback(msg); err != nil {
				log.Errorf("Failed to subscript mqtt-server: %v", err)
				return
			}
		},
	)
	if qos != QOS_ZERO {
		token.Wait()
	}
	if token.Error() != nil {
		return fmt.Errorf("Failed to subscript mqtt-server: %v", token.Error())
	}
	return nil
}