@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    mapView.addLayer(
        new ArcGISTiledMapServiceLayer(
            "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer"));

    layer = new GraphicsLayer();
    mapView.addLayer(layer);

    mapView.getLocationDisplayManager().start();
    mapView.getLocationDisplayManager().setLocationListener(this);

    try {
      client =
          new MqttAndroidClient(
              this, "tcp://broker.mqttdashboard.com:1883", "Likaci/MqttMap/" + id);
      client.setCallback(this);
      MqttConnectOptions options = new MqttConnectOptions();
      options.setKeepAliveInterval(10);
      options.setConnectionTimeout(1000);
      options.setCleanSession(false);
      client.connect(options, null, this);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Beispiel #2
0
  private void doConnect() {
    L.info(
        "Connecting to MQTT broker "
            + mqttc.getServerURI()
            + " with CLIENTID="
            + mqttc.getClientId()
            + " and TOPIC PREFIX="
            + topicPrefix);

    MqttConnectOptions copts = new MqttConnectOptions();
    copts.setWill(topicPrefix + "connected", "0".getBytes(), 1, true);
    copts.setCleanSession(true);
    copts.setUserName("emonpi");
    copts.setPassword("emonpimqtt2016".toCharArray());
    try {
      mqttc.connect(copts);
      sendConnectionState();
      L.info("Successfully connected to broker, subscribing to " + topicPrefix + "(set|get)/#");
      try {
        mqttc.subscribe(topicPrefix + "set/#", 1);
        mqttc.subscribe(topicPrefix + "get/#", 1);
        shouldBeConnected = true;
      } catch (MqttException mqe) {
        L.log(Level.WARNING, "Error subscribing to topic hierarchy, check your configuration", mqe);
        throw mqe;
      }
    } catch (MqttException mqe) {
      L.log(
          Level.WARNING,
          "Error while connecting to MQTT broker, will retry: " + mqe.getMessage(),
          mqe);
      queueConnect(); // Attempt reconnect
    }
  }
Beispiel #3
0
 private void initMQTT() {
   getLogger().info("Init MQTT");
   try {
     MqttConnectOptions connOpts = new MqttConnectOptions();
     connOpts.setCleanSession(true);
     this.mqttClient =
         new MqttClient("tcp://elab.kr:1883", String.valueOf(this.getServiceId()), persistence);
     this.mqttClient.setCallback(this.mqttCallback);
     this.mqttClient.connect(connOpts);
   } catch (MqttException e) {
     getLogger().fatal("Not Connected MQTT broker", e);
   }
 }
  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);
    }
  }