2017-09-13 27 views
-1

問題陳述: - 我試圖自動化MQTT流程,因爲我需要按順序發佈和訂閱多個主題。技巧部分是從第一次發佈接收到的消息具有一些值,將在下一個子/ pub命令中傳遞。如何在主要方法Eclipse Paho中訪問回調方法(messageArrived)到達的消息的有效負載?

例如,

  1. 子到topicA/ABC
  2. 酒吧到topicA/ABC上topicA/ABC接收
  3. 消息爲xyz
  4. 子主題topicA/XYZ
  5. 酒館主題topicA/XYZ

我能夠接收關於第一個主題的消息,但我沒有得到如何訪問主方法中接收到的消息的有效載荷,並將其傳遞並附加到下一個主題爲下一個子。

有沒有辦法將messageArrived回調方法的消息有效載荷從客戶端實例創建的主方法獲取?

注意: - 我正在使用單個客戶端進行發佈和訂閱。

請幫助我,因爲我已經用盡了選擇和方法。

編輯: -

代碼片斷

主要類

public class MqttOverSSL { 
String deviceId; 
MqttClient client = null; 

public MqttOverSSL() { 

} 

public MqttOverSSL(String deviceId) throws MqttException, InterruptedException { 
    this.deviceId = deviceId; 
    MqttConnection mqttConObj = new MqttConnection(); 
    this.client = mqttConObj.mqttConnection(); 
} 

public void getLinkCodeMethod() throws MqttException, InterruptedException { 

    client.subscribe("abc/multi/" + deviceId + "/linkcode", 0); 
    publish(client, "abc/multi/" + deviceId + "/getlinkcode", 0, "".getBytes()); 


} 

} 

MQTT Claback IMPL: -

public class SimpleMqttCallBack implements MqttCallback { 

    String arrivedMessage; 

    @Override 
    public void connectionLost(Throwable throwable) { 
    System.out.println("Connection to MQTT broker lost!"); 
    } 

    @Override 
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { 

    arrivedMessage = mqttMessage.toString(); 

    System.out.println("Message received:\t" + arrivedMessage); 

    linkCode(arrivedMessage); 
    } 

    @Override 
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { 
    System.out.println("Delivery complete callback: Publish Completed "+ Arrays.toString(iMqttDeliveryToken.getTopics())); 
    } 


    public void linkCode(String arrivedMessage) throws MqttException { 
    System.out.println("String is "+ arrivedMessage); 
    Gson g = new Gson(); 
    GetCode code = g.fromJson(arrivedMessage, GetCode.class); 
    System.out.println(code.getLinkCode()); 
    } 

} 

Publisher類: -

public class Publisher { 
    public static void publish(MqttClient client, String topicName, int qos, byte[] payload) throws MqttException { 

     String time = new Timestamp(System.currentTimeMillis()).toString(); 
     log("Publishing at: "+time+ " to topic \""+topicName+"\" qos "+qos); 

     // Create and configure a message 
     MqttMessage message = new MqttMessage(payload); 
     message.setQos(qos); 

     // Send the message to the server, control is not returned until 
     // it has been delivered to the server meeting the specified 
     // quality of service. 
     client.publish(topicName, message); 
    } 

    static private void log(String message) { 
     boolean quietMode = false; 
     if (!quietMode) { 
      System.out.println(message); 
     } 
    } 
} 
+0

沒有與我們共享任何代碼,我們真的不能幫助 – hardillb

+0

嗨hardilib,在原來的帖子中添加了相同的內容。請看看。 –

回答

0

好的,現在你要做的更清楚一點。

簡短回答不,你不能將值傳遞迴「主要方法」。 MQTT是異步的,意味着您不知道消息何時到達您訂閱的主題。

您需要更新代碼來檢查傳入消息主題是什麼,然後處理在messageArrived()處理程序中執行的任何操作。如果你有一系列的任務需要完成,那麼你可能需要實現所謂的狀態機,以便跟蹤你在序列中的位置。

相關問題