예제 #1
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
    }
  }
예제 #2
0
 public static synchronized void queueNameFetch() {
   // We only attempt to fetch once every 10 minutes
   if (pendingFetch == null) {
     pendingFetch =
         new TimerTask() {
           @Override
           public void run() {
             fetchDeviceNames();
             pendingFetch = null;
           }
         };
     long delay = lastFetch + (10 * 60 * 1000) - System.currentTimeMillis();
     Main.t.schedule(pendingFetch, delay > 0 ? delay : 0);
     L.info("Queued Device name fetch in " + delay + "ms");
   }
 }