Exemplo n.º 1
1
  private void doInit() throws MqttException {
    String server = System.getProperty("knx2mqtt.mqtt.server", "tcp://localhost:1883");
    String clientID = System.getProperty("knx2mqtt.mqtt.clientid", "knx2mqtt");
    mqttc = new MqttClient(server, clientID, new MemoryPersistence());
    mqttc.setCallback(
        new MqttCallback() {
          @Override
          public void messageArrived(String topic, MqttMessage msg) throws Exception {
            try {
              processMessage(topic, msg);
            } catch (Exception e) {
              L.log(Level.WARNING, "Error when processing message " + msg + " for " + topic, e);
            }
          }

          @Override
          public void deliveryComplete(IMqttDeliveryToken token) {
            /* Intentionally ignored */
          }

          @Override
          public void connectionLost(Throwable t) {
            L.log(Level.WARNING, "Connection to MQTT broker lost", t);
            queueConnect();
          }
        });
    doConnect();
    Main.t.schedule(new StateChecker(), 30 * 1000, 30 * 1000);
  }
  public MQTTAdapterListener(
      MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration,
      String topic,
      String mqttClientId,
      InputEventAdapterListener inputEventAdapterListener,
      int tenantId) {

    if (mqttClientId == null || mqttClientId.trim().isEmpty()) {
      mqttClientId = MqttClient.generateClientId();
    }
    this.mqttBrokerConnectionConfiguration = mqttBrokerConnectionConfiguration;
    this.cleanSession = mqttBrokerConnectionConfiguration.isCleanSession();
    int keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive();
    this.topic = topic;
    this.eventAdapterListener = inputEventAdapterListener;
    this.tenantId = tenantId;

    // SORTING messages until the server fetches them
    String temp_directory = System.getProperty("java.io.tmpdir");
    MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(temp_directory);

    try {
      connectionOptions = new MqttConnectOptions();
      connectionOptions.setCleanSession(cleanSession);
      connectionOptions.setKeepAliveInterval(keepAlive);

      // Construct an MQTT blocking mode client
      mqttClient =
          new MqttClient(
              this.mqttBrokerConnectionConfiguration.getBrokerUrl(), mqttClientId, dataStore);

      // Set this wrapper as the callback handler
      mqttClient.setCallback(this);
      String contentValidatorClassName =
          this.mqttBrokerConnectionConfiguration.getContentValidatorClassName();

      if (contentValidatorClassName != null
          && contentValidatorClassName.equals(MQTTEventAdapterConstants.DEFAULT)) {
        contentValidator = new DefaultContentValidator();
      } else if (contentValidatorClassName != null && !contentValidatorClassName.isEmpty()) {
        try {
          Class<? extends ContentValidator> contentValidatorClass =
              Class.forName(contentValidatorClassName).asSubclass(ContentValidator.class);
          contentValidator = contentValidatorClass.newInstance();
        } catch (ClassNotFoundException e) {
          throw new MQTTContentInitializationException(
              "Unable to find the class validator: " + contentValidatorClassName, e);
        } catch (InstantiationException e) {
          throw new MQTTContentInitializationException(
              "Unable to create an instance of :" + contentValidatorClassName, e);
        } catch (IllegalAccessException e) {
          throw new MQTTContentInitializationException("Access of the instance in not allowed.", e);
        }
      }

      String contentTransformerClassName =
          this.mqttBrokerConnectionConfiguration.getContentTransformerClassName();
      if (contentTransformerClassName != null
          && contentTransformerClassName.equals(MQTTEventAdapterConstants.DEFAULT)) {
        contentTransformer = new DefaultContentTransformer();
      } else if (contentTransformerClassName != null && !contentTransformerClassName.isEmpty()) {
        try {
          Class<? extends ContentTransformer> contentTransformerClass =
              Class.forName(contentTransformerClassName).asSubclass(ContentTransformer.class);
          contentTransformer = contentTransformerClass.newInstance();
        } catch (ClassNotFoundException e) {
          throw new MQTTContentInitializationException(
              "Unable to find the class transfoer: " + contentTransformerClassName, e);
        } catch (InstantiationException e) {
          throw new MQTTContentInitializationException(
              "Unable to create an instance of :" + contentTransformerClassName, e);
        } catch (IllegalAccessException e) {
          throw new MQTTContentInitializationException("Access of the instance in not allowed.", e);
        }
      }
    } catch (MqttException e) {
      log.error(
          "Exception occurred while subscribing to MQTT broker at "
              + mqttBrokerConnectionConfiguration.getBrokerUrl());
      throw new InputEventAdapterRuntimeException(e);
    }
  }
Exemplo n.º 3
0
 private MQTTHandler() {
   String tp = System.getProperty("knx2mqtt.mqtt.topic", "knx");
   if (!tp.endsWith("/")) tp += "/";
   topicPrefix = tp;
 }