BladeMechanics

MQTT

Feb 28th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. package com.Carspace.demo.model;
  2.  
  3. /**
  4.  * Created by danie on 2/28/2019.
  5.  */
  6. import com.Carspace.demo.controller.MainController;
  7. import com.sun.media.sound.ModelAbstractChannelMixer;
  8. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  9. import org.eclipse.paho.client.mqttv3.MqttCallback;
  10. import org.eclipse.paho.client.mqttv3.MqttClient;
  11. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  12. import org.eclipse.paho.client.mqttv3.MqttException;
  13. import org.eclipse.paho.client.mqttv3.MqttMessage;
  14. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  15. import org.springframework.web.servlet.ModelAndView;
  16.  
  17.  
  18. public class MqttHandler implements MqttCallback {
  19.  
  20.     /** The broker url. */
  21.     private static final String brokerUrl = "tcp://localhost:1883";
  22.  
  23.     /** The client id. */
  24.     private static final String clientId = "CarSpace_Server";
  25.  
  26.     private String messageVar = "default_message";
  27.  
  28.     public String getMessageVar(){
  29.         return messageVar;
  30.     }
  31.  
  32.     public void setMessageVar(String message){
  33.         messageVar=message;
  34.     }
  35.  
  36.     public void subscribe(String topic) {
  37.  
  38.         MemoryPersistence persistence = new MemoryPersistence();
  39.  
  40.         try {
  41.  
  42.             MqttClient sampleClient = new MqttClient(brokerUrl, clientId, persistence);
  43.             MqttConnectOptions connOpts = new MqttConnectOptions();
  44.             connOpts.setCleanSession(true);
  45.  
  46.             System.out.println("checking");
  47.  
  48.             System.out.println("Mqtt Connecting to broker: " + brokerUrl);
  49.             sampleClient.connect(connOpts);
  50.             System.out.println("Mqtt Connected");
  51.  
  52.             sampleClient.setCallback(this);
  53.             sampleClient.subscribe(topic);
  54.  
  55.             System.out.println("Subscribed to " + topic);
  56.             System.out.println("Listening");
  57.  
  58.         } catch (MqttException me) {
  59.  
  60.             System.out.println("Mqtt reason " + me.getReasonCode());
  61.             System.out.println("Mqtt msg " + me.getMessage());
  62.             System.out.println("Mqtt loc " + me.getLocalizedMessage());
  63.             System.out.println("Mqtt cause " + me.getCause());
  64.             System.out.println("Mqtt excep " + me);
  65.         }
  66.     }
  67.  
  68.     /*
  69.      * (non-Javadoc)
  70.      *
  71.      * @see
  72.      * org.eclipse.paho.client.mqttv3.MqttCallback#connectionLost(java.lang.
  73.      * Throwable)
  74.      */
  75.     public void connectionLost(Throwable arg0) {
  76.  
  77.     }
  78.  
  79.     /*
  80.      * (non-Javadoc)
  81.      *
  82.      * @see
  83.      * org.eclipse.paho.client.mqttv3.MqttCallback#deliveryComplete(org.eclipse.
  84.      * paho.client.mqttv3.IMqttDeliveryToken)
  85.      */
  86.     public void deliveryComplete(IMqttDeliveryToken arg0) {
  87.  
  88.     }
  89.  
  90.     /*
  91.      * (non-Javadoc)
  92.      *
  93.      * @see
  94.      * org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.
  95.      * String, org.eclipse.paho.client.mqttv3.MqttMessage)
  96.      */
  97.     public void messageArrived(String topic, MqttMessage message) throws Exception {
  98.         System.out.println("Mqtt topic : " + topic);
  99.         System.out.println("Mqtt msg : " + message.toString());
  100.         setMessageVar(message.toString());
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment